Skip to content

Answer a malformed stdio line instead of ending the server - #67

Open
skybleak333 wants to merge 2 commits into
logiscape:mainfrom
skybleak333:stdio-malformed-lines
Open

skybleak333 wants to merge 2 commits into
logiscape:mainfrom
skybleak333:stdio-malformed-lines

Conversation

@skybleak333

Copy link
Copy Markdown

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() threw McpError for 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 sends initialize, then {"jsonrpc": "2.0", "id": 5, "method": "ping" (no closing brace), then a valid ping:

  • before: PHP Fatal error: Uncaught Mcp\Shared\McpError: Parse error: Syntax error, the process is gone and the second ping gets no answer;
  • after: {"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:

  • invalid JSON: -32700 with id: null;
  • valid JSON that is not a message (no "jsonrpc": "2.0", no method or result, a scalar such as 42): -32600, with the request's id if the line has a method and an id, otherwise null. A scalar used to stop the server with a TypeError from instantiateSingleMessage(array $data);
  • a blank line: skipped, no answer.

The parsing moved unchanged into a private parseLine(), which still throws McpError; readMessage() catches it, writes the error through the transport's write buffer and returns null, 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

  • Bug fix (non-breaking change that fixes an issue)

Versioning impact

  • Patch (v1.7.X) — non-breaking bug fix or minor feature/improvement

readMessage() no longer throws McpError. Inside the SDK only the stdio session loop calls it, and there the exception ended the process.

Test plan

  • composer check passes locally
  • composer conformance passes locally — not run: the suite drives the HTTP transport, this change is in the stdio one
  • Relevant unit tests added or updated: StdioServerTransportMalformedLineTest (invalid JSON then a valid message, an invalid request with an id, a scalar, a blank line); all four fail without the change
  • Smoke-tested with a real stdio server and client as described above

Compatibility

  • Change preserves core-feature compatibility under cPanel / Apache / PHP-FPM
  • No new required runtime dependencies

Documentation

  • Added an entry to CHANGELOG.md under [Unreleased]

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.
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.

1 participant