Day 3, 25 minutes · beginner
Why it matters
A stock check only reads. Romy also wants a tool that does something: put a book on her wish list when a customer asks for a title she does not have. Tools that change things are where MCP gets useful, and also where things go wrong. The customer asks for a book that is on the shelf, or the request makes no sense, and the tool has to say no in a way the model understands.
Today you add a second tool that writes to a small local file, and you make it refuse a request with a proper tool error instead of a crash. You test both paths with the Inspector, once on the command line and once in the Inspector's web client in your browser. At the end Romy's server has two tools, a wish list file, and you know what a failed tool call looks like to a client.
The words you need today
- Tool error: a result that says the tool ran but failed. In MCP the result then has
isErrorset to true, and the text explains what went wrong. It is the same idea asis_errorin a tool_result in the tool use course. - ToolError: the exception class from the Python SDK that you raise to return such an error. The build guide imports it from
mcp.server.mcpserver.exceptions. - Exit code: a number a command line program returns when it ends. 0 means success. The Inspector's command line client uses other numbers for each kind of failure.
- Web client: the Inspector's browser screen, with tabs for tools, resources and prompts.
Errors the model can read
A tool can fail by accident, or it can decide on purpose that a request is wrong. For the second kind, the build guide raises ToolError with a clear sentence, and so does Romy's server. The SDK turns it into a normal result with isError: true and your sentence in the text, and the model can read that sentence and act on it, for example by telling the customer the book is on the shelf.
Romy's rule is simple: a book that is in stock does not go on the wish list. The tool checks the stock first and raises a ToolError when there are copies. Everything else goes on the list.
Where the wish list lives
The tool writes to wishlist.json, a small file next to quillfeather.py. The code finds that folder with Path(__file__).parent, so the file ends up in the same place no matter which folder a host starts the server from. Claude Desktop and Claude Code start the server for you in lessons 5 and 6, so this matters later.
Keep this in mind for lesson 7: a tool that writes files can write anywhere your user account can write. Romy's tool only ever touches one file in one folder, on purpose.
Testing errors with the Inspector
The command line client is made for scripts, so it does not just print the error. The CLI page lists the exit codes: a tools/call that returns isError: true still prints its result, but ends with exit code 5. It also writes one line of JSON to stderr with an error code such as tool_is_error. That is useful later, when you want a script to check your server after each change.
The web client shows the same thing in a form. You pick a tool, fill in the fields and run it. The Protocol tab shows the JSON-RPC messages, and the Console tab shows what a stdio server wrote to stderr, which is where your log lines go.
The steps
- Add the add_to_wishlist tool to
quillfeather.py. - Check the tool list now shows two tools.
- Add a sold-out book to the wish list and look at
wishlist.json. - Try a book that is in stock and read the tool error and the exit code.
- Open the web client and run the same two calls with the form.
Follow the steps below.
Today's actions
Ticks are not saved without an accountDo it in Claude API
Claude API
- Open
quillfeather.pyand paste this code above the last two lines (theif __name__line andmcp.run). It adds the wish list tool.WISHLIST_FILE = Path(__file__).parent / "wishlist.json" @mcp.tool() def add_to_wishlist(title: str, customer: str) -> str: """Add a sold-out or missing book to the Quillfeather wish list, so Romy can look for a copy. Args: title: The book title the customer wants customer: The customer's first name """ book = BOOKS.get(title.strip().lower()) if book is not None and book["copies"] > 0: raise ToolError(f"{title} is in stock now, so it does not go on the wish list.") wishes = json.loads(WISHLIST_FILE.read_text()) if WISHLIST_FILE.exists() else [] wishes.append({"title": title.strip(), "customer": customer.strip()}) WISHLIST_FILE.write_text(json.dumps(wishes, indent=2)) logger.info("wish list now has %d entries", len(wishes)) return f"Added {title} for {customer}. Books on the wish list: {len(wishes)}." - List the tools again. You should now see two:
check_stockandadd_to_wishlist, withtitleandcustomeras required fields.npx @modelcontextprotocol/inspector --cli uv run quillfeather.py --method tools/list - Put a sold-out book on the wish list for a made-up customer, Hugo. The result says Added Little Women for Hugo. Then open
wishlist.jsonin the quillfeather folder and find the entry.npx @modelcontextprotocol/inspector --cli uv run quillfeather.py --method tools/call --tool-name add_to_wishlist --tool-arg "title=Little Women" --tool-arg customer=Hugo - Now try a book that is in stock. The result has
isErrortrue and the text from your ToolError. After it, the Inspector prints one line of JSON withtool_is_error.npx @modelcontextprotocol/inspector --cli uv run quillfeather.py --method tools/call --tool-name add_to_wishlist --tool-arg "title=Jane Eyre" --tool-arg customer=Hugo - Check the exit code. In PowerShell run
$LASTEXITCODE, on macOS or Linux runecho $?. You should see 5, the Inspector's code for a tool error. Write it inmcp-notes.txt. - Open the web client with this command. It prints an address with a token and opens it in your browser. On the Servers screen, turn on the switch of the server to connect.
npx @modelcontextprotocol/inspector uv run quillfeather.py - Open Tools, pick
add_to_wishlist, fill in the form and click Execute Tool, once for Little Women and once for Jane Eyre. Watch the Protocol and Console panels. Stop the web client with Ctrl+C in the terminal.
Note: Every successful call adds a line to wishlist.json, so it grows while you test. You can delete the file at any time: the tool makes a new one. If the browser does not open, copy the printed address with its token into the address bar.
Create a free account to save your progress
Free, and no payment details. Your ticks and finished lessons are kept, on every device.
Create a free account I already have an accountCreate a free account to save your progress. Sign in or sign up