Skip to content

Commit c6adf72

Browse files
authored
refactor(api): move MCP header resolution out of v1alpha3 (#3563)
# Description `go/api/v1alpha3` is the public API that external clients and wrapper types import, but it pulled in controller-runtime and `database/sql` for a few helpers that do I/O. This moves those helpers out so the API package only describes data. `RemoteMCPServerSpec` loses its `sql.Scanner`/`driver.Valuer` methods, which nothing used. `RemoteMCPServer.ResolveHeaders`, `ValueRef.Resolve`, and `ValueSource.Resolve` move into the tool service's MCP client, their only caller, and `go/api/utils` is removed. The translator keeps its existing `HeadersFrom` handling, which compiles Secret-backed headers into egress credentials rather than reading them. Header resolution behaves the same, except errors are now wrapped and name the failing header. Go consumers that called the removed methods need to read the referenced Secret or ConfigMap themselves. Signed-off-by: timflannagan <[email protected]>
1 parent 5c490d8 commit c6adf72

5 files changed

Lines changed: 118 additions & 118 deletions

File tree

‎go/api/utils/utils.go‎

Lines changed: 0 additions & 39 deletions
This file was deleted.

‎go/api/v1alpha3/common_types.go‎

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@ limitations under the License.
1717
package v1alpha3
1818

1919
import (
20-
"context"
2120
"fmt"
2221

2322
corev1 "k8s.io/api/core/v1"
2423
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2524
"k8s.io/apimachinery/pkg/labels"
2625
"k8s.io/apimachinery/pkg/types"
27-
"sigs.k8s.io/controller-runtime/pkg/client"
28-
29-
"github.com/kagent-dev/kagent/go/api/utils"
3026
)
3127

3228
// ObjectReference identifies a resource; an omitted namespace means the owner's namespace.
@@ -147,21 +143,6 @@ type ValueSource struct {
147143
Key string `json:"key"`
148144
}
149145

150-
func (s *ValueSource) Resolve(ctx context.Context, client client.Client, namespace string) (string, error) {
151-
if s == nil {
152-
return "", fmt.Errorf("ValueSource cannot be nil")
153-
}
154-
155-
switch s.Type {
156-
case ConfigMapValueSource:
157-
return utils.GetConfigMapValue(ctx, client, types.NamespacedName{Namespace: namespace, Name: s.Name}, s.Key)
158-
case SecretValueSource:
159-
return utils.GetSecretValue(ctx, client, types.NamespacedName{Namespace: namespace, Name: s.Name}, s.Key)
160-
default:
161-
return "", fmt.Errorf("unknown value source type: %s", s.Type)
162-
}
163-
}
164-
165146
// ValueRef represents a configuration value
166147
// +kubebuilder:validation:ExactlyOneOf=value;valueFrom
167148
type ValueRef struct {
@@ -173,23 +154,3 @@ type ValueRef struct {
173154
// +optional
174155
ValueFrom *ValueSource `json:"valueFrom,omitempty"`
175156
}
176-
177-
func (r *ValueRef) Resolve(ctx context.Context, client client.Client, namespace string) (string, string, error) {
178-
if r == nil {
179-
return "", "", fmt.Errorf("ValueRef cannot be nil")
180-
}
181-
182-
switch {
183-
case r.Value != "":
184-
return r.Name, r.Value, nil
185-
case r.ValueFrom != nil:
186-
value, err := r.ValueFrom.Resolve(ctx, client, namespace)
187-
if err != nil {
188-
return "", "", fmt.Errorf("failed to resolve value for ref %s: %v", r.Name, err)
189-
}
190-
191-
return r.Name, value, nil
192-
default:
193-
return r.Name, "", nil
194-
}
195-
}

‎go/api/v1alpha3/remotemcpserver_types.go‎

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,8 @@ limitations under the License.
1717
package v1alpha3
1818

1919
import (
20-
"context"
21-
"database/sql"
22-
"database/sql/driver"
23-
"encoding/json"
24-
"fmt"
25-
2620
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2721
"k8s.io/apimachinery/pkg/runtime"
28-
"sigs.k8s.io/controller-runtime/pkg/client"
2922
)
3023

3124
// +kubebuilder:validation:Enum=SSE;STREAMABLE_HTTP
@@ -95,22 +88,6 @@ type RemoteMCPServerSpec struct {
9588
TLS *TLSConfig `json:"tls,omitempty"`
9689
}
9790

98-
var _ sql.Scanner = (*RemoteMCPServerSpec)(nil)
99-
100-
func (t *RemoteMCPServerSpec) Scan(src any) error {
101-
switch v := src.(type) {
102-
case []uint8:
103-
return json.Unmarshal(v, t)
104-
}
105-
return nil
106-
}
107-
108-
var _ driver.Valuer = (*RemoteMCPServerSpec)(nil)
109-
110-
func (t RemoteMCPServerSpec) Value() (driver.Value, error) {
111-
return json.Marshal(t)
112-
}
113-
11491
// RemoteMCPServerStatus defines the observed state of RemoteMCPServer.
11592
type RemoteMCPServerStatus struct {
11693
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
@@ -153,22 +130,6 @@ type RemoteMCPServer struct {
153130
Status RemoteMCPServerStatus `json:"status,omitempty"`
154131
}
155132

156-
// ResolveHeaders resolves all HeadersFrom entries using the object's namespace.
157-
func (r *RemoteMCPServer) ResolveHeaders(ctx context.Context, client client.Client) (map[string]string, error) {
158-
result := map[string]string{}
159-
160-
for _, h := range r.Spec.HeadersFrom {
161-
k, v, err := h.Resolve(ctx, client, r.Namespace)
162-
if err != nil {
163-
return nil, fmt.Errorf("failed to resolve header: %v", err)
164-
}
165-
166-
result[k] = v
167-
}
168-
169-
return result, nil
170-
}
171-
172133
// +kubebuilder:object:root=true
173134
// RemoteMCPServerList contains a list of RemoteMCPServer.
174135
type RemoteMCPServerList struct {

‎go/core/internal/service/tool/mcp.go‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import (
1212
"github.com/modelcontextprotocol/go-sdk/mcp"
1313
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
1414
"go.opentelemetry.io/otel/trace"
15+
corev1 "k8s.io/api/core/v1"
1516
apierrors "k8s.io/apimachinery/pkg/api/errors"
17+
"k8s.io/apimachinery/pkg/types"
1618
"sigs.k8s.io/controller-runtime/pkg/client"
1719

1820
"github.com/kagent-dev/kagent/go/api/v1alpha3"
@@ -135,7 +137,7 @@ func (c *RuntimeMCPClient) connect(ctx context.Context, ref MCPServerRef) (*mcp.
135137
}
136138
connectCtx, cancel := context.WithTimeout(ctx, timeout)
137139

138-
headers, err := server.ResolveHeaders(connectCtx, c.kubeClient)
140+
headers, err := resolveHeaders(connectCtx, c.kubeClient, server.Namespace, server.Spec.HeadersFrom)
139141
if err != nil {
140142
cancel()
141143
return nil, nil, fmt.Errorf("failed to resolve RemoteMCPServer headers: %w", err)
@@ -276,3 +278,48 @@ func (t *mcpAppsHeaderTransport) RoundTrip(request *http.Request) (*http.Respons
276278
}
277279
return t.base.RoundTrip(request)
278280
}
281+
282+
// resolveHeaders reads RemoteMCPServer headers, loading ValueFrom entries from
283+
// Secrets or ConfigMaps in the server's namespace.
284+
func resolveHeaders(ctx context.Context, kube client.Reader, namespace string, refs []v1alpha3.ValueRef) (map[string]string, error) {
285+
headers := make(map[string]string, len(refs))
286+
for _, ref := range refs {
287+
value, err := resolveHeaderValue(ctx, kube, namespace, ref)
288+
if err != nil {
289+
return nil, fmt.Errorf("failed to resolve header %s: %w", ref.Name, err)
290+
}
291+
headers[ref.Name] = value
292+
}
293+
return headers, nil
294+
}
295+
296+
func resolveHeaderValue(ctx context.Context, kube client.Reader, namespace string, ref v1alpha3.ValueRef) (string, error) {
297+
if ref.ValueFrom == nil {
298+
return ref.Value, nil
299+
}
300+
key := types.NamespacedName{Namespace: namespace, Name: ref.ValueFrom.Name}
301+
switch ref.ValueFrom.Type {
302+
case v1alpha3.SecretValueSource:
303+
secret := &corev1.Secret{}
304+
if err := kube.Get(ctx, key, secret); err != nil {
305+
return "", fmt.Errorf("failed to get Secret %s: %w", key, err)
306+
}
307+
value, ok := secret.Data[ref.ValueFrom.Key]
308+
if !ok {
309+
return "", fmt.Errorf("key %s not found in Secret %s", ref.ValueFrom.Key, key)
310+
}
311+
return string(value), nil
312+
case v1alpha3.ConfigMapValueSource:
313+
configMap := &corev1.ConfigMap{}
314+
if err := kube.Get(ctx, key, configMap); err != nil {
315+
return "", fmt.Errorf("failed to get ConfigMap %s: %w", key, err)
316+
}
317+
value, ok := configMap.Data[ref.ValueFrom.Key]
318+
if !ok {
319+
return "", fmt.Errorf("key %s not found in ConfigMap %s", ref.ValueFrom.Key, key)
320+
}
321+
return value, nil
322+
default:
323+
return "", fmt.Errorf("unknown value source type %q", ref.ValueFrom.Type)
324+
}
325+
}

‎go/core/internal/service/tool/mcp_test.go‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/modelcontextprotocol/go-sdk/mcp"
88
"github.com/stretchr/testify/assert"
99
"github.com/stretchr/testify/require"
10+
corev1 "k8s.io/api/core/v1"
1011
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1112
"k8s.io/apimachinery/pkg/types"
1213
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -117,3 +118,72 @@ func TestRuntimeMCPClientResolveServerMatrix(t *testing.T) {
117118
})
118119
}
119120
}
121+
122+
func TestResolveHeaders(t *testing.T) {
123+
secret := &corev1.Secret{
124+
ObjectMeta: metav1.ObjectMeta{Name: "creds", Namespace: "team"},
125+
Data: map[string][]byte{"token": []byte("secret-token")},
126+
}
127+
configMap := &corev1.ConfigMap{
128+
ObjectMeta: metav1.ObjectMeta{Name: "settings", Namespace: "team"},
129+
Data: map[string]string{"tenant": "acme"},
130+
}
131+
132+
t.Run("Given inline, Secret, and ConfigMap headers, when the server headers are resolved, assert that every value is loaded from the server namespace", func(t *testing.T) {
133+
kube := toolTestKube(t, secret, configMap)
134+
135+
headers, err := resolveHeaders(t.Context(), kube, "team", []v1alpha3.ValueRef{
136+
{Name: "X-Static", Value: "static"},
137+
{Name: "Authorization", ValueFrom: &v1alpha3.ValueSource{Type: v1alpha3.SecretValueSource, Name: "creds", Key: "token"}},
138+
{Name: "X-Tenant", ValueFrom: &v1alpha3.ValueSource{Type: v1alpha3.ConfigMapValueSource, Name: "settings", Key: "tenant"}},
139+
})
140+
141+
require.NoError(t, err)
142+
assert.Equal(t, map[string]string{
143+
"X-Static": "static",
144+
"Authorization": "secret-token",
145+
"X-Tenant": "acme",
146+
}, headers)
147+
})
148+
149+
scenarios := []struct {
150+
name string
151+
objects []client.Object
152+
ref v1alpha3.ValueRef
153+
wantError string
154+
}{
155+
{
156+
name: "Given the Secret lives in another namespace, when a Secret header is resolved, assert that resolution fails",
157+
objects: []client.Object{&corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "creds", Namespace: "other"}, Data: secret.Data}},
158+
ref: v1alpha3.ValueRef{Name: "Authorization", ValueFrom: &v1alpha3.ValueSource{Type: v1alpha3.SecretValueSource, Name: "creds", Key: "token"}},
159+
wantError: "failed to get Secret team/creds",
160+
},
161+
{
162+
name: "Given the Secret lacks the key, when a Secret header is resolved, assert that resolution fails",
163+
objects: []client.Object{secret},
164+
ref: v1alpha3.ValueRef{Name: "Authorization", ValueFrom: &v1alpha3.ValueSource{Type: v1alpha3.SecretValueSource, Name: "creds", Key: "missing"}},
165+
wantError: "key missing not found in Secret team/creds",
166+
},
167+
{
168+
name: "Given the ConfigMap lacks the key, when a ConfigMap header is resolved, assert that resolution fails",
169+
objects: []client.Object{configMap},
170+
ref: v1alpha3.ValueRef{Name: "X-Tenant", ValueFrom: &v1alpha3.ValueSource{Type: v1alpha3.ConfigMapValueSource, Name: "settings", Key: "missing"}},
171+
wantError: "key missing not found in ConfigMap team/settings",
172+
},
173+
{
174+
name: "Given an unknown source type, when the header is resolved, assert that resolution fails",
175+
ref: v1alpha3.ValueRef{Name: "X-Other", ValueFrom: &v1alpha3.ValueSource{Type: "Vault", Name: "x", Key: "y"}},
176+
wantError: `unknown value source type "Vault"`,
177+
},
178+
}
179+
for _, scenario := range scenarios {
180+
t.Run(scenario.name, func(t *testing.T) {
181+
kube := toolTestKube(t, scenario.objects...)
182+
183+
headers, err := resolveHeaders(t.Context(), kube, "team", []v1alpha3.ValueRef{scenario.ref})
184+
185+
require.ErrorContains(t, err, scenario.wantError)
186+
assert.Nil(t, headers)
187+
})
188+
}
189+
}

0 commit comments

Comments
 (0)