|
| 1 | +package a2a |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "log/slog" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "sync/atomic" |
| 10 | + "testing" |
| 11 | + |
| 12 | + a2atype "github.com/a2aproject/a2a-go/v2/a2a" |
| 13 | + "github.com/a2aproject/a2a-go/v2/a2asrv" |
| 14 | + mcpsdk "github.com/modelcontextprotocol/go-sdk/mcp" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + adkagent "google.golang.org/adk/v2/agent" |
| 17 | + |
| 18 | + "github.com/kagent-dev/kagent/go/adk/pkg/mcp" |
| 19 | + configv1 "github.com/kagent-dev/kagent/go/api/gen/kagent/config/v1" |
| 20 | +) |
| 21 | + |
| 22 | +func TestKAgentExecutorScopesMCPDiscoveryFailures(t *testing.T) { |
| 23 | + // Given a temporarily unavailable MCP catalog, when execution continues and |
| 24 | + // later resumes with reused IDs, assert that omission lasts only one execution. |
| 25 | + var unavailable atomic.Bool |
| 26 | + unavailable.Store(true) |
| 27 | + var listings atomic.Int32 |
| 28 | + server := mcpsdk.NewServer(&mcpsdk.Implementation{Name: "execution-discovery", Version: "1"}, &mcpsdk.ServerOptions{ |
| 29 | + SupportedProtocolVersions: []string{"2025-11-25"}, |
| 30 | + }) |
| 31 | + mcpsdk.AddTool(server, &mcpsdk.Tool{Name: "available_tool"}, func(context.Context, *mcpsdk.CallToolRequest, map[string]any) (*mcpsdk.CallToolResult, map[string]any, error) { |
| 32 | + return nil, map[string]any{"result": "ok"}, nil |
| 33 | + }) |
| 34 | + server.AddReceivingMiddleware(func(next mcpsdk.MethodHandler) mcpsdk.MethodHandler { |
| 35 | + return func(ctx context.Context, method string, req mcpsdk.Request) (mcpsdk.Result, error) { |
| 36 | + if method == "tools/list" { |
| 37 | + listings.Add(1) |
| 38 | + if unavailable.Load() { |
| 39 | + return nil, errors.New("catalog temporarily unavailable") |
| 40 | + } |
| 41 | + } |
| 42 | + return next(ctx, method, req) |
| 43 | + } |
| 44 | + }) |
| 45 | + httpServer := httptest.NewServer(mcpsdk.NewStreamableHTTPHandler(func(*http.Request) *mcpsdk.Server { return server }, &mcpsdk.StreamableHTTPOptions{Stateless: true})) |
| 46 | + t.Cleanup(httpServer.Close) |
| 47 | + toolsets := mcp.CreateToolsets(t.Context(), []*configv1.ADKHTTPMCPServerConfig{{ |
| 48 | + Params: &configv1.ADKHTTPConnectionParams{Url: httpServer.URL}, |
| 49 | + }}, nil, nil, false, nil) |
| 50 | + require.Len(t, toolsets, 1) |
| 51 | + |
| 52 | + var visible [][]string |
| 53 | + builtin := &recordingExecutor{onExecute: func(ctx context.Context) { |
| 54 | + readonly := &executionDiscoveryContext{StrictContextMock: adkagent.NewStrictContextMock(ctx)} |
| 55 | + for range 2 { |
| 56 | + tools, err := toolsets[0].Tools(readonly) |
| 57 | + require.NoError(t, err, "discovery failures must leave the execution usable") |
| 58 | + names := make([]string, 0, len(tools)) |
| 59 | + for _, tool := range tools { |
| 60 | + names = append(names, tool.Name()) |
| 61 | + } |
| 62 | + visible = append(visible, names) |
| 63 | + unavailable.Store(false) |
| 64 | + } |
| 65 | + }} |
| 66 | + executor := &KAgentExecutor{builtin: builtin, logger: slog.New(slog.DiscardHandler)} |
| 67 | + request := &a2asrv.ExecutorContext{ |
| 68 | + TaskID: "reused-task", ContextID: "reused-session", |
| 69 | + Message: a2atype.NewMessage(a2atype.MessageRoleUser, a2atype.NewTextPart("continue")), |
| 70 | + } |
| 71 | + for _, err := range executor.Execute(t.Context(), request) { |
| 72 | + require.NoError(t, err) |
| 73 | + } |
| 74 | + require.Equal(t, [][]string{{}, {}}, visible, "recovery during an execution must not retry its omitted server") |
| 75 | + require.EqualValues(t, 1, listings.Load()) |
| 76 | + |
| 77 | + visible = nil |
| 78 | + for _, err := range executor.Execute(t.Context(), request) { |
| 79 | + require.NoError(t, err) |
| 80 | + } |
| 81 | + require.Equal(t, [][]string{{"available_tool"}, {"available_tool"}}, visible, "a later execution with the same IDs must retry discovery") |
| 82 | + require.Greater(t, listings.Load(), int32(1)) |
| 83 | +} |
| 84 | + |
| 85 | +type executionDiscoveryContext struct { |
| 86 | + adkagent.StrictContextMock |
| 87 | +} |
| 88 | + |
| 89 | +func (*executionDiscoveryContext) InvocationID() string { return "reused-invocation" } |
| 90 | +func (*executionDiscoveryContext) SessionID() string { return "reused-session" } |
0 commit comments