Files
archie/modules/connectors/connectors.go
T
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

95 lines
2.9 KiB
Go

// Package connectors hosts connector extensions: one confined subprocess per
// accepted connector, speaking the connector contract (proto/connector/v1).
// Other modules reach a connector only through the Connector this package
// returns, which validates every payload against the connector's declared
// schemas and checks that only the judged action runs.
package connectors
import (
"context"
"fmt"
"log/slog"
goplugin "github.com/hashicorp/go-plugin"
"google.golang.org/grpc"
pb "github.com/samcharles93/archie-core/internal/contracts/connector/v1"
"github.com/samcharles93/archie-core/modules/connectors/internal/host"
)
// surface names the connector contract in the plugin handshake.
const surface = "connector"
// contractVersion is the connector contract this host speaks.
const contractVersion = "v1"
// Spec is one accepted connector to run.
type Spec struct {
Name string
// Path is the connector binary; SHA256 is the digest it must match.
Path string
SHA256 string
// Env names host environment variables the connector may see.
Env []string
// Egress is the accepted host list. A connector that touches targets
// declares it, and runs confined to those hosts.
Egress []string
Settings map[string]string
Secrets map[string]string
}
// Host owns the running connectors.
type Host struct {
procs *host.Host
log *slog.Logger
}
// NewHost returns a host that logs plugin lifecycle to log.
func NewHost(log *slog.Logger) *Host {
return &Host{procs: host.NewHost(log), log: log}
}
// Start launches spec, configures it and reads its declaration. A connector
// already running under the same name is replaced.
func (h *Host) Start(ctx context.Context, spec Spec) (*Connector, error) {
raw, err := h.procs.Start(ctx, host.Spec{
Name: spec.Name, Path: spec.Path, SHA256: spec.SHA256, Env: spec.Env, Egress: spec.Egress,
}, surface, &plugin{})
if err != nil {
return nil, err
}
client, ok := raw.(pb.ConnectorServiceClient)
if !ok {
h.procs.Stop(spec.Name)
return nil, fmt.Errorf("connector %q: plugin is not a connector", spec.Name)
}
c, err := open(ctx, spec.Name, client, spec.Settings, spec.Secrets)
if err != nil {
h.procs.Stop(spec.Name)
return nil, err
}
return c, nil
}
// Stop ends the named connector.
func (h *Host) Stop(name string) { h.procs.Stop(name) }
// Alive reports whether the named connector's process is running.
func (h *Host) Alive(name string) bool { return h.procs.Alive(name) }
// Close stops every connector.
func (h *Host) Close() { h.procs.Close() }
// plugin is the host side of the go-plugin handshake for the contract.
type plugin struct {
goplugin.NetRPCUnsupportedPlugin
}
func (*plugin) GRPCServer(*goplugin.GRPCBroker, *grpc.Server) error {
return fmt.Errorf("connectors are served by extensions, not by the host")
}
func (*plugin) GRPCClient(_ context.Context, _ *goplugin.GRPCBroker, conn *grpc.ClientConn) (any, error) {
return pb.NewConnectorServiceClient(conn), nil
}