Skip to content

Return JSON-RPC error for non-string routing name in Streamable HTTP body - #1932

Open
pournasserian wants to merge 4 commits into
modelcontextprotocol:mainfrom
pournasserian:fix/streamable-http-non-string-routing-name
Open

pournasserian wants to merge 4 commits into
modelcontextprotocol:mainfrom
pournasserian:fix/streamable-http-non-string-routing-name

Conversation

@pournasserian

Copy link
Copy Markdown
Contributor

Fixes #1919

Problem

On the Streamable HTTP POST path, a tools/call, prompts/get or resources/read request (protocol version 2026-07-28) whose routing field (params.name / params.uri) is valid JSON but not a string made GetJsonNodeStringProperty call GetValue<string>() on a number, bool, object or array. That threw InvalidOperationException out of ValidateMcpHeaders, so the client received an HTTP 500 with no JSON-RPC envelope and the server logged an unhandled exception.

Fix

  • GetJsonNodeStringProperty now uses JsonValue.TryGetValue<string> and returns null for non-string values instead of throwing.
  • ValidateMcpHeaders rejects a routing field that is present but not a string, so the request is answered with HTTP 400 and a JSON-RPC error carrying the request id. It uses the same HeaderMismatch (-32020) code as the other ValidateMcpHeaders failures.

Tests

Added HttpHeaderConformanceTests.Server_RejectsNonStringRoutingNameInBody_WithJsonRpcError, covering numeric, boolean, object and array values for tools/call, prompts/get and resources/read. It asserts a 400 response whose JSON-RPC error has the original request id and the HeaderMismatch code. All 6 cases fail without the fix and pass with it; the rest of HttpHeaderConformanceTests passes on net9.0 and net10.0.

…body

A tools/call, prompts/get or resources/read request whose params.name or
params.uri was a number, bool, object or array made ValidateMcpHeaders throw
InvalidOperationException, so the client got an HTTP 500 without a JSON-RPC
envelope. Reject it as a header mismatch carrying the request id instead.

Fixes modelcontextprotocol#1919

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant