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"` }