Guides /
Call an MCP server from Python
The official mcp package connects to any remote MCP server. This lists the tools on The Inference's server and calls one. It was run against the live server with mcp 2.3.0.
pip install mcpimport asyncio
import os
import httpx2
from mcp import Client
from mcp.client.streamable_http import streamable_http_client
URL = "https://theinference.org/api/mcp"
KEY = os.environ.get("INFERENCE_KEY") # tik_...; six tools answer without one
async def main() -> None:
async with httpx2.AsyncClient(
headers={"Authorization": f"Bearer {KEY}"} if KEY else {},
timeout=httpx2.Timeout(30.0, read=300.0),
) as http_client:
transport = streamable_http_client(URL, http_client=http_client)
async with Client(transport) as client:
tools = await client.list_tools()
print([t.name for t in tools.tools])
result = await client.call_tool("search", {"query": "Nvidia"})
print(result.content[0].text[:400])
asyncio.run(main())What the parts do
- The API key goes on the
httpx2.AsyncClient.streamable_http_clientno longer takesheaders=itself; older examples that pass it fail with aTypeError. - Keep the timeout. Without it
httpx2waits 5 seconds, and a tool that takes longer fails. - Don't send an empty
Bearerheader when you have no key:httpx2rejects it as an illegal header value. call_toolreturns content blocks. Text blocks carry the tool's answer, usually JSON you can parse.
Hand the tools to a model
list_tools() gives each tool's name, description and JSON input schema: the same shape most model APIs take for tool definitions. Pass them in, and when the model asks for a tool, run it with call_tool and send the result back.