使用说明
Home Page | Documentation | Examples | Discord | Blog
VoltAgent is an open source TypeScript framework for building and orchestrating AI agents.
Escape the limitations of no-code builders and the complexity of starting from scratch.
VoltAgent MCP服务器示例
此示例显示了如何通过以下方式公开VoltAgent代理、工作流和工具 模型上下文协议(MCP) 使用 @voltagent/mcp-server 包裹。它还演示了如何在Hono HTTP服务器后面托管MCP传输,以便MCP兼容的IDE(Cursor、Windsurf、VS Code扩展等)可以发现和调用您的VoltAgent组件。
试试这个例子
npm create voltagent-app@latest -- --example with-mcp-server
你得到了什么
- 一个最小的VoltAgent项目
Assistant代理人和astatus工具。 - 一
MCPServer将您的VoltAgent注册表镜像到MCP客户端的实例。 - Hono线路通过
@voltagent/server-hono代理MCP HTTP/SSE流量。 - 支持Stdio传输,因此您可以将服务器直接连接到IDE。
项目结构
examples/with-mcp-server
├── src/
│ ├── agents/assistant.ts # Example agent that calls the status tool
│ ├── mcp/server.ts # MCPServer configuration (agents/workflows/tools + filters)
│ └── index.ts # VoltAgent bootstrap + Hono server
└── README.md # You are here
先决条件
- Node.js 20+
pnpmOPENAI_API_KEY在您的环境中(用于样品试剂)
在本地运行
安装依赖项并使用Hono HTTP服务器(端口)启动VoltAgent 3141 默认情况下):
pnpm install
pnpm --filter voltagent-example-with-mcp-server dev
REST+MCP路由现在可在 /mcp/*.参观 http://localhost:3141/mcp/servers 列出已注册的MCP服务器并检查其元数据。
启动MCP stdio会话
揭露同样的事情 MCPServer 在stdio上(对于生成子进程的IDE),运行:
pnpm --filter voltagent-example-with-mcp-server start -- --stdio
该进程与stdin/stdout保持连接,因此MCP客户端可以协商传输并调用工具。与需要本地MCP二进制文件的编辑器集成时,请使用此模式。
关键MCP配置(摘录)
import { MCPServer } from "@voltagent/mcp-server";
import { Agent, createTool } from "@voltagent/core";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";
const status = createTool({
name: "status",
description: "Return the current time",
parameters: z.object({}),
async execute() {
return { status: "ok", time: new Date().toISOString() };
},
});
const assistant = new Agent({
name: "Support Agent",
instructions: "Route customer tickets to the correct queue.",
model: openai("gpt-4o-mini"),
tools: [status],
});
export const mcpServer = new MCPServer({
name: "voltagent-example",
version: "0.1.0",
description: "Expose VoltAgent over MCP",
agents: { support: assistant },
tools: { status },
filterTools: ({ items }) => items.filter((tool) => tool.name !== "debug"),
});
这 agents, workflows,以及 tools 对象允许您仅显示MCP条目或镜像现有的VoltAgent注册表。可选的 filter* 函数按传输运行,允许您在不接触全局注册表的情况下隐藏或重新排序项目。
下一步行动
- 将正在运行的服务器连接到 VoltOps MCP控制台 并检查外露的工具。
- 添加更多代理或工作流,然后从
agents/workflows地图,使其可供MCP客户端使用。 - 通过注册您自己的控制器来定制传输
transportRegistry如果你需要stdio/HTTP/SSE的替代品。
黑客快乐! 🚀