(ctx context.Context, _ *mcp.CallToolRequest, input CallInput)
| 52 | } |
| 53 | |
| 54 | func (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 | |
| 110 | func formatCallOutput(output CallOutput, endpoint *EndpointInfo) string { |
| 111 | var sb strings.Builder |
nothing calls this directly
no test coverage detected