-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcpserver.go
More file actions
377 lines (347 loc) · 11.8 KB
/
Copy pathmcpserver.go
File metadata and controls
377 lines (347 loc) · 11.8 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// SPDX-License-Identifier: MIT
// Purpose: minimal JSON-RPC 2.0 stdio MCP server for vane. Exposes two
// tools (vane_research / vane_health) so the SIN-Code agent can query a
// self-hosted Vane instance at runtime over the standard MCP wire
// protocol. Stdlib-only: no third-party deps. Graceful Degradation: any
// tool error is returned with isError=true and a "fallback to
// websearch" hint — the process NEVER panics or returns a fatal JSON-RPC
// error to the caller (M7: race-clean, M2: stdlib only).
// Docs: vane.doc.md
package vane
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"strings"
"sync"
"time"
)
// Server is the stdio MCP server. Construct with NewServer, then call
// Serve(ctx) which blocks until ctx is cancelled or stdin reaches EOF.
type Server struct {
stdin io.Reader
stdout io.Writer
stderr io.Writer
// mu protects the cfgDir + the constructor's "have I built a Client
// yet" flag for the lazy initialization path. All shared-state reads
// are read-only-after-construction in production, but tests swap the
// underlying reader, so the mutex stays for safety.
mu sync.Mutex
cfgDir string
clientOnce sync.Once
client *Client
clientErr error
}
// NewServer returns a Server bound to the global stdin/stdout. The
// optional cfgDir overrides Home() resolution; pass "" for the
// production default.
func NewServer(cfgDir string) *Server {
if cfgDir == "" {
cfgDir = Home()
}
return &Server{
stdin: os.Stdin,
stdout: os.Stdout,
stderr: os.Stderr,
cfgDir: cfgDir,
}
}
// NewServerWithIO is a test-friendly constructor: pass arbitrary
// readers/writers and the resulting Server won't touch the real stdio.
// Mirrors superpowers.NewServerWithIO.
func NewServerWithIO(in io.Reader, out, err io.Writer, cfgDir string) *Server {
return &Server{stdin: in, stdout: out, stderr: err, cfgDir: cfgDir}
}
// lazyClient returns the long-lived Vane Client, building it on first
// use. Build errors are sticky — repeated calls return the original
// error so we don't re-read the config on every request.
func (s *Server) lazyClient() (*Client, error) {
s.clientOnce.Do(func() {
// SIN_CODE_HOME controls config resolution. Set the env to the
// cfgDir for the duration of LoadConfig so a server launched
// with `sin-code vane serve --home /tmp/x` honors that path.
prev := os.Getenv("SIN_CODE_HOME")
if s.cfgDir != "" {
_ = os.Setenv("SIN_CODE_HOME", s.cfgDir)
}
cfg, _, err := LoadConfig()
if prev == "" {
_ = os.Unsetenv("SIN_CODE_HOME")
} else {
_ = os.Setenv("SIN_CODE_HOME", prev)
}
if err != nil {
s.clientErr = err
return
}
s.client = NewClient(cfg)
})
return s.client, s.clientErr
}
// Serve runs the JSON-RPC 2.0 read loop over line-delimited JSON. One
// request per line, one response per line. We use bufio.Scanner (not
// json.Decoder) so a malformed line does not eat subsequent valid
// requests — the scanner can re-sync at the next newline.
func (s *Server) Serve(ctx context.Context) error {
scanner := bufio.NewScanner(s.stdin)
// Allow large JSON-RPC payloads (queries can carry long context).
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
enc := json.NewEncoder(s.stdout)
for scanner.Scan() {
// Honor context cancellation between messages.
if err := ctx.Err(); err != nil {
return err
}
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
var req jsonRPCRequest
if err := json.Unmarshal([]byte(line), &req); err != nil {
// A malformed line is not fatal — we just emit a parse-error
// reply (best-effort) and keep reading. This matches the
// spirit of MCP's "lenient" stdio transport.
fmt.Fprintf(s.stderr, "vane-mcp: decode error: %v\n", err)
continue
}
resp := s.dispatch(ctx, &req)
if resp == nil {
continue // notification — no reply required
}
if err := enc.Encode(resp); err != nil {
return err
}
}
if err := scanner.Err(); err != nil && err != io.EOF {
return err
}
return nil
}
// dispatch handles one request. Returns nil for notifications (no
// reply required by JSON-RPC 2.0).
func (s *Server) dispatch(ctx context.Context, req *jsonRPCRequest) *jsonRPCResponse {
if req.ID == nil {
return nil
}
switch req.Method {
case "initialize":
return s.result(req, map[string]any{
"protocolVersion": "2025-06-18",
"serverInfo": map[string]string{
"name": "sin-code-vane",
"version": "v3.8.0",
},
"capabilities": map[string]any{"tools": map[string]string{}},
})
case "notifications/initialized":
// Client is acknowledging initialize. No reply.
return nil
case "tools/list":
return s.result(req, map[string]any{"tools": s.toolList()})
case "tools/call":
var p toolCallParams
_ = json.Unmarshal(req.Params, &p)
return s.callTool(ctx, req, &p)
case "ping":
return s.result(req, map[string]string{"pong": "ok"})
default:
return &jsonRPCResponse{
JSONRPC: "2.0",
ID: req.ID,
Error: &jsonRPCError{
Code: -32601,
Message: "method not found: " + req.Method,
},
}
}
}
func (s *Server) result(req *jsonRPCRequest, v any) *jsonRPCResponse {
data, err := jsonMarshalFn(v)
if err != nil {
return &jsonRPCResponse{
JSONRPC: "2.0",
ID: req.ID,
Error: &jsonRPCError{Code: -32603, Message: err.Error()},
}
}
return &jsonRPCResponse{JSONRPC: "2.0", ID: req.ID, Result: data}
}
// callTool dispatches the named tool. ANY error path returns
// isError=true with a fallback hint — we never return a JSON-RPC error
// for tool-level failures (the caller is a friendly LLM, not a
// programmer who wants -32602 codes).
func (s *Server) callTool(ctx context.Context, req *jsonRPCRequest, p *toolCallParams) *jsonRPCResponse {
switch p.Name {
case "vane_research":
return s.callResearch(ctx, req, p)
case "vane_health":
return s.callHealth(ctx, req)
default:
return &jsonRPCResponse{
JSONRPC: "2.0",
ID: req.ID,
Error: &jsonRPCError{
Code: -32602,
Message: "unknown tool: " + p.Name,
},
}
}
}
func (s *Server) callResearch(ctx context.Context, req *jsonRPCRequest, p *toolCallParams) *jsonRPCResponse {
var args struct {
Query string `json:"query"`
FocusMode string `json:"focus_mode"`
Optimization string `json:"optimization"`
}
_ = json.Unmarshal(p.Arguments, &args)
// Per-tool timeout: cap at the client's configured timeout or 30s,
// whichever is smaller, so a stuck Vane instance never blocks the
// agent loop indefinitely.
if _, hasDeadline := ctx.Deadline(); !hasDeadline {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, 30*time.Second)
defer cancel()
}
client, err := s.lazyClient()
if err != nil {
return s.toolError(req, "vane_research: bridge init failed: "+err.Error())
}
ans, err := client.Search(ctx, args.Query, args.FocusMode, args.Optimization)
if err != nil {
// Graceful Degradation contract: surface the failure to the
// model with isError=true and an explicit fallback hint. The
// model can then ask the user to fall back to the websearch
// ecosystem skill rather than us aborting the request.
return s.toolError(req, fmt.Sprintf(
"%s\n\nFallback: use the websearch ecosystem skill instead.",
err.Error(),
))
}
return s.result(req, toolResult{Content: marshalText(FormatAnswer(ans))})
}
func (s *Server) callHealth(ctx context.Context, req *jsonRPCRequest) *jsonRPCResponse {
// Health-check is bounded to 5s so a hung instance does not pin a
// tool slot.
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
client, err := s.lazyClient()
if err != nil {
return s.toolError(req, "vane_health: bridge init failed: "+err.Error())
}
if err := client.Healthy(ctx); err != nil {
return s.toolError(req, "vane_health: "+err.Error())
}
return s.result(req, toolResult{Content: marshalText("vane: healthy")})
}
// toolError returns a successful JSON-RPC envelope whose body carries
// isError=true. The text content is human-readable so an LLM can
// surface it directly to the user.
func (s *Server) toolError(req *jsonRPCRequest, msg string) *jsonRPCResponse {
return s.result(req, toolResult{
Content: []toolContent{{Type: "text", Text: msg}},
IsError: true,
})
}
// toolList returns the schema for both vane tools. Focus modes and
// optimizations are emitted as JSON-Schema enums so strict clients
// validate before they call.
func (s *Server) toolList() []toolSpec {
focusEnums := make([]string, 0, len(FocusModes))
for k := range FocusModes {
focusEnums = append(focusEnums, k)
}
optEnums := make([]string, 0, len(Optimizations))
for k := range Optimizations {
optEnums = append(optEnums, k)
}
return []toolSpec{
{
Name: "vane_research",
Description: "Query a self-hosted Vane AI-answering engine. Returns a markdown answer with a 'Cited Sources' section. Graceful Degradation: on bridge failure returns isError=true with a fallback hint to the websearch ecosystem skill.",
InputSchema: map[string]any{
"type": "object",
"properties": map[string]any{
"query": map[string]any{
"type": "string",
"description": "The research question. Required.",
},
"focus_mode": map[string]any{
"type": "string",
"enum": focusEnums,
"description": "Which source domain Vane should prioritize. Defaults to webSearch.",
},
"optimization": map[string]any{
"type": "string",
"enum": optEnums,
"description": "Quality knob: speed | balanced | quality. Defaults to balanced.",
},
},
"required": []string{"query"},
},
},
{
Name: "vane_health",
Description: "Check whether the configured Vane instance is reachable. Returns isError=true if the bridge is down.",
InputSchema: emptySchema(),
},
}
}
// ── JSON-RPC plumbing ─────────────────────────────────────────────────
type jsonRPCRequest struct {
JSONRPC string `json:"jsonrpc"`
ID *json.RawMessage `json:"id,omitempty"`
Method string `json:"method"`
Params json.RawMessage `json:"params,omitempty"`
}
type jsonRPCResponse struct {
JSONRPC string `json:"jsonrpc"`
ID *json.RawMessage `json:"id,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
Error *jsonRPCError `json:"error,omitempty"`
}
type jsonRPCError struct {
Code int `json:"code"`
Message string `json:"message"`
}
type toolCallParams struct {
Name string `json:"name"`
Arguments json.RawMessage `json:"arguments,omitempty"`
}
type toolSpec struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema map[string]any `json:"inputSchema"`
}
type toolResult struct {
Content []toolContent `json:"content"`
IsError bool `json:"isError,omitempty"`
}
type toolContent struct {
Type string `json:"type"`
Text string `json:"text"`
}
func emptySchema() map[string]any {
return map[string]any{"type": "object", "properties": map[string]any{}}
}
func marshalText(v any) []toolContent {
b, _ := json.MarshalIndent(v, "", " ")
return []toolContent{{Type: "text", Text: string(b)}}
}
// ── Public entry point used by cobra `vane serve` ─────────────────────
// Serve is a convenience wrapper: build a default Server and run Serve
// until stdin closes or ctx is cancelled. Used by the `sin-code vane
// serve` cobra command (owned by a different subagent).
func Serve(ctx context.Context) error {
return NewServer("").Serve(ctx)
}
// formatIntBytes is a tiny helper used by Health replies — declared
// here to keep the import surface small even though it is currently
// only used in tests. Unused-but-exported helpers would clutter the
// public API, so it stays unexported and only used in this file.
func formatIntBytes(n int64) string {
return strconv.FormatInt(n, 10) + "B"
}