From 741bdd682ca98aaa2c03f845f8575dd252951f50 Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Mon, 14 Apr 2025 10:19:49 +0800 Subject: [PATCH] Fix: API forwarding header setting failure issue --- mcp-server/tool.go | 21 +++++++++++++++++++-- module/publish/iml.go | 6 ++++++ module/router/dto/input.go | 7 ++++--- module/router/dto/output.go | 14 ++++++++------ service/api/model.go | 7 ++++--- 5 files changed, 41 insertions(+), 14 deletions(-) diff --git a/mcp-server/tool.go b/mcp-server/tool.go index c91611c6..b7bddd04 100644 --- a/mcp-server/tool.go +++ b/mcp-server/tool.go @@ -64,8 +64,25 @@ func (t *Tool) RegisterMCP(s *server.MCPServer) { body := "" for k, v := range request.Params.Arguments { if k == "Body" { - tmp, _ := json.Marshal(v) - body = string(tmp) + switch a := v.(type) { + case string: + body = a + case map[string]interface{}: + switch t.contentType { + case "application/json": + tmp, _ := json.Marshal(a) + body = string(tmp) + case "application/x-www-form-urlencoded": + bodyValue := url.Values{} + for kk, vv := range a { + bodyValue.Set(kk, fmt.Sprintf("%v", vv)) + } + body = bodyValue.Encode() + } + default: + tmp, _ := json.Marshal(a) + body = string(tmp) + } continue } tmp, ok := v.(map[string]interface{}) diff --git a/module/publish/iml.go b/module/publish/iml.go index 692a5f3a..b39db372 100644 --- a/module/publish/iml.go +++ b/module/publish/iml.go @@ -159,6 +159,7 @@ func (m *imlPublishModule) getProjectRelease(ctx context.Context, projectID stri return &gateway.ProxyHeader{ Key: h.Key, Value: h.Value, + Opt: h.OptType, } }) apiInfo.Retry = proxy.Retry @@ -633,12 +634,17 @@ func (i *imlPublishModule) updateMCPServer(ctx context.Context, sid string, name if err != nil { return err } + //switch a.ContentType { + //case "application/json": switch tmp.Type { case "object": toolOptions = append(toolOptions, mcp.WithObject(mcp_server.MCPBody, mcp.Properties(tmp.Properties), mcp.Description("request body,it is avalible when method is POST、PUT、PATCH."))) case "array": toolOptions = append(toolOptions, mcp.WithArray(mcp_server.MCPBody, mcp.Items(tmp.Items), mcp.Description("request body,it is avalible when method is POST、PUT、PATCH."))) } + //case "application/x-www-form-urlencoded": + // toolOptions = append(toolOptions, mcp.WithString(mcp_server.MCPBody, mcp.Items(tmp.Items), mcp.Description("request body,it is avalible when method is POST、PUT、PATCH."))) + } tools = append(tools, mcp_server.NewTool(a.Summary, a.Path, a.Method, a.ContentType, toolOptions...)) } diff --git a/module/router/dto/input.go b/module/router/dto/input.go index 41198b00..dccb5221 100644 --- a/module/router/dto/input.go +++ b/module/router/dto/input.go @@ -80,9 +80,10 @@ func ToServiceProxy(proxy *InputProxy) *api.Proxy { } headers := utils.SliceToSlice(proxy.Headers, func(h *Header) *api.Header { return &api.Header{ - Key: h.Key, - Value: h.Value, - Opt: h.Opt, + Key: h.Key, + Value: h.Value, + Opt: h.Opt, + OptType: h.OptType, } }) diff --git a/module/router/dto/output.go b/module/router/dto/output.go index 50611785..ca5c488b 100644 --- a/module/router/dto/output.go +++ b/module/router/dto/output.go @@ -84,9 +84,10 @@ func FromServiceProxy(proxy *api.Proxy) *Proxy { Retry: proxy.Retry, Headers: utils.SliceToSlice(proxy.Headers, func(header *api.Header) *Header { return &Header{ - Key: header.Key, - Value: header.Value, - Opt: header.Opt, + Key: header.Key, + Value: header.Value, + Opt: header.Opt, + OptType: header.OptType, } }), Extends: proxy.Extends, @@ -104,9 +105,10 @@ type Proxy struct { } type Header struct { - Key string `json:"key"` - Value string `json:"value"` - Opt string `json:"opt"` + Key string `json:"key"` + Value string `json:"value"` + Opt string `json:"opt"` + OptType string `json:"optType"` } type Export struct { diff --git a/service/api/model.go b/service/api/model.go index 4254f94a..5923236a 100644 --- a/service/api/model.go +++ b/service/api/model.go @@ -144,9 +144,10 @@ type Proxy struct { } type Header struct { - Key string `json:"key"` - Value string `json:"value"` - Opt string `json:"opt"` + Key string `json:"key"` + Value string `json:"value"` + Opt string `json:"opt"` + OptType string `json:"opt_type"` } type Router struct {