Skip to content

[plan] Add context cancellation checks to MCP tool handlers #6253

Closed

Description

@github-actions

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

  1. Identify tool handlers in pkg/cli/mcp_server.go that perform long-running operations
  2. Add context cancellation checks at appropriate points
  3. 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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions