Phase 11, part 4: agents' limits, recent calls and a pause switch
Each agent has calls a minute and things it may ask to send out a day, checked on every tool call and every request to send; every call is recorded (tool, outcome, time) and the agent's page lists the last 50 and what it's waiting on. Pause refuses all its calls at once. Also fixes each agent's MCP server being shared with others that had the same scopes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -32,8 +32,21 @@ type Caller struct {
|
||||
// Can says whether the agent may read (act false) or change things in
|
||||
// a module or scope (market, crm, …).
|
||||
Can func(scope string, act bool) bool
|
||||
// Before is asked before each tool call (a rate limit); an error
|
||||
// refuses it. After hears how it went. Either may be nil.
|
||||
Before func(ctx context.Context, tool string) error
|
||||
After func(tool string, res *mcp.CallToolResult, err error, took time.Duration)
|
||||
}
|
||||
|
||||
// Refusal is an Authenticator's error that says why, with an HTTP status
|
||||
// (a paused agent: 403), rather than a plain 401.
|
||||
type Refusal struct {
|
||||
Status int
|
||||
Reason string
|
||||
}
|
||||
|
||||
func (r *Refusal) Error() string { return r.Reason }
|
||||
|
||||
// Authenticator finds the caller of a request from its bearer key; an
|
||||
// error refuses it.
|
||||
type Authenticator func(r *http.Request, key string) (*Caller, error)
|
||||
@@ -46,10 +59,15 @@ type Authenticator func(r *http.Request, key string) (*Caller, error)
|
||||
// them, as they call what its pages call); nil for none.
|
||||
func Handler(d *db.DB, version string, auth Authenticator, more func(*mcp.Server, *Caller)) http.Handler {
|
||||
var mu sync.Mutex
|
||||
servers := map[string]*mcp.Server{} // by the scopes' signature: a few at most
|
||||
// One server per agent and its scopes: tools know who calls them, and a
|
||||
// change of scopes makes a new one.
|
||||
servers := map[string]*mcp.Server{}
|
||||
h := mcp.NewStreamableHTTPHandler(func(r *http.Request) *mcp.Server {
|
||||
c, _ := r.Context().Value(callerKey{}).(*Caller)
|
||||
sig := signature(c)
|
||||
if c != nil {
|
||||
sig = fmt.Sprintf("%d/%s", c.ID, sig)
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if srv, ok := servers[sig]; ok {
|
||||
@@ -60,6 +78,9 @@ func Handler(d *db.DB, version string, auth Authenticator, more func(*mcp.Server
|
||||
if more != nil {
|
||||
more(srv, c)
|
||||
}
|
||||
if c != nil {
|
||||
srv.AddReceivingMiddleware(watch(c))
|
||||
}
|
||||
servers[sig] = srv
|
||||
return srv
|
||||
}, &mcp.StreamableHTTPOptions{Stateless: true, JSONResponse: true})
|
||||
@@ -71,6 +92,11 @@ func Handler(d *db.DB, version string, auth Authenticator, more func(*mcp.Server
|
||||
if ok && key != "" {
|
||||
c, err = auth(r, strings.TrimSpace(key))
|
||||
}
|
||||
var refusal *Refusal
|
||||
if errors.As(err, &refusal) {
|
||||
http.Error(w, refusal.Reason, refusal.Status)
|
||||
return
|
||||
}
|
||||
if !ok || key == "" || err != nil || c == nil {
|
||||
w.Header().Set("WWW-Authenticate", `Bearer realm="supermarket"`)
|
||||
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||
@@ -82,6 +108,30 @@ func Handler(d *db.DB, version string, auth Authenticator, more func(*mcp.Server
|
||||
|
||||
type callerKey struct{}
|
||||
|
||||
// watch asks the caller's Before and tells its After about each tool call.
|
||||
func watch(c *Caller) mcp.Middleware {
|
||||
return func(next mcp.MethodHandler) mcp.MethodHandler {
|
||||
return func(ctx context.Context, method string, req mcp.Request) (mcp.Result, error) {
|
||||
p, ok := req.GetParams().(*mcp.CallToolParamsRaw)
|
||||
if method != "tools/call" || !ok {
|
||||
return next(ctx, method, req)
|
||||
}
|
||||
if c.Before != nil {
|
||||
if err := c.Before(ctx, p.Name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
start := time.Now()
|
||||
res, err := next(ctx, method, req)
|
||||
if c.After != nil {
|
||||
out, _ := res.(*mcp.CallToolResult)
|
||||
c.After(p.Name, out, err, time.Since(start))
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// scopes are what tools are offered by: each, read or act.
|
||||
var scopes = []string{"content", "prospects", "seo", "sites", "metrics", "market", "crm"}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user