Topic 5 of 5 · 4 min read
Debugging failed MCP connections and tool calls
Goal: Read MCP connection and tool-call logs and identify whether a failure comes from transport, auth, schema or the server itself.
When an MCP server fails, find which layer broke before changing anything: transport, authentication, schema, or the server's own code. Each layer leaves a different mark in the logs, and each has a different fix.
Check yourself · 1 of 3
A log shows 401 invalid_token from a remote MCP server. Which layer failed?
Why it matters
Agents report most failures the same way: "tool unavailable" or "call failed". Guessing leads to reinstalling things that were fine. A two-minute look at the right log usually names the layer.
The four layers
- Transport. The process did not start, the URL is unreachable or the connection dropped. Marks:
command not found,ENOENT,ECONNREFUSED, a timeout during startup. Fix the command path, the runtime version, the URL or the network. - Auth. The server is reached but refuses you. Marks:
401,403,invalid_token,token expired. Re-authenticate, check the variable is set in the shell that launched the agent, and check the token's scopes. - Schema. The call reached the server but the arguments were rejected. Marks:
-32602 Invalid params,required property, a type error. The model sent wrong arguments, or the tool's schema changed after an update. Compare the definition with what was sent. - Server. The tool ran and failed inside. Marks: a stack trace, a database error, an upstream
500. This is a bug or a bad input for the server, not a connection problem.
Where to look
- The agent's own MCP view and debug mode. In Claude Code,
/mcpshows status andclaude --mcp-debugprints details. - The server's stderr. A stdio server writes logs there, never to standard output, which carries the protocol.
- The MCP Inspector. It runs the server by itself and lets you call a tool by hand, so you can tell whether the fault is the server or the agent.
npx @modelcontextprotocol/inspector npx -y @example/db-mcp
Before and after
Weak: the call fails, so you reinstall the server three times.
Fixed: read the log first.
[issues] connecting https://mcp.example.com/mcp
[issues] POST /mcp -> 401 {"error":"invalid_token","detail":"expired"}
That line is auth. Re-run sign-in; reinstalling would not have helped.
What goes wrong
- Printing debug text to standard output in a custom server, which corrupts the protocol stream.
- A variable set in one terminal but not in the one the agent started from.
- A slow server hitting the startup timeout and being marked as failed.
Examples
Server will not start
The log says spawn npx ENOENT. That is transport: the agent cannot find npx, often because it runs with a different PATH than your terminal. You give the command's full path in the config.
Call fails after an upgrade
After a server update, create_ticket returns Invalid params: missing required property priority. The schema gained a required field. The agent was still using the old shape from a cached definition. Reconnect the server so the new schema loads.
Key points
- Classify the failure first: transport, auth, schema or the server itself.
- Status codes like
401orinvalid_tokenpoint to auth;Invalid paramspoints to schema. - A stdio server logs to stderr, since standard output carries the protocol messages.
- The MCP Inspector calls a tool without the agent, which separates server faults from agent faults.
- Check that environment variables are set in the shell that launched the agent.
Common mistakes
- Reinstalling or upgrading before reading any log.
- Testing in a terminal where the secret variable is set, while the agent runs without it.
- Logging to standard output from a stdio server.
- Blaming the model for a failure that the Inspector shows is in the server.
Check yourself · 2 of 3
Where should a stdio MCP server write its debug messages?
Check yourself · 3 of 3
What is the MCP Inspector most useful for?
Practice items, not counted towards your mastery.
Start here
- Connect Claude Code to tools via MCP (Claude Code docs) (opens in a new tab)DocsFree
Exact commands, scopes, environment-variable expansion and authentication for adding servers.
- MCP security best practices (opens in a new tab)DocsFree
The protocol's own list of attacks on servers and clients, with the mitigations to apply.
- MCP Inspector (opens in a new tab)DocsFree
A standalone tool to call a server's tools by hand and see raw requests and errors.