-
-
Notifications
You must be signed in to change notification settings - Fork 169
feat(gateway): add support for authorization headers in MCP server co… #298
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
x22x22
wants to merge
4
commits into
AmoyLab:main
Choose a base branch
from
x22x22:feat/mcp-headers-auth-sse-streamable
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f26969e
feat(gateway): add support for authorization headers in MCP server co…
x22x22 188e40c
Update web/src/pages/gateway/components/MCPServersConfig.tsx
x22x22 99e3cbb
Update internal/core/state/state.go
x22x22 2058c27
feat: add unit tests for header rendering and state configuration
x22x22 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,3 +75,6 @@ bin | |
|
|
||
| .history/* | ||
| coverage.html | ||
| .gobuildcache | ||
| .gocache | ||
| .gopath | ||
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
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,26 @@ | ||
| package mcpproxy | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/amoylab/unla/internal/template" | ||
| ) | ||
|
|
||
| func renderHeaders(headers map[string]string, tmplCtx *template.Context) (map[string]string, error) { | ||
| if len(headers) == 0 { | ||
| return nil, nil | ||
| } | ||
| if tmplCtx == nil { | ||
| tmplCtx = template.NewContext() | ||
| } | ||
|
|
||
| rendered := make(map[string]string, len(headers)) | ||
| for k, v := range headers { | ||
| out, err := template.RenderTemplate(v, tmplCtx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to render header template: %w", err) | ||
| } | ||
| rendered[k] = out | ||
| } | ||
| return rendered, nil | ||
| } |
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,46 @@ | ||
| package mcpproxy | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/amoylab/unla/internal/template" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestRenderHeaders_Empty(t *testing.T) { | ||
| headers, err := renderHeaders(nil, nil) | ||
| assert.NoError(t, err) | ||
| assert.Nil(t, headers) | ||
|
|
||
| headers, err = renderHeaders(map[string]string{}, nil) | ||
| assert.NoError(t, err) | ||
| assert.Nil(t, headers) | ||
| } | ||
|
|
||
| func TestRenderHeaders_WithTemplateContext(t *testing.T) { | ||
| tmplCtx := template.NewContext() | ||
| tmplCtx.Env = func(key string) string { | ||
| if key == "MCP_AUTH_TOKEN" { | ||
| return "token" | ||
| } | ||
| return "" | ||
| } | ||
| tmplCtx.Request.Headers["X-Req"] = "req" | ||
|
|
||
| headers, err := renderHeaders(map[string]string{ | ||
| "Authorization": "Bearer {{ env \"MCP_AUTH_TOKEN\" }}", | ||
| "X-Req": "{{ index .Request.Headers \"X-Req\" }}", | ||
| }, tmplCtx) | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.Equal(t, "Bearer token", headers["Authorization"]) | ||
| assert.Equal(t, "req", headers["X-Req"]) | ||
| } | ||
|
|
||
| func TestRenderHeaders_InvalidTemplate(t *testing.T) { | ||
| headers, err := renderHeaders(map[string]string{ | ||
| "X-Bad": "{{ .Request.Headers", | ||
| }, template.NewContext()) | ||
| assert.Error(t, err) | ||
| assert.Nil(t, headers) | ||
| } |
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
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,58 @@ | ||
| package state | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/amoylab/unla/internal/common/cnst" | ||
| "github.com/amoylab/unla/internal/common/config" | ||
| "github.com/stretchr/testify/assert" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| func TestBuildStateFromConfig_ReusesTransportWhenHeadersUnchanged(t *testing.T) { | ||
| ctx := context.Background() | ||
| logger := zap.NewNop() | ||
|
|
||
| cfg := buildMCPConfigWithHeaders(map[string]string{"Authorization": "Bearer a"}) | ||
| oldState, err := BuildStateFromConfig(ctx, []*config.MCPConfig{cfg}, nil, logger) | ||
| assert.NoError(t, err) | ||
|
|
||
| newState, err := BuildStateFromConfig(ctx, []*config.MCPConfig{cfg}, oldState, logger) | ||
| assert.NoError(t, err) | ||
|
|
||
| assert.Same(t, oldState.GetTransport("/m"), newState.GetTransport("/m")) | ||
| } | ||
|
|
||
| func TestBuildStateFromConfig_RebuildsTransportWhenHeadersChange(t *testing.T) { | ||
| ctx := context.Background() | ||
| logger := zap.NewNop() | ||
|
|
||
| oldCfg := buildMCPConfigWithHeaders(map[string]string{"Authorization": "Bearer a"}) | ||
| oldState, err := BuildStateFromConfig(ctx, []*config.MCPConfig{oldCfg}, nil, logger) | ||
| assert.NoError(t, err) | ||
|
|
||
| newCfg := buildMCPConfigWithHeaders(map[string]string{"Authorization": "Bearer b"}) | ||
| newState, err := BuildStateFromConfig(ctx, []*config.MCPConfig{newCfg}, oldState, logger) | ||
| assert.NoError(t, err) | ||
|
|
||
| assert.NotSame(t, oldState.GetTransport("/m"), newState.GetTransport("/m")) | ||
| } | ||
|
|
||
| func buildMCPConfigWithHeaders(headers map[string]string) *config.MCPConfig { | ||
| return &config.MCPConfig{ | ||
| Name: "c1", | ||
| Tenant: "t1", | ||
| Routers: []config.RouterConfig{{ | ||
| Server: "ms1", | ||
| Prefix: "/m", | ||
| }}, | ||
| McpServers: []config.MCPServerConfig{{ | ||
| Type: cnst.BackendProtoSSE.String(), | ||
| Name: "ms1", | ||
| URL: "http://127.0.0.1:9/", | ||
| Policy: cnst.PolicyOnDemand, | ||
| Headers: headers, | ||
| }}, | ||
| } | ||
| } |
Oops, something went wrong.
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.
The refactored CallTool method now creates template context only when the transport is not running. However, if the transport is already running (e.g., with PolicyOnStart), any dynamic header templates that depend on the current tool arguments won't be re-evaluated. This means the first request's template values will be reused for all subsequent requests. Consider whether this is the intended behavior or if headers should be re-rendered per request.
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.
@copilot open a new pull request to apply changes based on this feedback