MCPcopy Index your code
hub / github.com/APIParkLab/APIPark / generateInvokeTool

Function generateInvokeTool

mcp-server/tool.go:48–131  ·  view source on GitHub ↗
(path string, method string, contentType string, params map[string]*Param)

Source from the content-addressed store, hash-verified

46}
47
48func generateInvokeTool(path string, method string, contentType string, params map[string]*Param) func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
49 return func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
50 invokeAddress := utils.GatewayInvoke(ctx)
51 if invokeAddress == "" {
52 return nil, fmt.Errorf("invoke address is empty")
53 }
54 u, err := url.Parse(invokeAddress)
55 if err != nil {
56 return nil, fmt.Errorf("invalid invoke address %s", invokeAddress)
57 }
58 if u.Scheme == "" {
59 u.Scheme = "http"
60 }
61
62 queries := url.Values{}
63 headers := make(map[string]string)
64 bodyParam := NewBodyParam(contentType)
65 for k, p := range params {
66 vv, ok := request.GetArguments()[k]
67 if !ok && p.required {
68 return nil, fmt.Errorf("param %s is required", k)
69 }
70 if p.position == PositionHeader || p.position == PositionQuery || p.position == PositionPath {
71 v, ok := vv.(string)
72 if !ok || v == "<nil>" {
73 if p.required {
74 return nil, fmt.Errorf("param %s is required", k)
75 }
76 continue
77 }
78 }
79
80 switch p.position {
81 case PositionPath:
82 path = strings.ReplaceAll(path, "{"+k+"}", fmt.Sprintf("%v", vv))
83 case PositionQuery:
84 queries.Set(k, fmt.Sprintf("%v", vv))
85 case PositionHeader:
86 headers[k] = fmt.Sprintf("%v", vv)
87 case PositionBody:
88 if vv == nil {
89 continue
90 }
91 bodyParam.Set(k, vv)
92 }
93 }
94 bodyData, err := bodyParam.Encode()
95 if err != nil {
96 return nil, err
97 }
98 u.Path = path
99 u.RawQuery = queries.Encode()
100
101 req, err := http.NewRequest(method, u.String(), strings.NewReader(bodyData))
102 if err != nil {
103 return nil, err
104 }
105 for k, v := range headers {

Callers 1

ToolMethod · 0.85

Calls 7

SetMethod · 0.95
EncodeMethod · 0.95
NewBodyParamFunction · 0.85
LabelMethod · 0.80
SetMethod · 0.65
CloseMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected