-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathhandler_test.go
More file actions
107 lines (97 loc) · 3.1 KB
/
handler_test.go
File metadata and controls
107 lines (97 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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/http/headers"
"github.com/github/github-mcp-server/pkg/inventory"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/stretchr/testify/assert"
)
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
headers map[string]string
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.WithToolset(ctx, "repos")
},
expectedTools: []string{"get_file_contents", "create_repository"},
},
{
name: "context toolset takes precedence over header",
contextSetup: func(ctx context.Context) context.Context {
return ghcontext.WithToolset(ctx, "repos")
},
headers: map[string]string{
headers.MCPToolsetsHeader: "issues",
},
expectedTools: []string{"get_file_contents", "create_repository"},
},
{
name: "tools are additive with toolsets",
contextSetup: func(ctx context.Context) context.Context { return ctx },
headers: map[string]string{
headers.MCPToolsetsHeader: "repos",
headers.MCPToolsHeader: "list_issues",
},
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)
for k, v := range tt.headers {
req.Header.Set(k, v)
}
req = req.WithContext(tt.contextSetup(req.Context()))
builder := inventory.NewBuilder().
SetTools(tools).
WithToolsets([]string{"all"})
builder = InventoryFiltersForRequest(req, builder)
inv := builder.Build()
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)
})
}
}