opencode mcp工具搜索
一个动态MCP工具搜索插件 OpenCode 那 通过按需加载工具来减少上下文使用。
问题
当您配置了许多MCP服务器,每个服务器都有多个工具时 定义可能会占用上下文窗口的很大一部分:
- 50+MCP工具 可以使用 约6700个代币 仅用于工具定义
- 这会减少实际对话和代码的上下文
解决方案
此插件实现了 基于搜索的元工具模式:
- 与其预先加载所有工具,不如提供一个
mcp_tool_search
元工具
- 按名称、描述或服务器搜索工具
- 直接通过以下方式调用工具
mcp_call_tool
User: "What's the weather in NYC?"
|
v
OpenCode decides: need a weather tool
|
v
Calls: mcp_tool_search({ query: "weather" })
|
v
Search returns: [{ name: "get_weather", server: "weather-api", ... }]
|
v
Calls: mcp_call_tool({ server: "weather-api", tool: "get_weather", args: { location: "NYC" } })代币节省
| 场景 | 令牌使用 |
|---|---|
| 已加载所有工具 | ~6.7万个令牌 |
| 使用此插件 | ~500个令牌 |
| 储蓄 | ~99% |
需求
- OpenCode v1.0.0或更高版本
- Node.js 18+或Bun 1.0+
安装
npm install opencode-mcp-tool-search然后添加到您的 opencode.json:
{
"$schema": "https://opencode.ai/config.json",
"plugins": ["opencode-mcp-tool-search"]
}重要提示: 使用此插件时,您必须 全部删除mcpServers从你的opencode.json。此插件接管MCP服务器管理-在插件中配置服务器(请参阅下面的快速入门)。
快速开始
1.从OpenCode配置中删除MCP服务器
重要提示: 此插件直接管理MCP服务器连接。您必须删除任何 mcpServers 从你的 opencode.json 以避免重复连接和冲突。
之前 (删除此项):
{
"$schema": "https://opencode.ai/config.json",
"mcpServers": {
"github": { ... },
"filesystem": { ... }
}
}之后:
{
"$schema": "https://opencode.ai/config.json",
"plugins": ["opencode-mcp-tool-search"]
}2.在插件中配置MCP服务器
创建插件配置文件并将MCP服务器配置移到此处:
// .opencode/plugin/mcp-search.ts
import { createMCPToolSearchPlugin } from "opencode-mcp-tool-search";
export const MCPToolSearch = createMCPToolSearchPlugin({
mcpServers: [
{
name: "github",
command: "npx",
args: ["-y", "@modelcontextprotocol/server-github"],
env: { GITHUB_TOKEN: process.env.GITHUB_TOKEN },
},
{
name: "filesystem",
command: "npx",
args: [
"-y",
"@modelcontextprotocol/server-filesystem",
"/path/to/allowed/dir",
],
},
],
});3.使用工具
配置后,OpenCode可以搜索和调用MCP工具:
# Search for tools
mcp_tool_search({ query: "github pull request" })
# Call a tool
mcp_call_tool({ server: "github", tool: "create_pull_request", args: { ... } })
# List all servers
mcp_tool_search({ list_servers: true })可用工具
mcp_tool_search
按查询搜索MCP工具。
| 参数 | 类型 | 说明 |
|---|---|---|
query | string | 搜索查询(搜索名称、描述、服务器指令) |
server | string | 可选:将结果筛选到特定服务器 |
list_servers | boolean | 可选:列出所有连接的服务器,而不是搜索 |
示例:
// Search by keyword
mcp_tool_search({ query: "weather" });
// Filter by server
mcp_tool_search({ query: "pull request", server: "github" });
// List all servers
mcp_tool_search({ list_servers: true });mcp_call_tool
调用特定服务器上的MCP工具。
| 参数 | 类型 | 说明 |
|---|---|---|
server | 字符串 | 必需:服务器名称 |
tool | 字符串 | 必需:工具名称 |
args | object | 可选:传递给工具的参数 |
例子:
mcp_call_tool({
server: "github",
tool: "create_issue",
args: {
owner: "myorg",
repo: "myrepo",
title: "Bug report",
body: "Description of the issue",
},
});mcp_tool_info
获取特定工具的详细信息,包括其完整的JSON模式。
| 参数 | 类型 | 说明 |
|---|---|---|
tool_name | string | 要检查的工具的名称 |
例子:
mcp_tool_info({ tool_name: "create_pull_request" });mcp_tool_search_status
检查插件和服务器连接状态。
例子:
mcp_tool_search_status();
// Returns: server count, connection status, tool count, any errors配置
完整配置示例
// .opencode/plugin/mcp-search.ts
import { createMCPToolSearchPlugin } from "opencode-mcp-tool-search";
export const MCPToolSearch = createMCPToolSearchPlugin({
// MCP servers to connect to
mcpServers: [
{
name: "my-server",
command: "node",
args: ["./my-mcp-server.js"],
env: { API_KEY: process.env.API_KEY },
cwd: "/path/to/server",
instructions: "This server provides weather and calendar tools",
timeout: 30000, // Connection timeout in ms
},
],
// Search configuration
maxResults: 10, // Maximum results to return (default: 10)
fuzzyMatch: true, // Enable fuzzy matching for typos (default: true)
minScore: 0.1, // Minimum match score 0-1 (default: 0.1)
searchInstructions: true, // Search server instructions too (default: true)
});配置选项
| 选项 | 类型 | 默认值 | 描述 |
|---|---|---|---|
mcpServers | MCPServerConfig[] | [] | MCP服务器配置数组 |
maxResults | number | 10 | 搜索返回的最大工具数 |
fuzzyMatch | boolean | true | 启用模糊名称匹配 |
minScore | number | 0.1 | 结果的最低相关性得分(0-1) |
searchInstructions | boolean | true | 在搜索中包含服务器说明 |
MCP服务器配置
| 选项 | 类型 | 必填 | 描述 |
|---|---|---|---|
name | string | 是 | 唯一服务器标识符 |
command | string | Yes | 启动服务器的命令 |
args | string[] | 否 | 命令参数 |
env | Record | 否 | 环境变量 |
cwd | string | 否 | 工作目录 |
instructions | string | 否 | 服务器描述(改进搜索) |
timeout | number | 否 | 连接超时(毫秒)(默认值:30000) |
搜索工作原理
搜索使用 Fuse.js 加权模糊匹配 领域:
| 字段 | 重量 | 用途 |
|---|---|---|
| 工具名称 | 50% | 主要标识符,处理拼写错误 |
| 工具描述 | 35% | 语义相关性 |
| 服务器说明 | 15% | 路由到正确的服务器 |
搜索功能:
- 模糊匹配处理拼写错误(例如,“gihub”匹配“github”)
- 高级查询的扩展搜索语法
- 按相关性得分排序的结果
建筑
+----------------------------------------------------------+
| OpenCode |
+----------------------------------------------------------+
| Plugin: opencode-mcp-tool-search |
| +----------------------------------------------------+ |
| | State | |
| | - servers: Map | |
| | - allTools: MCPTool[] | |
| +----------------------------------------------------+ |
| | |
| +----------------------------------------------------+ |
| | Tools (exposed to OpenCode) | |
| | - mcp_tool_search (search tools) | |
| | - mcp_call_tool (execute tools) | |
| | - mcp_tool_info (inspect tools) | |
| | - mcp_tool_search_status (check status) | |
| +----------------------------------------------------+ |
| | |
| +----------------------------------------------------+ |
| | MCP Client | |
| | - Stdio transport to MCP servers | |
| | - Parallel server connections | |
| | - Tool listing and execution | |
| +----------------------------------------------------+ |
+----------------------------------------------------------+
| | |
+-----+----+ +------+-----+ +------+-----+
| MCP | | MCP | | MCP |
| Server 1 | | Server 2 | | Server N |
+----------+ +------------+ +------------+好处
- 大量代币节省:工具令牌使用量减少约99%
- 无协议更改:适用于标准MCP服务器
- 透明:MCP服务器不需要任何修改
- 更好的发现:搜索有助于更快地找到合适的工具
- 并联连接:同时连接到所有服务器
权衡
- 调用工具前搜索的额外步骤
- 需要良好的搜索查询才能找到合适的工具
- 服务器指令提高了可发现性
发展
# Clone the repository
git clone https://github.com/francisco-m001/opencode-mcp-tool-search.git
cd opencode-mcp-tool-search
# Install dependencies
npm install
# or
bun install
# Build
npm run build
# or
bun run build
# Watch mode for development
npm run dev
# or
bun run dev项目结构
opencode-mcp-tool-search/
├── src/
│ ├── index.ts # Plugin entry point and tool definitions
│ ├── search.ts # Fuse.js search implementation
│ ├── mcp-client.ts # MCP SDK client wrapper
│ ├── types.ts # TypeScript interfaces
│ └── utils.ts # Helper functions
├── package.json
├── tsconfig.json
└── README.md贡献
欢迎投稿!请随时提交拉取请求。
- 分叉存储库
- 创建功能分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add some amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
相关项目
规格
MCP TypeScript官方SDK
许可证
MIT许可证-请参阅 许可证 了解详情。
