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 typeMCPClientSettingsthat 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
name
string
Required. The name of the MCP client instance.
version
string
Required. The version of the MCP client.
transport
StdioClientTransport
| SSEClientTransport
| StreamableHTTPClientTransport
Required. The transport mechanism used to connect to the MCP client.
Key Features of MCP
Connection Management: The
connectmethod establishes a connection to the MCP client using the specified transport. If the client is already connected, the method does nothing.Tool Management:
The
toolsmethod fetches tools from the MCP client and converts them intoToolinstances. These tools are stored in thetoolMapfor easy access.Tools fetched from the MCP client can be dynamically added to agents.
Tool Execution: The
executeToolmethod allows you to execute a tool by its name and parameters. It connects to the MCP client, calls the tool, and retrieves the result.Disconnection: The
disconnectmethod 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.
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.
Last updated