fix: 将 MCP 工具名 sanitize 为 OpenAI 兼容格式 - #255
Open
zhaji2333 wants to merge 2 commits into
Open
Conversation
execute 流式 shell 硬编码 /bin/sh -c,Windows 平台直接报 exec: "/bin/sh": executable file not found。按 runtime.GOOS 选择: Windows 走 cmd /c,类 Unix 保持 /bin/sh -c 不变;同时 Windows 下跳过 sh 专用命令预处理(export、/dev/null 等),避免 cmd 执行失败。 为依赖 sh 语法断言的流式测试补充 Windows skip(修复前后在 Windows 均无法通过)。
MCP 工具名含 . 或 ::(如 fs.read、nezha::fs.read)时,作为 OpenAI function.name 提交会被网关拒绝:不符合 ^[a-zA-Z0-9_-]+$。 在 toolInfoFromDefinition 中对展示给模型的 ToolInfo.Name 做 sanitize(:: → __、. → _、其余非法字符 → _);执行路径仍使用 原始 mcpBridgeTool.name,不受影响。附带 sanitize 单元测试。
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.
Fixes #250
问题
通过 OpenAI 兼容网关(如 OpenRouter、DashScope)使用模型时,MCP 工具名含
.或::(如fs.read、server.exec、nezha::fs.read)会被网关以 400 拒绝:根因:
toolInfoFromDefinition将 MCP 原始工具名直接作为 OpenAI 的function.name。修复
sanitizeOpenAIToolName:::→__,.→_,其余非法字符 →_;ToolInfo.Name做 sanitize;执行路径仍使用原始mcpBridgeTool.name(runMCPToolInvocation按原始名调用 MCP),不受影响;fs.read、nezha::fs.read、大小写/数字/连字符/中文等用例。验证
go build ./...全项目编译通过;go test ./internal/einomcp/通过(含新增TestSanitizeOpenAIToolName)。