-
Notifications
You must be signed in to change notification settings - Fork 633
fix(controller): recover MCP auth session from RequestExtra in tool handlers
#1853
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| package mcp | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/kagent-dev/kagent/go/core/pkg/auth" | ||
| mcpsdk "github.com/modelcontextprotocol/go-sdk/mcp" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // fakeSession is a minimal auth.Session for testing. | ||
| type fakeSession struct{ principal auth.Principal } | ||
|
|
||
| func (s *fakeSession) Principal() auth.Principal { return s.principal } | ||
|
|
||
| // fakeAuthProvider propagates the incoming Bearer token to upstream requests unchanged. | ||
| type fakeAuthProvider struct { | ||
| session auth.Session | ||
| } | ||
|
|
||
| func (f *fakeAuthProvider) Authenticate(_ context.Context, headers http.Header, _ url.Values) (auth.Session, error) { | ||
| if headers.Get("Authorization") != "" { | ||
| return f.session, nil | ||
| } | ||
| return nil, http.ErrNoCookie | ||
| } | ||
|
|
||
| func (f *fakeAuthProvider) UpstreamAuth(r *http.Request, _ auth.Session, _ auth.Principal) error { | ||
| r.Header.Set("Authorization", "Bearer upstream-token") | ||
| return nil | ||
| } | ||
|
|
||
| // a2aBackend is a fake A2A server that records the Authorization header of each request. | ||
| type a2aBackend struct { | ||
| server *httptest.Server | ||
| mu sync.Mutex | ||
| lastAuthHeader string | ||
| } | ||
|
|
||
| func (b *a2aBackend) getLastAuthHeader() string { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
| return b.lastAuthHeader | ||
| } | ||
|
|
||
| func newA2ABackend(t *testing.T) *a2aBackend { | ||
| t.Helper() | ||
| b := &a2aBackend{} | ||
| b.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| b.mu.Lock() | ||
| b.lastAuthHeader = r.Header.Get("Authorization") | ||
| b.mu.Unlock() | ||
| resp := map[string]any{ | ||
|
onematchfox marked this conversation as resolved.
|
||
| "jsonrpc": "2.0", | ||
| "id": "", | ||
| "result": map[string]any{ | ||
| "kind": "message", | ||
| "messageId": "test-msg", | ||
| "role": "agent", | ||
| "parts": []any{map[string]any{"kind": "text", "text": "hello from agent"}}, | ||
| }, | ||
| } | ||
| w.Header().Set("Content-Type", "application/json") | ||
| if err := json.NewEncoder(w).Encode(resp); err != nil { | ||
| t.Errorf("failed to encode fake A2A response: %v", err) | ||
| } | ||
| })) | ||
| t.Cleanup(b.server.Close) | ||
| return b | ||
| } | ||
|
|
||
| // authRoundTripper injects a fixed Authorization header into every outgoing request. | ||
| type authRoundTripper struct { | ||
| base http.RoundTripper | ||
| token string | ||
| } | ||
|
|
||
| func (a *authRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { | ||
| r = r.Clone(r.Context()) | ||
| r.Header.Set("Authorization", "Bearer "+a.token) | ||
| return a.base.RoundTrip(r) | ||
| } | ||
|
|
||
| // TestInvokeAgent_AuthPropagation exercises the full MCP HTTP stack: | ||
| // the MCP client sends a request with an Authorization header, the handler | ||
| // recovers the auth session from RequestExtra, and the A2A backend receives | ||
| // the token produced by UpstreamAuth. | ||
| func TestInvokeAgent_AuthPropagation(t *testing.T) { | ||
| // Fake A2A backend — records the Authorization header it receives. | ||
| backend := newA2ABackend(t) | ||
|
|
||
| authProvider := &fakeAuthProvider{session: &fakeSession{}} | ||
|
|
||
| // Real MCP handler (kubeClient is nil; invoke_agent does not use it). | ||
| mcpHandler, err := NewMCPHandler(nil, backend.server.URL, authProvider, 5*time.Second) | ||
| require.NoError(t, err) | ||
|
|
||
| mcpServer := httptest.NewServer(mcpHandler) | ||
| t.Cleanup(mcpServer.Close) | ||
|
|
||
| // MCP client whose HTTP transport injects an Authorization header on every request. | ||
| transport := &mcpsdk.StreamableClientTransport{ | ||
| Endpoint: mcpServer.URL, | ||
| HTTPClient: &http.Client{ | ||
| Transport: &authRoundTripper{ | ||
| base: http.DefaultTransport, | ||
| token: "test-token", | ||
| }, | ||
| }, | ||
| DisableStandaloneSSE: true, | ||
| } | ||
|
|
||
| ctx := context.Background() | ||
| cs, err := mcpsdk.NewClient(&mcpsdk.Implementation{Name: "test", Version: "1.0"}, nil). | ||
| Connect(ctx, transport, nil) | ||
| require.NoError(t, err) | ||
| t.Cleanup(func() { cs.Close() }) | ||
|
|
||
| result, err := cs.CallTool(ctx, &mcpsdk.CallToolParams{ | ||
| Name: "invoke_agent", | ||
| Arguments: map[string]any{ | ||
| "agent": "default/test-agent", | ||
| "task": "say hello", | ||
| }, | ||
| }) | ||
| require.NoError(t, err) | ||
| assert.False(t, result.IsError, "expected successful tool result, got: %v", result.Content) | ||
| assert.Equal(t, "Bearer upstream-token", backend.getLastAuthHeader(), "A2A backend should receive the token produced by UpstreamAuth") | ||
| } | ||
|
|
||
| // TestInvokeAgent_NoAuthPropagationWithoutHeader verifies that when the MCP | ||
| // client sends no Authorization header, no Authorization header is | ||
| // propagated to the A2A backend. | ||
| func TestInvokeAgent_NoAuthPropagationWithoutHeader(t *testing.T) { | ||
| backend := newA2ABackend(t) | ||
|
|
||
| authProvider := &fakeAuthProvider{session: &fakeSession{}} | ||
|
|
||
| mcpHandler, err := NewMCPHandler(nil, backend.server.URL, authProvider, 5*time.Second) | ||
| require.NoError(t, err) | ||
|
|
||
| mcpServer := httptest.NewServer(mcpHandler) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests were passing without your re-authenticate change if you add the auth middleware
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @supreme-gg-gg - thanks for pointing this out. You're completely right - the original root cause was wrong. I originally ran into this when trying to set up access to the agents MCP via agentgateway but I think the actual underlying issue in my case was most likely #1855 since the token used to access the MCP gateway domain (which is then passed on to the container) is not valid for accessing KAgent controller (via This code was my first attempt to fix this and I thought the tests proved the issue but as you correctly point out, the tests didn't attach the auth middleware, which meant they were testing a code path that doesn't exist in reality and, were passing (or rather failing 😄) for the wrong reasons. I'm going to go ahead and close this out... |
||
| t.Cleanup(mcpServer.Close) | ||
|
|
||
| // No custom transport — requests carry no Authorization header. | ||
| transport := &mcpsdk.StreamableClientTransport{ | ||
| Endpoint: mcpServer.URL, | ||
| DisableStandaloneSSE: true, | ||
| } | ||
|
|
||
| ctx := context.Background() | ||
| cs, err := mcpsdk.NewClient(&mcpsdk.Implementation{Name: "test", Version: "1.0"}, nil). | ||
| Connect(ctx, transport, nil) | ||
| require.NoError(t, err) | ||
| t.Cleanup(func() { cs.Close() }) | ||
|
|
||
| result, err := cs.CallTool(ctx, &mcpsdk.CallToolParams{ | ||
| Name: "invoke_agent", | ||
| Arguments: map[string]any{ | ||
| "agent": "default/test-agent", | ||
| "task": "say hello", | ||
| }, | ||
| }) | ||
| require.NoError(t, err) | ||
| assert.False(t, result.IsError) | ||
| assert.Empty(t, backend.getLastAuthHeader(), "A2A backend should receive no Authorization header when the client sends none") | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RequestExtradoesn't carry query params