> For the complete documentation index, see [llms.txt](https://docs.ainu.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ainu.pro/core-concepts/mcp.md).

# MCP

The **Model Context Protocol (MCP)** provides an interface between the AINU framework and the MCP SDK. It allows agents to interact with external tools, resources, and prompts dynamically. The `MCP` class is responsible for managing the connection to the MCP client, fetching tools, and executing them.

***

### MCP Constructor

The `MCP` class is instantiated using the `MCPClientSettings` object, which defines the configuration for the MCP client.

#### Constructor Signature

The `MCP` constructor is defined as:

* **`settings`**: An object of type `MCPClientSettings` that specifies the name, version, and transport details for the MCP client.

***

### MCPClientSettings

The `MCPClientSettings` type defines the configuration options for the MCP client. These settings are required to initialize and connect the MCP instance.

#### Available Parameters

<table><thead><tr><th width="140">Parameter</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><code>name</code></td><td><code>string</code></td><td><strong>Required.</strong> The name of the MCP client instance.</td></tr><tr><td><code>version</code></td><td><code>string</code></td><td><strong>Required.</strong> The version of the MCP client.</td></tr><tr><td><code>transport</code></td><td><p><code>StdioClientTransport</code> </p><p><code>| SSEClientTransport</code> </p><p><code>| StreamableHTTPClientTransport</code></p></td><td><strong>Required.</strong> The transport mechanism used to connect to the MCP client.</td></tr></tbody></table>

***

### Key Features of MCP

1. **Connection Management**:\
   The `connect` method establishes a connection to the MCP client using the specified transport. If the client is already connected, the method does nothing.
2. **Tool Management**:
   * The `tools` method fetches tools from the MCP client and converts them into `Tool` instances. These tools are stored in the `toolMap` for easy access.
   * Tools fetched from the MCP client can be dynamically added to agents.
3. **Tool Execution**:\
   The `executeTool` method allows you to execute a tool by its name and parameters. It connects to the MCP client, calls the tool, and retrieves the result.
4. **Disconnection**:\
   The `disconnect` method safely disconnects the MCP client, ensuring that no lingering connections remain.

***

### Example: Using MCP with an Agent

To use MCP with an agent, you first create an `MCP` instance and fetch tools from the MCP client. These tools can then be passed to the agent dynamically.

```typescript
import { MCP } from "@ainulabs/ainu";

// Define MCP client settings
const mcpSettings = {
  name: "example-client",
  version: "1.0.0",
  transport: someTransportInstance, // Replace with your transport instance
};

// Create an MCP instance
const mcp = new MCP(mcpSettings);

// Fetch tools and use them with an agent
(async () => {
  const toolsResult = await mcp.tools();
  if (toolsResult.ok) {
    const tools = Object.values(toolsResult.data);
    agent.putTool(tools[0]); // Add a tool to the agent
  }
})();
```

***

### Summary

The MCP class provides a powerful way to integrate external tools, resources, and prompts into the AINU framework. By leveraging the MCP SDK, you can dynamically fetch and execute tools, making your agents more flexible and capable. The `MCPClientSettings` object allows you to configure the client, while methods like `connect`, `tools`, and `executeTool` handle the core functionality.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ainu.pro/core-concepts/mcp.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
