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

178 lines
7.4 KiB
Go

package connectors
import (
"context"
"encoding/json/jsontext"
"errors"
"log/slog"
"strings"
"testing"
sdk "github.com/modelcontextprotocol/go-sdk/mcp"
)
// fakeTools is a server offering one lookup, one action and one tool that
// answers with an error, the last of them only when ask names it.
func fakeTools(t *testing.T, list ...string) *sdk.Server {
t.Helper()
srv := sdk.NewServer(&sdk.Implementation{Name: "fake", Version: "1.0.0"}, nil)
for _, name := range list {
schema := map[string]any{
"type": "object",
"properties": map[string]any{"query": map[string]any{"type": "string"}},
}
srv.AddTool(&sdk.Tool{Name: name, Description: name + " looks something up", InputSchema: schema},
func(_ context.Context, req *sdk.CallToolRequest) (*sdk.CallToolResult, error) {
text := `{"status":"success","tool":"` + req.Params.Name + `"}`
return &sdk.CallToolResult{Content: []sdk.Content{&sdk.TextContent{Text: text}}}, nil
})
}
return srv
}
// inMemory wires a Server to srv over an in-memory transport, so the client
// side is exercised without a process.
func inMemory(t *testing.T, spec ServerSpec, srv *sdk.Server) *Server {
t.Helper()
s := &Server{name: spec.Name, spec: spec, log: slog.New(slog.DiscardHandler), tools: map[string]mcpTool{}}
s.dial = func(ctx context.Context) (*sdk.ClientSession, func(), error) {
serverTransport, clientTransport := sdk.NewInMemoryTransports()
if _, err := srv.Connect(ctx, serverTransport, nil); err != nil {
return nil, nil, err
}
client := sdk.NewClient(&sdk.Implementation{Name: "archie-core", Version: "1.0.0"}, nil)
cs, err := client.Connect(ctx, clientTransport, nil)
if err != nil {
return nil, nil, err
}
return cs, func() { _ = cs.Close() }, nil
}
return s
}
// TestOnlyDeclaredToolsAreOffered guards 2.5's allow-list: a tool the server
// lists but the acceptance doesn't name is never offered to an agent and
// can't be called by name, while a permitted tool keeps the server's own
// description and schema. A declared tool the server doesn't list fails the
// start, so an operator sees it rather than a silently missing tool.
func TestOnlyDeclaredToolsAreOffered(t *testing.T) {
spec := ServerSpec{Name: "metrics", Tools: []ToolDecl{{Name: "query", Kind: KindLookup}}}
s := inMemory(t, spec, fakeTools(t, "query", "reload"))
if err := s.Connect(t.Context()); err != nil {
t.Fatalf("connect: %v", err)
}
t.Cleanup(s.Close)
decls := s.Declarations()
if len(decls) != 1 || decls[0].Name != "query" {
t.Fatalf("declarations = %+v, want only query", decls)
}
if !decls[0].ReadOnly || decls[0].Description != "query looks something up" {
t.Fatalf("declaration = %+v, want the server's read-only description", decls[0])
}
if !strings.Contains(string(decls[0].Schema), "query") {
t.Fatalf("schema = %s, want the server's own", decls[0].Schema)
}
if _, _, err := s.Lookup(t.Context(), "reload", nil); !errors.Is(err, ErrUnknownName) {
t.Fatalf("calling the undeclared tool: %v, want %v", err, ErrUnknownName)
}
}
// TestUndeclaredListedToolFailsTheStart covers the other direction: the server
// doesn't list a tool the acceptance declares.
func TestUndeclaredListedToolFailsTheStart(t *testing.T) {
spec := ServerSpec{Name: "metrics", Tools: []ToolDecl{{Name: "query", Kind: KindLookup}, {Name: "series", Kind: KindLookup}}}
s := inMemory(t, spec, fakeTools(t, "query"))
err := s.Connect(t.Context())
if err == nil || !strings.Contains(err.Error(), "series") {
t.Fatalf("connect: %v, want an error naming the missing tool", err)
}
if rerr := s.Ready(); rerr == nil {
t.Fatal("readiness is clean after a failed start")
}
}
// TestErrorResultFailsOnlyItsCall guards the contract an agent depends on: the
// server's own error text comes back verbatim, marked as an ordinary failure
// rather than an unknown outcome, and input that breaks the tool's schema never
// reaches the server.
func TestErrorResultFailsOnlyItsCall(t *testing.T) {
var called int
srv := sdk.NewServer(&sdk.Implementation{Name: "fake", Version: "1.0.0"}, nil)
schema := map[string]any{"type": "object", "required": []string{"query"}, "properties": map[string]any{"query": map[string]any{"type": "string"}}}
srv.AddTool(&sdk.Tool{Name: "query", Description: "runs promql", InputSchema: schema},
func(context.Context, *sdk.CallToolRequest) (*sdk.CallToolResult, error) {
called++
return &sdk.CallToolResult{
IsError: true,
Content: []sdk.Content{&sdk.TextContent{Text: "parse error: unexpected end of input"}},
}, nil
})
s := inMemory(t, ServerSpec{Name: "metrics", Tools: []ToolDecl{{Name: "query", Kind: KindLookup}}}, srv)
if err := s.Connect(t.Context()); err != nil {
t.Fatalf("connect: %v", err)
}
t.Cleanup(s.Close)
out, found, err := s.Lookup(t.Context(), "query", jsontext.Value(`{"query":"rate("}`))
if err == nil || found || out != "" {
t.Fatalf("lookup = %q, %v, %v; want the server's error", out, found, err)
}
if !strings.Contains(err.Error(), "parse error: unexpected end of input") {
t.Fatalf("error = %v, want the server's text verbatim", err)
}
if errors.Is(err, ErrOutcomeUnknown) {
t.Fatalf("error = %v, want a failure that ends the call and not the run", err)
}
// A call that breaks the schema is refused before it reaches the server.
if _, _, err := s.Lookup(t.Context(), "query", jsontext.Value(`{"query":7}`)); err == nil {
t.Fatal("a payload breaking the tool's schema was accepted")
}
if called != 1 {
t.Fatalf("the server was called %d times, want 1", called)
}
// An empty answer reads as nothing found, not as an error.
empty := sdk.NewServer(&sdk.Implementation{Name: "fake", Version: "1.0.0"}, nil)
empty.AddTool(&sdk.Tool{Name: "query", InputSchema: map[string]any{"type": "object"}},
func(context.Context, *sdk.CallToolRequest) (*sdk.CallToolResult, error) {
return &sdk.CallToolResult{}, nil
})
nothing := inMemory(t, ServerSpec{Name: "quiet", Tools: []ToolDecl{{Name: "query", Kind: KindLookup}}}, empty)
if err := nothing.Connect(t.Context()); err != nil {
t.Fatalf("connect: %v", err)
}
t.Cleanup(nothing.Close)
out, found, err = nothing.Lookup(t.Context(), "query", nil)
if err != nil || found || out != "" {
t.Fatalf("lookup = %q, %v, %v; want found=false and no error", out, found, err)
}
}
// TestResultIsCapped guards the size bound: a result larger than the cap is cut
// at a character boundary and says how much it dropped, so a series explosion
// can't spend the run's context.
func TestResultIsCapped(t *testing.T) {
srv := sdk.NewServer(&sdk.Implementation{Name: "fake", Version: "1.0.0"}, nil)
srv.AddTool(&sdk.Tool{Name: "range_query", InputSchema: map[string]any{"type": "object"}},
func(context.Context, *sdk.CallToolRequest) (*sdk.CallToolResult, error) {
return &sdk.CallToolResult{Content: []sdk.Content{&sdk.TextContent{Text: strings.Repeat("ée", mcpResultLimit)}}}, nil
})
s := inMemory(t, ServerSpec{Name: "metrics", Tools: []ToolDecl{{Name: "range_query", Kind: KindLookup}}}, srv)
if err := s.Connect(t.Context()); err != nil {
t.Fatalf("connect: %v", err)
}
t.Cleanup(s.Close)
out, _, err := s.Lookup(t.Context(), "range_query", nil)
if err != nil {
t.Fatalf("lookup: %v", err)
}
if len(out) > mcpResultLimit+128 {
t.Fatalf("result is %d bytes, want it capped near %d", len(out), mcpResultLimit)
}
if !strings.Contains(out, "truncated") || !strings.Contains(out, "capped at") {
t.Fatalf("result doesn't say it was truncated: %q", out[max(0, len(out)-120):])
}
}