Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
310 changes: 301 additions & 9 deletions cmd/root/mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import (
"context"
"errors"
"fmt"
"slices"
"strconv"
"strings"
"sync"
"time"

"github.com/spf13/cobra"

Expand All @@ -15,6 +20,18 @@ import (
"github.com/docker/docker-agent/pkg/telemetry"
)

// attachLatest is the value pflag assigns to --attach when it is given without
// a value: it selects the most recent run.
const attachLatest = "latest"

// attachDiscoveryTimeout bounds how long --attach spends discovering the live
// sessions of every registered run. A TUI suspended with Ctrl+Z (SIGSTOP) keeps
// its registry record and its listener, but the stopped process cannot answer
// HTTP; probing it serially would wait for the whole request timeout before the
// attach proxy could start for a healthy run. Probing the runs concurrently under
// one short deadline keeps such an unrelated stalled run from delaying the attach.
const attachDiscoveryTimeout = 2 * time.Second

type mcpFlags struct {
agentName string
http bool
Expand All @@ -24,10 +41,26 @@ type mcpFlags struct {
authToken string
insecureNoAuth bool
runConfig config.RuntimeConfig

// runRegistry resolves --attach targets. Nil means the process-wide
// runregistry.Default(); tests inject an isolated registry so the CLI
// lookup never reads the user's real run registry.
runRegistry attachRuns

// startAttach runs the attach proxy once the target is resolved. Nil means
// mcp.StartAttachStdio; tests inject a recorder so the advertised command
// can be executed without starting a stdio server.
startAttach func(ctx context.Context, addr, sessionID string) error
}

func newMCPCmd() *cobra.Command {
// mcpCmdOption customises the mcp command's flags before they are bound.
type mcpCmdOption func(*mcpFlags)

func newMCPCmd(opts ...mcpCmdOption) *cobra.Command {
var flags mcpFlags
for _, opt := range opts {
opt(&flags)
}

cmd := &cobra.Command{
Use: "mcp <agent-file>|<registry-ref>",
Expand All @@ -37,16 +70,17 @@ func newMCPCmd() *cobra.Command {
docker-agent serve mcp ./team.yaml
docker-agent serve mcp myorg/agent:tag
docker-agent serve mcp ./agent.yaml --http --listen 127.0.0.1:9090
docker-agent serve mcp --attach`,
docker-agent serve mcp --attach # the most recent TUI run
docker-agent serve mcp --attach=<session-id> # a specific TUI tab, including a spawned one`,
Args: cobra.MaximumNArgs(1),
RunE: flags.runMCPCommand,
}

cmd.PersistentFlags().StringVarP(&flags.agentName, "agent", "a", "", "Name of the agent to run (all agents if not specified)")
cmd.PersistentFlags().BoolVar(&flags.http, "http", false, "Use streaming HTTP transport instead of stdio")
cmd.PersistentFlags().StringVarP(&flags.listenAddr, "listen", "l", "127.0.0.1:8081", "Address to listen on")
cmd.PersistentFlags().StringVar(&flags.attach, "attach", "", "Attach to a running TUI run by pid, address, or session id (or empty for the most recent)")
cmd.PersistentFlags().Lookup("attach").NoOptDefVal = "latest"
cmd.PersistentFlags().StringVar(&flags.attach, "attach", "", "Attach to a running TUI run by pid, address, or session id, including a tab spawned after the run started; pass a target as --attach=<target>, or --attach alone for the most recent run")
cmd.PersistentFlags().Lookup("attach").NoOptDefVal = attachLatest
cmd.PersistentFlags().StringVar(&flags.safety, "safety", "", "Tool safety policy (strict, balanced, restricted, autonomous); only valid with --http")
cmd.PersistentFlags().StringVar(&flags.authToken, "auth-token", "", "Bearer token required for HTTP MCP requests; only valid with --http")
cmd.PersistentFlags().BoolVar(&flags.insecureNoAuth, "insecure-no-auth", false, "Allow unauthenticated non-loopback HTTP binding (insecure); only valid with --http")
Expand All @@ -71,6 +105,13 @@ func (f *mcpFlags) runMCPCommand(cmd *cobra.Command, args []string) (commandErr
if f.runConfig.MCPKeepAlive != 0 {
return errors.New("--mcp-keepalive cannot be used with --attach: the attach proxy ignores runtime configuration")
}
// pflag gives --attach a NoOptDefVal, so it never consumes the next
// argument: "--attach <session-id>" parses as --attach=latest plus one
// positional argument. Honour that spelling by using the positional as
// the target instead of silently attaching to the most recent run.
if f.attach == attachLatest && len(args) == 1 {
f.attach = args[0]
}
return f.runAttach(ctx)
}

Expand Down Expand Up @@ -114,13 +155,264 @@ func (f *mcpFlags) runMCPCommand(cmd *cobra.Command, args []string) (commandErr
}

func (f *mcpFlags) runAttach(ctx context.Context) error {
target := f.attach
if target == "latest" {
addr, sessionID, err := resolveAttachTarget(ctx, f.attachRuns(), f.attach)
if err != nil {
return err
}
if f.startAttach != nil {
return f.startAttach(ctx, addr, sessionID)
}
return mcp.StartAttachStdio(ctx, addr, sessionID)
}

// attachRuns is the subset of the run registry the --attach lookup needs.
// *runregistry.Registry is the production implementation; tests inject an
// in-memory fake so the CLI lookup never reads the user's real registry.
type attachRuns interface {
Find(target string) (runregistry.Record, error)
List() ([]runregistry.Record, error)
}

var _ attachRuns = (*runregistry.Registry)(nil)

// attachRuns returns the registry used to resolve --attach targets.
func (f *mcpFlags) attachRuns() attachRuns {
if f.runRegistry != nil {
return f.runRegistry
}
return runregistry.Default()
}

// attachSession is a live tab resolved on a discovered run.
type attachSession struct {
addr string
sessionID string
}

// resolveAttachTarget maps a --attach value to the control-plane address of the
// live run that serves it and the session ID the MCP proxy should drive.
//
// A pid, an http(s) address, or an empty/"latest" target discovers a run through
// the registry; the session is then resolved inside that run: its recorded
// initial session while that is still attached, the only live session when the
// initial tab was closed, or a clear error listing the candidates.
//
// A session ID selects that live session. A run's registry record only stores
// the session the run started with, so a TUI tab spawned after startup is not on
// any record. The lookup therefore resolves the ID against the attached sessions
// of every live run, which is what makes "serve mcp --attach=<spawned-tab-id>"
// work. An explicitly selected session that is attached nowhere fails clearly
// instead of being silently redirected to another tab.
func resolveAttachTarget(ctx context.Context, runs attachRuns, target string) (addr, sessionID string, err error) {
target = strings.TrimSpace(target)
if target == attachLatest {
target = ""
}
rec, err := runregistry.Default().Find(target)

if !attachTargetIsSessionID(target) {
rec, err := runs.Find(target)
if err != nil {
return "", "", err
}
sessionID, err := mcp.ResolveAttachSession(ctx, rec.Addr, rec.SessionID, false)
if err != nil {
return "", "", err
}
return rec.Addr, sessionID, nil
}

return resolveExplicitAttachSession(ctx, runs, target)
}

// resolveExplicitAttachSession resolves an explicitly named session ID against
// the live tabs of every discovered run.
//
// A run's registry record only names the session the run started with, and that
// ID may contain (or be contained by) another tab's ID, so resolving the record
// first can silently bind the proxy to the wrong tab. Resolve from the live view
// in order instead:
//
// 1. a live exact match always wins, so an initial ID that contains a spawned
// tab's full ID cannot shadow that tab;
// 2. an exactly named recorded initial session whose tab is closed keeps the
// resolver's clear "tab was closed" error, even when a live tab's ID
// extends it;
// 3. a partial ID (the same substring match the run registry uses) selects a
// live tab only when it matches a single live session and every discovered
// run answered; several matches are reported as ambiguous instead of
// picking one of them. When a run could not be queried, a partial match is
// not known to be unique - the unanswered run may host another tab whose ID
// contains the target - so the lookup asks for the full session ID or a
// retry instead of binding the proxy.
func resolveExplicitAttachSession(ctx context.Context, runs attachRuns, target string) (addr, sessionID string, err error) {
records, err := runs.List()
if err != nil {
return err
return "", "", fmt.Errorf("listing live runs: %w", err)
}

// Probe every discovered run concurrently: an unrelated suspended TUI can
// otherwise consume the full HTTP timeout before a healthy run is queried.
probes := probeAttachSessions(ctx, records)

var sessions []attachSession
var unreachable []string
for i, rec := range records {
if probes[i].err != nil {
// One unreachable run must not hide a tab that lives on another;
// report it only when nothing matches anywhere. Remember that the
// live view is incomplete: a partial match can no longer be assumed
// unique, because the run that did not answer may host a tab whose
// ID also contains the target.
unreachable = append(unreachable, fmt.Sprintf("%s: %v", rec.Addr, probes[i].err))
Comment thread
PerryLink marked this conversation as resolved.
Resolved
continue
}
for _, id := range probes[i].ids {
sessions = append(sessions, attachSession{addr: rec.Addr, sessionID: id})
}
}

var exact []attachSession
for _, sess := range sessions {
if sess.sessionID == target {
exact = append(exact, sess)
}
}
switch {
case len(exact) > 1:
return "", "", ambiguousAttachSessionError(target, exact)
case len(exact) == 1:
return exact[0].addr, exact[0].sessionID, nil
}

for _, rec := range records {
if rec.SessionID != target {
continue
}
resolved, err := resolveRecordedAttachSession(ctx, rec.Addr, rec.SessionID)
if err != nil {
return "", "", err
}
return rec.Addr, resolved, nil
}

var partial []attachSession
for _, sess := range sessions {
if strings.Contains(sess.sessionID, target) {
partial = append(partial, sess)
}
}
switch {
case len(partial) > 1:
return "", "", ambiguousAttachSessionError(target, partial)
case len(partial) == 1 && len(unreachable) > 0:
// The bounded discovery deadline turned this probe into a partial view:
// the run that did not answer in time may serve another tab whose ID
// also contains the target, so the match is not known to be unique.
// Binding the proxy here would let later MCP sends go to the wrong tab.
return "", "", unverifiedPartialAttachError(target, partial[0], unreachable)
case len(partial) == 1:
return partial[0].addr, partial[0].sessionID, nil
default:
return "", "", noLiveAttachSessionError(target, sessions, unreachable)
}
}

// attachProbe is the live-session probe of one discovered run.
type attachProbe struct {
ids []string
err error
}

// probeAttachSessions queries every discovered run concurrently, under one short
// overall deadline, and returns the probes in the same order as records so callers
// keep a deterministic view. A run whose TUI is suspended still has a listener
// that accepts the connection but never answers, so the per-request timeout is
// what makes a serial scan slow; probing concurrently and canceling the whole
// discovery at attachDiscoveryTimeout bounds that wait for every run at once.
func probeAttachSessions(ctx context.Context, records []runregistry.Record) []attachProbe {
probes := make([]attachProbe, len(records))

ctx, cancel := context.WithTimeout(ctx, attachDiscoveryTimeout)
defer cancel()

var group sync.WaitGroup
for i, rec := range records {
group.Go(func() {
ids, err := mcp.LiveAttachSessions(ctx, rec.Addr)
probes[i] = attachProbe{ids: ids, err: err}
})
}
group.Wait()

return probes
}

// resolveRecordedAttachSession resolves the recorded initial session of the run
// whose record names the requested target. It is bounded like discovery so a
// suspended target run cannot reintroduce the long request timeout after the
// concurrent probe returned.
func resolveRecordedAttachSession(ctx context.Context, addr, recorded string) (string, error) {
ctx, cancel := context.WithTimeout(ctx, attachDiscoveryTimeout)
defer cancel()
return mcp.ResolveAttachSession(ctx, addr, recorded, true)
}

// ambiguousAttachSessionError reports every live tab a target matches instead of
// silently selecting one of them.
func ambiguousAttachSessionError(target string, matches []attachSession) error {
slices.SortFunc(matches, func(a, b attachSession) int {
if c := strings.Compare(a.sessionID, b.sessionID); c != 0 {
return c
}
return strings.Compare(a.addr, b.addr)
})
options := make([]string, 0, len(matches))
for _, match := range matches {
options = append(options, fmt.Sprintf("%s (run at %s)", match.sessionID, match.addr))
}
return fmt.Errorf("session %q is ambiguous: it matches %s; select one explicitly with --attach=<session-id>", target, strings.Join(options, ", "))
}

// unverifiedPartialAttachError reports a partial match that cannot be trusted
// because at least one discovered run did not answer. A partial ID is only
// unique across the live tabs the lookup could actually see; ask the caller for
// the full session ID or a retry instead of starting the proxy on a guess.
func unverifiedPartialAttachError(target string, match attachSession, unreachable []string) error {
slices.Sort(unreachable)
return fmt.Errorf("session %q only partly matches the live tab %s on the run at %s, but not every docker-agent run could be queried: %s; a partial session ID may also occur on a tab of a run that did not answer, so it is not safe to bind: pass the full session ID with --attach=<session-id> or retry", target, match.sessionID, match.addr, strings.Join(unreachable, ", "))
}

// noLiveAttachSessionError reports that an explicitly named session is not
// attached anywhere and lists the live tabs that could be selected instead.
func noLiveAttachSessionError(target string, sessions []attachSession, unreachable []string) error {
live := make([]string, 0, len(sessions))
for _, sess := range sessions {
live = append(live, sess.sessionID)
}
slices.Sort(live)
msg := fmt.Sprintf("session %s is not attached to any live docker-agent run; live sessions: %s", target, formatLiveSessions(live))
if len(unreachable) > 0 {
slices.Sort(unreachable)
msg += "; could not query runs: " + strings.Join(unreachable, ", ")
}
return errors.New(msg)
}

func formatLiveSessions(ids []string) string {
if len(ids) == 0 {
return "none"
}
return strings.Join(ids, ", ")
}

// attachTargetIsSessionID reports whether a --attach value names a session ID
// explicitly rather than discovering the run by pid, address, or "latest".
func attachTargetIsSessionID(target string) bool {
if target == "" || target == "latest" {
return false
}
if _, err := strconv.Atoi(target); err == nil {
return false
}
return mcp.StartAttachStdio(ctx, rec.Addr, rec.SessionID)
return !strings.HasPrefix(target, "http://") && !strings.HasPrefix(target, "https://")
}
Loading
Loading