Skip to content

Using MCP

An agent can use the tools of any MCP server. Register the server, and you get back a description of each of its tools. Pass those to the AgentBuilder with tools(...).

Each tool is registered as {prefix}__{name}, so tools from different servers don’t clash. For example, the fetch tool of a server registered under fetch becomes fetch__fetch.

Stdio servers

register_mcp_stdio starts the server as a local process and talks to it over stdin and stdout. This runs mcp-server-fetch with uvx:

import asyncio
from ailoy import AgentBuilder, register_mcp_stdio
async def main() -> None:
tools = await register_mcp_stdio("fetch", "uvx", ["mcp-server-fetch"])
agent = await (
AgentBuilder("anthropic/claude-haiku-5-5")
.tools(tools)
.build()
)
async for output in agent.run("Summarize https://modelcontextprotocol.io in three sentences."):
for part in output["message"]["contents"]:
if part["type"] == "text":
print(part["text"])
asyncio.run(main())

A stdio server runs on the host, not in the agent’s sandbox, with the same access as your program. Register only servers you trust.

Streamable HTTP servers

register_mcp_streamable_http connects to a remote server by its URL. This registers DeepWiki’s server, which answers questions about GitHub repositories:

from ailoy import register_mcp_streamable_http
tools = await register_mcp_streamable_http("deepwiki", "https://mcp.deepwiki.com/mcp")

Pass tools to AgentBuilder.tools(...) as above.

Unregistering

unregister_mcp (unregisterMcp in Node.js) removes a server’s tools by their prefix and closes the connection to the server.

from ailoy import unregister_mcp
unregister_mcp("fetch")

Troubleshooting

What you see Why, and what to do
An OS error such as No such file or directory (os error 2) or program not found from register_mcp_stdio The command, such as uvx, isn’t installed or isn’t on your program’s PATH. Install it (uvx comes with uv), or give its full path.
connection closed: initialize response The server started and exited before answering. Its error output isn’t shown, so run the same command in a terminal to see why.
A tool’s name isn’t the one the server gives it Tools are named <prefix>__<name>, and any character outside letters, digits, _ and - becomes _: prefix my.server and tool odd/name give my_server__odd_name. A name over 64 characters logs a warning, since some model APIs refuse it.