Repository navigation
Error from MCP server: SyntaxError: Unexpected token 'E', "Error: typ"... is not valid JSON #409
Description
Activity
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 JSONIt 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?= <tiangolo@gmail.com> License: Location: C:\Users\%USER%\AppData\Local\Programs\Python\Python312\Lib\site-packages Requires: click, rich, shellingham, typing-extensions Required-by: fastapi-cliAnother common possibility is a Python version mismatch:
Your system’s default Python version (used by pip when installing packages globally) is 3.YY, while theuvtool is executing commands using Python 3.XX. As a result, when you ranuv run --with mcp mcp run main.py, it will attempt to use Python 3.XX. However, thetyperpackage was installed in the Python 3.YY site-packages directory, making it inaccessible to the Python 3.YY environment.Reacted by FuYungI'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 toolsGot 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.0Any solutions? I got the same problem as @wyxpku ....
Any solutions? I got the same problem as @wyxpku ....
modelcontextprotocol/inspector#295
someone fixed it, but not merged yet
Reacted by Subi pra NuvemAny 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.
OBSERVATIONS
- The server @abdulsaminexusaeon, @eug, and @ASaiAnudeep showed are using STDIO transport.
- The error @wyxpku and @subipranuvem reported was not the same error, but instead the one mentioned in the inspector PR, regarding
ERR_HTTP_HEADERS_SENT.
OUTPUT RELEVANT TO 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 ()Error from MCP server: SyntaxError: Unexpected token 'E', "Error: typ"... is not valid JSON
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 toolsCONCLUSION
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.
Reacted by Babak Naffas, Alexandre D. Roberge, Yannick Gibson and Iris MuGot 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
AnythingLLMis you want him to correctly launch your servers.Reacted by Cliff Hall and RAIMUNDO VALTERFor anyone encountering this issue
[error] unable to parse stdout from codex mcp: SyntaxError: Unexpected token '?', "?[?2004h" is not valid JSON, it may be due to setting a custom Codex CLI binary in your VSCode settings. Just removing that would help.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
AnythingLLMis you want him to correctly launch your servers.this worked for me, i forgot to initialize uv project

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