Model Context Protocol · sdk · fg-0532

stdio MCP server fails with 'Unexpected token ... is not valid JSON' because something printed to stdout

If you see SyntaxError: Unexpected token ... is not valid JSON in the host's MCP log, the cause and the fix for Model Context Protocol are below, last verified 2026-10-15.

Verified Oct 2026trust 50/100
Symptom
SyntaxError: Unexpected token ... is not valid JSON in the host's MCP logThe server connects in a terminal test but fails or disconnects inside the client[dotenv@17.x.x] injecting env (N) ... appears in the MCP log
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
Affected
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

Last verified 2026-10-15 · Confirmed by 0 builders

Related traps

Have your agent know this before it fails

$ /plugin marketplace add getfootgun/footgun
$ /plugin install footgun@footgun