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
104 changes: 56 additions & 48 deletions src/mcp-proxy/pkg/infra/proxy/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func openapiSchemaRefToJSONSchema(schemaRef *openapi3.SchemaRef) jsonschema.Sche
}

func marshalOpenAPISchemaRefJSON(schemaRef *openapi3.SchemaRef) ([]byte, error) {
if schemaRef == nil {
if !openapiSchemaRefHasSchema(schemaRef) {
return nil, nil
}
if schemaRef.Value != nil {
Expand All @@ -53,6 +53,11 @@ func marshalOpenAPISchemaRefJSON(schemaRef *openapi3.SchemaRef) ([]byte, error)
return schemaRef.MarshalJSON()
}

func openapiSchemaRefHasSchema(schemaRef *openapi3.SchemaRef) bool {
// LoadFromData resolves valid refs and rejects dangling refs before reload conversion.
return schemaRef != nil && (schemaRef.Value != nil || schemaRef.Ref != "")
}

func fallbackOpenAPIJSONSchema(schemaJSON []byte) jsonschema.Schema {
var rawSchema map[string]any
if err := json.Unmarshal(schemaJSON, &rawSchema); err != nil {
Expand Down Expand Up @@ -212,58 +217,61 @@ func OpenapiToMcpToolConfig(
Type: j.WithSimpleTypes(jsonschema.Object),
}
for _, param := range operation.Parameters {
if param.Value.Schema != nil {
jsonSchema := openapiSchemaRefToJSONSchema(param.Value.Schema)
if param.Value.Description != "" {
jsonSchema.Description = &param.Value.Description
}
if param.Value.Example != nil {
if jsonSchema.ExtraProperties == nil {
jsonSchema.ExtraProperties = map[string]any{}
}
jsonSchema.ExtraProperties["example"] = param.Value.Example
}
if param.Value.In == "header" {
headerParamSchema.WithPropertiesItem(
param.Value.Name,
jsonschema.SchemaOrBool{
TypeObject: &jsonSchema,
},
)
if param.Value.Required {
headerParamSchema.Required = append(
headerParamSchema.Required,
param.Value.Name,
)
}
if param == nil || param.Value == nil ||
!openapiSchemaRefHasSchema(param.Value.Schema) {
continue
}
parameter := param.Value
jsonSchema := openapiSchemaRefToJSONSchema(parameter.Schema)
if parameter.Description != "" {
jsonSchema.Description = &parameter.Description
}
if parameter.Example != nil {
if jsonSchema.ExtraProperties == nil {
jsonSchema.ExtraProperties = map[string]any{}
}
if param.Value.In == "query" {
queryParamSchema.WithPropertiesItem(
param.Value.Name,
jsonschema.SchemaOrBool{
TypeObject: &jsonSchema,
},
jsonSchema.ExtraProperties["example"] = parameter.Example
}
if parameter.In == "header" {
headerParamSchema.WithPropertiesItem(
parameter.Name,
jsonschema.SchemaOrBool{
TypeObject: &jsonSchema,
},
)
if parameter.Required {
headerParamSchema.Required = append(
headerParamSchema.Required,
parameter.Name,
)
if param.Value.Required {
queryParamSchema.Required = append(
queryParamSchema.Required,
param.Value.Name,
)
}
}
if param.Value.In == "path" {
pathParamSchema.Required = append(
pathParamSchema.Required,
param.Value.Name,
)
pathParamSchema.WithPropertiesItem(
param.Value.Name,
jsonschema.SchemaOrBool{
TypeObject: &jsonSchema,
},
}
if parameter.In == "query" {
queryParamSchema.WithPropertiesItem(
parameter.Name,
jsonschema.SchemaOrBool{
TypeObject: &jsonSchema,
},
)
if parameter.Required {
queryParamSchema.Required = append(
queryParamSchema.Required,
parameter.Name,
)
}
}
if parameter.In == "path" {
pathParamSchema.Required = append(
pathParamSchema.Required,
parameter.Name,
)
pathParamSchema.WithPropertiesItem(
parameter.Name,
jsonschema.SchemaOrBool{
TypeObject: &jsonSchema,
},
)
}
}
if len(headerParamSchema.Properties) > 0 {
headerParamDesc := "HTTP request header parameters, " +
Expand Down Expand Up @@ -299,7 +307,7 @@ func OpenapiToMcpToolConfig(

if operation.RequestBody != nil && operation.RequestBody.Value != nil {
if content, ok := operation.RequestBody.Value.Content["application/json"]; ok &&
content != nil && content.Schema != nil {
content != nil && openapiSchemaRefHasSchema(content.Schema) {
jsonSchema := openapiSchemaRefToJSONSchema(content.Schema)
if jsonSchemaDescriptionIsEmpty(&jsonSchema) {
bodyParamDesc := bodyParamDefaultDescription
Expand Down
42 changes: 42 additions & 0 deletions src/mcp-proxy/pkg/infra/proxy/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,48 @@ var _ = Describe("Converter", func() {
Expect(result[0].ParamSchema.Required).To(ContainElement("header_param"))
})

It("should skip invalid parameters without panicking", func() {
spec := &openapi3.T{
OpenAPI: "3.0.0",
Info: &openapi3.Info{Title: "Test API", Version: "1.0.0"},
Servers: []*openapi3.Server{{URL: "https://api.example.com/v1"}},
Paths: &openapi3.Paths{},
}

pathItem := &openapi3.PathItem{
Get: &openapi3.Operation{
OperationID: "getUsers",
Summary: "Get users",
Parameters: openapi3.Parameters{
nil,
&openapi3.ParameterRef{},
&openapi3.ParameterRef{
Value: &openapi3.Parameter{
Name: "limit",
In: "query",
},
},
&openapi3.ParameterRef{
Value: &openapi3.Parameter{
Name: "offset",
In: "query",
Schema: &openapi3.SchemaRef{},
},
},
},
Responses: &openapi3.Responses{},
},
}
spec.Paths.Set("/users", pathItem)

var result []*ToolConfig
Expect(func() {
result = OpenapiToMcpToolConfig(spec, nil, nil)
}).NotTo(Panic())
Expect(result).To(HaveLen(1))
Expect(result[0].ParamSchema.Properties).NotTo(HaveKey("query_param"))
})

It("should skip JSON request body without schema", func() {
spec := &openapi3.T{
OpenAPI: "3.0.0",
Expand Down
23 changes: 23 additions & 0 deletions src/mcp-proxy/pkg/mcp/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package mcp
import (
"context"

"github.com/getkin/kin-openapi/openapi3"
sdkmcp "github.com/modelcontextprotocol/go-sdk/mcp"

"mcp_proxy/pkg/entity/model"
Expand Down Expand Up @@ -104,11 +105,33 @@ func NewConfig(resourceVersion int) *Config {
}
}

// NewConfigWithOpenAPISpec creates a Config with OpenAPI data for testing.
func NewConfigWithOpenAPISpec(resourceVersion int, openapiFileData *openapi3.T) *Config {
return &Config{
resourceVersion: resourceVersion,
openapiFileData: openapiFileData,
}
}

// GetLoadStatsValues returns stats values for testing.
func GetLoadStatsValues(stats *loadStats) (added, updated, skipped, errorCount int) {
return stats.addedCount, stats.updatedCount, stats.skippedCount, stats.errorCount
}

// GetServerLoadResultError returns the load result error for testing.
func GetServerLoadResultError(result *serverLoadResult) error {
return result.err
}

// BuildReloadPanicReportForTest exposes buildReloadPanicReport for testing.
func BuildReloadPanicReportForTest(
phase string,
serverName string,
panicErr any,
) (string, map[string]string, map[string]any, error) {
return buildReloadPanicReport(phase, serverName, panicErr)
}

// PrefetchServerConfigsForTest exposes prefetchServerConfigs for benchmark testing.
func PrefetchServerConfigsForTest(
ctx context.Context,
Expand Down
Loading
Loading