Repository navigation
Expand file tree
/
Copy pathmcp.go
More file actions
277 lines (236 loc) · 7.86 KB
/
Copy pathmcp.go
File metadata and controls
277 lines (236 loc) · 7.86 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Package client implements an MCP JSON-RPC client for Pipeboard servers.
package client
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"sync/atomic"
)
// Client talks to Pipeboard MCP servers over HTTP.
type Client struct {
baseURL string
token string
version string
userAgent string
httpClient *http.Client
reqID atomic.Int64
}
// New creates an MCP client. version is used in the User-Agent header
// and the MCP clientInfo.
func New(baseURL, token, version string) *Client {
return &Client{
baseURL: baseURL,
token: token,
version: version,
userAgent: "pipeboard-cli/" + version,
httpClient: &http.Client{},
}
}
// jsonRPCRequest is a JSON-RPC 2.0 request.
type jsonRPCRequest struct {
JSONRPC string `json:"jsonrpc"`
ID int64 `json:"id"`
Method string `json:"method"`
Params interface{} `json:"params,omitempty"`
}
// jsonRPCResponse is a JSON-RPC 2.0 response.
type jsonRPCResponse struct {
JSONRPC string `json:"jsonrpc"`
ID int64 `json:"id"`
Result json.RawMessage `json:"result,omitempty"`
Error *jsonRPCError `json:"error,omitempty"`
}
type jsonRPCError struct {
Code int `json:"code"`
Message string `json:"message"`
}
func (e *jsonRPCError) Error() string {
return fmt.Sprintf("MCP error %d: %s", e.Code, e.Message)
}
// ToolAnnotations carries the MCP tool behavior hints (spec 2025-06-18 / 2025-11-25).
// All fields are HINTS that clients use for UX (confirmation prompts, grouping)
// not enforcement. Spec defaults when unset: readOnlyHint=false,
// destructiveHint=true, idempotentHint=false, openWorldHint=true.
type ToolAnnotations struct {
Title string `json:"title,omitempty"`
ReadOnlyHint *bool `json:"readOnlyHint,omitempty"`
DestructiveHint *bool `json:"destructiveHint,omitempty"`
IdempotentHint *bool `json:"idempotentHint,omitempty"`
OpenWorldHint *bool `json:"openWorldHint,omitempty"`
}
// ToolDefinition represents a tool from tools/list.
//
// Title is the tool's human-readable display name. Note there are TWO places a
// title can live: this top-level field (MCP spec 2025-06-18 onward, what
// Pipeboard servers populate) and the legacy `annotations.title`. Without the
// top-level field here, every tool decoded as untitled — including in `--json`
// output and the on-disk tools cache, both of which re-marshal this struct — so
// `tools-list` reported 0/63 titled against a server that returns 63/63.
type ToolDefinition struct {
Name string `json:"name"`
Title string `json:"title,omitempty"`
Description string `json:"description"`
InputSchema json.RawMessage `json:"inputSchema"`
Annotations *ToolAnnotations `json:"annotations,omitempty"`
}
// DisplayTitle returns the tool's human-readable title, preferring the
// top-level field and falling back to the legacy `annotations.title`. Empty
// when the server sends neither — which for a Pipeboard server means a real
// compliance gap (a title is a blocking Claude Connectors submission
// requirement), so callers should show its absence rather than paper over it
// with the machine name.
func (t ToolDefinition) DisplayTitle() string {
if t.Title != "" {
return t.Title
}
if t.Annotations != nil {
return t.Annotations.Title
}
return ""
}
// ToolsListResult is the result of tools/list.
type ToolsListResult struct {
Tools []ToolDefinition `json:"tools"`
}
// Initialize performs the MCP handshake with a server.
func (c *Client) Initialize(serverPath string) error {
params := map[string]interface{}{
"protocolVersion": "2025-03-26",
"capabilities": map[string]interface{}{},
"clientInfo": map[string]string{
"name": "pipeboard-cli",
"version": c.version,
},
}
_, err := c.call(serverPath, "initialize", params)
if err != nil {
return fmt.Errorf("initialize: %w", err)
}
// Send initialized notification (no response expected)
_ = c.notify(serverPath, "notifications/initialized", nil)
return nil
}
// ListTools fetches the list of tools from an MCP server.
func (c *Client) ListTools(serverPath string) (*ToolsListResult, error) {
raw, err := c.call(serverPath, "tools/list", nil)
if err != nil {
return nil, fmt.Errorf("tools/list: %w", err)
}
var result ToolsListResult
if err := json.Unmarshal(raw, &result); err != nil {
return nil, fmt.Errorf("parsing tools/list result: %w", err)
}
return &result, nil
}
// CallTool invokes a tool on an MCP server and returns the raw result.
func (c *Client) CallTool(serverPath, toolName string, arguments map[string]interface{}) (json.RawMessage, error) {
params := map[string]interface{}{
"name": toolName,
"arguments": arguments,
}
raw, err := c.call(serverPath, "tools/call", params)
if err != nil {
return nil, fmt.Errorf("tools/call %s: %w", toolName, err)
}
return raw, nil
}
func (c *Client) call(serverPath, method string, params interface{}) (json.RawMessage, error) {
id := c.reqID.Add(1)
req := jsonRPCRequest{
JSONRPC: "2.0",
ID: id,
Method: method,
Params: params,
}
body, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("marshaling request: %w", err)
}
url := fmt.Sprintf("%s/%s", c.baseURL, serverPath)
httpReq, err := http.NewRequest("POST", url, bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("creating request: %w", err)
}
httpReq.Header.Set("Content-Type", "application/json")
setCommonHeaders(httpReq, c.token, c.userAgent)
resp, err := c.httpClient.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("sending request: %w", err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("reading response: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, string(respBody))
}
var rpcResp jsonRPCResponse
if err := json.Unmarshal(respBody, &rpcResp); err != nil {
return nil, fmt.Errorf("parsing response: %w", err)
}
if rpcResp.Error != nil {
return nil, rpcResp.Error
}
return rpcResp.Result, nil
}
// ToolsHashResult is the response from GET /tools-hash.
type ToolsHashResult struct {
Hash string `json:"hash"`
}
// FetchToolsHash retrieves the combined tools hash from the server.
// baseURL is the MCP base URL (e.g. https://mcp.pipeboard.co).
// This endpoint requires no authentication, but it still needs the Vercel
// protection bypass when baseURL is a preview deployment.
func FetchToolsHash(baseURL, version string) (*ToolsHashResult, error) {
req, err := http.NewRequest("GET", baseURL+"/api/tools-hash", nil)
if err != nil {
return nil, fmt.Errorf("creating request: %w", err)
}
setCommonHeaders(req, "", "pipeboard-cli/"+version)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("fetching tools hash: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("tools-hash HTTP %d", resp.StatusCode)
}
var result ToolsHashResult
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("parsing tools-hash: %w", err)
}
return &result, nil
}
func (c *Client) notify(serverPath, method string, params interface{}) error {
// Notifications have no ID and expect no response.
type notification struct {
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params interface{} `json:"params,omitempty"`
}
req := notification{
JSONRPC: "2.0",
Method: method,
Params: params,
}
body, err := json.Marshal(req)
if err != nil {
return err
}
url := fmt.Sprintf("%s/%s", c.baseURL, serverPath)
httpReq, err := http.NewRequest("POST", url, bytes.NewReader(body))
if err != nil {
return err
}
httpReq.Header.Set("Content-Type", "application/json")
setCommonHeaders(httpReq, c.token, c.userAgent)
resp, err := c.httpClient.Do(httpReq)
if err != nil {
return err
}
resp.Body.Close()
return nil
}