Skip to content

Conversation

@saharis9988
Copy link
Contributor

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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

reference to the issue #700

@saharis9988 saharis9988 requested a review from a team as a code owner December 3, 2025 14:30
@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 3, 2025

Open in StackBlitz

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/sdk@1225

commit: ab96800

Copy link
Contributor

@KKonstantinov KKonstantinov left a comment

Choose a reason for hiding this comment

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

Thank you for your work on this.

Leaving one comment with a question

} 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;
Copy link
Contributor

Choose a reason for hiding this comment

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

Understand the SynthaxError, however previously, the SDK did not return null if JSONRPCMessageSchema.parse threw, why the change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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 👍

@saharis9988 saharis9988 force-pushed the deserializing-guard-check branch from a8d6474 to a807cf7 Compare December 4, 2025 20:43
@saharis9988
Copy link
Contributor Author

saharis9988 commented Dec 5, 2025

UPDATE:
While testing the previous implementation in the MCP Inspector, I noticed that when an invalid JSON message appeared at the start of a buffer chunk, the SDK discarded all subsequent lines in that chunk as well.
In the new implementation, invalid JSON lines are filtered out at append time, ensuring the buffer remains intact.
After testing this updated approach in the Inspector with the RC version, no errors are thrown and everything works as expected.
Integration tests have been added as well..

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.

2 participants