feat(params): add structured argument binding helper

Add params.Bind, which unmarshals a tool call's map[string]any args into a
typed struct via JSON round-trip so JSON numbers land in the correct Go
numeric field types, and enforces `required:"true"` struct tags with clear
errors. Migrate the branch, tree, and file repo handlers to use it instead
of repeated args["x"].(string)/!ok extraction, preserving existing
validation behavior for each field.

Co-Authored-By: Codet <codet@commitgo.dev> (GPT-5-Codex)
This commit is contained in:
Lunny Xiao
2026-08-24 00:14:34 -07:00
parent 815a0e26aa
commit b22ee74148
5 changed files with 288 additions and 117 deletions
+12 -14
View File
@@ -37,20 +37,18 @@ func init() {
})
}
type getRepoTreeArgs struct {
Owner string `json:"owner" required:"true"`
Repo string `json:"repo" required:"true"`
TreeSHA string `json:"tree_sha" required:"true"`
Recursive bool `json:"recursive"`
}
func GetRepoTreeFn(ctx context.Context, args map[string]any) (*mcp.CallToolResult, error) {
owner, err := params.GetString(args, "owner")
if err != nil {
var in getRepoTreeArgs
if err := params.Bind(args, &in); err != nil {
return to.ErrorResult(err)
}
repo, err := params.GetString(args, "repo")
if err != nil {
return to.ErrorResult(err)
}
treeSHA, err := params.GetString(args, "tree_sha")
if err != nil {
return to.ErrorResult(err)
}
recursive, _ := args["recursive"].(bool)
page, pageSize := params.GetPagination(args, 30)
opt := gitea_sdk.ListTreeOptions{
@@ -58,14 +56,14 @@ func GetRepoTreeFn(ctx context.Context, args map[string]any) (*mcp.CallToolResul
Page: page,
PageSize: pageSize,
},
Ref: treeSHA,
Recursive: recursive,
Ref: in.TreeSHA,
Recursive: in.Recursive,
}
client, err := gitea.ClientFromContext(ctx)
if err != nil {
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
}
tree, _, err := client.Git.GetTrees(ctx, owner, repo, opt)
tree, _, err := client.Git.GetTrees(ctx, in.Owner, in.Repo, opt)
if err != nil {
return to.ErrorResult(fmt.Errorf("get repository tree err: %v", err))
}