Day 2, 25 minutes · beginner
Why it matters
A tool you write for MCP looks almost the same as a plain Python function. The difference is one line on top of it. That line tells the SDK to publish the function as a tool, with a name, a description and an input schema, so any MCP client can find it and call it. You do not write the JSON schema by hand any more, and you do not write the loop: the host does that part.
Today you write Romy's first tool, a stock check for second-hand books, and you call it without any AI in between. You use the MCP Inspector, the official test tool, to ask the server which tools it has and to call one. At the end you have a running MCP server and you have seen exactly what a client sees.
The words you need today
- Decorator: a line starting with
@above a Python function. It wraps the function with extra behaviour. Here@mcp.tool()registers the function as an MCP tool. - MCPServer: the class from the official Python SDK that holds your server. You give it a name, and it handles the protocol.
- MCP Inspector: the reference tool for testing and debugging MCP servers. It is one package,
@modelcontextprotocol/inspector, with a web client in the browser, a command line client and a terminal client. Today you use the command line client, which you start with--cli. - stdout and stderr: a program has two output streams. Standard output (stdout) is the normal one. Standard error (stderr) is meant for messages and errors.
How the SDK turns a function into a tool
The build guide on the MCP site says the server class uses Python type hints and docstrings to generate the tool definitions. In practice:
- the function name becomes the tool name, so
check_stockis the toolcheck_stock; - the type hint
title: strbecomes a required string field in the input schema; - the docstring becomes the tool's description, which the model reads to decide when to use it.
So the description matters as much as it did in the tool use course. Write it for the model: what the tool does, and an example of the input.
Never print on a stdio server
Romy's server uses the stdio transport: the host talks to it through standard input and standard output. The build guide is strict about this: never write to stdout in a stdio server, because it can corrupt the JSON-RPC messages. In Python that means no print() in the server. Use the logging module instead, which writes to stderr. That is why the code has a logger and calls logger.info(...) inside the tool. When you run the Inspector you will see those log lines appear next to the result, because the Inspector shows stderr too.
What the Inspector shows you
When you list the tools, the Inspector prints the same tool definition a host such as Claude Desktop receives: the name, the description and the inputSchema. When you call a tool, you get the result as a list of content blocks, here one block of type text, plus isError, which says whether the tool failed. The SDK also adds a structuredContent copy of the result, because the function has a return type. You do not need it today.
These are the same ideas as in the tool use course, with new names: tools/list is how a client discovers the tools, and tools/call is how it runs one with arguments. The difference is that the host does the discovering and calling for you.
The steps
- Write quillfeather.py with Romy's made-up stock and one tool,
check_stock. - Start the server once by hand to see that it waits for messages.
- List the tools with the Inspector.
- Call the tool for a book that is in stock, one that is sold out and one that is not in the catalogue.
- Save the output in your notes.
Follow the steps below.
Today's actions
Ticks are not saved without an accountDo it in Claude API
Claude API
- Open a terminal in your quillfeather folder. Open
quillfeather.pyin your code editor and paste this code. The imports for json, Path and ToolError are for lesson 3."""Quillfeather Books: a small MCP server with made-up shop data.""" import json import logging from pathlib import Path from mcp.server import MCPServer from mcp.server.mcpserver.exceptions import ToolError logger = logging.getLogger(__name__) mcp = MCPServer("quillfeather") # Made-up stock of Romy's second-hand book shop. No network, no real data. BOOKS = { "jane eyre": {"author": "Charlotte Bronte", "copies": 2, "price": 6.50}, "moby dick": {"author": "Herman Melville", "copies": 1, "price": 8.00}, "little women": {"author": "Louisa May Alcott", "copies": 0, "price": 5.50}, "treasure island": {"author": "Robert Louis Stevenson", "copies": 3, "price": 4.00}, "pride and prejudice": {"author": "Jane Austen", "copies": 0, "price": 6.00}, } @mcp.tool() def check_stock(title: str) -> str: """Check whether Quillfeather Books has a second-hand copy of a book in stock. Args: title: The book title, for example Jane Eyre """ logger.info("check_stock called for %s", title) book = BOOKS.get(title.strip().lower()) if book is None: return f"'{title}' is not in the Quillfeather catalogue." if book["copies"] == 0: return f"{title} by {book['author']} is sold out right now." return f"{title} by {book['author']}: {book['copies']} in stock, EUR {book['price']:.2f} each." if __name__ == "__main__": mcp.run(transport="stdio") - Start the server by hand with
uv run quillfeather.py. Nothing seems to happen: a stdio server waits for messages on its input. Stop it with Ctrl+C. - List the tools with the Inspector's command line client. The first time, npx asks to install the package: type
y. You should see one tool,check_stock, with its description and inputSchema.npx @modelcontextprotocol/inspector --cli uv run quillfeather.py --method tools/list - Call the tool for a book that is in stock. You should see a log line from your server, then a result with the text Jane Eyre by Charlotte Bronte: 2 in stock, EUR 6.50 each, and
isErrorfalse.npx @modelcontextprotocol/inspector --cli uv run quillfeather.py --method tools/call --tool-name check_stock --tool-arg "title=Jane Eyre" - Call it again with
title=Little Women(sold out) and withtitle=Dune(not in the catalogue). Copy the three result texts intomcp-notes.txt.
Note: The INFO lines are your logger.info call, written to stderr. That is correct: on a stdio server only the protocol may use stdout.
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