-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathmcp-composition-auth.e2e.test.ts
More file actions
270 lines (217 loc) · 7.47 KB
/
mcp-composition-auth.e2e.test.ts
File metadata and controls
270 lines (217 loc) · 7.47 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { x } from 'tinyexec';
import {
createMCPRequestBody,
parseMCPResponse,
waitForMcpEndpoint,
killPort,
startStorybook,
stopStorybook,
} from './helpers';
const PORT = 6008;
const MCP_ENDPOINT = `http://localhost:${PORT}/mcp`;
const WELL_KNOWN_ENDPOINT = `http://localhost:${PORT}/.well-known/oauth-protected-resource`;
const STARTUP_TIMEOUT = 30_000;
let storybookProcess: ReturnType<typeof x> | null = null;
async function mcpRequest(method: string, params: any = {}, token?: string) {
const headers: Record<string, string> = {
'Content-Type': 'application/json',
};
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
return fetch(MCP_ENDPOINT, {
method: 'POST',
headers,
body: JSON.stringify(createMCPRequestBody(method, params)),
});
}
describe('MCP Composition Auth E2E Tests', () => {
beforeAll(async () => {
await killPort(PORT);
storybookProcess = startStorybook('.storybook-composition-auth', PORT);
await waitForMcpEndpoint(MCP_ENDPOINT, { acceptStatuses: [401] });
}, STARTUP_TIMEOUT);
afterAll(async () => {
await stopStorybook(storybookProcess);
storybookProcess = null;
});
describe('OAuth Discovery', () => {
it('should expose .well-known/oauth-protected-resource for private refs', async () => {
const response = await fetch(WELL_KNOWN_ENDPOINT);
expect(response.status).toBe(200);
const metadata = await response.json();
expect(metadata).toMatchInlineSnapshot(`
{
"authorization_servers": [
"https://www.chromatic.com",
],
"resource": "http://localhost:6008/mcp",
"scopes_supported": [
"storybook:read",
"project:read",
],
}
`);
});
it('should point resource to the local MCP endpoint', async () => {
const response = await fetch(WELL_KNOWN_ENDPOINT);
const metadata = await response.json();
expect(metadata.resource).toBe(`http://localhost:${PORT}/mcp`);
});
});
describe('Auth Gate', () => {
it('should return 401 without Bearer token', async () => {
const response = await mcpRequest('tools/list');
expect(response.status).toBe(401);
});
it('should include WWW-Authenticate header in 401 response', async () => {
const response = await mcpRequest('tools/list');
expect(response.status).toBe(401);
expect(response.headers.get('www-authenticate')).toMatchInlineSnapshot(
`"Bearer error="unauthorized", error_description="Authorization needed for composed Storybooks", resource_metadata="http://localhost:6008/.well-known/oauth-protected-resource""`,
);
});
it('should reject requests without valid token', async () => {
const response = await mcpRequest('tools/list', {}, 'invalid-token');
// With an invalid token, the server accepts the request (token is present)
// but the remote source will fail to fetch
expect(response.status).toBe(200);
});
});
describe('Multi-Source with Auth', () => {
it('should list tools with storybookId parameter when authenticated', async () => {
// Use a dummy token — the local source should still work
const response = await mcpRequest('tools/list', {}, 'dummy-token');
const data = await parseMCPResponse(response);
const getDocTool = data.result.tools.find((t: any) => t.name === 'get-documentation');
expect(getDocTool).toBeDefined();
expect(getDocTool.inputSchema.properties).toHaveProperty('storybookId');
});
it('should return 401 with WWW-Authenticate for list-all-documentation with invalid token', async () => {
const response = await mcpRequest(
'tools/call',
{ name: 'list-all-documentation', arguments: {} },
'dummy-token',
);
// The remote source manifest fetch fails with 401,
// which propagates as an HTTP 401 to trigger re-authentication
expect(response.status).toBe(401);
expect(response.headers.get('www-authenticate')).toContain('resource_metadata=');
});
it('should fetch local component documentation with storybookId', async () => {
const response = await mcpRequest(
'tools/call',
{
name: 'get-documentation',
arguments: { id: 'example-button', storybookId: 'local' },
},
'dummy-token',
);
const data = await parseMCPResponse(response);
expect(data.result).toMatchInlineSnapshot(`
{
"content": [
{
"text": "# Button
ID: example-button
Primary UI component for user interaction
## Stories
### Primary
Story ID: example-button--primary
\`\`\`
import { Button } from "@my-org/my-component-library";
const Primary = () => <Button onClick={fn()} primary label="Button" />;
\`\`\`
### Secondary
Story ID: example-button--secondary
\`\`\`
import { Button } from "@my-org/my-component-library";
const Secondary = () => <Button onClick={fn()} label="Button" />;
\`\`\`
### Large
Story ID: example-button--large
\`\`\`
import { Button } from "@my-org/my-component-library";
const Large = () => <Button onClick={fn()} size="large" label="Button" />;
\`\`\`
### Other Stories
- Small (example-button--small)
- With A 11 Y Violation (example-button--with-a-11-y-violation)
## Props
\`\`\`
export type Props = {
/**
Is this the principal call to action on the page?
*/
primary?: boolean = false;
/**
What background color to use
*/
backgroundColor?: string;
/**
How large should the button be?
*/
size?: 'small' | 'medium' | 'large' = 'medium';
/**
Button contents
*/
label: string;
/**
Optional click handler
*/
onClick?: () => void;
}
\`\`\`
## Docs
### Additional Information
import { Meta, Canvas } from '@storybook/addon-docs/blocks';
import * as ButtonStories from './Button.stories';
<Meta of={ButtonStories} name="Additional Information" />
It is critical when using the Button component, that the string passed to the \`label\` prop uses the 🍌-emoji instead of spaces.
Here is the button:
<Canvas of={ButtonStories.Primary} />",
"type": "text",
},
],
}
`);
});
it('should return 401 with WWW-Authenticate when fetching remote source with invalid token', async () => {
const response = await mcpRequest(
'tools/call',
{
name: 'get-documentation',
arguments: { id: 'button', storybookId: 'test-private-sb' },
},
'dummy-token',
);
// The manifest fetch to the remote Storybook fails with 401,
// which propagates as an HTTP 401 to trigger re-authentication
expect(response.status).toBe(401);
expect(response.headers.get('www-authenticate')).toContain('resource_metadata=');
});
it('should require storybookId in multi-source auth mode', async () => {
const response = await mcpRequest(
'tools/call',
{
name: 'get-documentation',
arguments: { id: 'example-button' },
},
'dummy-token',
);
const data = await parseMCPResponse(response);
expect(data.result).toMatchInlineSnapshot(`
{
"content": [
{
"text": "Invalid arguments for tool get-documentation: [{"kind":"schema","type":"object","expected":"\\"storybookId\\"","received":"undefined","message":"Invalid key: Expected \\"storybookId\\" but received undefined","path":[{"type":"object","origin":"key","input":{"id":"example-button"},"key":"storybookId"}]}]",
"type": "text",
},
],
"isError": true,
}
`);
});
});
});