Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"log"
"log/slog"
"os"
"os/signal"
"syscall"

"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
Expand Down Expand Up @@ -700,15 +702,38 @@

registerAllTools(s, appInstance, debugLogger)

// Cleanup on exit
defer func() {
// Cleanup function
cleanup := func() {
debugLogger.Info("Starting cleanup...")
if err := appInstance.Disconnect(); err != nil {
debugLogger.Error("Failed to disconnect from database", "error", err)
} else {
debugLogger.Info("Database connection closed successfully")
}
debugLogger.Info("Server shutdown complete")
}
defer cleanup()

// Set up signal handling for graceful shutdown
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)

go func() {
sig := <-sigChan
debugLogger.Info("Received shutdown signal", "signal", sig.String())
cancel()
}()

// Start the stdio server
if err := server.ServeStdio(s); err != nil {
debugLogger.Info("Starting PostgreSQL MCP Server", "version", version)

// Create a custom StdioServer with context support
stdioServer := server.NewStdioServer(s)
if err := stdioServer.Listen(ctx, os.Stdin, os.Stdout); err != nil && err != context.Canceled {

Check failure on line 735 in main.go

View workflow job for this annotation

GitHub Actions / linter

do not compare errors directly "err != context.Canceled", use "!errors.Is(err, context.Canceled)" instead (err113)
debugLogger.Error("Server error", "error", err)
fmt.Fprintf(os.Stderr, "Server error: %v\n", err)
return
}
Expand Down
Loading