MCP 服务器 - 简化模型上下文协议开发
MCP 服务器是一个实现模型上下文协议 (MCP) 的简单服务器,提供更简洁的 API。
快速入门
- 创建项目目录并初始化: mkdir my-server && cd my-server yarn init -y
- 安装依赖: yarn add @agentico/mcp-server @modelcontextprotocol/sdk zod zod-to-json-schema typescript @types/node
- 创建工具类(如 EchoTool),定义执行逻辑:
import { Tool, ToolSchema } from "@agentico/mcp-server";
export class EchoTool extends Tool {
toolSchema: ToolSchema = { name: "echo", description: "回显输入消息", schema: { type: "object", properties: { message: { type: "string" } }, required: ["message"] } };
async execute(input: any): Promise
{ return { content: [{ type: "text", text: input.message }] }; } } - 在入口文件 index.ts 中注册工具并启动服务器: import { MCPServer } from '@agentico/mcp-server'; const server = new MCPServer('My MCP Server', '1.0.0'); server.registerTool("echo", EchoTool); server.run();
- 构建并运行: yarn build && yarn start