-
Notifications
You must be signed in to change notification settings - Fork 4
Add graceful shutdown for stdio transport #15
Copy link
Copy link
Open
Labels
hacktoberfestHacktoberfest eligibleHacktoberfest eligiblemcp: transportMCP transport relatedMCP transport relatedpriority: highHigh priorityHigh prioritytype: featureNew feature or enhancementNew feature or enhancement
Description
Summary
The stdio transport lacks graceful shutdown handling, unlike the HTTP transport.
Current State
```go
// cmd/server/main.go:68-86
func runStdioTransport() {
logger.Info("Starting MCP server with stdio transport")
sdkServer := mcp.NewServerSDK()
ctx := context.Background() // No cancellation!
logger.Info("Server is ready to accept stdio connections")
if err := sdkServer.GetServer().Run(ctx, &mcpSdk.StdioTransport{}); err != nil {
logger.Fatal(fmt.Sprintf("stdio server failed: %v", err))
}
logger.Info("stdio server shutting down")
}
```
Problem
- No signal handling (SIGINT, SIGTERM)
- Context has no cancellation capability
- No cleanup on shutdown
- Contrast with HTTP transport which has proper signal handling
Expected Outcome
```go
func runStdioTransport() {
logger.Info("Starting MCP server with stdio transport")
sdkServer := mcp.NewServerSDK()
// Create cancellable context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Handle shutdown signals
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigChan
logger.Info(fmt.Sprintf("Received signal %v, initiating shutdown", sig))
cancel()
}()
logger.Info("Server is ready to accept stdio connections")
if err := sdkServer.GetServer().Run(ctx, &mcpSdk.StdioTransport{}); err != nil {
if ctx.Err() == context.Canceled {
logger.Info("stdio server shutdown complete")
} else {
logger.Fatal(fmt.Sprintf("stdio server failed: %v", err))
}
}
}
```
Acceptance Criteria
- Signal handling added for SIGINT and SIGTERM
- Context properly cancelled on shutdown
- Graceful shutdown logged
- No resource leaks on shutdown
- Consistent with HTTP transport shutdown behavior
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
hacktoberfestHacktoberfest eligibleHacktoberfest eligiblemcp: transportMCP transport relatedMCP transport relatedpriority: highHigh priorityHigh prioritytype: featureNew feature or enhancementNew feature or enhancement