-
Notifications
You must be signed in to change notification settings - Fork 558
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
Open
onematchfox
wants to merge
1
commit into
kagent-dev:main
Choose a base branch
from
onematchfox:fix-mcp-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+185
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
||
| 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") | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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