Sub topic 10
Debugging with the MCP Inspector
This chapter assumes you have written the minimal add server from the previous chapter and know the basics of JSON-RPC messages, the stdio transport, and the mcpServers configuration. You will learn to test a server by itself, before the assistant is involved.
Why debug outside the assistant
When a tool fails inside an assistant, several things could be wrong: the server, the configuration, the model's choice of arguments, or the client. Testing the server alone removes the model and the host from the picture. If the server works under the Inspector, the fault is almost always in your configuration or in how the model uses the tool.
The MCP Inspector is a developer tool that starts your server, connects to it as a client, and shows a web UI for listing and calling its tools, resources, and prompts.
Starting the Inspector
Pass the same command you would put in the command and args fields of your configuration:
npx @modelcontextprotocol/inspector python server.pyThe Inspector launches python server.py over stdio, prints a local URL, and opens the UI in your browser. Run it from the directory that contains server.py, because the relative path is resolved from where you start it.
A debugging session
Work through the server in this order.
- Open the connection. The Inspector performs the initialization handshake. If it cannot connect, the server failed to start or exited early. Read the error output first.
- Open the Tools tab and choose List Tools. You should see
add, its description (from the docstring), and its input schema (from the type hints). - Select
add, entera = 2andb = 3, and run it. The result should contain5. - Check the message history. The Inspector shows each request and response, so you can see the exact JSON-RPC traffic.
The call you made corresponds to this request:
{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "add", "arguments": {"a": 2, "b": 3}}}The matching response carries the same id. Compare the request the Inspector sends with the one your assistant sends (see the logs section below). A difference in argument names or types often explains a failing tool.
Reading logs
With stdio, the protocol uses the server's standard output. Anything else you write to stdout corrupts the message stream. A stray print() in a tool is a common cause of "invalid JSON" or connection errors.
Write diagnostic output to standard error instead:
import sys
print("add called", file=sys.stderr)The Inspector shows stderr output from the server, and most assistants write it to a log file or a log panel. Check your assistant's documentation for where its MCP logs are stored.
Common failures
| Symptom | Likely cause | What to check |
|---|---|---|
| Inspector cannot connect | Wrong command, missing dependency, or wrong working directory | Run `python server.py` directly and read the error |
| Connection drops right after start | Output printed to stdout, or a crash during import | Move prints to stderr and read the traceback |
| Tool missing from the list | Decorator missing, or the server file is not the one you launched | Confirm the file path and the `@mcp.tool()` decorator |
| Works in the Inspector, fails in the assistant | Different environment, path, or `env` values | Compare the `command`, `args`, and `env` in your configuration |
| Tool call returns an error | Wrong argument types or an exception in the function | Read the error in the response and the stderr output |
Exercises
Exercise
Your server works when you run it under the Inspector, but the assistant reports that it failed to connect. Name two things you would compare between the two setups.
Show answer
Compare the command and arguments, including the working directory and relative paths, and compare the environment: the PATH and the values in the env field. The assistant may launch the server from a different directory or with a different PATH than your terminal.
Exercise
A tool you added a print("debug") call to now makes the stdio connection fail with a JSON error. Why, and how do you fix it?
Show answer
With stdio, the server's stdout carries the JSON-RPC messages, so the extra text corrupts the stream. Write debug output to stderr instead, for example print("debug", file=sys.stderr).