MCPcopy Create free account
hub / github.com/bytebase/bytebase / handleCallAPI

Method handleCallAPI

backend/api/mcp/tool_call.go:54–108  ·  view source on GitHub ↗
(ctx context.Context, _ *mcp.CallToolRequest, input CallInput)

Source from the content-addressed store, hash-verified

52}
53
54func (s *Server) handleCallAPI(ctx context.Context, _ *mcp.CallToolRequest, input CallInput) (*mcp.CallToolResult, any, error) {
55 // Validate input
56 if input.OperationID == "" {
57 return nil, nil, errors.New("operationId is required")
58 }
59
60 // Look up endpoint
61 endpoint, ok := s.openAPIIndex.GetEndpoint(input.OperationID)
62 if !ok {
63 return nil, nil, errors.Errorf("unknown operation %s, use search_api to find valid operations", input.OperationID)
64 }
65
66 // Build request body
67 body := input.Body
68 if body == nil {
69 body = make(map[string]any)
70 }
71
72 // Execute API request
73 resp, err := s.apiRequest(ctx, endpoint.Path, body)
74 if err != nil {
75 return nil, nil, err
76 }
77
78 // Check for binary response - not supported
79 contentType := resp.Headers.Get("Content-Type")
80 if isBinaryContentType(contentType) {
81 return nil, nil, errors.Errorf("binary response not supported (content-type: %s)", contentType)
82 }
83
84 // Parse JSON response
85 output := CallOutput{
86 Status: resp.Status,
87 }
88 if len(resp.Body) > 0 {
89 var respJSON any
90 if json.Unmarshal(resp.Body, &respJSON) != nil {
91 respJSON = string(resp.Body)
92 }
93 output.Response = respJSON
94 }
95
96 // Check for error response
97 if resp.Status >= 400 {
98 output.Error = parseError(resp.Body)
99 if output.Error == "" {
100 output.Error = fmt.Sprintf("HTTP %d", resp.Status)
101 }
102 }
103
104 text := formatCallOutput(output, endpoint)
105 return &mcp.CallToolResult{
106 Content: []mcp.Content{&mcp.TextContent{Text: text}},
107 }, output, nil
108}
109
110func formatCallOutput(output CallOutput, endpoint *EndpointInfo) string {
111 var sb strings.Builder

Callers

nothing calls this directly

Calls 8

apiRequestMethod · 0.95
isBinaryContentTypeFunction · 0.85
parseErrorFunction · 0.85
formatCallOutputFunction · 0.85
ErrorfMethod · 0.80
GetMethod · 0.80
UnmarshalMethod · 0.80
GetEndpointMethod · 0.45

Tested by

no test coverage detected