Skip to content

SDK Crashes on Hot Reload Due to Invalid JSON in Stdio Stream #700

Closed

Description

@saharis9988

Describe the bug
When using the MCP Inspector in development, I want to automatically restart the server between code iterations to improve developer experience (DX), without having to restart it manually.
To achieve this, I used tsx to watch for file changes and restart the process. However, after hot reloading, the SDK throws a JSON parsing error.

To Reproduce
Steps to reproduce the behavior:
1.Open an MCP server project
2.Use tsx, nodemon, or a similar tool to hot reload the server and run it with the MCP Inspector
3.Make a change to any source file to trigger a reload
4.Observe JSON parsing errors in the inspector process console

Expected behavior
No errors should be thrown in the console between reloads. The inspector should continue to function correctly after each restart.

Logs

Error from MCP server:  
SyntaxError: Unexpected token 'G', "Gracefully"... is not valid JSON  
    at JSON.parse (<anonymous>)  
    at deserializeMessage (.../sdk/dist/esm/shared/stdio.js:26:44)  
    at ReadBuffer.readMessage (.../sdk/dist/esm/shared/stdio.js:19:16)  
    at StdioClientTransport.processReadBuffer (.../sdk/dist/esm/client/stdio.js:127:50)  
    at Socket.<anonymous> (.../sdk/dist/esm/client/stdio.js:98:22)  
    ...

Additional context
The error occurs during message deserialization in the SDK and is likely caused by non-JSON output being sent over the stdio stream during shutdown or restart (e.g., messages like "Gracefully restarting..." from tsx).
A possible solution is to add a guard that checks whether a line from the stdio stream is valid JSON before attempting to parse it, and gracefully skip non-JSON lines.
Let me know if you’d prefer a different approach—I'd be happy to submit a PR with a fix 🙏

Image

Activity

  1. saharis9988 commented on Jul 8, 2025

    @saharis9988
    ContributorAuthor

    Hi @ihrpr,
    Happy to help push this along with a PR if that works for you.
    I’d be happy to contribute! 😊

  2. added
    good first issueGood for newcomers - can be tackled without deep knowledge of the codebase
    P1Significant bug affecting many users, highly requested feature
    on Oct 8, 2025
  3. ochafik commented on Oct 13, 2025

    @ochafik
    Contributor

    @saharis9988 Thanks for reporting this.

    I'd be a bit weary of hiding transport-level issues if we skip invalid lines.

    If you need a dirty workaround, you could wrap your tsx --watch command in this stricter_stdio bash script (

    #!/bin/bash
    # Usage: ./stricter_stdio npx -y --silent tsx --watch ....ts
    set -euo pipefail
    
    "$@" | while read -r line; do echo "$line" | jq -c . 2>/dev/null || echo "$line" >&2; done
  4. removed
    good first issueGood for newcomers - can be tackled without deep knowledge of the codebase
    on Dec 18, 2025
  5. added a commit that references this issue on Mar 25, 2026
    55a5dc4
  6. moved this from To triage to Done in 🐛 🛠on Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1Significant bug affecting many users, highly requested featurebugSomething isn't workingready for workEnough information for someone to start working on

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions