Files
sam cf249d7643 feat(connectors): MCP servers as a fourth accepted package, with their declared tools offered to agents (archie #2005)
An investigation needs Prometheus's own metrics, and Prometheus publishes an
MCP server, so the connectors module now runs MCP servers as published instead
of wrapping them: a stdio binary in its own user and network namespace with an
egress allow-list, or an HTTP endpoint dialled through declared egress.

Acceptance names the transport, the tools it permits, and the credential slots
that resolve into the server's environment (stdio) or Authorization header
(HTTP). Each permitted tool is declared as a lookup, or as an action with its
effect; a tool the server lists but the acceptance doesn't name is never
offered, and a declared tool the server doesn't list fails the start rather
than going silently missing. Declared tools reach agents beside connector
lookups through the same declaration and the same call path, with the server's
own input schema, under a name no connector can take: an accepted name runs one
package.

A server that fails to start, exits or stops answering is reported by readiness
and reconnected with backoff. Tool calls carry a deadline, results are capped,
and an error result fails its call with the server's own text rather than the
run.

The investigate prompt now asks for a time-until-full figure from
`predict_linear` through the metrics server instead of an estimate from raw
points.

The old core MCP path stays for now: the legacy gateway and task containers
still read config.Tools.MCPServers, so it goes with them.
2026-11-15 23:43:39 +11:00

50 lines
1.8 KiB
Go

package host
import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"os/exec"
)
// Command returns the command that runs a verified stdio MCP server with args,
// in its own user and network namespace.
//
// An extension is confined only when its package names egress hosts; a stdio
// MCP server always is, because there is no contract behind it to hold it: the
// namespace and the allow-list are what limit it, and an acceptance that names
// no host leaves it able to reach nothing.
//
// env is added to the server's environment as given, for the settings and
// credentials its acceptance resolves. The returned release ends the server's
// egress gate and closes the verified binary.
func (h *Host) Command(ctx context.Context, spec Spec, args, env []string) (*exec.Cmd, func(), error) {
sum, err := hex.DecodeString(spec.SHA256)
if err != nil || len(sum) != sha256.Size {
return nil, nil, fmt.Errorf("mcp server %q: sha256 must be a hex digest", spec.Name)
}
binary, err := openVerified(spec.Path, sum)
if err != nil {
return nil, nil, fmt.Errorf("mcp server %q: %w", spec.Name, err)
}
release := func() { _ = binary.Close() }
g, err := openGate(ctx, spec.Egress)
if err != nil {
release()
return nil, nil, fmt.Errorf("mcp server %q: egress: %w", spec.Name, err)
}
// The server's lifetime is its session's, not the caller's deadline: the
// accept path and the supervisor both connect under a timeout.
lifetime, stop := context.WithCancel(context.WithoutCancel(ctx))
cmd, err := confinedCommand(lifetime, binary, args, g.socket)
if err != nil {
stop()
g.close()
release()
return nil, nil, fmt.Errorf("mcp server %q: %w", spec.Name, err)
}
cmd.Env = append(passEnv(spec.Env), env...)
return cmd, func() { stop(); g.close(); release() }, nil
}