mcp跑步者
用于运行MCP(模型上下文协议)服务器的TypeScript SDK和CLI。
概述
mcp-runner 旨在根据中定义的配置促进MCP服务器的执行 cline_mcp_settings.json。它支持可重用的服务器进程和受控清理,允许使用同一服务器实例执行多个操作。
您可以从命令行调用它,也可以将其用作您自己的TypeScript项目中的库,甚至可以从其他MCP服务器调用。
特性
- 跨多个调用重用服务器进程
- 优雅的终止和超时处理
- 自动服务器生命周期管理
- TypeScript支持
- 错误处理和日志
安装
npm installCLI使用情况
该软件包包括一个用于与MCP服务器交互的命令行界面。
命令
列出工具
列出指定MCP服务器的所有可用工具:
npm run cli -- list-tools 例子:
npm run cli -- list-tools sequential-thinking运行服务器
使用可选工具名称和参数运行指定的MCP服务器:
npm run cli -- runserver [tool-name] [params] [options]
# or
npm run cli -- runserver [params] [options] # uses first available tool选项:
--text:仅输出响应中的文本内容,而不是完整的JSON
示例:
# Run a specific tool
npm run cli -- runserver sequential-thinking sequentialthinking '{"thought": "Initial thought", "thoughtNumber": 1, "totalThoughts": 5, "nextThoughtNeeded": true}'
# Use first available tool
npm run cli -- runserver sequential-thinking '{"thought": "Initial thought", "thoughtNumber": 1, "totalThoughts": 5, "nextThoughtNeeded": true}'
# Output only text content
npm run cli -- runserver sequential-thinking sequentialthinking '{"thought": "Initial thought", "thoughtNumber": 1, "totalThoughts": 1}' --text程序化使用
基础示例
import { runServer, terminateServer } from 'mcp-runner';
async function main() {
try {
// First call (specific tool)
const result1 = await runServer('openrouterai', 'chat_completion', {
messages: [
{ role: 'user', content: 'Say hello!' }
]
});
console.log('Result 1:', result1);
// Second call (uses first available tool)
const result2 = await runServer('openrouterai', undefined, {
messages: [
{ role: 'user', content: 'How are you?' }
]
});
console.log('Result 2:', result2);
// Terminate server when done
await terminateServer();
} catch (error) {
console.error('Error:', error);
await terminateServer();
}
}错误处理
SDK包括全面的错误处理:
- 服务器进程错误
- 工具执行错误
- 优雅终止的超时处理
- 自动清除错误
API
runServer(服务器名称:字符串,参数:记录\)
使用提供的参数在指定的服务器上运行工具。在明确终止之前,服务器进程将重复用于后续调用。
参数:
serverName:配置中的服务器名称params:传递给服务器工具的参数
返回:使用服务器的响应解析Promise
terminateServer()
终止SDK管理的服务器进程。应在所有操作完成后调用。
返回:当服务器终止时解析的Promise
建筑
服务器管理器
这 ServerManager 类被实现为管理MCP服务器进程生命周期的单例。主要职责包括:
- 流程生命周期管理
- 客户端连接处理
- 优雅的终止
- 错误处理和日志
管理器确保在任何给定时间只有一个服务器进程在运行,并提供启动、重用和终止服务器的方法。
配置
SDK从以下位置读取服务器配置 cline_mcp_settings.json,它应该位于标准配置目录中。每个服务器配置包括:
{
"command": "string",
"args": "string[]",
"env": "Record",
"disabled": "boolean",
"alwaysAllow": "string[]"
}发展
建筑
npm run build运行测试
npm test许可证
此项目根据Mozilla公共许可证2.0获得许可-请参阅 许可证 文件以获取详细信息。
贡献
欢迎投稿!请随时提交拉取请求。
