运行时依赖
安装命令
点击复制技能文档
MCP Server 构建器 功能说明
构建 模型 上下文 Protocol 服务器,扩展 AI 能力边界。
MCP 协议概述
MCP 是 Anthropic 推出的 AI 模型上下文协议,让 AI 能调用外部工具和数据源。
项目结构 mcp-server/ ├── package.json ├── tsconfig.json ├── src/ │ ├── 索引.ts # 主入口 │ ├── 工具s/ # 工具定义 │ └── resources/ # 资源定义 └── tsconfig.json
完整实现
- 初始化项目
// package.json { "name": "my-mcp-server", "version": "1.0.0", "type": "模块", "scripts": { "build": "tsc", "启动": "node dist/索引.js", "dev": "ts-node src/索引.ts" }, "dependencies": { "@模型上下文protocol/sdk": "^0.5.0", "zod": "^3.22.0" } }
// tsconfig.json { "编译器Options": { "tar获取": "ES2022", "模块": "NodeNext", "模块Resolution": "NodeNext", "outDir": "./dist", "rootDir": "./src", "strict": true, "es模块Interop": true }, "include": ["src/*/"] }
- 定义工具
导出 const 搜索工具 = { name: '网页_搜索', description: '搜索互联网获取最新信息', 输入模式: z.object({ 查询: z.string().describe('搜索关键词'), limit: z.number().optional().default(5).describe('返回结果数量') }),
a同步 处理器(args: { 查询: string; limit?: number }) {
// 实际实现
const 结果s = awAIt perform搜索(args.查询, args.limit || 5);
return {
content: 结果s.map(r => ({
type: 'text' as const,
text: 标题: ${r.title}\n链接: ${r.url}\n摘要: ${r.snippet}
}))
};
}
};
- 定义资源
列出: a同步 () => [ { uri: 'knowledge://docs/latest', name: '最新文档', description: '系统最新文档版本', mimeType: 'text/markdown' } ],
read: a同步 (uri: string) => { if (uri === 'knowledge://docs/latest') { return { contents: [{ uri, mimeType: 'text/markdown', text: '# 最新文档\n\n...' }] }; } throw new Error('Resource not found'); } };
- 主入口
导入 { 搜索工具 } from './工具s/搜索.js'; 导入 { knowledgeResources } from './resources/knowledge.js';
class MyMCPServer { private server: Server;
constructor() { this.server = new Server( { name: 'my-mcp-server', version: '1.0.0' }, { capabilities: { 工具s: {}, resources: {} } } );
this.设置up工具处理器s(); this.设置upResource处理器s(); }
private 设置up工具处理器s() { this.server.设置请求处理器(列出工具s请求模式, a同步 () => ({ 工具s: [ { name: 搜索工具.name, description: 搜索工具.description, 输入模式: 搜索工具.输入模式 } ] }));
this.server.设置请求处理器(Call工具请求模式, a同步 (请求) => {
const { name, arguments: args } = 请求.params;
if (name === '网页_搜索') {
return awAIt 搜索工具.处理器(args as any);
}
throw new Error(Unknown 工具: ${name});
});
}
private 设置upResource处理器s() { this.server.设置请求处理器(列出Resources请求模式, a同步 () => ({ resources: awAIt knowledgeResources.列出() }));
this.server.设置请求处理器(ReadResource请求模式, a同步 (请求) => { return awAIt knowledgeResources.read(请求.params.uri); }); }
a同步 启动() { const transport = new StdioServerTransport(); awAIt this.server.connect(transport); console.error('MCP Server 启动ed on stdio'); } }
new MyMCPServer().启动().catch(console.error);
- 更多工具示例
a同步 处理器(args: any) {
const fs = awAIt 导入('fs/promises');
switch (args.operation) {
case 'read': {
const content = awAIt fs.readFile(args.path, 'utf-8');
return { content: [{ type: 'text', text: content }] };
}
case 'write': {
awAIt fs.writeFile(args.path, args.content || '');
return { content: [{ type: 'text', text: 'File written 成功fully' }] };
}
case '列出': {
const files = awAIt fs.re添加ir(args.path);
return { content: [{ type: 'text', text: files.join('\n') }] };
}
default:
throw new Error(Unknown operation: ${args.operation});
}
}
};
// 数据库查询工具 导出 const db工具 = { name: 'database_查询', description: '执行数据库查询', 输入模式: z.object({