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
42 lines
1.7 KiB
Go
42 lines
1.7 KiB
Go
package mcp
|
|
|
|
import "fmt"
|
|
|
|
// JSON-RPC 2.0 error codes used by the MCP endpoint. The -320xx range mirrors
|
|
// the Win32 server (McpServer.cpp:296-312) so a model sees the same numbers
|
|
// regardless of which backend it talks to; reusing them matters more than
|
|
// inventing Go-specific ones.
|
|
const (
|
|
CodeInternal = -32000 // unexpected server-side failure
|
|
CodeTimeout = -32001 // sub-connection or device reply did not arrive
|
|
CodeOffline = -32002 // host unknown or offline
|
|
CodeBusy = -32003 // per-device lock / terminal slot occupied
|
|
CodeSendFailed = -32004 // command could not be delivered to the host
|
|
CodeUnsupported = -32005 // host lacks the required capability
|
|
CodeDenied = -32006 // policy refusal (readonly mode, disabled tool)
|
|
CodeNotFound = -32009 // target file/directory does not exist
|
|
CodePermission = -32010 // target rejected the operation (access denied)
|
|
CodeParseError = -32700 // invalid JSON
|
|
CodeInvalidRequest = -32600 // not a valid JSON-RPC request
|
|
CodeMethodNotFound = -32601 // unknown method or tool name
|
|
CodeInvalidParams = -32602 // schema/argument validation failed
|
|
)
|
|
|
|
// rpcError is the JSON-RPC "error" object. It is returned as a Go error from
|
|
// tool handlers, which lets call sites use plain returns instead of carrying
|
|
// a response object through every layer.
|
|
type rpcError struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data any `json:"data,omitempty"`
|
|
}
|
|
|
|
func (e *rpcError) Error() string {
|
|
return fmt.Sprintf("mcp error %d: %s", e.Code, e.Message)
|
|
}
|
|
|
|
// errf builds an rpcError with a formatted message.
|
|
func errf(code int, format string, args ...any) *rpcError {
|
|
return &rpcError{Code: code, Message: fmt.Sprintf(format, args...)}
|
|
}
|