MCPcopy
hub / github.com/docker/docker-agent / ResolveCommand

Function ResolveCommand

pkg/runtime/commands.go:57–93  ·  view source on GitHub ↗

ResolveCommand transforms a /command into its expanded instruction text. It processes: 1. Command lookup from agent commands 2. Tool command execution (!tool_name(arg=value)) - tools executed and output inserted 3. JavaScript expressions (${...}) - evaluated with access to all agent tools and args a

(ctx context.Context, rt Runtime, userInput string)

Source from the content-addressed store, hash-verified

55// command has no instruction and no arguments, the result is the empty
56// string, signalling "no message to send".
57func ResolveCommand(ctx context.Context, rt Runtime, userInput string) string {
58 command, rest, ok := LookupCommand(ctx, rt, userInput)
59 if !ok {
60 return userInput
61 }
62
63 instruction := command.Instruction
64
65 // Agent-only commands (no instruction): forward the trailing args verbatim
66 // so the target sub-agent receives the user's original prompt.
67 if instruction == "" {
68 return rest
69 }
70
71 args := tokenize(rest)
72
73 // Execute JavaScript expressions (${...} syntax) with args array
74 // We execute JS first to prevent tool output (from !tool commands) from being evaluated as JS,
75 // which would be a security vulnerability (injection).
76 agentTools, err := rt.CurrentAgentTools(ctx)
77 if err != nil {
78 slog.WarnContext(ctx, "Failed to get agent tools for JS expression execution", "error", err)
79 } else {
80 evaluator := js.NewEvaluator(agentTools)
81 instruction = evaluator.Evaluate(ctx, instruction, args)
82 }
83
84 // Execute tool commands and substitute their output (legacy !tool() syntax)
85 instruction = executeToolCommands(ctx, rt, instruction)
86
87 // Append remaining text if no placeholders were used
88 if rest != "" && !argsPlaceholderRegex.MatchString(command.Instruction) {
89 instruction += " " + rest
90 }
91
92 return instruction
93}
94
95// tokenize splits input into tokens, respecting quoted strings.
96// Quotes are stripped from the tokens.

Calls 6

NewEvaluatorFunction · 0.92
LookupCommandFunction · 0.85
executeToolCommandsFunction · 0.85
EvaluateMethod · 0.80
tokenizeFunction · 0.70
CurrentAgentToolsMethod · 0.65