Sub topic 11
Testing and Debugging with MCP Inspector
This chapter assumes you have read the earlier chapters on JSON-RPC, session initialization, and building a minimal server. You should be able to run Python scripts and use a terminal. You also need Node.js installed, because the Inspector runs through npx.
Why a separate debugging tool
When a server fails inside a host such as Claude Desktop, the host often shows only a vague error. The server may have crashed at startup, printed to the wrong stream, or returned a malformed result. MCP Inspector is a developer tool that acts as a plain client. It connects to your server, shows every message, and lets you call tools by hand. It removes the host and the model from the picture, so you can test the server alone.
Starting the Inspector
Save the server from the previous chapter as server.py. Then run the Inspector and give it the command that starts your server:
npx @modelcontextprotocol/inspector python server.pyThe Inspector starts a local web interface and prints its address in the terminal. Open that address in a browser. The Inspector launches python server.py as a subprocess and talks to it over stdio, the same way a host would.
You can also point it at a server that uses Streamable HTTP. Choose that transport in the connection panel and enter the server's URL.
What you see in the interface
Click Connect. The Inspector sends an initialize request, then lists what the server offers. The main tabs map to the server features you already know:
| Tab | What it does |
|---|---|
| Tools | Lists tools, shows their input schemas, and lets you fill in arguments and run them |
| Resources | Lists resources and resource templates, and reads their content |
| Prompts | Lists prompts and lets you fetch one with arguments |
| Notifications | Shows server notifications and log messages |
A history pane records each request and response as raw JSON-RPC. This is the most useful part for debugging, because you can compare the real messages with the formats you learned earlier.
A first debugging session
- Connect and confirm the status shows connected. If it does not, read the error pane first.
- Open the Tools tab and select
add. - Enter
a = 2andb = 3, then run it. - Check the history. You should see a
tools/callrequest and a result with the sameid.
placeholderThe expected result matches the earlier example: a content list holding the text 5, with isError set to false.
Common errors and how to read them
- Connection fails immediately. The server probably crashed on startup. Run
python server.pydirectly and look for an import error or a missing package. - Parse errors or a hung handshake over stdio. With stdio, standard output carries the protocol. A stray
print()in your server corrupts the stream. Write diagnostics to standard error, or use the logging utility. - Tool missing from the list. Check that the decorator is applied and that the file you launched is the one you edited.
- Result has
isError: true. The tool ran but failed. Read the text content for the reason, then fix the tool's input handling. - Wrong command or path. The Inspector runs your command from its own working directory. Use absolute paths if relative ones fail.
Using the Inspector with your host config
Once the server works in the Inspector, you can reuse the same command and arguments in the host's mcpServers file. If the server works in the Inspector but fails in the host, the cause is usually the environment. The host may use a different PATH, a different Python interpreter, or a different working directory. Setting an absolute path to the interpreter in command fixes most of these cases.
Exercise
Your server works when you run python server.py by hand, but the Inspector shows a failed handshake after you add a print("starting") line at the top of the file. What is wrong, and how do you fix it?
Show answer
Over stdio, standard output carries the JSON-RPC messages, so the print text corrupts the stream and the client cannot parse it. Send the message to standard error with print("starting", file=sys.stderr) (after import sys) or use the logging module.