-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
152 lines (132 loc) · 4.32 KB
/
handler.go
File metadata and controls
152 lines (132 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"net/url"
"strings"
"github.com/mark3labs/mcp-go/mcp"
)
// formatValue converts a value to a string suitable for URL path/query params.
// JSON numbers are unmarshaled as float64 in Go; large integers like 95893899
// would format as "9.5893899e+07" with %v, breaking API URLs.
func formatValue(v any) string {
if f, ok := v.(float64); ok && f == math.Trunc(f) {
return fmt.Sprintf("%d", int64(f))
}
return fmt.Sprintf("%v", v)
}
// operationInfo captures everything needed to proxy a single API call.
type operationInfo struct {
Method string // HTTP method (GET, POST, etc.)
PathPattern string // e.g. "/api/v1/athlete/{id}/activities"
PathParams []string // path param names (excluding athlete ID)
QueryParams []string // query param names
AthleteIDParams map[string]bool // param names that should be auto-injected
HasBody bool // whether the operation accepts a JSON body
HasExt bool // whether the path has an {ext} param
}
// makeHandler creates an MCP tool handler that proxies calls to the Intervals.icu API.
func makeHandler(info operationInfo, cfg Config, client *http.Client) func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
return func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
args := req.GetArguments()
// Build the URL path by substituting path parameters.
path := info.PathPattern
for _, name := range info.PathParams {
val := formatValue(args[name])
path = strings.ReplaceAll(path, "{"+name+"}", val)
}
// Auto-inject athlete ID parameters.
for paramName := range info.AthleteIDParams {
path = strings.ReplaceAll(path, "{"+paramName+"}", cfg.AthleteID)
}
// Handle {ext} parameter.
if info.HasExt {
ext := ""
if v, ok := args["ext"]; ok {
ext = fmt.Sprintf("%v", v)
}
if ext == "" {
// Remove the {ext} placeholder entirely (default to JSON).
path = strings.ReplaceAll(path, "{ext}", "")
} else {
// Ensure dot prefix.
if !strings.HasPrefix(ext, ".") {
ext = "." + ext
}
path = strings.ReplaceAll(path, "{ext}", ext)
}
}
// Build query string.
q := url.Values{}
for _, name := range info.QueryParams {
v, ok := args[name]
if !ok {
continue
}
switch val := v.(type) {
case []any:
for _, item := range val {
q.Add(name, formatValue(item))
}
default:
q.Set(name, formatValue(val))
}
}
fullURL := cfg.BaseURL + path
if encoded := q.Encode(); encoded != "" {
fullURL += "?" + encoded
}
// Build request body if present.
var bodyReader io.Reader
if info.HasBody {
if bodyArg, ok := args["body"]; ok {
bodyBytes, err := json.Marshal(bodyArg)
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("failed to marshal request body: %v", err)), nil
}
bodyReader = bytes.NewReader(bodyBytes)
}
}
httpReq, err := http.NewRequestWithContext(ctx, info.Method, fullURL, bodyReader)
if err != nil {
return nil, fmt.Errorf("failed to create HTTP request: %w", err)
}
// Basic Auth: API_KEY as username, api_key as password.
httpReq.SetBasicAuth("API_KEY", cfg.APIKey)
if info.HasBody && bodyReader != nil {
httpReq.Header.Set("Content-Type", "application/json")
}
resp, err := client.Do(httpReq)
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("API request failed: %v", err)), nil
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("failed to read response: %v", err)), nil
}
// 204 No Content.
if resp.StatusCode == http.StatusNoContent {
return mcp.NewToolResultText("Success (204 No Content)"), nil
}
// Error responses.
if resp.StatusCode >= 400 {
return mcp.NewToolResultError(fmt.Sprintf("API error %d: %s", resp.StatusCode, string(respBody))), nil
}
// Try to pretty-print JSON responses.
var jsonData any
if err := json.Unmarshal(respBody, &jsonData); err == nil {
pretty, err := json.MarshalIndent(jsonData, "", " ")
if err == nil {
return mcp.NewToolResultText(string(pretty)), nil
}
}
// Return raw response if not JSON.
return mcp.NewToolResultText(string(respBody)), nil
}
}