Skip to content

Commit bf0b26e

Browse files
committed
fix(controller): keep a kagent.dev/discovery=disabled MCPServer out of the catalog
main honors the label nowhere: kagent-dev#2565 removed predicates.DiscoveryDisabledPredicate together with the old MCPServer tool controller, and the catalog reconciler restored in kagent-dev#2644 watches every kmcp MCPServer unfiltered, so the opt-out the docs describe for the agentgateway setup was a no-op on main. The MCPServer catalog reconciler now skips discovery for a labeled server and drops its catalog projection. That is what the 0.10.x predicate achieved, minus two stale-row cases: a label added after discovery left the row in place, and the deletion of a labeled server never removed it. The controller filters no events, so a label change re-enters Reconcile at once. A RemoteMCPServer stays in the catalog with the DiscoveryDisabled status on purpose: it is kagent's own CR that agents reference. An MCPServer is kmcp's resource that kagent only projects, has no kagent status to explain an empty row, and AgentTemplates cannot reference it directly. Signed-off-by: Timo Derstappen <[email protected]>
1 parent 7c0e532 commit bf0b26e

3 files changed

Lines changed: 63 additions & 2 deletions

File tree

‎go/core/internal/controller/mcpserver/reconciler.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/kagent-dev/kagent/go/core/internal/controller/toolcatalog"
2929
"github.com/kagent-dev/kagent/go/core/internal/database"
3030
toolservice "github.com/kagent-dev/kagent/go/core/internal/service/tool"
31+
"github.com/kagent-dev/kagent/go/core/pkg/consts"
3132
"github.com/kagent-dev/kagent/go/pkg/logging"
3233
kmcp "github.com/kagent-dev/kmcp/api/v1alpha1"
3334
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -110,6 +111,18 @@ func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (
110111
return reconcile.Result{}, r.deleteCatalog(ctx, request.String())
111112
}
112113

114+
if discoveryDisabled(server) {
115+
// The operator opted this server out of discovery (for example because
116+
// agentgateway fronts it and agents reach it through a RemoteMCPServer):
117+
// do not connect, and keep it out of the catalog altogether. This
118+
// controller filters no events, so adding or removing the label re-enters
119+
// Reconcile at once; no requeue is needed.
120+
if err := r.deleteCatalog(ctx, request.String()); err != nil {
121+
return reconcile.Result{}, fmt.Errorf("remove opted-out MCPServer catalog: %w", err)
122+
}
123+
return reconcile.Result{}, nil
124+
}
125+
113126
if !isReady(server) {
114127
if err := r.updateCatalog(ctx, server, nil, false); err != nil {
115128
return reconcile.Result{}, fmt.Errorf("clear unready MCPServer catalog: %w", err)
@@ -147,6 +160,12 @@ func isReady(server *kmcp.MCPServer) bool {
147160
return condition != nil && condition.Status == metav1.ConditionTrue && condition.ObservedGeneration == server.Generation
148161
}
149162

163+
// discoveryDisabled reports whether the operator opted the server out of tool
164+
// discovery with the kagent.dev/discovery=disabled label.
165+
func discoveryDisabled(server *kmcp.MCPServer) bool {
166+
return server.Labels[consts.DiscoveryLabel] == consts.DiscoveryDisabled
167+
}
168+
150169
func (r *Reconciler) updateCatalog(ctx context.Context, server *kmcp.MCPServer, tools []*v1alpha3.MCPTool, connected bool) error {
151170
name := client.ObjectKeyFromObject(server).String()
152171
var lastConnected *time.Time

‎go/core/internal/controller/mcpserver/reconciler_test.go‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/kagent-dev/kagent/go/api/v1alpha3"
2626
"github.com/kagent-dev/kagent/go/core/internal/database"
2727
toolservice "github.com/kagent-dev/kagent/go/core/internal/service/tool"
28+
"github.com/kagent-dev/kagent/go/core/pkg/consts"
2829
kmcp "github.com/kagent-dev/kmcp/api/v1alpha1"
2930
apiMeta "k8s.io/apimachinery/pkg/api/meta"
3031
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -154,6 +155,44 @@ func TestReconcileClearsCatalogAfterDiscoveryFailure(t *testing.T) {
154155
}
155156
}
156157

158+
func TestReconcileRemovesCatalogWhenDiscoveryDisabled(t *testing.T) {
159+
server := readyServer()
160+
server.Labels = map[string]string{consts.DiscoveryLabel: consts.DiscoveryDisabled}
161+
discoverer := &fakeDiscoverer{err: errors.New("the controller must not dial an opted-out server")}
162+
catalog := &fakeCatalog{}
163+
164+
result, err := New(testClient(t, server), discoverer, catalog).Reconcile(t.Context(), ctrl.Request{
165+
NamespacedName: client.ObjectKeyFromObject(server),
166+
})
167+
if err != nil {
168+
t.Fatalf("Reconcile() error = %v", err)
169+
}
170+
if result.RequeueAfter != 0 || discoverer.calls != 0 {
171+
t.Fatalf("Reconcile() = %#v, discovery calls = %d; want no requeue and no discovery", result, discoverer.calls)
172+
}
173+
want := "test/tools|" + mcpServerGroupKind
174+
if catalog.deletedTools != want || catalog.deleted != want || catalog.server != nil {
175+
t.Fatalf("opted-out catalog = deletes tools %q, server %q, stored %#v; want %q deleted and nothing stored",
176+
catalog.deletedTools, catalog.deleted, catalog.server, want)
177+
}
178+
}
179+
180+
func TestReconcileDiscoversWhenDiscoveryLabelIsNotDisabled(t *testing.T) {
181+
server := readyServer()
182+
server.Labels = map[string]string{consts.DiscoveryLabel: "enabled"}
183+
discoverer := &fakeDiscoverer{tools: []toolservice.MCPAppTool{{Name: "alpha", Description: "first"}}}
184+
catalog := &fakeCatalog{}
185+
186+
if _, err := New(testClient(t, server), discoverer, catalog).Reconcile(t.Context(), ctrl.Request{
187+
NamespacedName: client.ObjectKeyFromObject(server),
188+
}); err != nil {
189+
t.Fatalf("Reconcile() error = %v", err)
190+
}
191+
if discoverer.calls != 1 || catalog.server == nil || catalog.server.LastConnected == nil || len(catalog.tools) != 1 {
192+
t.Fatalf("discovery calls = %d, catalog = server %#v, tools %#v", discoverer.calls, catalog.server, catalog.tools)
193+
}
194+
}
195+
157196
func TestReconcileDeletesCatalogProjection(t *testing.T) {
158197
catalog := &fakeCatalog{}
159198
request := ctrl.Request{NamespacedName: types.NamespacedName{Namespace: "test", Name: "gone"}}

‎go/core/pkg/consts/labels.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ package consts
66
// agents that reference it resolve the tool list at run time with the credentials
77
// they carry — for a server that authenticates every caller (a propagated caller
88
// token, which the controller does not hold), the controller-side listing would
9-
// otherwise fail and mark the server as not Accepted forever. Any other value, or
10-
// no label, keeps discovery on.
9+
// otherwise fail and mark the server as not Accepted forever. A kmcp MCPServer
10+
// with the same label is left out of the catalog entirely: the controller neither
11+
// lists its tools nor keeps a projection of it (the documented setup where
12+
// agentgateway fronts the server and agents reach it through a RemoteMCPServer).
13+
// Any other value, or no label, keeps discovery on.
1114
const DiscoveryLabel = "kagent.dev/discovery"
1215

1316
// DiscoveryDisabled is the DiscoveryLabel value that turns tool discovery off.

0 commit comments

Comments
 (0)