Feature(Go): per-device locks, a global call gate and tool-call audit
Add the three cross-cutting guards the Go MCP endpoint needs before any tool talks to a client. Each tools/call now takes one of MaxConcurrency global slots and, for single-host tools, that host's device lock; both are non-blocking, so contention returns -32003 instead of queueing. Panicking handlers are converted to -32000 rather than dropping the connection. Write one mcp.tool.call audit record per named call, including rejected ones: actor/role/source, tool, device, ok/code, duration, a size-only result summary and a truncated argument digest. Commands keep only their first word plus length, and free-form or credential fields (text, message, body, headers, password, token) are replaced by their size, so secrets do not reach the audit file. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
774c0d2545
commit
1076e7614a
4 files changed
+571
-4
No files matched your search
+51
-4
@@ -8,6 +8,7 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -63,6 +64,12 @@ type Server struct {
|
||||
log *logger.Logger
|
||||
tools *toolSet
|
||||
handlers map[string]toolFunc
|
||||
|
||||
// locks serialises device operations; sem bounds the global in-flight
|
||||
// tools/call count. Both are non-blocking: contention is -32003, not a
|
||||
// queue, so a slow call cannot pile up requests behind it (design §9).
|
||||
locks *deviceLocks
|
||||
sem chan struct{}
|
||||
}
|
||||
|
||||
// New builds the endpoint. Zero-valued knobs fall back to the documented
|
||||
@@ -92,6 +99,8 @@ func New(cfg Config) *Server {
|
||||
log: cfg.Log,
|
||||
tools: buildToolSet(toolsJSON),
|
||||
handlers: make(map[string]toolFunc),
|
||||
locks: newDeviceLocks(),
|
||||
sem: make(chan struct{}, cfg.MaxConcurrency),
|
||||
}
|
||||
s.registerLocalTools()
|
||||
return s
|
||||
@@ -231,10 +240,11 @@ func (s *Server) initializeResult() initializeResult {
|
||||
}
|
||||
}
|
||||
|
||||
// callTool parses tools/call, enforces visibility, and hands the raw
|
||||
// arguments to the handler. Readonly mode refuses hidden write tools with
|
||||
// -32006 rather than pretending they do not exist.
|
||||
func (s *Server) callTool(ctx context.Context, raw json.RawMessage) (any, *rpcError) {
|
||||
// callTool parses tools/call, enforces visibility, takes the concurrency and
|
||||
// device gates, then hands the raw arguments to the handler. Readonly mode
|
||||
// refuses hidden write tools with -32006 rather than pretending they do not
|
||||
// exist. Every call that names a tool is audited, including rejected ones.
|
||||
func (s *Server) callTool(ctx context.Context, raw json.RawMessage) (result any, rerr *rpcError) {
|
||||
if len(raw) == 0 || string(raw) == "null" {
|
||||
return nil, errf(CodeInvalidParams, "tools/call requires params")
|
||||
}
|
||||
@@ -245,6 +255,24 @@ func (s *Server) callTool(ctx context.Context, raw json.RawMessage) (any, *rpcEr
|
||||
if params.Name == "" {
|
||||
return nil, errf(CodeInvalidParams, "tool name is required")
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
deviceID := ""
|
||||
if !localReadTools[params.Name] {
|
||||
deviceID = deviceIDFromArgs(params.Arguments)
|
||||
}
|
||||
defer func() {
|
||||
// Convert a handler panic into -32000 instead of letting net/http drop
|
||||
// the connection, and make sure the audit record says the call failed:
|
||||
// without this the zero-valued rerr would be logged as success.
|
||||
if p := recover(); p != nil {
|
||||
s.log.Error("panic in MCP tool %s: %v\n%s", params.Name, p, debug.Stack())
|
||||
result = nil
|
||||
rerr = errf(CodeInternal, "internal error in tool %s", params.Name)
|
||||
}
|
||||
s.auditCall(params.Name, deviceID, params.Arguments, result, rerr, time.Since(start))
|
||||
}()
|
||||
|
||||
if !s.tools.known(params.Name) {
|
||||
return nil, errf(CodeMethodNotFound, "unknown tool: %s", params.Name)
|
||||
}
|
||||
@@ -257,6 +285,25 @@ func (s *Server) callTool(ctx context.Context, raw json.RawMessage) (any, *rpcEr
|
||||
// a temporary state between milestones, not a client mistake.
|
||||
return nil, errf(CodeInternal, "tool %s is not implemented by this server build", params.Name)
|
||||
}
|
||||
|
||||
// Global slot first: a call that would be rejected for concurrency must
|
||||
// not squat on a device lock while it fails.
|
||||
select {
|
||||
case s.sem <- struct{}{}:
|
||||
defer func() { <-s.sem }()
|
||||
default:
|
||||
return nil, errf(CodeBusy, "Server busy: too many concurrent MCP calls (limit %d)",
|
||||
s.cfg.MaxConcurrency)
|
||||
}
|
||||
|
||||
if deviceID != "" {
|
||||
held, acquired := s.locks.tryAcquire(deviceID, params.Name)
|
||||
if !acquired {
|
||||
return nil, errf(CodeBusy, "%s", busyMessage(held))
|
||||
}
|
||||
defer s.locks.release(deviceID)
|
||||
}
|
||||
|
||||
return handler(ctx, params.Arguments)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user