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
+32
-4
@@ -36,14 +36,23 @@ type Server struct {
|
||||
allowedOrigins []string // for WS Origin allowlist; empty = same-origin only
|
||||
loginIPLimit *wsauth.RateLimiter
|
||||
loginUserLimit *wsauth.RateLimiter
|
||||
trustForwardedFor bool // honor X-Forwarded-For (behind trusted proxy only)
|
||||
basePath string // web UI mount prefix (e.g. "/remoter"); "" = domain root
|
||||
brandName string // white-label title; "" = "SimpleRemoter" (official branding)
|
||||
sso SSODetector // nil = SSO disabled
|
||||
trustForwardedFor bool // honor X-Forwarded-For (behind trusted proxy only)
|
||||
basePath string // web UI mount prefix (e.g. "/remoter"); "" = domain root
|
||||
brandName string // white-label title; "" = "SimpleRemoter" (official branding)
|
||||
sso SSODetector // nil = SSO disabled
|
||||
mcp mcpEndpoint // nil = no /mcp route
|
||||
sweepDone chan struct{} // closes to stop the login-limiter sweeper
|
||||
sweepStopOnce sync.Once
|
||||
}
|
||||
|
||||
// mcpEndpoint is the part of *mcp.Server the web server mounts. Expressed as
|
||||
// an interface so web does not depend on the mcp package (the endpoint owns
|
||||
// its own auth and protocol; web only routes to it).
|
||||
type mcpEndpoint interface {
|
||||
Handle(http.ResponseWriter, *http.Request)
|
||||
Enabled() bool
|
||||
}
|
||||
|
||||
// Config tunes the server's exposed-on-public-HTTPS hardening knobs.
|
||||
// All fields are optional; zero values pick reasonable defaults.
|
||||
type Config struct {
|
||||
@@ -109,6 +118,15 @@ func (s *Server) WithConfig(cfg Config) *Server {
|
||||
return s
|
||||
}
|
||||
|
||||
// WithMCP attaches the MCP endpoint under /mcp (basePath-aware via mount).
|
||||
// When the endpoint reports itself disabled (no bearer token) the route is not
|
||||
// registered and /mcp 404s, matching "no token = feature off". Safe to call
|
||||
// before Start.
|
||||
func (s *Server) WithMCP(m mcpEndpoint) *Server {
|
||||
s.mcp = m
|
||||
return s
|
||||
}
|
||||
|
||||
// Start launches the server in a goroutine and returns immediately.
|
||||
// If port is 0, returns nil without starting anything.
|
||||
func (s *Server) Start() error {
|
||||
@@ -129,6 +147,16 @@ func (s *Server) Start() error {
|
||||
mux.HandleFunc("/api/devices", s.requireBearer(s.handleDevices))
|
||||
mux.HandleFunc("/sso", s.handleSSO)
|
||||
mux.HandleFunc("/ws", s.ws.serve)
|
||||
// MCP is its own protocol with its own static bearer token; it does not go
|
||||
// through requireBearer (that validates browser sessions). Mounted inside
|
||||
// the mux so basePath stripping applies like every other route.
|
||||
if s.mcp != nil {
|
||||
if s.mcp.Enabled() {
|
||||
mux.HandleFunc("/mcp", s.mcp.Handle)
|
||||
} else {
|
||||
s.log.Info("MCP endpoint disabled: YAMA_MCP_TOKEN not set")
|
||||
}
|
||||
}
|
||||
mux.HandleFunc("/static/xterm.js", staticHandler(xtermJS, "application/javascript; charset=utf-8"))
|
||||
mux.HandleFunc("/static/xterm.css", staticHandler(xtermCSS, "text/css; charset=utf-8"))
|
||||
mux.HandleFunc("/static/xterm-fit.js", staticHandler(xtermFitJS, "application/javascript; charset=utf-8"))
|
||||
|
||||
Reference in new issue
Block a user