Add an MCP tool that writes data and returns clear errors

Day 3, 25 minutes · beginner

Goal of this lessonAdd a wish list tool that writes to a local file, make it refuse a wrong request with a ToolError, and test both paths with the MCP Inspector.

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 isError set to true, and the text explains what went wrong. It is the same idea as is_error in 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

  1. Add the add_to_wishlist tool to quillfeather.py.
  2. Check the tool list now shows two tools.
  3. Add a sold-out book to the wish list and look at wishlist.json.
  4. Try a book that is in stock and read the tool error and the exit code.
  5. 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 account

Do it in Claude API

Claude API

  1. Open quillfeather.py and paste this code above the last two lines (the if __name__ line and mcp.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)}."
  2. List the tools again. You should now see two: check_stock and add_to_wishlist, with title and customer as required fields.
    npx @modelcontextprotocol/inspector --cli uv run quillfeather.py --method tools/list
  3. 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.json in 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
  4. Now try a book that is in stock. The result has isError true and the text from your ToolError. After it, the Inspector prints one line of JSON with tool_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
  5. Check the exit code. In PowerShell run $LASTEXITCODE, on macOS or Linux run echo $?. You should see 5, the Inspector's code for a tool error. Write it in mcp-notes.txt.
  6. 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
  7. 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.

Checked on 4 December 2026 · Source: MCP docs: Inspector CLI client (opens in a new tab)

Create a free account to save your progress. Sign in or sign up