Skip to content

Commit 4e33aa4

Browse files
committed
polish code
Signed-off-by: Huabing Zhao <[email protected]>
1 parent 24fd0ac commit 4e33aa4

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

api/v1alpha1/mcp_route.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ type MCPRouteAuthorizationRule struct {
268268
Action egv1a1.AuthorizationAction `json:"action"`
269269
}
270270

271+
// MCPAuthorizationTarget defines the target of an authorization rule.
271272
type MCPAuthorizationTarget struct {
272273
// Tools defines the list of tools this rule applies to.
273274
//
@@ -277,6 +278,7 @@ type MCPAuthorizationTarget struct {
277278
Tools []ToolCall `json:"tools"`
278279
}
279280

281+
// MCPAuthorizationSource defines the source of an authorization rule.
280282
type MCPAuthorizationSource struct {
281283
// JWTSource defines the JWT scopes required for this rule to match.
282284
//
@@ -286,6 +288,7 @@ type MCPAuthorizationSource struct {
286288
// TODO: JWTSource can be optional in the future when we support more source types.
287289
}
288290

291+
// JWTSource defines the MCP authorization source for JWT tokens.
289292
type JWTSource struct {
290293
// Scopes defines the list of JWT scopes required for the rule.
291294
// If multiple scopes are specified, all scopes must be present in the JWT for the rule to match.
@@ -298,6 +301,7 @@ type JWTSource struct {
298301
// TODO : we can add more fields in the future, e.g., audiences, claims, etc.
299302
}
300303

304+
// ToolCall represents a tool call in the MCP authorization target.
301305
type ToolCall struct {
302306
// BackendName is the name of the backend this tool belongs to.
303307
//

internal/mcpproxy/authorization.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ import (
1414
"strings"
1515

1616
"github.com/golang-jwt/jwt/v5"
17+
"k8s.io/apimachinery/pkg/util/sets"
1718

1819
"github.com/envoyproxy/ai-gateway/internal/filterapi"
1920
)
2021

22+
// authorizeRequest authorizes the request based on the given MCPRouteAuthorization configuration.
2123
func (m *MCPProxy) authorizeRequest(authorization *filterapi.MCPRouteAuthorization, headers http.Header, backendName, toolName string, argments any) bool {
24+
if authorization == nil {
25+
return true
26+
}
27+
2228
defaultAction := authorization.DefaultAction == filterapi.AuthorizationActionAllow
2329

2430
// If there are no rules, return the default action.
@@ -28,6 +34,8 @@ func (m *MCPProxy) authorizeRequest(authorization *filterapi.MCPRouteAuthorizati
2834

2935
// If the rules are defined, a valid bearer token is required.
3036
token, err := bearerToken(headers.Get("Authorization"))
37+
// This is just a sanity check. The actual JWT verification is performed by Envoy before reaching here, and the token
38+
// should always be present and valid.
3139
if err != nil {
3240
m.l.Info("missing or invalid bearer token", slog.String("error", err.Error()))
3341
return false
@@ -40,10 +48,7 @@ func (m *MCPProxy) authorizeRequest(authorization *filterapi.MCPRouteAuthorizati
4048
return false
4149
}
4250

43-
scopeSet := make(map[string]struct{})
44-
for _, scope := range extractScopes(claims) {
45-
scopeSet[scope] = struct{}{}
46-
}
51+
scopeSet := sets.New[string](extractScopes(claims)...)
4752

4853
for _, rule := range authorization.Rules {
4954
var args map[string]any
@@ -52,7 +57,7 @@ func (m *MCPProxy) authorizeRequest(authorization *filterapi.MCPRouteAuthorizati
5257
args = cast
5358
}
5459
}
55-
if !toolMatches(args, filterapi.ToolCall{BackendName: backendName, ToolName: toolName}, rule.Target.Tools) {
60+
if !m.toolMatches(filterapi.ToolCall{BackendName: backendName, ToolName: toolName}, rule.Target.Tools, args) {
5661
continue
5762
}
5863
if scopesSatisfied(scopeSet, rule.Source.JWTSource.Scopes) {
@@ -104,7 +109,7 @@ func extractScopes(claims jwt.MapClaims) []string {
104109
}
105110
}
106111

107-
func toolMatches(args map[string]any, target filterapi.ToolCall, tools []filterapi.ToolCall) bool {
112+
func (m *MCPProxy) toolMatches(target filterapi.ToolCall, tools []filterapi.ToolCall, args map[string]any) bool {
108113
if len(tools) == 0 {
109114
return true
110115
}
@@ -128,6 +133,7 @@ func toolMatches(args map[string]any, target filterapi.ToolCall, tools []filtera
128133
}
129134
re, err := regexp.Compile(pattern)
130135
if err != nil {
136+
m.l.Error("invalid argument regex pattern", slog.String("pattern", pattern), slog.String("error", err.Error()))
131137
allMatch = false
132138
break
133139
}
@@ -137,6 +143,7 @@ func toolMatches(args map[string]any, target filterapi.ToolCall, tools []filtera
137143
} else {
138144
jsonVal, err := json.Marshal(rawVal)
139145
if err != nil {
146+
m.l.Error("failed to marshal argument value to json", slog.String("key", key), slog.String("error", err.Error()))
140147
allMatch = false
141148
break
142149
}
@@ -155,7 +162,7 @@ func toolMatches(args map[string]any, target filterapi.ToolCall, tools []filtera
155162
return false
156163
}
157164

158-
func scopesSatisfied(have map[string]struct{}, required []string) bool {
165+
func scopesSatisfied(have sets.Set[string], required []string) bool {
159166
if len(required) == 0 {
160167
return true
161168
}

manifests/charts/ai-gateway-crds-helm/templates/aigateway.envoyproxy.io_mcproutes.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,8 @@ spec:
643643
description: Tools defines the list of tools this
644644
rule applies to.
645645
items:
646+
description: ToolCall represents a tool call in
647+
the MCP authorization target.
646648
properties:
647649
arguments:
648650
additionalProperties:

site/docs/api/api.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@ JWKS defines how to obtain JSON Web Key Sets (JWKS) either from a remote HTTP/HT
14561456
**Appears in:**
14571457
- [MCPAuthorizationSource](#mcpauthorizationsource)
14581458

1459-
1459+
JWTSource defines the MCP authorization source for JWT tokens.
14601460

14611461
##### Fields
14621462

@@ -1547,7 +1547,7 @@ LLMRequestCostType specifies the type of the LLMRequestCost.
15471547
**Appears in:**
15481548
- [MCPRouteAuthorizationRule](#mcprouteauthorizationrule)
15491549

1550-
1550+
MCPAuthorizationSource defines the source of an authorization rule.
15511551

15521552
##### Fields
15531553

@@ -1568,7 +1568,7 @@ LLMRequestCostType specifies the type of the LLMRequestCost.
15681568
**Appears in:**
15691569
- [MCPRouteAuthorizationRule](#mcprouteauthorizationrule)
15701570

1571-
1571+
MCPAuthorizationTarget defines the target of an authorization rule.
15721572

15731573
##### Fields
15741574

@@ -1969,7 +1969,7 @@ References:
19691969
**Appears in:**
19701970
- [MCPAuthorizationTarget](#mcpauthorizationtarget)
19711971

1972-
1972+
ToolCall represents a tool call in the MCP authorization target.
19731973

19741974
##### Fields
19751975

0 commit comments

Comments
 (0)