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.
118 lines
3.5 KiB
Go
118 lines
3.5 KiB
Go
package connectors
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"maps"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/samcharles93/archie-core/modules/connectors/internal/store"
|
|
)
|
|
|
|
// Credential kinds an MCP server's slot can hold: one HTTP auth scheme each.
|
|
const (
|
|
kindBasic = "basic"
|
|
kindBearer = "bearer"
|
|
)
|
|
|
|
// mcpEnv turns one slot's fields into the environment a stdio server is
|
|
// launched with, named <SLOT>_<FIELD> in upper case.
|
|
func mcpEnv(slot, kind string, fields map[string]string) ([]string, error) {
|
|
var names []string
|
|
switch kind {
|
|
case kindBearer:
|
|
names = []string{"token"}
|
|
case kindBasic:
|
|
names = []string{"username", "password"}
|
|
default:
|
|
return nil, fmt.Errorf("a credential slot for an MCP server needs a basic or bearer credential, not %s", kind)
|
|
}
|
|
out := make([]string, 0, len(names))
|
|
for _, name := range names {
|
|
value, ok := fields[name]
|
|
if !ok {
|
|
return nil, fmt.Errorf("a %s credential has no %s", kind, name)
|
|
}
|
|
out = append(out, envName(slot, name)+"="+value)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// mcpAuthorization is the header an HTTP server is called with, from the same
|
|
// fields.
|
|
func mcpAuthorization(kind string, fields map[string]string) (string, error) {
|
|
switch kind {
|
|
case kindBearer:
|
|
token, ok := fields["token"]
|
|
if !ok {
|
|
return "", fmt.Errorf("a %s credential has no token", kind)
|
|
}
|
|
return "Bearer " + token, nil
|
|
case kindBasic:
|
|
username, ok := fields["username"]
|
|
if !ok {
|
|
return "", fmt.Errorf("a %s credential has no username", kind)
|
|
}
|
|
return "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+fields["password"])), nil
|
|
}
|
|
return "", fmt.Errorf("a credential slot for an MCP server needs a basic or bearer credential, not %s", kind)
|
|
}
|
|
|
|
// envName is the environment variable one slot's field is handed to a stdio
|
|
// server in.
|
|
func envName(slot, field string) string {
|
|
name := strings.NewReplacer("-", "_", ".", "_").Replace(slot)
|
|
return strings.ToUpper(name + "_" + field)
|
|
}
|
|
|
|
// mcpCredentials resolves an MCP server's credential slots into the environment
|
|
// a stdio server is launched with, or the headers an HTTP one is called with.
|
|
// Only the slots this acceptance names are read, so one server's credentials
|
|
// never reach another.
|
|
func (m *Module) mcpCredentials(ctx context.Context, a store.Accepted, httpTransport bool) ([]string, map[string]string, error) {
|
|
slots, err := decodeSlots(a.Credentials)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
var env []string
|
|
headers := map[string]string{}
|
|
for _, slot := range slices.Sorted(maps.Keys(slots)) {
|
|
name := slots[slot]
|
|
c, ok, err := m.store.Credential(ctx, m.orgID, name)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
if !ok {
|
|
return nil, nil, fmt.Errorf("credential %q for slot %q doesn't exist", name, slot)
|
|
}
|
|
fields, err := m.openCredential(c)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
values := make(map[string]string, len(fields))
|
|
for field, f := range fields {
|
|
value, err := m.resolveField(ctx, a, f)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("credential %q field %s: %w", name, field, err)
|
|
}
|
|
values[field] = value
|
|
}
|
|
if httpTransport {
|
|
auth, err := mcpAuthorization(c.Kind, values)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("credential %q for slot %q: %w", name, slot, err)
|
|
}
|
|
headers["Authorization"] = auth
|
|
continue
|
|
}
|
|
lines, err := mcpEnv(slot, c.Kind, values)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("credential %q for slot %q: %w", name, slot, err)
|
|
}
|
|
env = append(env, lines...)
|
|
}
|
|
return env, headers, nil
|
|
}
|