Sub topic 11
Testing and debugging with MCP Inspector
This chapter assumes you have read the earlier chapters on JSON-RPC messages, the initialization handshake, and tools. You should also have Python 3.10 or later with the mcp package installed, and Node.js, because Inspector runs through npx.
A server that works in your head can still fail in a host. The failure may be a malformed message, a wrong path in a config file, or a tool schema the model misreads. MCP Inspector is the official developer tool for finding these problems. It acts as a minimal MCP client with a visual interface, so you can test a server without involving a host or a model.
What Inspector is
Inspector has two parts that run on your machine:
- A web interface that you open in a browser.
- A proxy process that the web interface talks to. The proxy speaks MCP to your server over stdio or HTTP.
A browser cannot launch subprocesses or open raw stdio pipes. The proxy does that on its behalf and relays messages in both directions.
Because Inspector is a real MCP client, it sends the same initialize request a host would. If your server works in Inspector, protocol-level faults are unlikely. If it fails there, the fault is in the server itself.
A server to debug
Save this as server.py. It has one tool that can fail, and it writes diagnostics to stderr.
import sys
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("debug-demo")
@mcp.tool()
def divide(a: float, b: float) -> float:
"""Divide a by b."""
print(f"divide called with a={a}, b={b}", file=sys.stderr)
return a / b
@mcp.resource("greeting://{name}")
def greeting(name: str) -> str:
"""Return a personalized greeting."""
return f"Hello, {name}!"
@mcp.prompt()
def review_code(code: str) -> str:
"""Ask the model to review a piece of code."""
return f"Please review this code:\n\n{code}"
if __name__ == "__main__":
mcp.run(transport="stdio")The print goes to stderr on purpose. With the stdio transport, stdout carries the JSON-RPC stream, so any stray text on stdout corrupts the protocol. Stderr is safe for logs.
Launching Inspector
Run Inspector and give it the command that starts your server:
npx @modelcontextprotocol/inspector python server.pyEverything after the Inspector name is the server command and its arguments. Inspector starts its proxy, prints a local URL, and usually opens your browser. The interface defaults to port 6274 and the proxy to port 6277. Recent versions generate a session token and include it in the printed URL. Open that URL rather than typing the port yourself, or the browser will not be authorized to use the proxy.
If you installed the Python SDK with its CLI extra (pip install "mcp[cli]"), a shortcut does the same job:
mcp dev server.pyIn the interface, check the connection panel. It shows the transport type, the command, and the arguments. Click Connect. A successful handshake shows the server's name and capabilities.
Walking through the interface
The interface has one tab for each server feature. Which tabs are active depends on the capabilities the server declared during initialization.
| Tab | What it exercises | What to check |
|---|---|---|
| Tools | `tools/list` and `tools/call` | Names, descriptions, and input schemas. Results, and whether `isError` is set. |
| Resources | `resources/list`, `resources/templates/list`, `resources/read` | URIs, MIME types, and the content returned for a template. |
| Prompts | `prompts/list` and `prompts/get` | Argument names and the messages the prompt produces. |
| Sampling, Roots | Client features Inspector can simulate | Whether your server handles a client that does or does not offer them. |
| Notifications | Messages the server sends on its own | Log messages, progress, and list-changed notices. |
A message history panel at the bottom of the screen records each request and response as raw JSON. Expand an entry to see the exact method, params, and id. This panel is the most useful part for protocol debugging, because it shows what actually crossed the wire.
Test a tool
- Open the Tools tab and click List Tools. You should see
divide. - Select
divide. The form is generated from the tool's input schema, so you can confirm that parameter names and types are what you intended. - Enter
a = 10andb = 4, then click Run Tool. The result contains a text item with2.5. - Run it again with
b = 0.
The second call raises ZeroDivisionError inside your function. FastMCP catches it and returns a normal JSON-RPC result with isError set to true and an error message in the content. This is the tool-execution error path. The protocol call succeeded, but the tool reported failure. The model receives this text and can react to it. A protocol error, such as calling an unknown tool name, comes back as a JSON-RPC error object instead. Telling these two apart in the history panel is a core debugging skill.
Test a resource and a prompt
On the Resources tab, open the resource templates list and choose greeting://{name}. Enter a value for name and read the resource. Check that the returned text matches your expectation and that the MIME type is sensible.
On the Prompts tab, choose review_code, supply a code argument, and get the prompt. Inspector shows the resulting message list. This is exactly what a host would insert into the conversation when the user picks the prompt.
Using the command-line mode
The web interface is awkward in scripts and continuous integration. Inspector also has a CLI mode that sends one request and prints the JSON response:
npx @modelcontextprotocol/inspector --cli python server.py --method tools/listnpx @modelcontextprotocol/inspector --cli python server.py \
--method tools/call --tool-name divide --tool-arg a=10 --tool-arg b=4The first command lists tools. The second calls divide with two arguments. Because the output is JSON, you can pipe it into jq or compare it in a test. Flag names have changed between Inspector releases, so run npx @modelcontextprotocol/inspector --help if a flag is rejected.
Connecting to an HTTP server
For a server using Streamable HTTP, choose the transport in the connection panel and enter the server's URL, for example http://localhost:8000/mcp. The path must match the one your server mounts. A wrong path usually appears as a 404 in the proxy's terminal output. If your server requires authentication, add the needed header in the panel's authentication fields.
Common failures and how to read them
| Symptom | Likely cause | What to do |
|---|---|---|
| Connection error immediately after Connect | Wrong command, or the executable is not on the proxy's `PATH` | Run the same command in a terminal. Use an absolute path to `python` or the virtual environment's interpreter. |
| Parse errors or "invalid JSON" in the history | Something wrote to stdout, such as `print()` or a library banner | Send all logs to stderr. Check imports that print on load. |
| Connects, but a tab is missing or empty | The server did not declare that capability | Inspect the `initialize` result in the history. Register at least one tool, resource, or prompt. |
| `Method not found` error | The client called a method the server does not implement | Compare the method name with the specification and with the declared capabilities. |
| `Invalid params` error | Arguments do not match the input schema | Compare the request `arguments` with the tool's `inputSchema`. |
| Tool returns `isError: true` | An exception inside your function | Read the stderr output and fix the function. |
| Request hangs, then times out | The handler blocks, or the server crashed after connecting | Check whether the process is still running. Look at stderr for a traceback. |
| Browser shows an authorization failure | The page was opened without the session token | Use the full URL printed in the terminal. |
Stderr from a stdio server appears in the Inspector interface's notification area or in the terminal that launched the proxy. When the server crashes at startup, the traceback there usually names the cause.
A debugging routine
- Start Inspector with the same command the host will use.
- Confirm the handshake. Read the
initializeresult and check the protocol version and capabilities. - List each feature and read the schemas and descriptions as the model would see them. Unclear descriptions cause wrong tool choices later.
- Call each tool with valid input, then with invalid input, then with input that triggers failures.
- Watch stderr and the history panel for each call.
- Only then add the server to a host.
The deliberate failure test in step 4 matters. A tool that has never been seen failing often returns confusing text that a model cannot act on. Make error messages specific, such as "b must not be zero".
Exercise
You add your server to a host and the host reports that the server "failed to start". In Inspector, the same command connects, but the Tools tab shows a JSON parse error whenever you run any tool. What is the most likely cause, and how do you fix it?
Show answer
Something in the server writes non-JSON text to stdout, such as a print() call or a library banner. Over stdio, stdout carries the JSON-RPC stream, so extra text corrupts the messages. Move all logging to stderr (for example print(..., file=sys.stderr) or Python's logging module configured to stderr) and run the test again in Inspector.
Exercise
In Inspector, calling divide with b = 0 returns a response with isError: true, while calling a tool named modulo (which does not exist) returns a JSON-RPC error object. What distinguishes these two failures, and which one can the model see and react to as ordinary tool output?
Show answer
The divide failure is a tool-execution error. The request was valid and reached your function, which failed, so the server returns a normal result with isError: true and explanatory content. The model sees this as tool output and can react to it. The modulo call is a protocol error. The request itself was invalid, so the server returns a JSON-RPC error object instead of a result.