fix: prevent server crash on JSON-RPC requests with non-scalar id#72
Open
fuleinist wants to merge 1 commit into
Open
fix: prevent server crash on JSON-RPC requests with non-scalar id#72fuleinist wants to merge 1 commit into
fuleinist wants to merge 1 commit into
Conversation
When the MCP SDK receives a JSON-RPC request whose 'id' field is a non-scalar (e.g. an object), it creates an error response that either drops the id (if id was undefined) or crashes the server when JSON.stringify throws on a non-serialisable object id. The crash path: Session.handleRequest() creates an error response with the invalid id, JSON.stringify() throws, the StdioServerTransport emits an 'error' event, the top-level error handler calls cleanup(), which calls process.exit(0). Fix: install a process.stdout.write guard BEFORE the MCP server connects the transport. Every JSON string written to stdout is intercepted, parsed, and validated. If the 'id' field in a JSON-RPC response is not a valid JSON-RPC type (string | number | null), it is replaced with null before the string reaches the OS-level stdout buffer. This is a belt-and-suspenders guard: it is harmless for valid responses (no-op) and prevents the crash for the invalid-id path. Fixes: 21st-dev#71
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
When the MCP SDK receives a JSON-RPC request whose
idfield is a non-scalar (e.g. an object like{"bad":"id"}), the SDK creates an error response that either drops theid(if it wasundefined) or causesJSON.stringifyto throw when it encounters a non-serialisable objectid.The crash path:
Session.handleRequest()creates an error response with the invalididJSON.stringify()throws (or omitsidleaving an invalid response)StdioServerTransportemits an'error'event.catch()handler callscleanup()→process.exit(0)The server also crashes on valid-but-unsupported
idtypes likeboolean.Fix
Install a
process.stdout.writeguard before the MCP server connects the transport. Every JSON string written to stdout is intercepted, parsed, and validated. If theidfield in a JSON-RPC response is not a valid JSON-RPC type (string | number | null), it is replaced withnullbefore the string reaches the OS-level stdout buffer.This is a belt-and-suspenders guard that is completely harmless for valid responses (no-op) and prevents the crash for the invalid-
idpath.Before (crash):
After (safe):
Changes
src/utils/safe-transport.ts: New module withinstallStdoutWriteGuard()— patchesprocess.stdout.writeto validate and sanitise JSON-RPC responseidfieldssrc/index.ts: CallinstallStdoutWriteGuard()beforeserver.connect(transport)src/utils/safe-transport.test.ts: 21 tests coveringisValidId,looksLikeJsonRpcResponse, andsanitiseJsonRpcIdTesting
npm test→ 21/21 tests green (2 test suites)Issue
Fixes #71.