Skip to content

fix(streamable_http): include MCP method in client JSON-RPC error messages - #912

Closed
syf2211 wants to merge 1 commit into
mark3labs:mainfrom
syf2211:fix/streamable-http-error-method-name
Closed

fix(streamable_http): include MCP method in client JSON-RPC error messages#912
syf2211 wants to merge 1 commit into
mark3labs:mainfrom
syf2211:fix/streamable-http-error-method-name

Conversation

@syf2211

@syf2211 syf2211 commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Summary

Include the originating MCP method name when wrapping JSON-RPC errors returned by the client over streamable HTTP transport.

Motivation

handleSamplingResponse is shared by sampling/createMessage, elicitation/create, and roots/list, but previously hardcoded all client-side JSON-RPC errors as sampling error %d: %s. This made it impossible to distinguish elicitation failures from sampling failures without fragile string matching.

Fixes #817

Changes

  • Introduce pendingClientRequest to track both the response channel and MCP method for pending client requests
  • Store the correct method when registering sampling, elicitation, and roots/list requests
  • Format client JSON-RPC errors as <method> error <code>: <message> (e.g. elicitation/create error -32601: Method not found)
  • Add regression test TestStreamableHTTPServer_ClientRequestErrorIncludesMethod

Tests

go test ./server/... -run 'TestStreamableHTTPServer_(ClientRequestErrorIncludesMethod|Sampling)' -count=1 -v

All tests passed.

Notes

  • Sampling errors now read sampling/createMessage error ... instead of sampling error ... for accuracy. Error strings are not a stable API; callers should inspect error values rather than parse message text.
  • stdio transport error messages are unchanged (out of scope for this issue).

Summary by CodeRabbit

  • Bug Fixes
    • Improved error reporting for sampling-related requests so client-side errors now include the originating request type.
    • Fixed handling of pending responses in Streamable HTTP sessions to better detect missing or invalid requests.
  • Tests
    • Added coverage for error messages returned from sampling requests to ensure they include the correct method context.

…sages

Track the originating MCP method on pending streamable-HTTP client
requests (sampling/createMessage, elicitation/create, roots/list) and
use it when wrapping JSON-RPC errors returned by the client.

Fixes mark3labs#817
@mark-iii-labs-huly

Copy link
Copy Markdown

Connected to Huly®: MCP_G-476

@coderabbitai

coderabbitai Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: c990252c-892a-4047-9c80-f7eb88bc3464

📥 Commits

Reviewing files that changed from the base of the PR and between b6e6224 and b68951d.

📒 Files selected for processing (2)
  • server/streamable_http.go
  • server/streamable_http_sampling_test.go

Walkthrough

Adds a pendingClientRequest struct that pairs a response channel with the originating MCP method. RequestSampling, ListRoots, and RequestElicitation now store this struct in samplingRequests. handleSamplingResponse and deliverSamplingResponse use the stored method to produce method-specific error messages. A test verifies the method name appears in errors.

Method-aware sampling error plumbing

Layer / File(s) Summary
pendingClientRequest struct and storage updates
server/streamable_http.go
Introduces pendingClientRequest holding a response channel and MCP method string; updates RequestSampling, ListRoots, and RequestElicitation to store it in samplingRequests instead of a bare channel.
Error and delivery logic using stored method
server/streamable_http.go
handleSamplingResponse loads the stored pendingClientRequest to extract the method name and formats errors as "{method} error {code}: {msg}". deliverSamplingResponse type-asserts the loaded value and adds explicit HTTP error responses for missing or invalid entries.
Test: error string includes originating method
server/streamable_http_sampling_test.go
Adds TestStreamableHTTPServer_ClientRequestErrorIncludesMethod which injects a seeded pendingClientRequest for an elicitation/create request, sends a JSON-RPC error payload, and asserts the resulting error string is exactly elicitation/create error -32601: Method not found.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • mark3labs/mcp-go#764: Modifies the same handleSamplingResponse/deliverSamplingResponse code paths in server/streamable_http.go.

Suggested labels

type: bug

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@syf2211

syf2211 commented Jun 28, 2026

Copy link
Copy Markdown
Contributor Author

Closing as duplicate of #822, which already addresses the same issue with a similar approach. Thanks!

@syf2211 syf2211 closed this Jun 28, 2026
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.

streamable_http: client-returned JSON-RPC errors for elicitation/create reported as "sampling error"

1 participant