Sub topic 10
Turning MCP tools into LLM tool definitions
This chapter assumes you can write async Python, know what JSON Schema is, and have used tool calling with an LLM API. It also assumes you have a connected ClientSession and have listed tools, as in the previous chapters.
Why the mapping is small
An MCP tool definition and an LLM API tool definition describe the same thing: a name, a description, and a JSON Schema for the arguments. MCP calls the schema field inputSchema. The Anthropic Messages API calls it input_schema. Other providers wrap the same schema in a different structure. So the conversion is mostly renaming fields.
| MCP field (`Tool`) | Anthropic Messages API field |
|---|---|
| `name` | `name` |
| `description` (may be `None`) | `description` |
| `inputSchema` | `input_schema` |
The conversion function
def to_llm_tools(mcp_tools):
return [
{
"name": t.name,
"description": t.description or "",
"input_schema": t.inputSchema,
}
for t in mcp_tools
]The function takes the list from session.list_tools(), not the whole response. Call it as to_llm_tools((await session.list_tools()).tools).
Using t.description or "" matters because a server is not required to provide a description. Passing None to the API would fail validation.
For the add tool from the minimal server, the result looks like this:
{
"name": "add",
"description": "Add two integers.",
"input_schema": {
"type": "object",
"properties": {
"a": {"title": "A", "type": "integer"},
"b": {"title": "B", "type": "integer"},
},
"required": ["a", "b"],
},
}The server's SDK built that schema from the type hints. You did not write it by hand.
Common errors
- Reading the wrong attribute. The MCP field is
inputSchemain camelCase. Writingt.input_schemaraises anAttributeError. - Passing the response object.
list_tools()returns an object. The list is in its.toolsattribute. - Assuming one wrapper fits all providers. The Anthropic format is flat. Some providers nest the same schema under a
functionkey or a similar wrapper. Check your provider's documentation and change only the wrapper. - Schemas the provider rejects. Some providers accept only a subset of JSON Schema. If a request fails on a schema keyword, check the keyword against the provider's documented limits before suspecting the server.
- Converting once and caching forever. A server can change its tool list while running. Convert again if you refresh the list.
Check your understanding
Exercise
A server returns a tool with description=None. What does to_llm_tools send as the description, and why?
Show answer
It sends an empty string, because t.description or "" replaces None. The server is not required to supply a description, and passing None to the LLM API can fail request validation.
Exercise
You switch to a provider that expects each tool as {"type": "function", "function": {"name": ..., "description": ..., "parameters": ...}}. Which part of to_llm_tools changes?
Show answer
Only the wrapper. Build the nested dictionary and put t.inputSchema under parameters. The schema content stays the same.