-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add readonly and toolset request handlers #1858
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
Merged
+326
−41
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c35ab93
add readonly and toolset support
kerobbi e91a875
address feedback
kerobbi d9e9bae
forgotten files
kerobbi 9320ca0
remove redundant checks in WithRequestConfig
kerobbi d45c414
move middleware in RegisterRoutes
kerobbi 854bbd2
improve comment and add TestParseCommaSeparated
kerobbi 622599b
Merge branch 'http-stack-2' into http-routes
kerobbi 283b9ba
fix broken TestInventoryFiltersForRequest
kerobbi 53dfd4c
parse X-MCP-Tools into ctx
kerobbi a92b77b
clean up TestInventoryFiltersForRequest
kerobbi 1d7e656
Pass context to handler, but use request context for per-request data
omgitsads 7b7b45d
Merge branch 'http-routes' of https://github.com/github/github-mcp-se…
omgitsads d518f49
Pass through the context in MCP server creation functions
omgitsads 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
Some comments aren't visible on the classic Files Changed page.
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,51 @@ | ||
| package context | ||
|
|
||
| import "context" | ||
|
|
||
| // readonlyCtxKey is a context key for read-only mode | ||
| type readonlyCtxKey struct{} | ||
|
|
||
| // WithReadonly adds read-only mode state to the context | ||
| func WithReadonly(ctx context.Context, enabled bool) context.Context { | ||
| return context.WithValue(ctx, readonlyCtxKey{}, enabled) | ||
| } | ||
|
|
||
| // IsReadonly retrieves the read-only mode state from the context | ||
| func IsReadonly(ctx context.Context) bool { | ||
| if enabled, ok := ctx.Value(readonlyCtxKey{}).(bool); ok { | ||
| return enabled | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // toolsetsCtxKey is a context key for the active toolsets | ||
| type toolsetsCtxKey struct{} | ||
|
|
||
| // WithToolsets adds the active toolsets to the context | ||
| func WithToolsets(ctx context.Context, toolsets []string) context.Context { | ||
| return context.WithValue(ctx, toolsetsCtxKey{}, toolsets) | ||
| } | ||
|
|
||
| // GetToolsets retrieves the active toolsets from the context | ||
| func GetToolsets(ctx context.Context) []string { | ||
| if toolsets, ok := ctx.Value(toolsetsCtxKey{}).([]string); ok { | ||
| return toolsets | ||
| } | ||
| return nil | ||
| } | ||
kerobbi marked this conversation as resolved.
Show resolved
Hide resolved
kerobbi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // toolsCtxKey is a context key for tools | ||
| type toolsCtxKey struct{} | ||
|
|
||
| // WithTools adds the tools to the context | ||
| func WithTools(ctx context.Context, tools []string) context.Context { | ||
| return context.WithValue(ctx, toolsCtxKey{}, tools) | ||
| } | ||
|
|
||
| // GetTools retrieves the tools from the context | ||
| func GetTools(ctx context.Context) []string { | ||
| if tools, ok := ctx.Value(toolsCtxKey{}).([]string); ok { | ||
| return tools | ||
| } | ||
| return 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
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,101 @@ | ||
| package http | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| ghcontext "github.com/github/github-mcp-server/pkg/context" | ||
| "github.com/github/github-mcp-server/pkg/inventory" | ||
| "github.com/modelcontextprotocol/go-sdk/mcp" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func mockTool(name, toolsetID string, readOnly bool) inventory.ServerTool { | ||
| return inventory.ServerTool{ | ||
| Tool: mcp.Tool{ | ||
| Name: name, | ||
| Annotations: &mcp.ToolAnnotations{ReadOnlyHint: readOnly}, | ||
| }, | ||
| Toolset: inventory.ToolsetMetadata{ | ||
| ID: inventory.ToolsetID(toolsetID), | ||
| Description: "Test: " + toolsetID, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func TestInventoryFiltersForRequest(t *testing.T) { | ||
| tools := []inventory.ServerTool{ | ||
| mockTool("get_file_contents", "repos", true), | ||
| mockTool("create_repository", "repos", false), | ||
| mockTool("list_issues", "issues", true), | ||
| mockTool("issue_write", "issues", false), | ||
| } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| contextSetup func(context.Context) context.Context | ||
| expectedTools []string | ||
| }{ | ||
| { | ||
| name: "no filters applies defaults", | ||
| contextSetup: func(ctx context.Context) context.Context { return ctx }, | ||
| expectedTools: []string{"get_file_contents", "create_repository", "list_issues", "issue_write"}, | ||
| }, | ||
| { | ||
| name: "readonly from context filters write tools", | ||
| contextSetup: func(ctx context.Context) context.Context { | ||
| return ghcontext.WithReadonly(ctx, true) | ||
| }, | ||
| expectedTools: []string{"get_file_contents", "list_issues"}, | ||
| }, | ||
| { | ||
| name: "toolset from context filters to toolset", | ||
| contextSetup: func(ctx context.Context) context.Context { | ||
| return ghcontext.WithToolsets(ctx, []string{"repos"}) | ||
| }, | ||
| expectedTools: []string{"get_file_contents", "create_repository"}, | ||
| }, | ||
| { | ||
| name: "tools alone clears default toolsets", | ||
| contextSetup: func(ctx context.Context) context.Context { | ||
| return ghcontext.WithTools(ctx, []string{"list_issues"}) | ||
| }, | ||
| expectedTools: []string{"list_issues"}, | ||
| }, | ||
| { | ||
| name: "tools are additive with toolsets", | ||
| contextSetup: func(ctx context.Context) context.Context { | ||
| ctx = ghcontext.WithToolsets(ctx, []string{"repos"}) | ||
| ctx = ghcontext.WithTools(ctx, []string{"list_issues"}) | ||
| return ctx | ||
| }, | ||
| expectedTools: []string{"get_file_contents", "create_repository", "list_issues"}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
| req = req.WithContext(tt.contextSetup(req.Context())) | ||
|
|
||
| builder := inventory.NewBuilder(). | ||
| SetTools(tools). | ||
| WithToolsets([]string{"all"}) | ||
|
|
||
| builder = InventoryFiltersForRequest(req, builder) | ||
| inv, err := builder.Build() | ||
| require.NoError(t, err) | ||
|
|
||
| available := inv.AvailableTools(context.Background()) | ||
| toolNames := make([]string, len(available)) | ||
| for i, tool := range available { | ||
| toolNames[i] = tool.Tool.Name | ||
| } | ||
|
|
||
| assert.ElementsMatch(t, tt.expectedTools, toolNames) | ||
| }) | ||
| } | ||
| } |
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,21 @@ | ||
| package headers | ||
|
|
||
| import "strings" | ||
|
|
||
| // ParseCommaSeparated splits a header value by comma, trims whitespace, | ||
| // and filters out empty values | ||
| func ParseCommaSeparated(value string) []string { | ||
| if value == "" { | ||
| return []string{} | ||
| } | ||
|
|
||
| parts := strings.Split(value, ",") | ||
| result := make([]string, 0, len(parts)) | ||
| for _, p := range parts { | ||
| trimmed := strings.TrimSpace(p) | ||
| if trimmed != "" { | ||
| result = append(result, trimmed) | ||
| } | ||
| } | ||
| return result | ||
| } | ||
kerobbi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
WithToolsetsfunction stores a slice directly in the context without copying it. If the caller modifies the slice after callingWithToolsets, it could lead to unexpected behavior or race conditions since the same slice reference is stored. Consider documenting that the slice should not be modified after being passed, or make a defensive copy of the slice before storing it in the context.