Skip to content

go-sdk: fix context propagation through SDK calls and add prompt passthrough#8095

Merged
lpcox merged 3 commits into
mainfrom
copilot/go-fan-go-module-review
Jun 25, 2026
Merged

go-sdk: fix context propagation through SDK calls and add prompt passthrough#8095
lpcox merged 3 commits into
mainfrom
copilot/go-fan-go-module-review

Conversation

Copilot AI commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Per-request tool timeout contexts were silently dropped at the SDK layer — callSDKMethod always used the long-lived connection context c.ctx, so toolTimeout deadlines never actually cancelled backend calls. Additionally, backend prompts were never registered with the frontend SDK server, leaving prompts/list responses empty for all clients.

Context propagation (internal/mcp/)

  • Added ctx context.Context to callSDKMethod, callSDKMethodWithReconnect, callTool, readResource, and getPrompt
  • All six SDK session methods (ListTools, CallTool, ListResources, ReadResource, ListPrompts, GetPrompt) now receive the per-request context instead of c.ctx
  • SendRequestWithServerID forwards ctx to both the plain JSON-RPC and SDK paths
// Before: tool timeout deadline silently dropped
result, err = c.callSDKMethodWithReconnect(method, params)

// After: deadline propagates to the SDK session call
result, err = c.callSDKMethodWithReconnect(ctx, method, params)

Prompt passthrough (internal/server/, internal/mcp/)

  • Added registerPromptsFromBackend(ctx, serverID, conn) called from registerToolsFromBackend; prompts registered as serverID___promptName matching the tool convention
  • Added BackendHasPromptsCapability() on Connection — checks session.InitializeResult().Capabilities.Prompts != nil before issuing prompts/list; guards against EOF responses on backends that don't declare prompt support, which would corrupt SDK session state and break subsequent tool calls
  • Prompt handlers use per-session connections via GetOrLaunchForSession and forward ctx to the backend prompts/get call
  • Failures in prompt registration are non-fatal (logged as warnings)

GitHub Advanced Security started work on behalf of lpcox June 25, 2026 16:09 View session
GitHub Advanced Security finished work on behalf of lpcox June 25, 2026 16:09
GitHub Advanced Security started work on behalf of lpcox June 25, 2026 16:25 View session
GitHub Advanced Security finished work on behalf of lpcox June 25, 2026 16:25
GitHub Advanced Security started work on behalf of lpcox June 25, 2026 16:27 View session
Copilot AI changed the title [WIP] Review Go SDK for modelcontextprotocol go-sdk: fix context propagation through SDK calls and add prompt passthrough Jun 25, 2026
Copilot AI requested a review from lpcox June 25, 2026 16:27
Copilot finished work on behalf of lpcox June 25, 2026 16:27
GitHub Advanced Security finished work on behalf of lpcox June 25, 2026 16:27
@lpcox lpcox marked this pull request as ready for review June 25, 2026 16:54
Copilot AI review requested due to automatic review settings June 25, 2026 16:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes dropped per-request context propagation in the MCP go-sdk dispatch path (so toolTimeout deadlines can actually cancel backend calls) and adds backend prompt passthrough by registering backend prompts with the unified frontend SDK server.

Changes:

  • Thread a per-request context.Context through SDK method dispatch (callSDKMethod*, callTool, readResource, getPrompt) and forward it from SendRequestWithServerID.
  • Add Connection.BackendHasPromptsCapability() and use it when registering prompts to avoid issuing unsupported prompts/list calls on SDK sessions.
  • Register backend prompts as <serverID>___<promptName> and add a prompt handler that forwards prompts/get to the appropriate backend connection.
Show a summary per file
File Description
internal/server/tool_registry.go Registers backend prompts into the unified frontend server and wires a prompts/get passthrough handler.
internal/mcp/connection.go Adds prompt-capability detection and forwards per-request context through SDK reconnect + dispatch paths.
internal/mcp/connection_methods.go Refactors SDK dispatch helpers to accept and use per-request context instead of the long-lived connection context.
internal/mcp/sdk_method_dispatch_test.go Updates unit tests to pass request contexts into callSDKMethod.
internal/mcp/reconnect_sdk_test.go Updates reconnect/SDK transport tests to pass request contexts into callSDKMethod*.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 5/5 changed files
  • Comments generated: 1

Comment on lines +358 to +367
// Only call prompts/list on backends that explicitly declared prompt support in
// their initialize response. For SDK-based connections (streamable, SSE, stdio),
// an unsupported prompts/list request can return EOF which the SDK interprets as
// a session close, breaking subsequent tool calls on the same connection.
// Plain JSON-RPC connections return false here too; their initialize response is
// not parsed into typed capabilities, so we cannot safely detect support.
if !conn.BackendHasPromptsCapability() {
logUnified.Printf("Backend %s does not declare prompts capability (skipping)", serverID)
return nil
}
@lpcox lpcox merged commit 269af21 into main Jun 25, 2026
40 checks passed
@lpcox lpcox deleted the copilot/go-fan-go-module-review branch June 25, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants