# Using MCP

https://ailoy.co/docs/guides/using-mcp/

Give an agent the tools of any MCP server.

An agent can use the tools of any [MCP](https://modelcontextprotocol.io) 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`](https://github.com/modelcontextprotocol/servers/tree/main/src/fetch) with `uvx`:

**Python**

```python
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())
```

**Node.js**

```js
const { AgentBuilder, registerMcpStdio } = require('@brekkylab/ailoy')

async function main() {
  const tools = await registerMcpStdio('fetch', 'uvx', ['mcp-server-fetch'])

  const agent = await new AgentBuilder('anthropic/claude-haiku-5-5')
    .tools(tools)
    .build()

  try {
    for await (const { message } of agent.run('Summarize https://modelcontextprotocol.io in three sentences.')) {
      for (const part of message.contents) {
        if (part.type === 'text') console.log(part.text)
      }
    }
  } finally {
    await agent.close()
  }
}

main()
```

**Rust**

```rust
use ailoy::{
    agent::AgentBuilder,
    message::{Message, Part, Role},
    tool::register_mcp_stdio,
};
use futures::StreamExt as _;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let tools = register_mcp_stdio("fetch", "uvx", ["mcp-server-fetch"]).await?;

    let mut agent = AgentBuilder::new("anthropic/claude-haiku-5-5")
        .tools(tools)
        .build()
        .await?;

    let query = Message::new(Role::User)
        .with_contents([Part::text("Summarize https://modelcontextprotocol.io in three sentences.")]);
    let mut stream = agent.run(query);
    while let Some(output) = stream.next().await {
        let message = output?.message;
        if message.role == Role::Assistant {
            for text in message.contents.iter().filter_map(Part::as_text) {
                println!("{text}");
            }
        }
    }
    Ok(())
}
```

> **Caution:**
> 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](https://deepwiki.com)'s server, which answers questions about GitHub repositories:

**Python**

```python
from ailoy import register_mcp_streamable_http

tools = await register_mcp_streamable_http("deepwiki", "https://mcp.deepwiki.com/mcp")
```

**Node.js**

```js
const { registerMcpStreamableHttp } = require('@brekkylab/ailoy')

const tools = await registerMcpStreamableHttp('deepwiki', 'https://mcp.deepwiki.com/mcp')
```

**Rust**

```rust
use ailoy::tool::register_mcp_streamable_http;

let tools = register_mcp_streamable_http("deepwiki", "https://mcp.deepwiki.com/mcp").await?;
```

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.

**Python**

```python
from ailoy import unregister_mcp

unregister_mcp("fetch")
```

**Node.js**

```js
const { unregisterMcp } = require('@brekkylab/ailoy')

unregisterMcp('fetch')
```

**Rust**

```rust
ailoy::tool::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](https://docs.astral.sh/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. |
