Skip to content
Open
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
9 changes: 9 additions & 0 deletions pkg/vmcp/aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ type AggregationMetadata struct {

// ConflictStrategy is the strategy used for conflict resolution.
ConflictStrategy vmcp.ConflictResolutionStrategy

// Partial reports whether the capability sweep was incomplete: at least one
// backend in the aggregated set failed its live capability query, so its
// capabilities are absent from the result. A partial result is the best
// currently-known view and is safe to serve for a fresh read, but it must
// not REPLACE an established view — e.g. a session resync that replaces its
// tool store on a partial sweep would drop the failed backend's tools until
// the next health transition (see server.resyncSessionTools).
Partial bool
}

// ConflictResolver handles tool name conflicts across backends.
Expand Down
6 changes: 5 additions & 1 deletion pkg/vmcp/aggregator/default_aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,12 @@ func (a *defaultAggregator) AggregateCapabilities(
return nil, fmt.Errorf("failed to merge capabilities: %w", err)
}

// Update metadata with backend count
// Update metadata with backend count and sweep completeness. The sweep is
// partial when at least one backend failed its capability query and was
// omitted from `capabilities` (QueryAllCapabilities logs and continues on a
// per-backend failure, returning nil error as long as one backend answered).
aggregated.Metadata.BackendCount = len(backends)
aggregated.Metadata.Partial = len(capabilities) < len(backends)

span.SetAttributes(
attribute.Int("aggregated.backends", aggregated.Metadata.BackendCount),
Expand Down
52 changes: 52 additions & 0 deletions pkg/vmcp/aggregator/default_aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,58 @@ func TestDefaultAggregator_QueryAllCapabilities(t *testing.T) {
})
}

// TestDefaultAggregator_AggregateCapabilities_Partial verifies (#6829) that
// AggregateCapabilities reports, via Metadata.Partial, whether the backend sweep
// was incomplete — a still-healthy backend that failed its live capability query
// is omitted from the result, and a caller must be able to tell that apart from
// a complete (even tool-less) sweep before replacing an established view.
func TestDefaultAggregator_AggregateCapabilities_Partial(t *testing.T) {
t.Parallel()

newAgg := func(t *testing.T, failBackend2 bool) Aggregator {
t.Helper()
ctrl := gomock.NewController(t)
caps1 := newTestCapabilityList(withTools(newTestTool("tool1", "backend1")))
mockClient := mocks.NewMockBackendClient(ctrl)
mockClient.EXPECT().ListCapabilities(gomock.Any(), gomock.Any()).
DoAndReturn(func(_ context.Context, target *vmcp.BackendTarget) (*vmcp.CapabilityList, error) {
if target.WorkloadID == "backend1" {
return caps1, nil
}
if failBackend2 {
return nil, errors.New("connection timeout")
}
return newTestCapabilityList(withTools(newTestTool("tool2", "backend2"))), nil
}).Times(2)
return NewDefaultAggregator(mockClient, NewPrefixConflictResolver("{workload}_"), nil, nil)
}

backends := []vmcp.Backend{
newTestBackend("backend1"),
newTestBackend("backend2", withBackendURL("http://localhost:8081")),
}

t.Run("complete sweep is not partial", func(t *testing.T) {
t.Parallel()
agg := newAgg(t, false)
result, err := agg.AggregateCapabilities(context.Background(), backends)
require.NoError(t, err)
require.NotNil(t, result.Metadata)
assert.False(t, result.Metadata.Partial)
})

t.Run("partial sweep is reported", func(t *testing.T) {
t.Parallel()
agg := newAgg(t, true)
result, err := agg.AggregateCapabilities(context.Background(), backends)
require.NoError(t, err)
require.NotNil(t, result.Metadata)
assert.True(t, result.Metadata.Partial,
"a sweep where a healthy backend fails its query must be reported partial")
assert.Equal(t, 2, result.Metadata.BackendCount)
})
}

func TestDefaultAggregator_ResolveConflicts(t *testing.T) {
t.Parallel()

Expand Down
9 changes: 9 additions & 0 deletions pkg/vmcp/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ type DiscoverCapabilities struct {
HasResources bool
HasResourceTemplates bool
HasPrompts bool

// Partial reports whether the underlying capability sweep was incomplete: at
// least one eligible backend failed its live capability query, so its
// capabilities are absent from the aggregated view these presence flags
// summarize. Unlike the Has* flags — which reflect admission filtering —
// Partial is a property of the AGGREGATION itself. A partial view is safe for
// discovery, but must not replace an established view (see
// server.resyncSessionTools).
Partial bool
}

// VMCP is the core Virtual MCP domain object.
Expand Down
1 change: 1 addition & 0 deletions pkg/vmcp/core/core_vmcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ func (c *coreVMCP) Discover(ctx context.Context, identity *auth.Identity) (Disco
HasResources: len(resources) > 0,
HasResourceTemplates: len(templates) > 0,
HasPrompts: len(prompts) > 0,
Partial: agg.Metadata != nil && agg.Metadata.Partial,
}, nil
}

Expand Down
23 changes: 22 additions & 1 deletion pkg/vmcp/server/serve_list_changed.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,33 @@ func (s *Server) runListChangedResync(
// notifications/tools/list_changed to the downstream client, since serve.go
// enables WithToolCapabilities(true).
//
// A PARTIAL sweep short-circuits that replacement (#6829): a still-healthy
// backend that fails its live capability query mid-sweep is silently omitted
// from the re-derived set (best-effort aggregation logs and continues), and
// replacing the store with that set would drop the failed backend's tools until
// the next health transition. Partiality is read from core.Discover — the same
// aggregation the re-derivation below reads, since Discover and ListTools share
// the capability cache key — and a partial sweep keeps the existing store
// instead; the next health change (or backend-notification resync) retries with
// a fresh sweep.
//
// ctx must already carry the resyncing principal's identity and forwarded
// headers (runListChangedResync builds it) so serveSessionTools ->
// core.ListTools enumerates backends with the correct credentials and cache key.
// core.ListTools (and core.Discover) enumerate backends with the correct
// credentials and cache key.
func (s *Server) resyncSessionTools(
ctx context.Context, session server.ClientSession, sessionID string, identity *auth.Identity,
) error {
discovery, err := s.core.Discover(ctx, identity)
if err != nil {
return fmt.Errorf("resync session tools: core Discover for session %s: %w", sessionID, err)
}
if discovery.Partial {
slog.Warn("skipping session tool replacement after partial backend sweep",
"session_id", sessionID)
return nil
}

tools, err := s.serveSessionTools(ctx, sessionID, identity)
if err != nil {
return fmt.Errorf("resync session tools: core ListTools for session %s: %w", sessionID, err)
Expand Down
29 changes: 29 additions & 0 deletions pkg/vmcp/server/serve_list_changed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,35 @@ func TestResyncSessionTools_ReplacesRatherThanMerges(t *testing.T) {
assert.Equal(t, 1, sess.setSessionToolsCalls)
}

// TestResyncSessionTools_KeepsExistingToolsOnPartialSweep verifies (#6829) that
// resyncSessionTools does NOT replace the session's tool store when the
// underlying backend sweep was partial — a still-healthy backend that failed its
// live capability query would otherwise have its tools dropped from the session
// until the next health transition.
func TestResyncSessionTools_KeepsExistingToolsOnPartialSweep(t *testing.T) {
t.Parallel()

fc := &fakeCore{
tools: []vmcp.Tool{{Name: "kept"}, {Name: "added"}},
discoverPartial: true,
}
srv := &Server{core: fc}

sess := &fakeToolsSession{id: "sess-1", tools: map[string]server.ServerTool{
"kept": {Tool: mcp.Tool{Name: "kept"}},
"removed": {Tool: mcp.Tool{Name: "removed"}}, // must survive a partial sweep
}}

err := srv.resyncSessionTools(context.Background(), sess, "sess-1", nil)
require.NoError(t, err)

got := sess.GetSessionTools()
assert.Len(t, got, 2)
assert.Contains(t, got, "kept")
assert.Contains(t, got, "removed", "a partial sweep must not drop an existing backend's tools")
assert.Equal(t, 0, sess.setSessionToolsCalls, "a partial sweep must not replace the tool store")
}

// TestResyncSessionTools_SessionWithoutToolSupport verifies resyncSessionTools
// fails loudly (rather than silently no-opping) when the session does not
// implement server.SessionWithTools.
Expand Down
8 changes: 6 additions & 2 deletions pkg/vmcp/server/serve_session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ type fakeCore struct {
resourceTemplates []vmcp.ResourceTemplate
prompts []vmcp.Prompt

// discoverPartial, when true, makes Discover report a partial backend sweep
// so resyncSessionTools' keep-on-partial path (#6829) can be exercised.
discoverPartial bool

listToolsCalls atomic.Int32
listResourcesCalls atomic.Int32
listResourceTemplatesCalls atomic.Int32
Expand Down Expand Up @@ -366,8 +370,8 @@ func (*fakeCore) LookupBackend(context.Context, *auth.Identity, string) (*vmcp.B
return nil, vmcp.ErrNotFound
}

func (*fakeCore) Discover(context.Context, *auth.Identity) (core.DiscoverCapabilities, error) {
return core.DiscoverCapabilities{}, nil
func (f *fakeCore) Discover(context.Context, *auth.Identity) (core.DiscoverCapabilities, error) {
return core.DiscoverCapabilities{Partial: f.discoverPartial}, nil
}

func (*fakeCore) Close() error { return nil }
Expand Down