Skip to content
This repository was archived by the owner on Jun 29, 2025. It is now read-only.

Commit 3ef22b1

Browse files
committed
Support application/vnd.api+json in addition to application/json
1 parent 5f8cd16 commit 3ef22b1

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

pkg/openapi2mcp/register.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -692,11 +692,11 @@ func generateAI5xxErrorResponse(op OpenAPIOperation, inputSchemaJSON []byte, arg
692692
// Add tool usage information for AI agents
693693
var schemaObj map[string]any
694694
_ = json.Unmarshal(inputSchemaJSON, &schemaObj)
695-
695+
696696
if properties, ok := schemaObj["properties"].(map[string]any); ok && len(properties) > 0 {
697697
response.WriteString("\nTOOL USAGE INFORMATION:\n")
698698
response.WriteString(fmt.Sprintf("Tool Name: %s\n", op.OperationID))
699-
699+
700700
// Show required parameters
701701
if required, ok := schemaObj["required"].([]any); ok && len(required) > 0 {
702702
response.WriteString("Required Parameters (mandatory for all calls):\n")
@@ -716,11 +716,11 @@ func generateAI5xxErrorResponse(op OpenAPIOperation, inputSchemaJSON []byte, arg
716716
}
717717
}
718718
}
719-
719+
720720
// Generate example usage with correct parameters
721721
response.WriteString("\nExample Usage (retry with these correct parameters):\n")
722722
exampleArgs := map[string]any{}
723-
723+
724724
// Add required parameters to example
725725
if required, ok := schemaObj["required"].([]any); ok {
726726
for _, req := range required {
@@ -731,7 +731,7 @@ func generateAI5xxErrorResponse(op OpenAPIOperation, inputSchemaJSON []byte, arg
731731
}
732732
}
733733
}
734-
734+
735735
// Add a few optional parameters for completeness
736736
count := 0
737737
for paramName, paramDef := range properties {
@@ -742,7 +742,7 @@ func generateAI5xxErrorResponse(op OpenAPIOperation, inputSchemaJSON []byte, arg
742742
}
743743
}
744744
}
745-
745+
746746
exampleJSON, _ := json.MarshalIndent(exampleArgs, "", " ")
747747
response.WriteString(fmt.Sprintf("call %s %s\n", op.OperationID, string(exampleJSON)))
748748
}
@@ -1003,8 +1003,20 @@ func RegisterOpenAPITools(server *mcpserver.MCPServer, ops []OpenAPIOperation, d
10031003
}
10041004
// Build request body if needed
10051005
var body []byte
1006+
var requestContentType string
10061007
if opCopy.RequestBody != nil && opCopy.RequestBody.Value != nil {
1007-
if mt := opCopy.RequestBody.Value.Content.Get("application/json"); mt != nil && mt.Schema != nil && mt.Schema.Value != nil {
1008+
// Check for application/json first, then application/vnd.api+json
1009+
mt := opCopy.RequestBody.Value.Content.Get("application/json")
1010+
if mt != nil {
1011+
requestContentType = "application/json"
1012+
} else {
1013+
mt = opCopy.RequestBody.Value.Content.Get("application/vnd.api+json")
1014+
if mt != nil {
1015+
requestContentType = "application/vnd.api+json"
1016+
}
1017+
}
1018+
1019+
if mt != nil && mt.Schema != nil && mt.Schema.Value != nil {
10081020
if v, ok := args["requestBody"]; ok && v != nil {
10091021
body, _ = json.Marshal(v)
10101022
}
@@ -1016,9 +1028,11 @@ func RegisterOpenAPITools(server *mcpserver.MCPServer, ops []OpenAPIOperation, d
10161028
if err != nil {
10171029
return nil, err
10181030
}
1019-
if len(body) > 0 {
1020-
httpReq.Header.Set("Content-Type", "application/json")
1031+
if len(body) > 0 && requestContentType != "" {
1032+
httpReq.Header.Set("Content-Type", requestContentType)
10211033
}
1034+
// Set Accept header to accept both JSON and JSON:API responses
1035+
httpReq.Header.Set("Accept", "application/json, application/vnd.api+json")
10221036
// --- AUTH HANDLING: inject per-operation security requirements ---
10231037
// For each security requirement object, try to satisfy at least one scheme
10241038
securitySatisfied := false
@@ -1134,7 +1148,7 @@ func RegisterOpenAPITools(server *mcpserver.MCPServer, ops []OpenAPIOperation, d
11341148
}
11351149

11361150
contentType := resp.Header.Get("Content-Type")
1137-
isJSON := strings.HasPrefix(contentType, "application/json")
1151+
isJSON := strings.HasPrefix(contentType, "application/json") || strings.HasPrefix(contentType, "application/vnd.api+json")
11381152
isText := strings.HasPrefix(contentType, "text/")
11391153
isBinary := !isJSON && !isText
11401154

pkg/openapi2mcp/schema.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,19 @@ func BuildInputSchema(params openapi3.Parameters, requestBody *openapi3.RequestB
179179
}
180180
}
181181

182-
// Request body (only application/json for now)
182+
// Request body (application/json and application/vnd.api+json)
183183
if requestBody != nil && requestBody.Value != nil {
184184
for mtName := range requestBody.Value.Content {
185-
if mtName != "application/json" {
186-
fmt.Fprintf(os.Stderr, "[WARN] Request body uses media type '%s'. Only 'application/json' is fully supported.\n", mtName)
185+
if mtName != "application/json" && mtName != "application/vnd.api+json" {
186+
fmt.Fprintf(os.Stderr, "[WARN] Request body uses media type '%s'. Only 'application/json' and 'application/vnd.api+json' are fully supported.\n", mtName)
187187
}
188188
}
189-
if mt := requestBody.Value.Content.Get("application/json"); mt != nil && mt.Schema != nil && mt.Schema.Value != nil {
189+
// Try application/json first, then application/vnd.api+json
190+
mt := requestBody.Value.Content.Get("application/json")
191+
if mt == nil {
192+
mt = requestBody.Value.Content.Get("application/vnd.api+json")
193+
}
194+
if mt != nil && mt.Schema != nil && mt.Schema.Value != nil {
190195
bodyProp := extractProperty(mt.Schema)
191196
bodyProp["description"] = "The JSON request body."
192197
properties["requestBody"] = bodyProp

0 commit comments

Comments
 (0)