Feature(Go): MCP endpoint with bearer auth and tools/list
Add a stateless POST /mcp endpoint to the Go server, mounted by the web server and built from an environment-driven Config in cmd. The endpoint gates on YAMA_MCP_TOKEN: without it the route is not registered and /mcp 404s. Requests use a constant-time bearer compare, and transport failures stay transport failures (401 with a WWW-Authenticate challenge, 405 for anything but POST, 413 above 8 MiB, 204 for notifications). Everything after a parsed JSON-RPC request is HTTP 200, including -32600 for batch arrays and -32601 for unknown methods or tools. tools/list returns the nine minimal tools verbatim from an embedded copy of the canonical schemas. tools/mcp_tools/gen_go_tools.py extracts them from mcp/tools.json, preserving order and only the four published fields, and its --check mode detects drift. Readonly mode hides and refuses terminal_exec/terminal_close (-32006); tools that are listed but not implemented yet answer -32000 until their milestone lands. Also sync design §3.2 and the M2/M7/M8 plan text to tools.json, which is the sole schema contract: files not entries, line-based read_file, no since, camelCase capabilities, max_width [64,1920] defaulting to 1024, and outputSchema included in the M8 subset comparison. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
5f124fcb3a
commit
3f1957a9c9
11 files changed
+2022
-60
No files matched your search
@@ -0,0 +1,80 @@
|
||||
package mcp
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
// tools_minimal.json is generated from mcp/tools.json by
|
||||
// tools/mcp_tools/gen_go_tools.py — never edit it by hand. It is embedded so
|
||||
// tools/list is a compile-time constant: no disk I/O, no drift between the
|
||||
// schema the model sees and the schema the handlers were written against.
|
||||
//
|
||||
//go:embed tools_minimal.json
|
||||
var toolsJSON []byte
|
||||
|
||||
// writeTools are hidden from tools/list in readonly mode and rejected with
|
||||
// -32006 if called anyway (design §3.1: readonly exposes 7 of 9 tools).
|
||||
var writeTools = map[string]bool{
|
||||
"terminal_exec": true,
|
||||
"terminal_close": true,
|
||||
}
|
||||
|
||||
// toolDef holds one published definition plus its name, so filtering does not
|
||||
// re-parse the JSON on every tools/list call.
|
||||
type toolDef struct {
|
||||
name string
|
||||
raw json.RawMessage
|
||||
}
|
||||
|
||||
type toolSet struct {
|
||||
defs []toolDef
|
||||
names map[string]bool
|
||||
}
|
||||
|
||||
// buildToolSet validates the embedded payload once at startup. A malformed
|
||||
// file is a build defect, so panic loudly rather than serve a broken list.
|
||||
func buildToolSet(raw []byte) *toolSet {
|
||||
var raws []json.RawMessage
|
||||
if err := json.Unmarshal(raw, &raws); err != nil {
|
||||
panic("mcp: embedded tools_minimal.json is not a JSON array: " + err.Error())
|
||||
}
|
||||
ts := &toolSet{defs: make([]toolDef, 0, len(raws)), names: make(map[string]bool, len(raws))}
|
||||
for _, r := range raws {
|
||||
var head struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
if err := json.Unmarshal(r, &head); err != nil || head.Name == "" {
|
||||
panic("mcp: embedded tool definition without a name")
|
||||
}
|
||||
if ts.names[head.Name] {
|
||||
panic("mcp: duplicate embedded tool " + head.Name)
|
||||
}
|
||||
ts.names[head.Name] = true
|
||||
ts.defs = append(ts.defs, toolDef{name: head.Name, raw: r})
|
||||
}
|
||||
return ts
|
||||
}
|
||||
|
||||
// visible returns the tool definitions to publish, preserving tools.json
|
||||
// order. The raw bytes are passed through untouched, so descriptions and
|
||||
// schemas stay exactly what mcp/tools.json says (no annotations, like MFC).
|
||||
func (ts *toolSet) visible(readonly bool) []json.RawMessage {
|
||||
out := make([]json.RawMessage, 0, len(ts.defs))
|
||||
for _, def := range ts.defs {
|
||||
if readonly && writeTools[def.name] {
|
||||
continue
|
||||
}
|
||||
out = append(out, def.raw)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// known reports whether name is a tool this server defines, including tools
|
||||
// hidden by readonly mode. Unknown names are -32601; hidden ones are -32006.
|
||||
func (ts *toolSet) known(name string) bool { return ts.names[name] }
|
||||
|
||||
// toolsListResult is the MCP tools/list payload.
|
||||
type toolsListResult struct {
|
||||
Tools []json.RawMessage `json:"tools"`
|
||||
}
|
||||
Reference in new issue
Block a user