diff --git a/pkgs/mcpx/internal/openapi/openapi.go b/pkgs/mcpx/internal/openapi/openapi.go index 1e8ab2db4b..f20063aec6 100644 --- a/pkgs/mcpx/internal/openapi/openapi.go +++ b/pkgs/mcpx/internal/openapi/openapi.go @@ -309,7 +309,10 @@ func Build(spec *Spec, opt Options) (*API, error) { } seen[toolName] = true - params := append(append([]Parameter{}, item.Parameters...), op.Parameters...) + params, perr := resolveParams(toolName, item.Parameters, op.Parameters) + if perr != nil { + return nil, perr + } tool := Tool{ Name: toolName, Description: describe(method, path, op), @@ -454,6 +457,45 @@ func firstSentences(s string, n int) string { return s } +// resolveParams merges a path item's parameters with an operation's +// under the OpenAPI parameter-identity rule: a parameter is identified +// by the pair (name, location), and an operation-level parameter with +// the same pair as a path-level one REPLACES it (the operation wins), +// rather than duplicating it. Concatenating the two lists without the +// rule published a schema whose required list named the parameter +// twice -- not valid JSON Schema -- for a normal, spec-endorsed +// pattern (#1669). +// +// The flat tool model keys arguments by name alone, so a name that +// appears in two different locations cannot be represented honestly: +// the schema would show one property while Call sent the value to +// both places. That shape is a Build error naming the tool, the +// parameter and the two locations, so the schema and the wire can +// never silently disagree. +func resolveParams(tool string, pathLevel, opLevel []Parameter) ([]Parameter, error) { + out := make([]Parameter, 0, len(pathLevel)+len(opLevel)) + byPair := make(map[[2]string]int, len(pathLevel)+len(opLevel)) + byName := make(map[string]string, len(pathLevel)+len(opLevel)) + for _, list := range [][]Parameter{pathLevel, opLevel} { + for _, p := range list { + if prev, seen := byName[p.Name]; seen && prev != p.In { + return nil, fmt.Errorf("%s: parameter %q appears in both the %s and %s locations; "+ + "a tool's arguments are keyed by name alone, so one name cannot stand for two "+ + "parameters -- rename one of them in the document", tool, p.Name, prev, p.In) + } + byName[p.Name] = p.In + pair := [2]string{p.Name, p.In} + if i, dup := byPair[pair]; dup { + out[i] = p // the later declaration replaces the earlier one + continue + } + byPair[pair] = len(out) + out = append(out, p) + } + } + return out, nil +} + // buildSchema renders an operation's arguments as one JSON Schema object. // // Path, query and header parameters and the request body are flattened into diff --git a/pkgs/mcpx/internal/openapi/openapi_test.go b/pkgs/mcpx/internal/openapi/openapi_test.go index d9b8c7ca11..373943b1b6 100644 --- a/pkgs/mcpx/internal/openapi/openapi_test.go +++ b/pkgs/mcpx/internal/openapi/openapi_test.go @@ -386,3 +386,105 @@ func TestARelativeURLWithNoHTTPSourceSaysWhatToDo(t *testing.T) { t.Errorf("the remedy should be named: %v", err) } } + +// #1669: a path-level parameter redeclared at operation level is ONE +// parameter (identity is the (name, in) pair), the operation's version +// wins, and the generated schema must not list it twice in required -- +// a duplicated required entry is not valid JSON Schema. +func TestOperationParameterOverridesPathLevel(t *testing.T) { + spec, err := openapi.Parse([]byte(`{ + "openapi": "3.0.0", + "info": { "title": "Override", "version": "1" }, + "servers": [{ "url": "https://api.example.com" }], + "paths": { + "/things/{id}": { + "parameters": [ + { "name": "id", "in": "path", "required": true, + "description": "the path-level declaration", + "schema": { "type": "string" } } + ], + "get": { + "operationId": "getThing", + "parameters": [ + { "name": "id", "in": "path", "required": true, + "description": "the operation-level declaration", + "schema": { "type": "integer" } } + ] + } + } + } + }`)) + if err != nil { + t.Fatal(err) + } + api, err := openapi.Build(spec, openapi.Options{}) + if err != nil { + t.Fatal(err) + } + if len(api.Tools) != 1 { + t.Fatalf("tools: %v", names(api)) + } + tool := api.Tools[0] + if len(tool.Params) != 1 { + t.Fatalf("the override must resolve to one parameter, got %+v", tool.Params) + } + if got := tool.Params[0].Description; got != "the operation-level declaration" { + t.Errorf("the operation-level declaration wins, got %q", got) + } + var schema struct { + Required []string `json:"required"` + Properties map[string]any `json:"properties"` + } + if err := json.Unmarshal(tool.InputSchema, &schema); err != nil { + t.Fatal(err) + } + count := 0 + for _, r := range schema.Required { + if r == "id" { + count++ + } + } + if count != 1 { + t.Errorf("required lists id %d times (%v); it must appear exactly once", count, schema.Required) + } + if _, ok := schema.Properties["id"]; !ok { + t.Errorf("schema properties missing id: %v", schema.Properties) + } +} + +// #1669: the same name in two different locations cannot be one flat +// argument -- the schema would describe one property while Call sent +// the value to both places -- so Build refuses it, naming the tool, +// the parameter and both locations. +func TestSameNameTwoLocationsIsABuildError(t *testing.T) { + spec, err := openapi.Parse([]byte(`{ + "openapi": "3.0.0", + "info": { "title": "Collision", "version": "1" }, + "servers": [{ "url": "https://api.example.com" }], + "paths": { + "/things": { + "parameters": [ + { "name": "key", "in": "query", "schema": { "type": "string" } } + ], + "get": { + "operationId": "listThings", + "parameters": [ + { "name": "key", "in": "header", "schema": { "type": "string" } } + ] + } + } + } + }`)) + if err != nil { + t.Fatal(err) + } + _, err = openapi.Build(spec, openapi.Options{}) + if err == nil { + t.Fatal("a same-name parameter in two locations must fail Build") + } + for _, want := range []string{"listthings", `"key"`, "query", "header"} { + if !strings.Contains(err.Error(), want) { + t.Errorf("the error must name %s, got: %v", want, err) + } + } +}