Repository navigation
Answer a malformed stdio line instead of ending the server - #67
Open
skybleak333 wants to merge 2 commits into
Open
skybleak333 wants to merge 2 commits into
skybleak333 wants to merge 2 commits into
Conversation
On the non-blocking stdin, fgets() returns whatever has arrived so far, so a message larger than the pipe buffer was decoded in parts and the server died on the parse error.
readMessage() threw McpError for a line that is not a JSON-RPC message, and nothing in the stdio session loop caught it.
8 of 9 tasks
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.
Summary
A line on stdin that is not a JSON-RPC message is now answered with a JSON-RPC error, and the server goes on reading, instead of ending.
Motivation
StdioServerTransport::readMessage()threwMcpErrorfor invalid JSON or an invalid request, and nothing in the stdio session loop catches it, so one bad line from the client ended the whole server process. PHP also printed the fatal error to stdout, in the middle of the JSON-RPC stream. With a stdio server and a client that sendsinitialize, then{"jsonrpc": "2.0", "id": 5, "method": "ping"(no closing brace), then a validping:PHP Fatal error: Uncaught Mcp\Shared\McpError: Parse error: Syntax error, the process is gone and the secondpinggets no answer;{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error: Syntax error"}}, then{"jsonrpc":"2.0","id":6,"result":{}}.What the line gets, following JSON-RPC 2.0:
-32700withid: null;"jsonrpc": "2.0", no method or result, a scalar such as42):-32600, with the request'sidif the line has amethodand anid, otherwisenull. A scalar used to stop the server with aTypeErrorfrominstantiateSingleMessage(array $data);The parsing moved unchanged into a private
parseLine(), which still throwsMcpError;readMessage()catches it, writes the error through the transport's write buffer and returnsnull, so the session loop polls again as for "no data yet".Builds on #66 (both change
readMessage()), so until that one is merged this diff also shows its commit.Type of change
Versioning impact
v1.7.X) — non-breaking bug fix or minor feature/improvementreadMessage()no longer throwsMcpError. Inside the SDK only the stdio session loop calls it, and there the exception ended the process.Test plan
composer checkpasses locallycomposer conformancepasses locally — not run: the suite drives the HTTP transport, this change is in the stdio oneStdioServerTransportMalformedLineTest(invalid JSON then a valid message, an invalid request with an id, a scalar, a blank line); all four fail without the changeCompatibility
Documentation
CHANGELOG.mdunder[Unreleased]