Closed
Labels
Description
Objective
Add context cancellation checks to MCP tool handlers to properly handle client disconnections and cancelled operations.
Context
Currently, MCP tool handlers receive a context but don't check for cancellation. Adding ctx.Done() checks before long-running operations will improve resource management and allow tools to exit early when clients disconnect.
Approach
- Identify tool handlers in
pkg/cli/mcp_server.gothat perform long-running operations - Add context cancellation checks at appropriate points
- Return early with context error when cancelled
Example Pattern
func handleLongRunningTool(ctx context.Context, params ToolParams) (string, error) {
// Check for cancellation before starting
select {
case <-ctx.Done():
return "", ctx.Err()
default:
}
// Perform work...
result := performWork()
// Check again before next step
select {
case <-ctx.Done():
return "", ctx.Err()
default:
}
return result, nil
}Files to Modify
pkg/cli/mcp_server.go- Add context checks to tool handlers that perform I/O or exec operations
Acceptance Criteria
- Tool handlers check
ctx.Done()before significant operations - Cancelled operations return
ctx.Err()promptly - Normal tool invocations continue to work
- Tests pass:
make test - Manual test: Tool responds to cancellation (e.g., interrupt during execution)
Related to [plan] Adopt MCP Go SDK v1.1.0 improvements #6249
AI generated by Plan Command for discussion #6222
Reacted by Copilot
Activity