fix: improve error handling and add guard check to deserializeMessage#1225
fix: improve error handling and add guard check to deserializeMessage#1225saharis9988 wants to merge 8 commits intomodelcontextprotocol:mainfrom
deserializeMessage#1225Conversation
commit: |
KKonstantinov
left a comment
There was a problem hiding this comment.
Thank you for your work on this.
Leaving one comment with a question
src/shared/stdio.ts
Outdated
| } catch (error: unknown) { | ||
| // When Non JSONRPC message is received (parsing error or schema validation error), return null | ||
| if (error instanceof ZodError || error instanceof SyntaxError) { | ||
| return null; |
There was a problem hiding this comment.
Understand the SynthaxError, however previously, the SDK did not return null if JSONRPCMessageSchema.parse threw, why the change?
There was a problem hiding this comment.
Hey @KKonstantinov, thanks for the review 🙏
I agree - the SDK shouldn’t swallow JSONRPCMessageSchema.parse errors, since throwing in those cases is intentional and important for detecting real protocol violations.
My initial thought was to ignore all invalid JSON-RPC messages, but that would indeed mask real protocol issues.
I’ve updated the fix so that only non-JSON lines are skipped while invalid JSON-RPC messages continue to throw, and I also added an additional test to cover this use case 👍
a8d6474 to
a807cf7
Compare
|
UPDATE: |
ab96800 to
157bad0
Compare
f00ef1d to
8f2a360
Compare
|
@felixweinberger @KKonstantinov |
ReadBuffer.readMessage() now loops past lines that throw SyntaxError (e.g., debug output from tsx/nodemon writing to stdout) instead of propagating them to onerror. Lines that parse as JSON but fail JSONRPC schema validation still throw, so genuinely malformed messages still surface. Keeps the existing Buffer-based architecture to preserve UTF-8 correctness across chunk boundaries and the deserializeMessage(string) public signature. Fixes #700 Closes #1225 Co-authored-by: Sahar Shemesh <sahar.shemesh@zoominfo.com>
|
Opened #1762 with a simpler approach to the same issue. It keeps the existing Buffer-based ReadBuffer (preserving UTF-8 correctness across chunk boundaries and the public Ported your test cases and added you as co-author. Closing in favor of #1762. |
This PR adds a guard check to the MCP TypeScript SDK’s stdio message deserialization logic to prevent JSON parsing errors when non-JSON RPC lines are received on the stdio stream.
Motivation and Context
The MCP SDK currently assumes that every line received over stdio is a valid JSON-RPC message.
However, there are scenarios where non-JSON or otherwise invalid JSON RPC data may be written to stdout
whether from the runtime environment, external tooling, or other processes that share the same output stream.
When these invalid messages reach the SDK, the deserializer attempts to parse them and throws a SyntaxError, which impacts overall stability.
This update adds a safeguard that gracefully skips any line that is not valid JSON, making the stdio transport more robust and tolerant of incidental or malformed output on the stream.
How Has This Been Tested?
I added a unit test covering this scenario to ensure that non-JSON RPC valid input is gracefully ignored by the stdio transport.
I'm going to build the MCP Inspector against the RC version of this fix and verify that the issue is no longer reproducible and that invalid JSON-RPC messages are handled safely without causing errors.
Breaking Changes
None.
This change is fully backward compatible
Types of changes
Checklist
Additional context
reference to the issue #700