MCP链式工具
一个MCP(模型上下文协议)服务器,通过将调用链式传递给其他MCP工具,减少令牌使用量,允许顺序工具执行并传递结果。
MCP 服务配置
复制以下 JSON 到 OPClaw 或其他 MCP 客户端的配置文件中即可使用
{
"mcpServers": {
"mcp_tool_chainer": {
"args": [
"-y",
"@thirdstrandstudio/mcp-tool-chainer",
"`claude_desktop_config.json` or `mcp.json`"
],
"command": "npx",
"env": {}
}
}
}
服务介绍
MCP 工具链
这是一个MCP(模型上下文协议)服务器,它将调用链接到其他MCP工具上,通过允许顺序执行工具并传递结果来减少令牌使用。
设计用于解决 https://github.com/modelcontextprotocol/specification/issues/215
类似步骤函数的JSON路径:
功能
- 顺序链接多个MCP工具
- 使用
CHAIN_RESULT占位符将一个工具的结果作为另一个工具的输入 - 使用带有
inputPath和outputPath参数的JsonPath过滤并提取特定数据 - 从配置的MCP服务器自动发现工具
- 相对于单独调用工具而言,最小化令牌使用
工具
此服务器实现了以下MCP工具:
mcp_chain- 链接多个MCP服务器chainable_tools- 从所有MCP服务器中发现工具,以便可以使用mcp_chain工具discover_tools- 重新从所有MCP服务器中发现工具
安装
前提条件
- Node.js (v16 或更高版本)
- npm
从npm安装
# Install
npm install @thirdstrandstudio/mcp-tool-chainer
# Or use with npx directly
npx -y @thirdstrandstudio/mcp-tool-chainer
从源代码安装
# Clone the repository
git clone https://github.com/thirdstrandstudio/mcp-tool-chainer.git
cd mcp-tool-chainer
# Install dependencies
npm install
# Build the package
npm run build
与Claude Desktop、Cursor等一起使用
确保这是最后一个运行的MCP,否则它需要再次运行发现过程。
将以下内容添加到您的claude_desktop_config.json或mcp.json文件中:
如果是从npm全局安装
{
"mcpServers": {
"mcp_tool_chainer": {
"command": "npx",
"args": ["-y", "@thirdstrandstudio/mcp-tool-chainer", "`claude_desktop_config.json` or `mcp.json`"],
"env": {}
}
}
}
如果是从源代码安装
{
"mcpServers": {
"mcp_tool_chainer": {
"command": "node",
"args": ["/path/to/mcp-tool-chainer/dist/index.js", "`claude_desktop_config.json` or `mcp.json`"],
"env": {}
}
}
}
请将/path/to/mcp-tool-chainer替换为您实际仓库的路径。
示例
链接浏览器和XPath工具
// Fetch a webpage and then extract specific content with XPath
const result = await callTool("mcp_chain", {
"mcpPath": [
{
"toolName": "mcp_browser_mcp_fetch_url",
"toolArgs": "{\"url\": \"https://example.com\"}"
},
{
"toolName": "mcp_xpath_xpath",
"toolArgs": "{\"xml\": CHAIN_RESULT, \"query\": \"//h1\"}"
}
]
});
使用带有InputPath和OutputPath的JsonPath
// Fetch a webpage, extract specific content with XPath, then extract part of the result
const result = await callTool("mcp_chain", {
"mcpPath": [
{
"toolName": "mcp_fetch_fetch",
"toolArgs": "{\"url\": \"https://api.example.com/data\"}"
},
{
"toolName": "web_search",
"toolArgs": "{\"search_term\": CHAIN_RESULT}",
"inputPath": "$.results[0].title", // Extract only the first result's title from previous output
"outputPath": "$.snippets[*].text" // Extract only the text snippets from the search results
},
{
"toolName": "another_tool",
"toolArgs": "{\"content\": CHAIN_RESULT}"
}
]
});
JsonPath支持
MCP工具链现在支持AWS Step Functions风格的InputPath和OutputPath功能:
- inputPath:在传递给工具之前,使用JsonPath表达式提取输入中的特定部分
- outputPath:在传递给下一个工具之前,使用JsonPath表达式提取输出中的特定部分
这些功能仅在输入/输出为有效的JSON时才有效。如果JsonPath提取失败,则使用原始输入/输出。
有关JsonPath语法参考,请参阅 JsonPath 语法。
优势
- 减少令牌使用:通过链接工具,避免将大量中间结果发送回LLM
- 简化工作流:通过单个工具调用创建复杂的数据处理管道
- 提高性能:通过最小化LLM与工具之间的往返次数来减少延迟
- 精确的数据流控制:使用JsonPath表达式仅提取所需的数据
开发
# Install dependencies
npm install
# Start the server
node dist/index.js config.json
# List available tools
node dist/index.js config.json discover_tools
许可证
此MCP服务器根据MIT许可证发布。
由 Third Strand Studio 创建