Skip to content

Add graceful shutdown for stdio transport #15

@NP-compete

Description

@NP-compete

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

  1. No signal handling (SIGINT, SIGTERM)
  2. Context has no cancellation capability
  3. No cleanup on shutdown
  4. 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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions