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,48 @@
|
||||
package mcp
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
// request is one JSON-RPC 2.0 call. The MCP endpoint is stateless, so there is
|
||||
// nothing else to track between requests.
|
||||
type request struct {
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
ID json.RawMessage `json:"id"`
|
||||
Method string `json:"method"`
|
||||
Params json.RawMessage `json:"params"`
|
||||
}
|
||||
|
||||
// isNotification reports whether the caller omitted "id". Such requests get
|
||||
// no JSON-RPC response at all (MCP uses them for notifications/initialized).
|
||||
func (r *request) isNotification() bool { return len(r.ID) == 0 }
|
||||
|
||||
// response is a single JSON-RPC 2.0 reply. Exactly one of Result/Error is set.
|
||||
type response struct {
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
ID json.RawMessage `json:"id"`
|
||||
Result any `json:"result,omitempty"`
|
||||
Error *rpcError `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// idOrNull keeps the reply valid when the request id is missing or malformed:
|
||||
// JSON-RPC allows "id": null on errors that cannot be correlated.
|
||||
func idOrNull(id json.RawMessage) json.RawMessage {
|
||||
if len(id) == 0 {
|
||||
return json.RawMessage("null")
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func resultResponse(id json.RawMessage, result any) *response {
|
||||
return &response{JSONRPC: "2.0", ID: idOrNull(id), Result: result}
|
||||
}
|
||||
|
||||
func errorResponse(id json.RawMessage, err *rpcError) *response {
|
||||
return &response{JSONRPC: "2.0", ID: idOrNull(id), Error: err}
|
||||
}
|
||||
|
||||
// toolCallParams is the tools/call payload. Arguments stay raw so each tool
|
||||
// decodes only the fields it understands (and reports -32602 itself).
|
||||
type toolCallParams struct {
|
||||
Name string `json:"name"`
|
||||
Arguments json.RawMessage `json:"arguments"`
|
||||
}
|
||||
Reference in new issue
Block a user