Use JSON-RPC error envelope for StreamableHTTPTransport errors#371
Open
koic wants to merge 1 commit into
Open
Conversation
## Motivation and Context PR modelcontextprotocol#347 introduced a single JSON-RPC error envelope response for unsupported `MCP-Protocol-Version` headers, while the rest of the transport-level error responses (Accept, Content-Type, Invalid JSON, session management, method not allowed, internal server error) still returned plain JSON of the form `{ "error": "..." }`. The Python SDK (`src/mcp/server/streamable_http.py`) and TypeScript SDK (`packages/server/src/server/streamableHttp.ts`) consistently use a JSON-RPC error envelope for all transport-level errors. Unify the Ruby SDK with them. ## Behavior All transport-level error responses now return a JSON-RPC error envelope: ```json { "jsonrpc": "2.0", "id": null, "error": { "code": -32600, "message": "..." } } ``` Affected helpers in `lib/mcp/server/transports/streamable_http_transport.rb`: - `validate_content_type` (HTTP 415) - `not_acceptable_response` (HTTP 406) - `parse_request_body` error (HTTP 400, `PARSE_ERROR` `-32700`) - `method_not_allowed_response` (HTTP 405) - `missing_session_id_response` (HTTP 400) - `session_not_found_response` (HTTP 404) - `session_already_connected_response` (HTTP 409) - Internal server error fallback in `handle_post` (HTTP 500, `INTERNAL_ERROR` `-32603`) - `validate_protocol_version_header` (HTTP 400, dedup with shared helper) A new private helper `json_rpc_error_response(status:, code:, message:)` centralizes the envelope construction. The "Invalid JSON" message wording is updated to "Parse error: Invalid JSON" to match the JSON-RPC 2.0 `PARSE_ERROR` semantics. ## How Has This Been Tested? Updated all affected tests in `test/mcp/server/transports/streamable_http_transport_test.rb` to assert the new envelope shape (`body["error"]["message"]` instead of `body["error"]`). ## Breaking Changes Clients that parsed the previous plain `{"error": "..."}` shape will need to read `body["error"]["message"]` (or `body["error"]["code"]`). The HTTP status codes are unchanged, only the response body structure changed.
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.
Motivation and Context
PR #347 introduced a single JSON-RPC error envelope response for unsupported
MCP-Protocol-Versionheaders, while the rest of the transport-level error responses (Accept, Content-Type, Invalid JSON, session management, method not allowed, internal server error) still returned plain JSON of the form{ "error": "..." }. The Python SDK (src/mcp/server/streamable_http.py) and TypeScript SDK (packages/server/src/server/streamableHttp.ts) consistently use a JSON-RPC error envelope for all transport-level errors. Unify the Ruby SDK with them.Behavior
All transport-level error responses now return a JSON-RPC error envelope:
{ "jsonrpc": "2.0", "id": null, "error": { "code": -32600, "message": "..." } }Affected helpers in
lib/mcp/server/transports/streamable_http_transport.rb:validate_content_type(HTTP 415)not_acceptable_response(HTTP 406)parse_request_bodyerror (HTTP 400,PARSE_ERROR-32700)method_not_allowed_response(HTTP 405)missing_session_id_response(HTTP 400)session_not_found_response(HTTP 404)session_already_connected_response(HTTP 409)handle_post(HTTP 500,INTERNAL_ERROR-32603)validate_protocol_version_header(HTTP 400, dedup with shared helper)A new private helper
json_rpc_error_response(status:, code:, message:)centralizes the envelope construction.The "Invalid JSON" message wording is updated to "Parse error: Invalid JSON" to match the JSON-RPC 2.0
PARSE_ERRORsemantics.How Has This Been Tested?
Updated all affected tests in
test/mcp/server/transports/streamable_http_transport_test.rbto assert the new envelope shape (body["error"]["message"]instead ofbody["error"]).Breaking Changes
Clients that parsed the previous plain
{"error": "..."}shape will need to readbody["error"]["message"](orbody["error"]["code"]). The HTTP status codes are unchanged, only the response body structure changed.Types of changes
Checklist