Files
archie/modules/connectors/mcpauth.go
T
sam f24c2b4455 feat(connectors): separate packages from scoped installations (#2027)
Packages are accepted once by the instance. Installations configure them at the instance, an organisation or a person; bindings and credentials resolve the person's, then their organisation's, then the instance's. Decisions, approval requests and held chat actions record the installation and scope that acted.

The dashboard lists packages separately from installations, shows every installation's scope, and creates, edits and disables installations at their scope.
2026-11-24 03:05:27 +11:00

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.Installed, 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.credentialFor(ctx, scopeOf(a.Owner()), 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.Contract, a.Owner(), 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
}