Skip to content

fix: Reject null-ID results on message streams - #414

Open
EhsanAzish80 wants to merge 2 commits into
modelcontextprotocol:rel/1.0.0from
EhsanAzish80:fix/stream-null-response-id
Open

EhsanAzish80 wants to merge 2 commits into
modelcontextprotocol:rel/1.0.0from
EhsanAzish80:fix/stream-null-response-id

Conversation

@EhsanAzish80

Copy link
Copy Markdown
Contributor

Motivation and Context

The server read a response before checking whether it was valid on the wire. A successful result with id: null was treated as a response with an empty string ID, so the server sent nothing back. This follows up on #401 and makes stream handling match the HTTP and batch paths.

How Has This Been Tested?

  • The regression test checks null-ID results with and without an extra method field.
  • swift build and swift test passed: 1,958 tests, no known issues.
  • Client and server conformance passed for 2025-11-25 and 2026-07-28.
  • The docs weave check and DocC warnings-as-errors check passed.

AI helped write the tests; I ran them.

Breaking Changes

None. No public API changes.

Types of changes

  • Bug fix

The server decoded responses before classifying the wire message, so a result with id null was read as an empty-string response and left unanswered. Use the shared JSON-RPC classifier before handling a response, and turn the known-issue test into a regression test. JSON-RPC 2.0 section 5 reserves null for an unknown response ID.
@EhsanAzish80
EhsanAzish80 requested a review from a team as a code owner December 4, 2026 08:42
Comment thread Sources/MCP/Server/Server.swift Outdated
}
}
} else if let response = try? decoder.decode(AnyResponse.self, from: data) {
} else if JSONRPCMessageKind(data: data)?.isResponse == true,

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.

Reuse the server's decoder (matching the .invalid branch below) instead of allocating a fresh JSONDecoder per message:

Suggested change
} else if JSONRPCMessageKind(data: data)?.isResponse == true,
} else if (try? decoder.decode(JSONRPCMessageKind.self, from: data))?.isResponse == true,

Comment on lines +1574 to +1575
#"{"jsonrpc":"2.0","id":null,"result":{}}"#,
#"{"jsonrpc":"2.0","id":null,"method":"tools/list","result":{}}"#,

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.

This change also makes null-ID error messages carrying method receive -32600 (matching HTTP and batch handling); consider adding a case for it:

Suggested change
#"{"jsonrpc":"2.0","id":null,"result":{}}"#,
#"{"jsonrpc":"2.0","id":null,"method":"tools/list","result":{}}"#,
#"{"jsonrpc":"2.0","id":null,"result":{}}"#,
#"{"jsonrpc":"2.0","id":null,"method":"tools/list","result":{}}"#,
#"{"jsonrpc":"2.0","id":null,"method":"tools/list","error":{"code":-32603,"message":"x"}}"#,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks @varun06 , I made both changes: stream responses now use the server decoder, and the null-ID error test covers a message with a method. The full test suite and both conformance versions pass locally. Pushed as a39b449.

Use the server decoder when classifying stream messages. Cover a null-ID error that also carries a method in the existing wire test.
@jeanmatthieu

Copy link
Copy Markdown
Contributor

Nice catch, thanks! We're doing a API consolidation on our end and hope to get that on GitHub in the next couple of days. We can rebase your branch onto it once it's there.

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