fix(tools): normalize integer arguments - #105
Open
print-happy wants to merge 1 commit into
Open
Conversation
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.
Summary
本 PR 在调用工具 handler 前,根据已注册工具的
InputSchema对 arguments 做归一化:integer的字段会转换为 Gointnumber的字段保持float64string的字段保持stringboolean的字段保持boolobject/array的字段保持原结构,并递归处理其中声明为integer的子字段Problem
用户反馈通过
mcp.WithInteger()注册工具参数后,在工具 handler 中从req.Params.Arguments取出的实际类型是float64,而不是预期的整数类型。问题发生在 JSON-RPC 请求解析阶段,Go 标准库在反序列化到
interface{}时,会把 JSON number 默认解成float64,tools/call之前直接把arguments作为map[string]interface{}传给 handler,因此 schema 中声明为integer的参数也会以float64暴露给用户。兼容性影响
对存量用户有潜在影响,假设用户之前通过
WithInteger("page")注册参数,但在 handler 中按旧行为读取,如:升级后该断言会失败。新行为应改为:
如果用户需要兼容新旧版本,可以临时同时支持两种类型:
Tests
GOCACHE=/data/tmp/go-build go test -count=1 ./... GOCACHE=/data/tmp/go-build go vet ./...