Sub topic 9
Listing tools and reading their schemas
This chapter assumes you can write async Python, read JSON Schema, and have already connected to an MCP server with ClientSession and called initialize(). The server below is the calc server from the earlier chapter, saved as server.py.
What `list_tools` returns
After the handshake, the client asks the server what it offers with the tools/list request. In the SDK this is session.list_tools(). The result has a tools list. Each entry has three fields you will use:
name: the identifier you send back when you call the tool.description: plain text the model reads to decide when to use the tool.inputSchema: a JSON Schema object describing the arguments.
A tool can also carry optional fields such as a title or annotations. Treat them as hints, because the server sets them and the client cannot verify them.
Listing the tools
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
server_params = StdioServerParameters(command="python", args=["server.py"])
async def main():
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
listed = await session.list_tools()
for tool in listed.tools:
print(tool.name, "-", tool.description)
print(tool.inputSchema)
asyncio.run(main())For the add tool, the output looks like this (key order may differ):
add - Add two integers.
{'properties': {'a': {'title': 'A', 'type': 'integer'}, 'b': {'title': 'B', 'type': 'integer'}}, 'required': ['a', 'b'], 'title': 'addArguments', 'type': 'object'}Reading the schema
The server generated this schema from the Python type hints a: int, b: int. The parts that matter to a host are:
| Field | Meaning |
|---|---|
| `type: "object"` | Arguments are always sent as a JSON object of named values. |
| `properties` | One entry per argument, with its type. |
| `required` | Arguments the model must supply. A parameter with a default value is not listed here. |
| `description` (per property) | Optional text that helps the model fill in the value. |
The model only sees what the server publishes. A vague description or an untyped parameter leads to poor calls, and you cannot fix that on the client side without rewriting the definition.
Common errors
- Listing before
initialize(). The session is not ready, so the call fails or hangs. Always complete the handshake first. - Assuming one page. The protocol allows paginated results. The result may carry a
nextCursor. If it is set, calllist_tools(cursor=...)again until it is empty. Small servers rarely paginate, but a general host should handle it. - Caching forever. A server can change its tools at runtime and may send a
notifications/tools/list_changedmessage if it declared that capability. A long-running host should refresh its list when that arrives. - Trusting descriptions. Descriptions come from the server and go straight into the model's context. Treat them as untrusted text from a third party.
Exercise
A server's tools/list result includes "required": ["a"] and the properties a and b. What does this tell the model, and what should your host do if the result also includes a nextCursor?
Show answer
The model must supply a, and b is optional. If nextCursor is set, the host should call list_tools again with that cursor and repeat until no cursor is returned, so it collects every tool.