Skip to content

Commit 53dfd4c

Browse files
committed
parse X-MCP-Tools into ctx
1 parent 283b9ba commit 53dfd4c

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

pkg/context/request.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,19 @@ func GetToolsets(ctx context.Context) []string {
3333
}
3434
return nil
3535
}
36+
37+
// toolsCtxKey is a context key for tools
38+
type toolsCtxKey struct{}
39+
40+
// WithTools adds the tools to the context
41+
func WithTools(ctx context.Context, tools []string) context.Context {
42+
return context.WithValue(ctx, toolsCtxKey{}, tools)
43+
}
44+
45+
// GetTools retrieves the tools from the context
46+
func GetTools(ctx context.Context) []string {
47+
if tools, ok := ctx.Value(toolsCtxKey{}).([]string); ok {
48+
return tools
49+
}
50+
return nil
51+
}

pkg/http/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func InventoryFiltersForRequest(r *http.Request, builder *inventory.Builder) *in
173173
builder = builder.WithToolsets(toolsets)
174174
}
175175

176-
if tools := headers.ParseCommaSeparated(r.Header.Get(headers.MCPToolsHeader)); len(tools) > 0 {
176+
if tools := ghcontext.GetTools(ctx); len(tools) > 0 {
177177
if len(ghcontext.GetToolsets(ctx)) == 0 {
178178
builder = builder.WithToolsets([]string{})
179179
}

pkg/http/handler_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@ func TestInventoryFiltersForRequest(t *testing.T) {
7373
{
7474
name: "tools are additive with toolsets",
7575
contextSetup: func(ctx context.Context) context.Context {
76-
return ghcontext.WithToolsets(ctx, []string{"repos"})
77-
},
78-
headers: map[string]string{
79-
headers.MCPToolsHeader: "list_issues",
76+
ctx = ghcontext.WithToolsets(ctx, []string{"repos"})
77+
ctx = ghcontext.WithTools(ctx, []string{"list_issues"})
78+
return ctx
8079
},
8180
expectedTools: []string{"get_file_contents", "create_repository", "list_issues"},
8281
},

pkg/http/middleware/request_config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ func WithRequestConfig(next http.Handler) http.Handler {
2222
ctx = ghcontext.WithToolsets(ctx, toolsets)
2323
}
2424

25+
if tools := headers.ParseCommaSeparated(r.Header.Get(headers.MCPToolsHeader)); len(tools) > 0 {
26+
ctx = ghcontext.WithTools(ctx, tools)
27+
}
28+
2529
next.ServeHTTP(w, r.WithContext(ctx))
2630
})
2731
}

0 commit comments

Comments
 (0)