Model Context Protocol · sdk · fg-0532
stdio MCP server fails with 'Unexpected token ... is not valid JSON' because something printed to stdout
SyntaxError: Unexpected token ... is not valid JSON in the host's MCP log
Verified 2026-10-15 · Confirmed by 0 builders · Trust 50/100
Cause
On stdio, stdout is the JSON-RPC wire, so any line that is not a protocol message corrupts it. A console.log or print in your code or a dependency is enough; dotenv 16.6.0 and 17.x log a line to stdout on load by default.
Fix
Log only to stderr (console.error, the logging module), silence chatty dependencies, or upgrade dotenv to 18, which logs to stderr. The first character quoted in the error usually points at the stray line.
Apply
require('dotenv').config({ quiet: true }); // dotenv 16.6 to 17.x print to stdout by default
console.error('server ready'); // never console.log in a stdio serverAffects: Any stdio MCP server; dotenv 16.6.0 and 17.0.0 to 17.4.2 (16.6.1 defaulted to quiet); dotenv 18.0.0 moved the message to stderr
Sources
- https://modelcontextprotocol.io/specification/2026-07-28/basic/transports/stdio
- https://modelcontextprotocol.io/docs/2026-07-28/develop/build-server
- https://github.com/modelcontextprotocol/typescript-sdk/blob/main/docs/troubleshooting.md
- https://github.com/motdotla/dotenv/blob/master/CHANGELOG.md
- https://github.com/motdotla/dotenv/issues/903
Have your agent know this before it fails
claude mcp add --transport http footgun https://getfootgun.com/api/mcp