go-sdk: fix context propagation through SDK calls and add prompt passthrough#8095
Merged
Conversation
…ormat, improve capability comment
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
Contributor
There was a problem hiding this comment.
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.Contextthrough SDK method dispatch (callSDKMethod*,callTool,readResource,getPrompt) and forward it fromSendRequestWithServerID. - Add
Connection.BackendHasPromptsCapability()and use it when registering prompts to avoid issuing unsupportedprompts/listcalls on SDK sessions. - Register backend prompts as
<serverID>___<promptName>and add a prompt handler that forwardsprompts/getto 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Per-request tool timeout contexts were silently dropped at the SDK layer —
callSDKMethodalways used the long-lived connection contextc.ctx, sotoolTimeoutdeadlines never actually cancelled backend calls. Additionally, backend prompts were never registered with the frontend SDK server, leavingprompts/listresponses empty for all clients.Context propagation (
internal/mcp/)ctx context.ContexttocallSDKMethod,callSDKMethodWithReconnect,callTool,readResource, andgetPromptListTools,CallTool,ListResources,ReadResource,ListPrompts,GetPrompt) now receive the per-request context instead ofc.ctxSendRequestWithServerIDforwardsctxto both the plain JSON-RPC and SDK pathsPrompt passthrough (
internal/server/,internal/mcp/)registerPromptsFromBackend(ctx, serverID, conn)called fromregisterToolsFromBackend; prompts registered asserverID___promptNamematching the tool conventionBackendHasPromptsCapability()onConnection— checkssession.InitializeResult().Capabilities.Prompts != nilbefore issuingprompts/list; guards against EOF responses on backends that don't declare prompt support, which would corrupt SDK session state and break subsequent tool callsGetOrLaunchForSessionand forwardctxto the backendprompts/getcall