Model Context Protocol (MCP)
The Model Context Protocol (MCP) provides a standardized way for large language models (LLMs) and AI agents to interact with external tools and services. VoltAgent implements MCP client capabilities, enabling your agents to seamlessly access diverse functionalities like filesystem operations, browser automation, database interactions, specific AI models hosted externally, and more, provided they adhere to the MCP specification.
Getting Started with MCPConfiguration
The MCPConfiguration
class is the central point for managing connections to one or more MCP servers. It handles the connection process and makes the tools offered by these servers available to your agents.
import { MCPConfiguration } from "@voltagent/core";
import path from "node:path"; // Used for filesystem path example
// Create MCP Configuration with multiple types of servers
const mcpConfig = new MCPConfiguration({
servers: {
// Example 1: HTTP-based server (e.g., external web service or API gateway)
reddit: {
type: "http",
url: "https://mcp.composio.dev/reddit/your-api-key-here", // URL of the MCP endpoint
// Optional: Custom headers or options for the initial fetch request
requestInit: {
headers: { "Custom-Header": "value" },
},
// Optional: Custom options for the EventSource connection used for streaming
eventSourceInit: { withCredentials: true },
},
// Example 2: stdio-based server (e.g., a local script or application)
filesystem: {
type: "stdio", // Connects via standard input/output
command: "npx", // The command to execute
args: [
// Arguments for the command
"-y",
"@modelcontextprotocol/server-filesystem", // Example: A filesystem server package
// Optional arguments for the server itself, like specifying allowed paths:
path.join(process.env.HOME || "", "Desktop"),
],
// Optional: Specify the working directory for the command
cwd: process.env.HOME,
// Optional: Provide environment variables to the spawned process
env: { NODE_ENV: "production" },
},
},
});
Working with MCP Tools
Once configured, you can retrieve the tools offered by the MCP servers. These fetched tools are standard AgentTool
objects, fully compatible with the VoltAgent Agent
.
Get All Tools as Flat Array (getTools()
)
Use getTools()
when you want a single list containing all tools from all configured MCP servers. This is useful if you want to provide one agent with the combined capabilities of all connected services.
// Fetch all tools from all configured MCP servers into a flat array
const allTools = await mcpConfig.getTools();
// Use these tools when interacting with an agent
// const response = await agent.generateText("What are the top posts on r/programming?", {
// userId: "user123",
// tools: allTools, // Pass the combined list of tools
// });
// Remember to disconnect later
// await mcpConfig.disconnect();