DEV Community

Your MCP server won't connect — and the client won't tell you why

Kamal Mosadegh on December 01, 2026

Your MCP server won't connect — and the client won't tell you why You add an MCP server to Claude Desktop or Cursor, restart, and get......
Collapse
 
smtahosin profile image
S M Tahosin • • Edited

Mine was a cousin of #4. The server started up just fine, then printed one nice startup message to stdout and messed up the JSON-RPC stream before it even got to the initialize method. From the client's side it behaved like the server simply was not there. That is exactly what got me interested in your STDOUT_POLLUTION code, the failure you cannot see when running the server in a terminal, because in a terminal that same line looks helpful.

Collapse
 
kamalmosadegh profile image
Kamal Mosadegh •

Exactly — that's the nastiest form of it, because the evidence points the wrong way: the server runs fine, the terminal looks fine, and the client still reports nothing there. The mechanism is that the client reads stdout as a JSON-RPC stream, so one non-JSON line before initialize poisons the parse — the client never gets a valid first message, and most clients report that as 'server not responding' rather than 'your server printed something'.

In mcpdoctor the STDOUT_POLLUTION check tests exactly this distinction: it launches the server, captures stdout separately from stderr, and flags any bytes on stdout before the first {. If the first line is valid JSON-RPC, it's clean; if it's a log line, that's the diagnosis. The fix is to log to stderr or a file, never stdout — including anything a dependency prints at import time.

Was the line from your server's own startup log, or from a library it imports? The import-time variant is the harder one — the fix is usually redirecting that library's logger to stderr before the MCP server starts.

Collapse
 
smtahosin profile image
S M Tahosin • • Edited

That was my easy case, my own startup line. I had my console.log sitting before the transport connected, so that ran before initialize and fouled up the stream just like you described. Moving it to stderr fixed it for me in one quick fix, almost annoying in hindsight. The import-time variant would have taken a lot longer for me, because grepping my code would show nothing at all.

Thread Thread
 
kamalmosadegh profile image
Kamal Mosadegh •

That's exactly it — a textbook case of the STDOUT_POLLUTION killer. One console.log on stdout before initialize, and the client is trying to parse a log line as JSON-RPC, so it just goes quiet instead of telling you what happened.

And yeah, the one-line fix to stderr is the annoying part — hours of silence, one change to fix it. The import-time variant you mention is the scarier one, for exactly the reason you said: grepping your own code comes up clean, because the log isn't yours.

That's the case mcpdoctor's run stage is built to catch: it captures stderr separately, so anything on stdout before the handshake gets flagged as pollution instead of looking like a dead server. Thanks for sharing the story — this is the clearest real-world example of it I've seen.

Thread Thread
 
smtahosin profile image
S M Tahosin • • Edited

Yes, actually the client staying quiet and not raising anything regarding the failed parse, that is what made it hard for me to trust my own eyes. Terminal was clean, server was up and the single bit of data I needed was gone before I could see it. Had I been able to separate stderr out, I wouldn't have needed the whole loop.

Thread Thread
 
kamalmosadegh profile image
Kamal Mosadegh •

Exactly — the quiet client is the expensive part. The bug was one line, but the client reporting 'server not responding' sent you looking everywhere except the first line of stdout. If the client surfaced the parse failure — 'first stdout line wasn't JSON' — the whole loop collapses to one look. That's the bet behind the run stage: capture stdout and stderr separately, and name the cause instead of letting silence do the talking.

Collapse
 
agentsearchhq profile image
AgentSearch •

One variant of #4 that might deserve its own cause code: npx-launched servers on first run. The package has to download before the server says anything, and some clients give up on initialize before that finishes, so the second launch works and the bug looks unreproducible. Running the npx command once in a terminal (or pinning a version) warms the cache.

Does the run stage time out at roughly what the clients use, or does it wait as long as it takes? That difference could hide exactly this one.

Collapse
 
kamalmosadegh profile image
Kamal Mosadegh •

That's a sharp catch — and yes, it can hide exactly there. The run stage waits 10 seconds for the initialize answer by default (--timeout to change it), so a first-run npx download that takes longer than that comes back TIMEOUT, and the second run, cache warm, comes back Healthy. Same server, two verdicts, and the bug looks unreproducible. You're right that it deserves its own cause code — TIMEOUT doesn't say whether the server was slow or npx was still downloading. Warming the cache first (or pinning the version) is the right workaround for now; I'll add the distinct cause.

Collapse
 
agentsearchhq profile image
AgentSearch •

That flip from TIMEOUT to Healthy on the second run is exactly what makes it look unreproducible. A separate code for "still installing" vs "server slow" would save people a lot of guessing.

Thread Thread
 
kamalmosadegh profile image
Kamal Mosadegh •

That's exactly the distinction the code needs to see. 'Still installing' means the process is alive and pulling the package — cold npx cache, network in flight. 'Server slow' means the process is up but not answering initialize. The way to separate them is to check whether the child is still making progress on the download vs sitting idle past the timeout — that's the cause code to add next. Thanks for pinning the symptom down: the TIMEOUT-to-Healthy flip was the most confusing part of the whole launch thread.

Thread Thread
 
agentsearchhq profile image
AgentSearch •

Agreed, watching the child beats a fixed timeout. If npm is still writing to stderr or its cache, give it more time; if the process is alive but silent after initialize, it's the server. Running npx -y @agentsearchhq/agentsearch-mcp once beforehand to warm the cache also takes the first-run case off the table.

Thread Thread
 
kamalmosadegh profile image
Kamal Mosadegh •

Good call — pre-warming the cache turns the first run from a two-variable problem into a one-variable problem. After a warm cache, if initialize still doesn't arrive, the install is out of the picture and it's the server.

The discriminator stays the same, just cleaner: stderr writes or cache growth means it's still installing, give it more time; alive but silent after initialize means the server never answered. One limit worth stating: the warm cache is per-machine (npx cache is local), so a fresh machine still pays the download on its first run — the pre-warm only helps where the cache already exists.

Collapse
 
kling4ai-production profile image
Kling4 AI •

That GUI PATH gap trips up so many first-time setups—running npx fine in your terminal only for the desktop app to fail silently because it never loaded your shell PATH. Testing the actual startup handshake saves hours of staring at config JSON.

Collapse
 
kamalmosadegh profile image
Kamal Mosadegh •

Exactly — the PATH gap is the one that makes people distrust their own config, when the config was never the problem. The handshake test is the fastest way to find out which side is lying: if initialize never answers, no amount of JSON editing will help.

Collapse
 
userxnom profile image
Abdullah Naseer •

The GUI PATH gap is the one that has bitten me — your terminal finds npx fine, so you assume the desktop app sees the same world, and it doesn't. Checking the command against both PATHs instead of guessing is the right fix. The question the run stage leaves me with: the handshake proves the server starts and speaks JSON-RPC, but what about the server that connects fine and then returns an empty tools/list, or never answers tools/list at all? Does mcpdoctor treat "connected" and "useful" as different verdicts, or does a healthy handshake mean the server passes?

Collapse
 
kamalmosadegh profile image
Kamal Mosadegh •

Great question — and the answer is split. If tools/list never answers, that's its own verdict: TOOLS_LIST_FAILED, distinct from a healthy handshake. But an empty list is the gap: right now it still comes back Healthy, just with '0 tool(s)' in the message. So the count is visible, but there's no separate warning for a server that connects and offers nothing. That's a fair catch — worth adding as its own cause code, because a 0-tool server is connected but not useful.

Collapse
 
nilesharma0108 profile image
Nilesh Sharma •

The STDOUT_POLLUTION cause code is the one I'd expect to catch the most people. The stdio section of the MCP spec explains why it's so quiet: a server MUST NOT write anything to stdout that isn't a valid MCP message, logging is meant to go to stderr, and the client MAY capture, forward or simply ignore stderr. So one stray print() breaks the JSON-RPC stream, and the only clue can end up in a stream the client throws away. Running the real initialize -> tools/list handshake with stderr captured separately is exactly the right way to surface that.

Collapse
 
syedfahads profile image
Syed Fahad •

one more silent one for the list: relative paths in args. the desktop app doesn't start in your project folder, so ./server.py points somewhere else and the server just never shows up

Collapse
 
kamalmosadegh profile image
Kamal Mosadegh •

Good catch — that's a real silent one. The desktop app doesn't start in your project folder, so ./server.py resolves somewhere else and the server just never shows up, with no error pointing at the path. The fix belongs with the others: the check should resolve the args against the project folder — or report the absolute path it actually tried — before the client launches. Adding it to the list: relative-path args get their own cause code.