From b0d443f8e95fe41f765e8cb6df207d1d222a29e8 Mon Sep 17 00:00:00 2001 From: Connor Moss Date: Wed, 18 Nov 2026 16:37:57 -0500 Subject: [PATCH] fix(server): advertise protocol 0.3 on the card in --protocol 0.3 mode --- internal/cli/cli_test.go | 59 ++++++++++++++++++++++++++++++++++ internal/cli/serve.go | 10 +++--- internal/localsrv/serve_cfg.go | 10 +++--- internal/localsrv/srv_card.go | 8 ++++- 4 files changed, 76 insertions(+), 11 deletions(-) diff --git a/internal/cli/cli_test.go b/internal/cli/cli_test.go index ad0de29..c30e671 100644 --- a/internal/cli/cli_test.go +++ b/internal/cli/cli_test.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "iter" + "net" "net/http" "net/http/httptest" "os" @@ -1353,6 +1354,64 @@ func newAgentCard(url string, cap a2a.AgentCapabilities) *a2a.AgentCard { } } +func TestServe_Protocol03(t *testing.T) { + t.Parallel() + for _, transport := range []a2a.TransportProtocol{a2a.TransportProtocolHTTPJSON, a2a.TransportProtocolJSONRPC} { + t.Run(string(transport), func(t *testing.T) { + t.Parallel() + url := startLocalServer(t, localsrv.Config{ + CardParams: localsrv.CardParams{Transport: transport, ProtocolVersion: a2av0.Version}, + }) + // v0.3 clients read the endpoint from the card's top-level url. + resp, err := http.Get(url + a2asrv.WellKnownAgentCardPath) + if err != nil { + t.Fatalf("GET agent card error = %v", err) + } + defer func() { _ = resp.Body.Close() }() + var card struct { + URL string `json:"url"` + } + if err := json.NewDecoder(resp.Body).Decode(&card); err != nil { + t.Fatalf("decoding agent card error = %v", err) + } + if card.URL != url { + t.Fatalf("agent card url = %q, want %q", card.URL, url) + } + out, err := runCMD(t, "send", "-a", url, "-o", "json", "hi") + if err != nil { + t.Fatalf("send to a --protocol 0.3 server error = %v", err) + } + if got := mustDecodeTask(t, out).Status.State; got != a2a.TaskStateCompleted { + t.Fatalf("send to a --protocol 0.3 server state = %q, want %q", got, a2a.TaskStateCompleted) + } + }) + } +} + +// startLocalServer runs `a2a server --echo` with cfg and returns its URL. +func startLocalServer(t *testing.T, cfg localsrv.Config) string { + t.Helper() + listener, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatalf("net.Listen() error = %v", err) + } + cfg.Listener = listener + cfg.AdvertiseAddress = listener.Addr().String() + cfg.Logger = func(string, ...any) {} + cfg.Quiet = true + ctx, cancel := context.WithCancel(context.Background()) + done := make(chan struct{}) + go func() { + defer close(done) + _ = localsrv.ServeEcho(ctx, cfg) + }() + t.Cleanup(func() { + cancel() + <-done + }) + return "http://" + cfg.AdvertiseAddress +} + func startLegacyTestServer(t *testing.T) string { t.Helper() diff --git a/internal/cli/serve.go b/internal/cli/serve.go index e0062cb..34c813b 100644 --- a/internal/cli/serve.go +++ b/internal/cli/serve.go @@ -91,17 +91,17 @@ func newServeCmd(cfg *globalConfig) *cobra.Command { return fmt.Errorf("listen: %w", err) } sc := localsrv.Config{ - Listener: listener, - Logger: cfg.logf, - ProtocolVersion: a2a.ProtocolVersion(protocol), - CardCompat: cardCompat, - Quiet: quiet, + Listener: listener, + Logger: cfg.logf, + CardCompat: cardCompat, + Quiet: quiet, CardParams: localsrv.CardParams{ AgentName: name, AgentDesc: desc, CardPath: cardFile, Transport: transport, AdvertiseAddress: advertiseAddress, + ProtocolVersion: a2a.ProtocolVersion(protocol), }, } if sc.AdvertiseAddress == "" { diff --git a/internal/localsrv/serve_cfg.go b/internal/localsrv/serve_cfg.go index c1af147..64ecbfd 100644 --- a/internal/localsrv/serve_cfg.go +++ b/internal/localsrv/serve_cfg.go @@ -40,10 +40,9 @@ type LoggerFunc func(format string, args ...any) type Config struct { CardParams - Listener net.Listener - ProtocolVersion a2a.ProtocolVersion - Logger LoggerFunc - CardCompat bool + Listener net.Listener + Logger LoggerFunc + CardCompat bool Quiet bool } @@ -151,7 +150,8 @@ func buildMux(handler a2asrv.RequestHandler, card *a2a.AgentCard, transport a2a. } func agentCardHandler(card *a2a.AgentCard, sc Config) http.Handler { - if sc.CardCompat { + // A v0.3 server needs a card v0.3 clients can read. + if sc.CardCompat || sc.ProtocolVersion == a2av0.Version { return a2asrv.NewAgentCardHandler(a2av0.NewStaticAgentCardProducer(card)) } return a2asrv.NewStaticAgentCardHandler(card) diff --git a/internal/localsrv/srv_card.go b/internal/localsrv/srv_card.go index 722bebe..63296f8 100644 --- a/internal/localsrv/srv_card.go +++ b/internal/localsrv/srv_card.go @@ -21,6 +21,7 @@ import ( "strings" "github.com/a2aproject/a2a-go/v2/a2a" + "github.com/a2aproject/a2a-go/v2/a2acompat/a2av0" ) // CardParams holds the parameters used to build a local agent card. @@ -31,6 +32,7 @@ type CardParams struct { AgentDesc string AdvertiseAddress string Transport a2a.TransportProtocol + ProtocolVersion a2a.ProtocolVersion } func createAgentCard(cfg CardParams) (*a2a.AgentCard, error) { @@ -72,5 +74,9 @@ func getAgentInterface(cfg CardParams) *a2a.AgentInterface { if cfg.Transport != a2a.TransportProtocolGRPC && !strings.Contains(addr, "://") { addr = "http://" + addr } - return a2a.NewAgentInterface(addr, cfg.Transport) + iface := a2a.NewAgentInterface(addr, cfg.Transport) + if cfg.ProtocolVersion == a2av0.Version { + iface.ProtocolVersion = a2av0.Version + } + return iface }