Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions pkgs/mcpx/internal/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,14 @@ func Build(spec *Spec, opt Options) (*API, error) {
seen := map[string]bool{}
for _, path := range paths {
item := spec.Paths[path]
for method, op := range item.operations() {
operations := item.operations()
operationMethods := make([]string, 0, len(operations))
for method := range operations {
operationMethods = append(operationMethods, method)
}
sort.Strings(operationMethods)
for _, method := range operationMethods {
op := operations[method]
if !allowed[method] {
continue
}
Expand Down Expand Up @@ -537,10 +544,26 @@ func buildSchema(params []Parameter, op *Operation) json.RawMessage {
}

if op.RequestBody != nil {
for media, c := range op.RequestBody.Content {
mediaTypes := make([]string, 0, len(op.RequestBody.Content))
for media := range op.RequestBody.Content {
if !strings.Contains(media, "json") {
continue
}
mediaTypes = append(mediaTypes, media)
}
sort.Strings(mediaTypes)
selectedMedia := ""
for _, media := range mediaTypes {
if media == "application/json" {
selectedMedia = media
break
}
}
if selectedMedia == "" && len(mediaTypes) > 0 {
selectedMedia = mediaTypes[0]
}
if selectedMedia != "" {
c := op.RequestBody.Content[selectedMedia]
entry := map[string]any{"description": "the request body"}
for k, v := range c.Schema {
entry[k] = v
Expand All @@ -549,7 +572,6 @@ func buildSchema(params []Parameter, op *Operation) json.RawMessage {
if op.RequestBody.Required {
required = append(required, "body")
}
break
}
}

Expand Down
83 changes: 83 additions & 0 deletions pkgs/mcpx/internal/openapi/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,89 @@ func TestARequestBodyBecomesABodyArgument(t *testing.T) {
}
}

func TestDuplicateOperationIDsAreDeterministic(t *testing.T) {
const spec = `{
"openapi": "3.0.0",
"info": {"title": "Pet Store", "version": "1.0"},
"servers": [{"url": "https://api.example.com"}],
"paths": {"/pets": {
"get": {"operationId": "listPets"},
"post": {"operationId": "listPets"}
}}
}`
parsed, err := openapi.Parse([]byte(spec))
if err != nil {
t.Fatal(err)
}

for i := 0; i < 200; i++ {
api, err := openapi.Build(parsed, openapi.Options{Methods: []string{"GET", "POST"}})
if err != nil {
t.Fatal(err)
}
owners := map[string]string{}
for _, tool := range api.Tools {
owners[tool.Name] = tool.Method
}
bare, suffixed := "", ""
for name := range owners {
if strings.HasSuffix(name, "_2") {
suffixed = name
} else {
bare = name
}
}
if bare == "" || suffixed == "" || owners[bare] != "GET" || owners[suffixed] != "POST" {
t.Fatalf("iteration %d: duplicate operation names have unstable ownership: %v", i, owners)
}
}
}

func TestRequestBodyJSONMediaPreferenceIsDeterministic(t *testing.T) {
tests := []struct {
name string
spec string
want string
}{
{
name: "prefers exact application/json",
spec: `{"application/vnd.z+json":{"schema":{"type":"string","title":"last"}},"application/vnd.a+json":{"schema":{"type":"string","title":"first"}},"application/json":{"schema":{"type":"string","title":"exact"}}}`,
want: "exact",
},
{
name: "uses lexicographically first JSON media type when exact is absent",
spec: `{"application/vnd.z+json":{"schema":{"type":"string","title":"last"}},"application/vnd.a+json":{"schema":{"type":"string","title":"first"}}}`,
want: "first",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
spec := `{"openapi":"3.0.0","info":{"title":"Pet Store","version":"1.0"},"servers":[{"url":"https://api.example.com"}],"paths":{"/pets":{"post":{"operationId":"createPet","requestBody":{"content":` + tt.spec + `}}}}}`
parsed, err := openapi.Parse([]byte(spec))
if err != nil {
t.Fatal(err)
}

for i := 0; i < 200; i++ {
api, err := openapi.Build(parsed, openapi.Options{Methods: []string{"POST"}})
if err != nil {
t.Fatal(err)
}
var schema struct {
Properties map[string]map[string]any `json:"properties"`
}
if err := json.Unmarshal(api.Tools[0].InputSchema, &schema); err != nil {
t.Fatal(err)
}
if got := schema.Properties["body"]["title"]; got != tt.want {
t.Fatalf("iteration %d: body schema title = %v, want %q", i, got, tt.want)
}
}
})
}
}

func TestCallsGoToTheRightPlace(t *testing.T) {
var gotPath, gotQuery, gotHeader string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
Loading