Skip to content

Commit af25e4a

Browse files
committed
Default to 127.0.0:8080 for 'cagent api', 'cagent a2a' and 'cagent mcp'
Signed-off-by: David Gageot <david.gageot@docker.com>
1 parent b28ad13 commit af25e4a

7 files changed

Lines changed: 50 additions & 50 deletions

File tree

cmd/root/a2a.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
)
1414

1515
type a2aFlags struct {
16-
agentName string
17-
port int
18-
runConfig config.RuntimeConfig
16+
agentName string
17+
listenAddr string
18+
runConfig config.RuntimeConfig
1919
}
2020

2121
func newA2ACmd() *cobra.Command {
@@ -26,15 +26,14 @@ func newA2ACmd() *cobra.Command {
2626
Short: "Start an agent as an A2A (Agent-to-Agent) server",
2727
Long: "Start an A2A server that exposes the agent via the Agent-to-Agent protocol",
2828
Example: ` cagent a2a ./agent.yaml
29-
cagent a2a ./team.yaml --port 8080
30-
cagent a2a agentcatalog/pirate --port 9000`,
29+
cagent a2a agentcatalog/pirate --listen 127.0.0.1:9090`,
3130
Args: cobra.ExactArgs(1),
3231
GroupID: "server",
3332
RunE: flags.runA2ACommand,
3433
}
3534

3635
cmd.PersistentFlags().StringVarP(&flags.agentName, "agent", "a", "root", "Name of the agent to run")
37-
cmd.PersistentFlags().IntVar(&flags.port, "port", 0, "Port to listen on (default: random available port)")
36+
cmd.PersistentFlags().StringVarP(&flags.listenAddr, "listen", "l", "127.0.0.1:8080", "Address to listen on")
3837
addRuntimeConfigFlags(cmd, &flags.runConfig)
3938

4039
return cmd
@@ -48,9 +47,9 @@ func (f *a2aFlags) runA2ACommand(cmd *cobra.Command, args []string) error {
4847
agentFilename := args[0]
4948

5049
// Listen as early as possible
51-
ln, err := server.Listen(ctx, fmt.Sprintf(":%d", f.port))
50+
ln, err := server.Listen(ctx, f.listenAddr)
5251
if err != nil {
53-
return fmt.Errorf("failed to bind to port %d: %w", f.port, err)
52+
return fmt.Errorf("failed to listen on %s: %w", f.listenAddr, err)
5453
}
5554
go func() {
5655
<-ctx.Done()

cmd/root/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func newAPICmd() *cobra.Command {
4040
RunE: flags.runAPICommand,
4141
}
4242

43-
cmd.PersistentFlags().StringVarP(&flags.listenAddr, "listen", "l", ":8080", "Address to listen on")
43+
cmd.PersistentFlags().StringVarP(&flags.listenAddr, "listen", "l", "127.0.0.1:8080", "Address to listen on")
4444
cmd.PersistentFlags().StringVarP(&flags.sessionDB, "session-db", "s", "session.db", "Path to the session database")
4545
cmd.PersistentFlags().IntVar(&flags.pullIntervalMins, "pull-interval", 0, "Auto-pull OCI reference every N minutes (0 = disabled)")
4646
cmd.PersistentFlags().StringVar(&flags.fakeResponses, "fake", "", "Replay AI responses from cassette file (for testing)")
@@ -132,7 +132,7 @@ func (f *apiFlags) runAPICommand(cmd *cobra.Command, args []string) error {
132132
_ = ln.Close()
133133
}()
134134

135-
out.Println("Listening on " + ln.Addr().String())
135+
out.Println("Listening on", ln.Addr().String())
136136

137137
slog.Debug("Starting server", "agents", agentsPath, "addr", ln.Addr().String())
138138

cmd/root/mcp.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
)
1313

1414
type mcpFlags struct {
15-
agentName string
16-
http bool
17-
port int
18-
runConfig config.RuntimeConfig
15+
agentName string
16+
http bool
17+
listenAddr string
18+
runConfig config.RuntimeConfig
1919
}
2020

2121
func newMCPCmd() *cobra.Command {
@@ -28,15 +28,15 @@ func newMCPCmd() *cobra.Command {
2828
Example: ` cagent mcp ./agent.yaml
2929
cagent mcp ./team.yaml
3030
cagent mcp agentcatalog/pirate
31-
cagent mcp ./agent.yaml --http --port 8080`,
31+
cagent mcp ./agent.yaml --http --listen 127.0.0.1:9090`,
3232
Args: cobra.ExactArgs(1),
3333
GroupID: "server",
3434
RunE: flags.runMCPCommand,
3535
}
3636

3737
cmd.PersistentFlags().StringVarP(&flags.agentName, "agent", "a", "", "Name of the agent to run (all agents if not specified)")
3838
cmd.PersistentFlags().BoolVar(&flags.http, "http", false, "Use streaming HTTP transport instead of stdio")
39-
cmd.PersistentFlags().IntVar(&flags.port, "port", 0, "Port to listen on when using HTTP transport (default: random available port)")
39+
cmd.PersistentFlags().StringVarP(&flags.listenAddr, "listen", "l", "127.0.0.1:8080", "Address to listen on")
4040
addRuntimeConfigFlags(cmd, &flags.runConfig)
4141

4242
return cmd
@@ -48,18 +48,19 @@ func (f *mcpFlags) runMCPCommand(cmd *cobra.Command, args []string) error {
4848
ctx := cmd.Context()
4949
agentFilename := args[0]
5050

51-
if f.http || f.port != 0 {
52-
ln, err := server.Listen(ctx, fmt.Sprintf(":%d", f.port))
53-
if err != nil {
54-
return fmt.Errorf("failed to bind to port %d: %w", f.port, err)
55-
}
56-
go func() {
57-
<-ctx.Done()
58-
_ = ln.Close()
59-
}()
51+
if !f.http {
52+
return mcp.StartMCPServer(ctx, agentFilename, f.agentName, &f.runConfig)
53+
}
6054

61-
return mcp.StartHTTPServer(ctx, agentFilename, f.agentName, &f.runConfig, ln)
55+
// Listen as early as possible
56+
ln, err := server.Listen(ctx, f.listenAddr)
57+
if err != nil {
58+
return fmt.Errorf("failed to listen on %s: %w", f.listenAddr, err)
6259
}
60+
go func() {
61+
<-ctx.Done()
62+
_ = ln.Close()
63+
}()
6364

64-
return mcp.StartMCPServer(ctx, agentFilename, f.agentName, &f.runConfig)
65+
return mcp.StartHTTPServer(ctx, agentFilename, f.agentName, &f.runConfig, ln)
6566
}

docs/a2a.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ This document describes how to expose Docker `cagent` agents via Google's A2A (A
1616
cagent a2a ./agent.yaml
1717

1818
# Specify a custom port
19-
cagent a2a ./agent.yaml --port 8080
19+
cagent a2a ./agent.yaml --listen 127.0.0.1:9000
2020

2121
# Use an agent from the catalog
22-
cagent a2a agentcatalog/pirate --port 9000
22+
cagent a2a agentcatalog/pirate
2323
```
2424

2525
## Limitations and Future Work

examples/tic-tac-toe-mcp.yaml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ metadata:
1010
The Game master talks to both players over MCP.
1111
1212
To run the demo:
13-
cagent mcp --http --port 8080 ./examples/tic-tac-toe-mcp.yaml --agent player_blue
14-
cagent mcp --http --port 8081 ./examples/tic-tac-toe-mcp.yaml --agent player_red
13+
cagent mcp --http --listen 127.0.0.1:8080 ./examples/tic-tac-toe-mcp.yaml --agent player_blue
14+
cagent mcp --http --listen 127.0.0.1:8081 ./examples/tic-tac-toe-mcp.yaml --agent player_red
1515
cagent run --yolo ./examples/tic-tac-toe-mcp.yaml 'Go!'
1616
1717
agents:
@@ -43,16 +43,16 @@ agents:
4343
</preparation>
4444
add_environment_info: true
4545
toolsets:
46-
- type: shell
47-
- type: filesystem
48-
- type: mcp
49-
remote:
50-
url: http://localhost:8080/
51-
transport_type: streamable
52-
- type: mcp
53-
remote:
54-
url: http://localhost:8081/
55-
transport_type: streamable
46+
- type: shell
47+
- type: filesystem
48+
- type: mcp
49+
remote:
50+
url: http://localhost:8080/
51+
transport_type: streamable
52+
- type: mcp
53+
remote:
54+
url: http://localhost:8081/
55+
transport_type: streamable
5656

5757
# BLUE Player
5858
player_blue:

examples/tic-tac-toe.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ metadata:
1010
The Game master talks to both players over A2A.
1111
1212
To run the demo:
13-
cagent a2a --port 8080 ./examples/tic-tac-toe.yaml --agent player_blue
14-
cagent a2a --port 8081 ./examples/tic-tac-toe.yaml --agent player_red
13+
cagent a2a --listen 127.0.0.1:8080 ./examples/tic-tac-toe.yaml --agent player_blue
14+
cagent a2a --listen 127.0.0.1:8081 ./examples/tic-tac-toe.yaml --agent player_red
1515
cagent run --yolo ./examples/tic-tac-toe.yaml 'Go!'
1616
1717
agents:
@@ -43,12 +43,12 @@ agents:
4343
</preparation>
4444
add_environment_info: true
4545
toolsets:
46-
- type: shell
47-
- type: filesystem
48-
- type: a2a
49-
url: http://localhost:8080/
50-
- type: a2a
51-
url: http://localhost:8081/
46+
- type: shell
47+
- type: filesystem
48+
- type: a2a
49+
url: http://localhost:8080/
50+
- type: a2a
51+
url: http://localhost:8081/
5252

5353
# BLUE Player
5454
player_blue:

pkg/telemetry/telemetry_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ func TestTrackServerStart(t *testing.T) {
539539
cmdInfo := CommandInfo{
540540
Action: "mcp",
541541
Args: []string{},
542-
Flags: []string{"--port", "8080"},
542+
Flags: []string{},
543543
}
544544
err := client.TrackServerStart(t.Context(), cmdInfo, func(ctx context.Context) error {
545545
executed = true

0 commit comments

Comments
 (0)