Skip to content

Error from MCP server: SyntaxError: Unexpected token 'E', "Error: typ"... is not valid JSON #409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
abdulsaminexusaeon opened this issue Apr 2, 2025 · 9 comments

Comments

@abdulsaminexusaeon
Copy link

abdulsaminexusaeon commented Apr 2, 2025

When i run the app with
cmd = mcp dev server.py

then when i click on connect button i got this issue

Stdio transport: command=C:\Users\User\AppData\Local\Programs\Python\Python310\Scripts\uv.exe, args=run,--with,mcp,mcp,run,server.py
Spawned stdio transport
Connected MCP client to backing server transport
Created web app transport
Created web app transport
Set up MCP proxy
Error from MCP server: SyntaxError: Unexpected token 'E', "Error: typ"... is not valid JSON
at JSON.parse ()
at deserializeMessage (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/shared/stdio.js:26:44)
at ReadBuffer.readMessage (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/shared/stdio.js:19:16)
at StdioClientTransport.processReadBuffer (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/client/stdio.js:114:50)
at Socket. (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/client/stdio.js:93:22)
at Socket.emit (node:events:518:28)
at addChunk (node:internal/streams/readable:561:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
at Readable.push (node:internal/streams/readable:392:5)
at Pipe.onStreamRead (node:internal/stream_base_commons:189:23)
Error from MCP server: SyntaxError: Unexpected token 'E', "Error: typ"... is not valid JSON
at JSON.parse ()
at deserializeMessage (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/shared/stdio.js:26:44)
at ReadBuffer.readMessage (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/shared/stdio.js:19:16)
at StdioClientTransport.processReadBuffer (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/client/stdio.js:114:50)
at Socket. (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/client/stdio.js:93:22)
at Socket.emit (node:events:518:28)
at addChunk (node:internal/streams/readable:561:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
at Readable.push (node:internal/streams/readable:392:5)
at Pipe.onStreamRead (node:internal/stream_base_commons:189:23)
Error from MCP server: SyntaxError: Unexpected token 'E', "Error: typ"... is not valid JSON
at JSON.parse ()
at deserializeMessage (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/shared/stdio.js:26:44)
at ReadBuffer.readMessage (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/shared/stdio.js:19:16)
at StdioClientTransport.processReadBuffer (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/client/stdio.js:114:50)
at Socket. (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/client/stdio.js:93:22)
at Socket.emit (node:events:518:28)
at addChunk (node:internal/streams/readable:561:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
at Readable.push (node:internal/streams/readable:392:5)
at Pipe.onStreamRead (node:internal/stream_base_commons:189:23)
Error from MCP server: SyntaxError: Unexpected token 'E', "Error: typ"... is not valid JSON
at JSON.parse ()
at deserializeMessage (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/shared/stdio.js:26:44)
at ReadBuffer.readMessage (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/shared/stdio.js:19:16)
at StdioClientTransport.processReadBuffer (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/client/stdio.js:114:50)
at Socket. (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/client/stdio.js:93:22)
at Socket.emit (node:events:518:28)
at addChunk (node:internal/streams/readable:561:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
at Readable.push (node:internal/streams/readable:392:5)
at Pipe.onStreamRead (node:internal/stream_base_commons:189:23)
Error from MCP server: SyntaxError: Unexpected token 'E', "Error: typ"... is not valid JSON
at JSON.parse ()
at deserializeMessage (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/shared/stdio.js:26:44)
at ReadBuffer.readMessage (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/shared/stdio.js:19:16)
at StdioClientTransport.processReadBuffer (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/client/stdio.js:114:50)
at Socket. (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/client/stdio.js:93:22)
at Socket.emit (node:events:518:28)
at addChunk (node:internal/streams/readable:561:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
at Readable.push (node:internal/streams/readable:392:5)
at Pipe.onStreamRead (node:internal/stream_base_commons:189:23)
Received message for sessionId ac082c68-292e-4cbd-b85e-42046b434b38
Error in /message route: Error: SSE connection not established
at SSEServerTransport.handlePostMessage (file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/server/sse.js:53:19)
at file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/inspector/server/build/index.js:129:25
at Layer.handle [as handle_request] (C:\Users\User\AppData\Local\npm-cache_npx\5a9d879542beca3a\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\User\AppData\Local\npm-cache_npx\5a9d879542beca3a\node_modules\express\lib\router\route.js:149:13)
at Route.dispatch (C:\Users\User\AppData\Local\npm-cache_npx\5a9d879542beca3a\node_modules\express\lib\router\route.js:119:3)
at Layer.handle [as handle_request] (C:\Users\User\AppData\Local\npm-cache_npx\5a9d879542beca3a\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\User\AppData\Local\npm-cache_npx\5a9d879542beca3a\node_modules\express\lib\router\index.js:284:15
at Function.process_params (C:\Users\User\AppData\Local\npm-cache_npx\5a9d879542beca3a\node_modules\express\lib\router\index.js:346:12)
at next (C:\Users\User\AppData\Local\npm-cache_npx\5a9d879542beca3a\node_modules\express\lib\router\index.js:280:10)
at cors (C:\Users\User\AppData\Local\npm-cache_npx\5a9d879542beca3a\node_modules\cors\lib\index.js:188:7)
node:_http_outgoing:699
throw new ERR_HTTP_HEADERS_SENT('set');
^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (node:_http_outgoing:699:11)
at ServerResponse.header (C:\Users\User\AppData\Local\npm-cache_npx\5a9d879542beca3a\node_modules\express\lib\response.js:794:10)
at ServerResponse.json (C:\Users\User\AppData\Local\npm-cache_npx\5a9d879542beca3a\node_modules\express\lib\response.js:275:10)
at file:///C:/Users/User/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/inspector/server/build/index.js:133:25
at process.processTicksAndRejections (node:internal/process/task_queues:105:5) {
code: 'ERR_HTTP_HEADERS_SENT'
}

Node.js v22.14.0

@eug
Copy link

eug commented Apr 6, 2025

I encountered the same issue.

The error message I got was (like yours):
Error from MCP server: SyntaxError: Unexpected token 'E', "Error: typ"... is not valid JSON

It seems related to the following error:
Error: typer is required. Install with 'pip install mcp[cli]'

In my case, the problem was that the required packages weren’t installed in the environment I was using. I resolved it by running:

pip install -U mcp[cli]
pip install -U typer

After that, everything worked as expected.

For reference, here are the package versions I'm using:

pip show mcp typer     
Name: mcp
Version: 1.6.0
Summary: Model Context Protocol SDK
Home-page: https://modelcontextprotocol.io
Author: Anthropic, PBC.
Author-email:
License: MIT
Location: C:\Users\%USER%\AppData\Local\Programs\Python\Python312\Lib\site-packages
Requires: anyio, httpx, httpx-sse, pydantic, pydantic-settings, sse-starlette, starlette, uvicorn
Required-by:
---
Name: typer
Version: 0.15.2
Summary: Typer, build great CLIs. Easy to code. Based on Python type hints.
Home-page: https://github.com/fastapi/typer
Author:
Author-email: =?utf-8?q?Sebasti=C3=A1n_Ram=C3=ADrez?= <[email protected]>
License:
Location: C:\Users\%USER%\AppData\Local\Programs\Python\Python312\Lib\site-packages
Requires: click, rich, shellingham, typing-extensions
Required-by: fastapi-cli

Another common possibility is a Python version mismatch:
Your system’s default Python version (used by pip when installing packages globally) is 3.YY, while the uv tool is executing commands using Python 3.XX. As a result, when you ran uv run --with mcp mcp run main.py, it will attempt to use Python 3.XX. However, the typer package was installed in the Python 3.YY site-packages directory, making it inaccessible to the Python 3.YY environment.

@ASaiAnudeep
Copy link

I'm also facing similar issues

2025-04-10 17:16:00.908 [error] iles: Client error for command Unexpected number in JSON at position 2 (line 1 column 3)
2025-04-10 17:16:00.908 [error] iles: Error in MCP: Unexpected number in JSON at position 2 (line 1 column 3)
2025-04-10 17:16:00.908 [error] iles: Client error for command Unexpected token 'L', ..."          ListToolsR"... is not valid JSON
2025-04-10 17:16:00.908 [error] iles: Error in MCP: Unexpected token 'L', ..."          ListToolsR"... is not valid JSON
2025-04-10 17:16:00.909 [info] listOfferings: Found 1 tools

@wyxpku
Copy link

wyxpku commented Apr 11, 2025

Got the same error on mac, python=3.11.11

Stdio transport: command=/opt/homebrew/bin/uv, args=run,--with,mcp,mcp,run,server.py
Spawned stdio transport
Connected MCP client to backing server transport
Created web app transport
Created web app transport
Set up MCP proxy
Received message for sessionId 3340924a-e289-4d42-9af5-2377ae3e9994
Error in /message route: Error: SSE connection not established
    at SSEServerTransport.handlePostMessage (file:///Users/ethan/.npm/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/server/sse.js:53:19)
    at file:///Users/ethan/.npm/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/inspector/server/build/index.js:130:25
    at Layer.handle [as handle_request] (/Users/ethan/.npm/_npx/5a9d879542beca3a/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/ethan/.npm/_npx/5a9d879542beca3a/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/ethan/.npm/_npx/5a9d879542beca3a/node_modules/express/lib/router/route.js:119:3)
    at Layer.handle [as handle_request] (/Users/ethan/.npm/_npx/5a9d879542beca3a/node_modules/express/lib/router/layer.js:95:5)
    at /Users/ethan/.npm/_npx/5a9d879542beca3a/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/ethan/.npm/_npx/5a9d879542beca3a/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/ethan/.npm/_npx/5a9d879542beca3a/node_modules/express/lib/router/index.js:280:10)
    at cors (/Users/ethan/.npm/_npx/5a9d879542beca3a/node_modules/cors/lib/index.js:188:7)
node:_http_outgoing:699
    throw new ERR_HTTP_HEADERS_SENT('set');
          ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (node:_http_outgoing:699:11)
    at ServerResponse.header (/Users/ethan/.npm/_npx/5a9d879542beca3a/node_modules/express/lib/response.js:794:10)
    at ServerResponse.json (/Users/ethan/.npm/_npx/5a9d879542beca3a/node_modules/express/lib/response.js:275:10)
    at file:///Users/ethan/.npm/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/inspector/server/build/index.js:134:25
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5) {
  code: 'ERR_HTTP_HEADERS_SENT'
}

Node.js v23.10.0

@subipranuvem
Copy link

Any solutions? I got the same problem as @wyxpku ....

@wyxpku
Copy link

wyxpku commented Apr 11, 2025

Any solutions? I got the same problem as @wyxpku ....

modelcontextprotocol/inspector#295

someone fixed it, but not merged yet

@subipranuvem
Copy link

I tried to use it using the VsCode Simple Browser... It worked, I don't know how ... But it's a "trick" while fix isn't merged...

Image

But, the Inspector still doesn't work in my browsers (Chrome, Edge and Firefox).

@cliffhall
Copy link

Any solutions? I got the same problem as @wyxpku ....

modelcontextprotocol/inspector#295

someone fixed it, but not merged yet

We're waiting for steps to reproduce.

@cliffhall
Copy link

cliffhall commented Apr 19, 2025

OBSERVATIONS

OUTPUT RELEVANT TO THIS ISSUE

@abdulsaminexusaeon

Stdio transport: command=C:\Users\User\AppData\Local\Programs\Python\Python310\Scripts\uv.exe, args=run,--with,mcp,mcp,run,server.py
Spawned stdio transport
Connected MCP client to backing server transport
Created web app transport
Created web app transport
Set up MCP proxy
Error from MCP server: SyntaxError: Unexpected token 'E', "Error: typ"... is not valid JSON
at JSON.parse ()

@eug

Error from MCP server: SyntaxError: Unexpected token 'E', "Error: typ"... is not valid JSON

@ASaiAnudeep

2025-04-10 17:16:00.908 [error] iles: Client error for command Unexpected number in JSON at position 2 (line 1 column 3)
2025-04-10 17:16:00.908 [error] iles: Error in MCP: Unexpected number in JSON at position 2 (line 1 column 3)
2025-04-10 17:16:00.908 [error] iles: Client error for command Unexpected token 'L', ..." ListToolsR"... is not valid JSON
2025-04-10 17:16:00.908 [error] iles: Error in MCP: Unexpected token 'L', ..." ListToolsR"... is not valid JSON
2025-04-10 17:16:00.909 [info] listOfferings: Found 1 tools

CONCLUSION

What's happening is that something other than a valid JSONRPCMessage is being emitted from those servers on STDOUT. They are writing plain error or log messages.

The spec for STDIO servers says:

The server MUST NOT write anything to its stdout that is not a valid MCP message.

If you want to log messages from your STDIO server, you can do that on on STDERR.

@rioultf
Copy link

rioultf commented Apr 24, 2025

Got it working using the venv generated by : (see here

uv init mcp-server-demo
cd mcp-server-demo/
uv add "mcp[cli]"
mcp dev mcp-server-demo.py

After that I can connect to http://127.0.0.1:6274 and test the server.

The server has to be launched in the correct environment.

This is also true when using AnythingLLM is you want him to correctly launch your servers.

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

No branches or pull requests

7 participants