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 server

Affects: 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

Have your agent know this before it fails

claude mcp add --transport http footgun https://getfootgun.com/api/mcp