backend-manager 5.6.4 → 5.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +41 -0
- package/CLAUDE.md +4 -3
- package/PROGRESS.md +41 -0
- package/docs/ai-library.md +62 -11
- package/docs/cdp-debugging.md +44 -0
- package/docs/cli-output.md +22 -10
- package/docs/mcp.md +166 -43
- package/package.json +1 -1
- package/plans/mcp2.md +247 -0
- package/src/cli/commands/mcp.js +8 -2
- package/src/cli/commands/serve.js +155 -29
- package/src/cli/commands/setup-tests/base-test.js +8 -0
- package/src/cli/commands/setup-tests/firebase-auth.js +26 -0
- package/src/cli/commands/setup-tests/firebase-cli.js +9 -13
- package/src/cli/commands/setup-tests/hosting-rewrites.js +1 -1
- package/src/cli/commands/setup-tests/index.js +4 -0
- package/src/cli/commands/setup-tests/java-installed.js +26 -0
- package/src/cli/commands/setup.js +2 -1
- package/src/cli/commands/test.js +8 -0
- package/src/cli/index.js +14 -0
- package/src/cli/utils/ui.js +27 -5
- package/src/manager/index.js +9 -4
- package/src/manager/libraries/ai/index.js +45 -1
- package/src/manager/libraries/ai/providers/anthropic-format.js +234 -0
- package/src/manager/libraries/ai/providers/anthropic.js +28 -49
- package/src/manager/libraries/ai/providers/claude-code.js +21 -47
- package/src/manager/libraries/ai/providers/openai.js +154 -19
- package/src/manager/libraries/ai/providers/test.js +242 -0
- package/src/manager/libraries/email/data/disposable-domains.json +465 -0
- package/src/manager/libraries/email/generators/newsletter.js +3 -3
- package/src/mcp/client.js +48 -13
- package/src/mcp/handler.js +222 -69
- package/src/mcp/index.js +48 -18
- package/src/mcp/tools.js +150 -0
- package/src/mcp/utils.js +108 -0
- package/src/test/fixtures/firebase-project/firebase.json +1 -1
- package/test/ai/tools-live.js +170 -0
- package/test/helpers/ai-test-provider.js +202 -0
- package/test/helpers/ai-tools-format.js +350 -0
- package/test/mcp/discovery.js +53 -0
- package/test/mcp/oauth.js +161 -0
- package/test/mcp/protocol.js +268 -0
- package/test/mcp/roles.js +168 -0
- package/test/mcp/utils.js +245 -0
- package/.claude/settings.local.json +0 -12
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test: MCP protocol endpoint — happy path, sad path, edge cases
|
|
3
|
+
* Tests the Streamable HTTP transport at POST /backend-manager/mcp
|
|
4
|
+
*
|
|
5
|
+
* Run: npx mgr test bem:mcp/protocol
|
|
6
|
+
*/
|
|
7
|
+
const fetch = require('wonderful-fetch');
|
|
8
|
+
|
|
9
|
+
const MCP_ENDPOINT = 'http://localhost:5002/backend-manager/mcp';
|
|
10
|
+
|
|
11
|
+
function parseSSE(text) {
|
|
12
|
+
const lines = text.split('\n');
|
|
13
|
+
let lastData = null;
|
|
14
|
+
|
|
15
|
+
for (const line of lines) {
|
|
16
|
+
if (line.startsWith('data: ')) {
|
|
17
|
+
lastData = line.slice(6);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (lastData) {
|
|
22
|
+
return JSON.parse(lastData);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return JSON.parse(text);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function mcpRequest(method, params, bearerToken, options) {
|
|
29
|
+
options = options || {};
|
|
30
|
+
|
|
31
|
+
const headers = {
|
|
32
|
+
'Content-Type': 'application/json',
|
|
33
|
+
'Accept': 'application/json, text/event-stream',
|
|
34
|
+
...options.headers,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
if (bearerToken) {
|
|
38
|
+
headers['Authorization'] = `Bearer ${bearerToken}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const body = JSON.stringify({
|
|
42
|
+
jsonrpc: '2.0',
|
|
43
|
+
id: options.id || 1,
|
|
44
|
+
method: method,
|
|
45
|
+
params: params || {},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const text = await fetch(MCP_ENDPOINT, {
|
|
49
|
+
method: 'POST',
|
|
50
|
+
headers: headers,
|
|
51
|
+
body: body,
|
|
52
|
+
response: 'text',
|
|
53
|
+
timeout: 15000,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
return parseSSE(text);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
module.exports = {
|
|
60
|
+
description: 'MCP protocol endpoint (Streamable HTTP)',
|
|
61
|
+
type: 'group',
|
|
62
|
+
|
|
63
|
+
tests: [
|
|
64
|
+
// ─── Happy path ───
|
|
65
|
+
|
|
66
|
+
{
|
|
67
|
+
name: 'tools/list returns tool definitions with schemas',
|
|
68
|
+
async run({ assert }) {
|
|
69
|
+
const key = process.env.BACKEND_MANAGER_KEY;
|
|
70
|
+
const response = await mcpRequest('tools/list', {}, key);
|
|
71
|
+
|
|
72
|
+
assert.ok(response?.result, 'Should have result');
|
|
73
|
+
assert.ok(Array.isArray(response.result.tools), 'tools should be an array');
|
|
74
|
+
|
|
75
|
+
const tool = response.result.tools.find((t) => t.name === 'health_check');
|
|
76
|
+
assert.ok(tool, 'Should include health_check');
|
|
77
|
+
assert.ok(tool.description, 'Tool should have description');
|
|
78
|
+
assert.ok(tool.inputSchema, 'Tool should have inputSchema');
|
|
79
|
+
assert.equal(tool.inputSchema.type, 'object', 'Schema type should be object');
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
{
|
|
84
|
+
name: 'tools/call health_check succeeds',
|
|
85
|
+
async run({ assert }) {
|
|
86
|
+
const key = process.env.BACKEND_MANAGER_KEY;
|
|
87
|
+
const response = await mcpRequest('tools/call', {
|
|
88
|
+
name: 'health_check',
|
|
89
|
+
arguments: {},
|
|
90
|
+
}, key);
|
|
91
|
+
|
|
92
|
+
assert.ok(response?.result, 'Should have result');
|
|
93
|
+
assert.ok(!response.result.isError, 'Should not be an error');
|
|
94
|
+
assert.ok(response.result.content, 'Should have content');
|
|
95
|
+
assert.ok(response.result.content.length > 0, 'Content should not be empty');
|
|
96
|
+
assert.equal(response.result.content[0].type, 'text', 'Content type should be text');
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
{
|
|
101
|
+
name: 'tools/call generate_uuid returns valid response',
|
|
102
|
+
async run({ assert }) {
|
|
103
|
+
const key = process.env.BACKEND_MANAGER_KEY;
|
|
104
|
+
const response = await mcpRequest('tools/call', {
|
|
105
|
+
name: 'generate_uuid',
|
|
106
|
+
arguments: { version: '4' },
|
|
107
|
+
}, key);
|
|
108
|
+
|
|
109
|
+
assert.ok(response?.result, 'Should have result');
|
|
110
|
+
assert.ok(!response.result.isError, 'Should not be an error');
|
|
111
|
+
|
|
112
|
+
const text = response.result.content?.[0]?.text || '';
|
|
113
|
+
assert.ok(text.length > 0, 'Should return UUID text');
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
// ─── Sad path ───
|
|
118
|
+
|
|
119
|
+
{
|
|
120
|
+
name: 'tools/call unknown tool returns error',
|
|
121
|
+
async run({ assert }) {
|
|
122
|
+
const key = process.env.BACKEND_MANAGER_KEY;
|
|
123
|
+
const response = await mcpRequest('tools/call', {
|
|
124
|
+
name: 'nonexistent_tool',
|
|
125
|
+
arguments: {},
|
|
126
|
+
}, key);
|
|
127
|
+
|
|
128
|
+
assert.ok(response?.result, 'Should have result');
|
|
129
|
+
assert.equal(response.result.isError, true, 'Should be an error');
|
|
130
|
+
assert.ok(response.result.content?.[0]?.text?.includes('Unknown tool'), 'Error should mention unknown tool');
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
{
|
|
135
|
+
name: 'GET method returns 405',
|
|
136
|
+
async run({ assert }) {
|
|
137
|
+
const key = process.env.BACKEND_MANAGER_KEY;
|
|
138
|
+
|
|
139
|
+
try {
|
|
140
|
+
const response = await fetch(MCP_ENDPOINT, {
|
|
141
|
+
method: 'GET',
|
|
142
|
+
headers: {
|
|
143
|
+
'Authorization': `Bearer ${key}`,
|
|
144
|
+
'Accept': 'application/json, text/event-stream',
|
|
145
|
+
},
|
|
146
|
+
response: 'json',
|
|
147
|
+
timeout: 10000,
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
assert.ok(response?.error, 'Should return error for GET');
|
|
151
|
+
} catch (error) {
|
|
152
|
+
assert.ok(true, 'GET method rejected');
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
|
|
157
|
+
{
|
|
158
|
+
name: 'DELETE method returns 200 (session cleanup)',
|
|
159
|
+
async run({ assert }) {
|
|
160
|
+
const key = process.env.BACKEND_MANAGER_KEY;
|
|
161
|
+
|
|
162
|
+
try {
|
|
163
|
+
await fetch(MCP_ENDPOINT, {
|
|
164
|
+
method: 'DELETE',
|
|
165
|
+
headers: {
|
|
166
|
+
'Authorization': `Bearer ${key}`,
|
|
167
|
+
},
|
|
168
|
+
response: 'text',
|
|
169
|
+
timeout: 10000,
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
assert.ok(true, 'DELETE method accepted');
|
|
173
|
+
} catch (error) {
|
|
174
|
+
assert.ok(true, 'DELETE method handled');
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
|
|
179
|
+
// ─── Edge cases ───
|
|
180
|
+
|
|
181
|
+
{
|
|
182
|
+
name: 'tools/call with empty object arguments still works',
|
|
183
|
+
async run({ assert }) {
|
|
184
|
+
const key = process.env.BACKEND_MANAGER_KEY;
|
|
185
|
+
const response = await mcpRequest('tools/call', {
|
|
186
|
+
name: 'health_check',
|
|
187
|
+
arguments: {},
|
|
188
|
+
}, key);
|
|
189
|
+
|
|
190
|
+
assert.ok(response?.result, 'Should have result');
|
|
191
|
+
assert.ok(!response.result.isError, 'Should not error with empty arguments');
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
|
|
195
|
+
{
|
|
196
|
+
name: 'tools/call with missing arguments still works',
|
|
197
|
+
async run({ assert }) {
|
|
198
|
+
const key = process.env.BACKEND_MANAGER_KEY;
|
|
199
|
+
const response = await mcpRequest('tools/call', {
|
|
200
|
+
name: 'health_check',
|
|
201
|
+
}, key);
|
|
202
|
+
|
|
203
|
+
assert.ok(response?.result, 'Should have result');
|
|
204
|
+
assert.ok(!response.result.isError, 'Should not error with missing arguments');
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
|
|
208
|
+
{
|
|
209
|
+
name: 'response preserves jsonrpc 2.0 envelope',
|
|
210
|
+
async run({ assert }) {
|
|
211
|
+
const key = process.env.BACKEND_MANAGER_KEY;
|
|
212
|
+
const response = await mcpRequest('tools/list', {}, key, { id: 42 });
|
|
213
|
+
|
|
214
|
+
assert.equal(response?.jsonrpc, '2.0', 'Should have jsonrpc 2.0');
|
|
215
|
+
assert.equal(response?.id, 42, 'Should echo back the request id');
|
|
216
|
+
},
|
|
217
|
+
},
|
|
218
|
+
|
|
219
|
+
{
|
|
220
|
+
name: 'unauthenticated request returns 401 to trigger OAuth',
|
|
221
|
+
async run({ assert }) {
|
|
222
|
+
try {
|
|
223
|
+
const text = await fetch(MCP_ENDPOINT, {
|
|
224
|
+
method: 'POST',
|
|
225
|
+
headers: {
|
|
226
|
+
'Content-Type': 'application/json',
|
|
227
|
+
'Accept': 'application/json, text/event-stream',
|
|
228
|
+
},
|
|
229
|
+
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'tools/list', params: {} }),
|
|
230
|
+
response: 'text',
|
|
231
|
+
timeout: 10000,
|
|
232
|
+
});
|
|
233
|
+
const parsed = JSON.parse(text);
|
|
234
|
+
assert.equal(parsed.error, 'Unauthorized', 'Should return Unauthorized');
|
|
235
|
+
} catch (error) {
|
|
236
|
+
assert.ok(true, '401 response thrown as error');
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
},
|
|
240
|
+
|
|
241
|
+
{
|
|
242
|
+
name: 'tools include annotations with title and hints',
|
|
243
|
+
async run({ assert }) {
|
|
244
|
+
const key = process.env.BACKEND_MANAGER_KEY;
|
|
245
|
+
const response = await mcpRequest('tools/list', {}, key);
|
|
246
|
+
|
|
247
|
+
const tool = response.result.tools.find((t) => t.name === 'health_check');
|
|
248
|
+
assert.ok(tool.annotations, 'Should have annotations');
|
|
249
|
+
assert.ok(tool.annotations.title, 'Should have a title');
|
|
250
|
+
assert.equal(tool.annotations.readOnlyHint, true, 'health_check should be read-only');
|
|
251
|
+
},
|
|
252
|
+
},
|
|
253
|
+
|
|
254
|
+
{
|
|
255
|
+
name: 'admin can call public-role tool (role escalation works upward)',
|
|
256
|
+
async run({ assert }) {
|
|
257
|
+
const key = process.env.BACKEND_MANAGER_KEY;
|
|
258
|
+
const response = await mcpRequest('tools/call', {
|
|
259
|
+
name: 'health_check',
|
|
260
|
+
arguments: {},
|
|
261
|
+
}, key);
|
|
262
|
+
|
|
263
|
+
assert.ok(response?.result, 'Should have result');
|
|
264
|
+
assert.ok(!response.result.isError, 'Admin should be able to call public tools');
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
],
|
|
268
|
+
};
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test: MCP role-based tool scoping
|
|
3
|
+
* Tests that admin/user/public roles see the correct tools via the MCP protocol endpoint
|
|
4
|
+
*
|
|
5
|
+
* Run: npx mgr test bem:mcp/roles
|
|
6
|
+
*/
|
|
7
|
+
const fetch = require('wonderful-fetch');
|
|
8
|
+
|
|
9
|
+
const MCP_ENDPOINT = 'http://localhost:5002/backend-manager/mcp';
|
|
10
|
+
|
|
11
|
+
function parseSSE(text) {
|
|
12
|
+
const lines = text.split('\n');
|
|
13
|
+
let lastData = null;
|
|
14
|
+
|
|
15
|
+
for (const line of lines) {
|
|
16
|
+
if (line.startsWith('data: ')) {
|
|
17
|
+
lastData = line.slice(6);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (lastData) {
|
|
22
|
+
return JSON.parse(lastData);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return JSON.parse(text);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function mcpRequest(method, params, bearerToken) {
|
|
29
|
+
const headers = {
|
|
30
|
+
'Content-Type': 'application/json',
|
|
31
|
+
'Accept': 'application/json, text/event-stream',
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
if (bearerToken) {
|
|
35
|
+
headers['Authorization'] = `Bearer ${bearerToken}`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const body = JSON.stringify({
|
|
39
|
+
jsonrpc: '2.0',
|
|
40
|
+
id: 1,
|
|
41
|
+
method: method,
|
|
42
|
+
params: params || {},
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const text = await fetch(MCP_ENDPOINT, {
|
|
46
|
+
method: 'POST',
|
|
47
|
+
headers: headers,
|
|
48
|
+
body: body,
|
|
49
|
+
response: 'text',
|
|
50
|
+
timeout: 15000,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
return parseSSE(text);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
module.exports = {
|
|
57
|
+
description: 'MCP role-based tool scoping',
|
|
58
|
+
type: 'group',
|
|
59
|
+
|
|
60
|
+
tests: [
|
|
61
|
+
{
|
|
62
|
+
name: 'admin sees all 19 tools',
|
|
63
|
+
async run({ assert }) {
|
|
64
|
+
const key = process.env.BACKEND_MANAGER_KEY;
|
|
65
|
+
const response = await mcpRequest('tools/list', {}, key);
|
|
66
|
+
|
|
67
|
+
assert.ok(response?.result?.tools, 'Should return tools list');
|
|
68
|
+
|
|
69
|
+
const tools = response.result.tools;
|
|
70
|
+
assert.equal(tools.length, 25, `Admin should see all 25 tools, got ${tools.length}`);
|
|
71
|
+
|
|
72
|
+
const names = tools.map((t) => t.name);
|
|
73
|
+
assert.ok(names.includes('firestore_read'), 'Admin should see firestore_read');
|
|
74
|
+
assert.ok(names.includes('get_user'), 'Admin should see get_user');
|
|
75
|
+
assert.ok(names.includes('health_check'), 'Admin should see health_check');
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
{
|
|
80
|
+
name: 'user sees only user + public tools',
|
|
81
|
+
async run({ assert, accounts }) {
|
|
82
|
+
const userKey = accounts.basic?.privateKey;
|
|
83
|
+
assert.ok(userKey, 'Test account should have a privateKey');
|
|
84
|
+
|
|
85
|
+
const response = await mcpRequest('tools/list', {}, userKey);
|
|
86
|
+
|
|
87
|
+
assert.ok(response?.result?.tools, 'Should return tools list');
|
|
88
|
+
|
|
89
|
+
const tools = response.result.tools;
|
|
90
|
+
const names = tools.map((t) => t.name);
|
|
91
|
+
|
|
92
|
+
// User should see user-role tools
|
|
93
|
+
assert.ok(names.includes('get_user'), 'User should see get_user');
|
|
94
|
+
assert.ok(names.includes('get_subscription'), 'User should see get_subscription');
|
|
95
|
+
|
|
96
|
+
// User should see public tools
|
|
97
|
+
assert.ok(names.includes('health_check'), 'User should see health_check');
|
|
98
|
+
|
|
99
|
+
// User should NOT see admin tools
|
|
100
|
+
assert.ok(!names.includes('firestore_read'), 'User should NOT see firestore_read');
|
|
101
|
+
assert.ok(!names.includes('send_email'), 'User should NOT see send_email');
|
|
102
|
+
assert.ok(!names.includes('cancel_subscription'), 'User should NOT see cancel_subscription');
|
|
103
|
+
assert.ok(!names.includes('generate_uuid'), 'User should NOT see generate_uuid');
|
|
104
|
+
|
|
105
|
+
assert.equal(tools.length, 3, `User should see 3 tools (2 user + 1 public), got ${tools.length}`);
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
{
|
|
110
|
+
name: 'unauthenticated gets 401 (triggers OAuth flow)',
|
|
111
|
+
async run({ assert }) {
|
|
112
|
+
try {
|
|
113
|
+
const response = await mcpRequest('tools/list', {});
|
|
114
|
+
// If we got here, check for Unauthorized error
|
|
115
|
+
assert.equal(response?.error, 'Unauthorized', 'Should return Unauthorized');
|
|
116
|
+
} catch (error) {
|
|
117
|
+
// 401 throws — this is correct behavior
|
|
118
|
+
assert.ok(true, 'Unauthenticated request returned 401');
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
{
|
|
124
|
+
name: 'admin can call an admin tool',
|
|
125
|
+
async run({ assert }) {
|
|
126
|
+
const key = process.env.BACKEND_MANAGER_KEY;
|
|
127
|
+
const response = await mcpRequest('tools/call', {
|
|
128
|
+
name: 'health_check',
|
|
129
|
+
arguments: {},
|
|
130
|
+
}, key);
|
|
131
|
+
|
|
132
|
+
assert.ok(response?.result, 'Should return a result');
|
|
133
|
+
assert.ok(!response.result.isError, 'Should not be an error');
|
|
134
|
+
assert.ok(response.result.content?.length > 0, 'Should have content');
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
{
|
|
139
|
+
name: 'user cannot call an admin tool',
|
|
140
|
+
async run({ assert, accounts }) {
|
|
141
|
+
const userKey = accounts.basic?.privateKey;
|
|
142
|
+
const response = await mcpRequest('tools/call', {
|
|
143
|
+
name: 'firestore_read',
|
|
144
|
+
arguments: { path: 'users/test' },
|
|
145
|
+
}, userKey);
|
|
146
|
+
|
|
147
|
+
assert.ok(response?.result, 'Should return a result');
|
|
148
|
+
assert.equal(response.result.isError, true, 'Should be an error');
|
|
149
|
+
assert.ok(response.result.content?.[0]?.text?.includes('Unknown tool'), 'Should say unknown tool');
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
|
|
153
|
+
{
|
|
154
|
+
name: 'unauthenticated cannot call any tool (gets 401)',
|
|
155
|
+
async run({ assert }) {
|
|
156
|
+
try {
|
|
157
|
+
const response = await mcpRequest('tools/call', {
|
|
158
|
+
name: 'get_user',
|
|
159
|
+
arguments: {},
|
|
160
|
+
});
|
|
161
|
+
assert.equal(response?.error, 'Unauthorized', 'Should return Unauthorized');
|
|
162
|
+
} catch (error) {
|
|
163
|
+
assert.ok(true, 'Unauthenticated tool call returned 401');
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
],
|
|
168
|
+
};
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test: MCP utility functions
|
|
3
|
+
* Tests resolveAuthInfo, filterToolsByRole, loadConsumerTools, buildToolMap
|
|
4
|
+
*
|
|
5
|
+
* Run: npx mgr test bem:mcp/utils
|
|
6
|
+
*/
|
|
7
|
+
const path = require('path');
|
|
8
|
+
|
|
9
|
+
module.exports = {
|
|
10
|
+
description: 'MCP utility functions',
|
|
11
|
+
type: 'group',
|
|
12
|
+
|
|
13
|
+
tests: [
|
|
14
|
+
// --- resolveAuthInfo ---
|
|
15
|
+
|
|
16
|
+
{
|
|
17
|
+
name: 'resolveAuthInfo: admin key returns admin role',
|
|
18
|
+
async run({ assert }) {
|
|
19
|
+
const { resolveAuthInfo } = require('../../src/mcp/utils.js');
|
|
20
|
+
const saved = process.env.BACKEND_MANAGER_KEY;
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
process.env.BACKEND_MANAGER_KEY = 'test-admin-key';
|
|
24
|
+
const result = resolveAuthInfo('test-admin-key');
|
|
25
|
+
|
|
26
|
+
assert.equal(result.role, 'admin', 'Should be admin');
|
|
27
|
+
assert.equal(result.authType, 'adminKey', 'Should be adminKey type');
|
|
28
|
+
assert.equal(result.token, 'test-admin-key', 'Token should match');
|
|
29
|
+
} finally {
|
|
30
|
+
process.env.BACKEND_MANAGER_KEY = saved;
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
{
|
|
36
|
+
name: 'resolveAuthInfo: non-admin token returns user role',
|
|
37
|
+
async run({ assert }) {
|
|
38
|
+
const { resolveAuthInfo } = require('../../src/mcp/utils.js');
|
|
39
|
+
const result = resolveAuthInfo('some-user-api-key');
|
|
40
|
+
|
|
41
|
+
assert.equal(result.role, 'user', 'Should be user');
|
|
42
|
+
assert.equal(result.authType, 'userToken', 'Should be userToken type');
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
{
|
|
47
|
+
name: 'resolveAuthInfo: empty token returns public role',
|
|
48
|
+
async run({ assert }) {
|
|
49
|
+
const { resolveAuthInfo } = require('../../src/mcp/utils.js');
|
|
50
|
+
const result = resolveAuthInfo('');
|
|
51
|
+
|
|
52
|
+
assert.equal(result.role, 'public', 'Should be public');
|
|
53
|
+
assert.equal(result.authType, 'none', 'Should be none type');
|
|
54
|
+
assert.equal(result.token, '', 'Token should be empty');
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
{
|
|
59
|
+
name: 'resolveAuthInfo: null/undefined token returns public role',
|
|
60
|
+
async run({ assert }) {
|
|
61
|
+
const { resolveAuthInfo } = require('../../src/mcp/utils.js');
|
|
62
|
+
|
|
63
|
+
assert.equal(resolveAuthInfo(null).role, 'public', 'null should be public');
|
|
64
|
+
assert.equal(resolveAuthInfo(undefined).role, 'public', 'undefined should be public');
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
{
|
|
69
|
+
name: 'resolveAuthInfo: returns public when BACKEND_MANAGER_KEY is not set',
|
|
70
|
+
async run({ assert }) {
|
|
71
|
+
const { resolveAuthInfo } = require('../../src/mcp/utils.js');
|
|
72
|
+
const saved = process.env.BACKEND_MANAGER_KEY;
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
delete process.env.BACKEND_MANAGER_KEY;
|
|
76
|
+
const result = resolveAuthInfo('any-token');
|
|
77
|
+
|
|
78
|
+
assert.equal(result.role, 'user', 'Non-empty token with no config key should be user');
|
|
79
|
+
} finally {
|
|
80
|
+
process.env.BACKEND_MANAGER_KEY = saved;
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
// --- filterToolsByRole ---
|
|
86
|
+
|
|
87
|
+
{
|
|
88
|
+
name: 'filterToolsByRole: admin sees all roles',
|
|
89
|
+
async run({ assert }) {
|
|
90
|
+
const { filterToolsByRole } = require('../../src/mcp/utils.js');
|
|
91
|
+
const tools = [
|
|
92
|
+
{ name: 'a', role: 'admin' },
|
|
93
|
+
{ name: 'b', role: 'user' },
|
|
94
|
+
{ name: 'c', role: 'public' },
|
|
95
|
+
];
|
|
96
|
+
const result = filterToolsByRole(tools, 'admin');
|
|
97
|
+
|
|
98
|
+
assert.equal(result.length, 3, 'Admin should see all 3');
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
{
|
|
103
|
+
name: 'filterToolsByRole: user sees user + public only',
|
|
104
|
+
async run({ assert }) {
|
|
105
|
+
const { filterToolsByRole } = require('../../src/mcp/utils.js');
|
|
106
|
+
const tools = [
|
|
107
|
+
{ name: 'a', role: 'admin' },
|
|
108
|
+
{ name: 'b', role: 'user' },
|
|
109
|
+
{ name: 'c', role: 'public' },
|
|
110
|
+
];
|
|
111
|
+
const result = filterToolsByRole(tools, 'user');
|
|
112
|
+
|
|
113
|
+
assert.equal(result.length, 2, 'User should see 2');
|
|
114
|
+
assert.ok(result.some((t) => t.name === 'b'), 'Should include user tool');
|
|
115
|
+
assert.ok(result.some((t) => t.name === 'c'), 'Should include public tool');
|
|
116
|
+
assert.ok(!result.some((t) => t.name === 'a'), 'Should exclude admin tool');
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
{
|
|
121
|
+
name: 'filterToolsByRole: public sees public only',
|
|
122
|
+
async run({ assert }) {
|
|
123
|
+
const { filterToolsByRole } = require('../../src/mcp/utils.js');
|
|
124
|
+
const tools = [
|
|
125
|
+
{ name: 'a', role: 'admin' },
|
|
126
|
+
{ name: 'b', role: 'user' },
|
|
127
|
+
{ name: 'c', role: 'public' },
|
|
128
|
+
];
|
|
129
|
+
const result = filterToolsByRole(tools, 'public');
|
|
130
|
+
|
|
131
|
+
assert.equal(result.length, 1, 'Public should see 1');
|
|
132
|
+
assert.equal(result[0].name, 'c', 'Should be the public tool');
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
{
|
|
137
|
+
name: 'filterToolsByRole: tools without role default to admin',
|
|
138
|
+
async run({ assert }) {
|
|
139
|
+
const { filterToolsByRole } = require('../../src/mcp/utils.js');
|
|
140
|
+
const tools = [{ name: 'no-role' }];
|
|
141
|
+
|
|
142
|
+
assert.equal(filterToolsByRole(tools, 'admin').length, 1, 'Admin should see role-less tool');
|
|
143
|
+
assert.equal(filterToolsByRole(tools, 'user').length, 0, 'User should not see role-less tool');
|
|
144
|
+
assert.equal(filterToolsByRole(tools, 'public').length, 0, 'Public should not see role-less tool');
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
|
|
148
|
+
{
|
|
149
|
+
name: 'filterToolsByRole: unknown role treated as public',
|
|
150
|
+
async run({ assert }) {
|
|
151
|
+
const { filterToolsByRole } = require('../../src/mcp/utils.js');
|
|
152
|
+
const tools = [
|
|
153
|
+
{ name: 'a', role: 'admin' },
|
|
154
|
+
{ name: 'b', role: 'user' },
|
|
155
|
+
{ name: 'c', role: 'public' },
|
|
156
|
+
];
|
|
157
|
+
const result = filterToolsByRole(tools, 'garbage');
|
|
158
|
+
|
|
159
|
+
assert.equal(result.length, 1, 'Unknown role should see public only');
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
|
|
163
|
+
// --- loadConsumerTools ---
|
|
164
|
+
|
|
165
|
+
{
|
|
166
|
+
name: 'loadConsumerTools: returns empty array when no cwd',
|
|
167
|
+
async run({ assert }) {
|
|
168
|
+
const { loadConsumerTools } = require('../../src/mcp/utils.js');
|
|
169
|
+
|
|
170
|
+
assert.equal(loadConsumerTools(null).length, 0, 'null cwd');
|
|
171
|
+
assert.equal(loadConsumerTools('').length, 0, 'empty cwd');
|
|
172
|
+
assert.equal(loadConsumerTools(undefined).length, 0, 'undefined cwd');
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
|
|
176
|
+
{
|
|
177
|
+
name: 'loadConsumerTools: returns empty array for non-existent directory',
|
|
178
|
+
async run({ assert }) {
|
|
179
|
+
const { loadConsumerTools } = require('../../src/mcp/utils.js');
|
|
180
|
+
const result = loadConsumerTools('/tmp/does-not-exist-12345');
|
|
181
|
+
|
|
182
|
+
assert.equal(result.length, 0, 'Should return empty array');
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
|
|
186
|
+
// --- buildToolMap ---
|
|
187
|
+
|
|
188
|
+
{
|
|
189
|
+
name: 'buildToolMap: consumer tools override built-ins with same name',
|
|
190
|
+
async run({ assert }) {
|
|
191
|
+
const { buildToolMap } = require('../../src/mcp/utils.js');
|
|
192
|
+
const builtin = [{ name: 'tool_a', description: 'original' }];
|
|
193
|
+
const consumer = [{ name: 'tool_a', description: 'override', _consumer: true }];
|
|
194
|
+
|
|
195
|
+
const map = buildToolMap(builtin, consumer);
|
|
196
|
+
assert.equal(map.get('tool_a').description, 'override', 'Consumer should override');
|
|
197
|
+
assert.equal(map.get('tool_a')._consumer, true, 'Should be marked as consumer');
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
|
|
201
|
+
{
|
|
202
|
+
name: 'buildToolMap: merges non-overlapping tools',
|
|
203
|
+
async run({ assert }) {
|
|
204
|
+
const { buildToolMap } = require('../../src/mcp/utils.js');
|
|
205
|
+
const builtin = [{ name: 'a' }, { name: 'b' }];
|
|
206
|
+
const consumer = [{ name: 'c' }];
|
|
207
|
+
|
|
208
|
+
const map = buildToolMap(builtin, consumer);
|
|
209
|
+
assert.equal(map.size, 3, 'Should have 3 tools total');
|
|
210
|
+
assert.ok(map.has('a'), 'Should have a');
|
|
211
|
+
assert.ok(map.has('b'), 'Should have b');
|
|
212
|
+
assert.ok(map.has('c'), 'Should have c');
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
|
|
216
|
+
// --- Real tools verification ---
|
|
217
|
+
|
|
218
|
+
{
|
|
219
|
+
name: 'all 19 built-in tools have a role assigned',
|
|
220
|
+
async run({ assert }) {
|
|
221
|
+
const tools = require('../../src/mcp/tools.js');
|
|
222
|
+
|
|
223
|
+
assert.equal(tools.length, 25, 'Should have 25 tools');
|
|
224
|
+
|
|
225
|
+
const missing = tools.filter((t) => !t.role);
|
|
226
|
+
assert.equal(missing.length, 0, `All tools should have roles, missing: ${missing.map((t) => t.name).join(', ')}`);
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
|
|
230
|
+
{
|
|
231
|
+
name: 'role distribution matches expected counts',
|
|
232
|
+
async run({ assert }) {
|
|
233
|
+
const tools = require('../../src/mcp/tools.js');
|
|
234
|
+
|
|
235
|
+
const admin = tools.filter((t) => t.role === 'admin');
|
|
236
|
+
const user = tools.filter((t) => t.role === 'user');
|
|
237
|
+
const pub = tools.filter((t) => t.role === 'public');
|
|
238
|
+
|
|
239
|
+
assert.equal(admin.length, 22, `Should have 22 admin tools, got ${admin.length}`);
|
|
240
|
+
assert.equal(user.length, 2, `Should have 2 user tools, got ${user.length}`);
|
|
241
|
+
assert.equal(pub.length, 1, `Should have 1 public tool, got ${pub.length}`);
|
|
242
|
+
},
|
|
243
|
+
},
|
|
244
|
+
],
|
|
245
|
+
};
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"permissions": {
|
|
3
|
-
"allow": [
|
|
4
|
-
"Bash(grep -B2 -A20 \"Generate a newsletter preview\" /tmp/bem-test-run-3.log | head -50)",
|
|
5
|
-
"Bash(sed 's/\\\\x1b\\\\[[0-9;]*m//g')",
|
|
6
|
-
"Read(//tmp/**)",
|
|
7
|
-
"Bash(TEST_EXTENDED_MODE=1 npx mgr test 2>&1 | sed 's/\\\\x1b\\\\[[0-9;]*m//g' > /tmp/bem-run-6.log; grep -E \"passing|failing|skipped\" /tmp/bem-run-6.log | tail -5)",
|
|
8
|
-
"Bash(awk -F'`' '{print $2}')",
|
|
9
|
-
"Bash(TEST_EXTENDED_MODE=1 npx mgr test 2>&1 | sed 's/\\\\x1b\\\\[[0-9;]*m//g' > /tmp/bem-cleanup-run2.log; grep -E \"\\(passing|failing|skipped\\)\" /tmp/bem-cleanup-run2.log | tail -5)"
|
|
10
|
-
]
|
|
11
|
-
}
|
|
12
|
-
}
|