-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathui_resources_test.go
More file actions
168 lines (140 loc) · 5.24 KB
/
Copy pathui_resources_test.go
File metadata and controls
168 lines (140 loc) · 5.24 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package github
import (
"context"
"slices"
"testing"
"github.com/github/github-mcp-server/pkg/inventory"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestRegisterUIResources_ReadableViaClient verifies that each UI resource URI
// advertised by an MCP App-enabled tool (e.g. issue_write, create_pull_request,
// get_me) actually resolves to a registered resource on the server.
//
// Regression test for the "Error loading MCP App: MPC -32002: Resource not
// found" bug reported in issue #2467, where the HTTP/remote server returned a
// resource URI in the tool's _meta.ui block but never registered the matching
// resource — so the follow-up resources/read call from the client failed.
func TestRegisterUIResources_ReadableViaClient(t *testing.T) {
t.Parallel()
if !UIAssetsAvailable() {
t.Skip("UI assets not built; run script/build-ui to enable this test")
}
srv := mcp.NewServer(&mcp.Implementation{Name: "test", Version: "0.0.1"}, nil)
RegisterUIResources(srv, false)
// Connect an in-memory client/server pair and read each advertised URI.
st, ct := mcp.NewInMemoryTransports()
type clientResult struct {
session *mcp.ClientSession
err error
}
clientCh := make(chan clientResult, 1)
go func() {
client := mcp.NewClient(&mcp.Implementation{Name: "test-client"}, nil)
cs, err := client.Connect(context.Background(), ct, nil)
clientCh <- clientResult{session: cs, err: err}
}()
ss, err := srv.Connect(context.Background(), st, nil)
require.NoError(t, err)
t.Cleanup(func() { _ = ss.Close() })
got := <-clientCh
require.NoError(t, got.err)
t.Cleanup(func() { _ = got.session.Close() })
uris := []string{
GetMeUIResourceURI,
IssueWriteUIResourceURI,
PullRequestWriteUIResourceURI,
PullRequestEditUIResourceURI,
}
for _, uri := range uris {
t.Run(uri, func(t *testing.T) {
res, err := got.session.ReadResource(context.Background(), &mcp.ReadResourceParams{URI: uri})
require.NoError(t, err, "resource %s should be registered (got -32002 means it isn't)", uri)
require.NotNil(t, res)
require.NotEmpty(t, res.Contents)
assert.Equal(t, uri, res.Contents[0].URI)
assert.Equal(t, MCPAppMIMEType, res.Contents[0].MIMEType)
assert.NotEmpty(t, res.Contents[0].Text, "UI resource should return HTML body")
})
}
}
// TestNewMCPServer_RegistersUIResources verifies that NewMCPServer — the
// shared constructor used by both the stdio and HTTP entry points — registers
// the UI resources when UI assets are embedded. Previously this registration
// only happened in the stdio bootstrap, so remote/HTTP clients hit -32002.
func TestNewMCPServer_RegistersUIResources(t *testing.T) {
t.Parallel()
if !UIAssetsAvailable() {
t.Skip("UI assets not built; run script/build-ui to enable this test")
}
srv, err := NewMCPServer(context.Background(), &MCPServerConfig{
Version: "test",
Translator: stubTranslator,
}, stubDeps{t: stubTranslator}, mustEmptyInventory(t))
require.NoError(t, err)
st, ct := mcp.NewInMemoryTransports()
type clientResult struct {
session *mcp.ClientSession
err error
}
clientCh := make(chan clientResult, 1)
go func() {
client := mcp.NewClient(&mcp.Implementation{Name: "test-client"}, nil)
cs, err := client.Connect(context.Background(), ct, nil)
clientCh <- clientResult{session: cs, err: err}
}()
ss, err := srv.Connect(context.Background(), st, nil)
require.NoError(t, err)
t.Cleanup(func() { _ = ss.Close() })
got := <-clientCh
require.NoError(t, got.err)
t.Cleanup(func() { _ = got.session.Close() })
res, err := got.session.ReadResource(context.Background(), &mcp.ReadResourceParams{URI: IssueWriteUIResourceURI})
require.NoError(t, err)
require.NotNil(t, res)
require.NotEmpty(t, res.Contents)
assert.Equal(t, MCPAppMIMEType, res.Contents[0].MIMEType)
}
func TestRegisterUIResources_ReadOnlySkipsWriteResources(t *testing.T) {
t.Parallel()
srv := mcp.NewServer(&mcp.Implementation{Name: "test", Version: "0.0.1"}, nil)
RegisterUIResources(srv, true)
st, ct := mcp.NewInMemoryTransports()
type clientResult struct {
res *mcp.ListResourcesResult
err error
}
clientCh := make(chan clientResult, 1)
go func() {
client := mcp.NewClient(&mcp.Implementation{Name: "test-client"}, nil)
cs, err := client.Connect(context.Background(), ct, nil)
if err != nil {
clientCh <- clientResult{err: err}
return
}
defer func() { _ = cs.Close() }()
res, err := cs.ListResources(context.Background(), nil)
clientCh <- clientResult{res: res, err: err}
}()
ss, err := srv.Connect(context.Background(), st, nil)
require.NoError(t, err)
t.Cleanup(func() { _ = ss.Close() })
got := <-clientCh
require.NoError(t, got.err)
require.NotNil(t, got.res)
names := make([]string, 0, len(got.res.Resources))
for _, res := range got.res.Resources {
names = append(names, res.Name)
}
slices.Sort(names)
assert.Equal(t, []string{"get_me_ui"}, names)
}
// mustEmptyInventory builds an empty inventory for tests that only care about
// resources/prompts registered outside the inventory (such as the UI resources).
func mustEmptyInventory(t *testing.T) *inventory.Inventory {
t.Helper()
inv, err := NewInventory(stubTranslator).WithToolsets([]string{}).Build()
require.NoError(t, err)
return inv
}