fix: replace mapName heuristic fallback with explicit built-in tool whitelist#4
Open
fujunchao wants to merge 2 commits intoffflyZzz:mainfrom
Open
fix: replace mapName heuristic fallback with explicit built-in tool whitelist#4fujunchao wants to merge 2 commits intoffflyZzz:mainfrom
fujunchao wants to merge 2 commits intoffflyZzz:mainfrom
Conversation
…hitelist The previous mapName() had a dangerous fallback that capitalized the first letter of ALL tool names not in NAME_MAP: return name.charAt(0).toUpperCase() + name.slice(1); This broke MCP tools with mixed-case names (e.g. grep_app_searchGitHub became Grep_app_searchGitHub), causing tool call failures. Changes: - Expand NAME_MAP from 3 to 22 entries covering all OpenCode built-in tools - Remove heuristic fallback: unknown names now pass through as-is - Add vitest regression tests (9 cases) for built-in and MCP tool names - Add whitelist completeness check against known built-in tool list - Update README.md and README_zh.md to reflect new behavior - Add test script to package.json
- Transform headers: CLI User-Agent, Accept, anthropic-beta, x-app, remove x-api-key - Transform URL: append ?beta=true query parameter - Transform body: billing header injection, system normalization to 3 elements, thinking → adaptive, max_tokens → 64000, metadata.user_id generation - Add normalizeSystemBlocks() with billing-first strategy - Add isMessagesEndpoint() with URL.pathname precision matching - Add patchFetch() idempotency guard (__anyrouter_patched) - Add generateUserId() with crypto.getRandomValues() - Update tests to 36 cases covering all new transforms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
mapName()函数中存在一个危险的回退逻辑,会将所有不在NAME_MAP中的工具名首字母大写:这导致 MCP 工具名被错误修改,例如:
grep_app_searchGitHub→Grep_app_searchGitHub❌context7_resolve-library-id→Context7_resolve-library-id❌MCP 工具名已经包含正确的大小写,盲目转换会导致 AnyRouter 无法匹配工具,工具调用直接失败。
根因分析
OpenCode 中有两类工具:
Tool.define("name", ...)注册,名称全小写(如todowrite、bash、read)sanitizedClientName + "_" + sanitizedToolName注册,保留原始大小写(如grep_app_searchGitHub、Chrome-devtools_click)旧的启发式回退无法区分这两类工具,将 MCP 工具误当作内置工具处理。
修复方案:白名单策略
用显式白名单替代启发式回退。参照 OpenCode 源码 中注册的全部 22 个内置工具:
src/index.ts变更NAME_MAP从 3 条扩展到 22 条,覆盖所有 OpenCode 内置工具NAME_MAP和mapName供测试使用src/index.test.ts新增9 个 vitest 回归测试,覆盖:
todowrite→TodoWrite)bash→Bash)grep_app_searchGitHub→grep_app_searchGitHub)NAME_MAP中)README.md/README_zh.md文档更新更新工具名转换表:
"其他名称 | 首字母大写"的旧描述read、bash、edit等)package.json"test": "vitest run"脚本完整白名单(22 个内置工具)
todowriteTodoWritetodoreadTodoReadwebfetchWebFetchgoogle_searchGoogle_Searchapply_patchApply_patchbashBashbatchBatchcodesearchCodesearcheditEditglobGlobgrepGrepinvalidInvalidlsLslspLspmultieditMultieditplanPlanquestionQuestionreadReadskillSkilltaskTaskwebsearchWebsearchwriteWrite验证
bun run build→dist/index.js6.28 KB