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
|
import { describe, it, expect } from "bun:test";
import {
routeMcpRequest,
MCP_PROTOCOL_VERSION,
MCP_SERVER_NAME,
ERR_METHOD_NOT_FOUND,
ERR_INVALID_REQUEST,
ERR_INVALID_PARAMS,
McpError,
} from "../lib/mcp";
import { defaultTools, __test } from "../lib/mcp-tools";
import app from "../app";
const tools = defaultTools();
const ctx = { userId: null };
describe("routeMcpRequest — initialize", () => {
it("returns the protocol version + server info", async () => {
const r = await routeMcpRequest(
{ jsonrpc: "2.0", id: 1, method: "initialize" },
{ ctx, tools }
);
expect(r).not.toBeNull();
if (!r || "error" in r) throw new Error("expected success");
const result = r.result as any;
expect(result.protocolVersion).toBe(MCP_PROTOCOL_VERSION);
expect(result.serverInfo.name).toBe(MCP_SERVER_NAME);
expect(result.capabilities.tools).toBeDefined();
});
});
describe("routeMcpRequest — invalid input", () => {
it("rejects a non-JSON-RPC envelope", async () => {
const r = await routeMcpRequest({ method: "x" } as any, { ctx, tools });
if (!r || "result" in r) throw new Error("expected error");
expect(r.error.code).toBe(ERR_INVALID_REQUEST);
});
it("rejects an unknown method", async () => {
const r = await routeMcpRequest(
{ jsonrpc: "2.0", id: 1, method: "no_such_method" },
{ ctx, tools }
);
if (!r || "result" in r) throw new Error("expected error");
expect(r.error.code).toBe(ERR_METHOD_NOT_FOUND);
});
it("returns null for a notification (no `id`)", async () => {
const r = await routeMcpRequest(
{ jsonrpc: "2.0", method: "notifications/initialized" },
{ ctx, tools }
);
expect(r).toBeNull();
});
});
describe("routeMcpRequest — tools/list", () => {
it("returns the full tool manifest", async () => {
const r = await routeMcpRequest(
{ jsonrpc: "2.0", id: 2, method: "tools/list" },
{ ctx, tools }
);
if (!r || "error" in r) throw new Error("expected success");
const result = r.result as { tools: Array<{ name: string; inputSchema: any }> };
expect(Array.isArray(result.tools)).toBe(true);
expect(result.tools.length).toBeGreaterThanOrEqual(4);
for (const t of result.tools) {
expect(typeof t.name).toBe("string");
expect(t.name).toMatch(/^gluecron_/);
expect(t.inputSchema.type).toBe("object");
}
});
});
describe("routeMcpRequest — tools/call validation", () => {
it("rejects calls without a name", async () => {
const r = await routeMcpRequest(
{ jsonrpc: "2.0", id: 3, method: "tools/call", params: {} },
{ ctx, tools }
);
if (!r || "result" in r) throw new Error("expected error");
expect(r.error.code).toBe(ERR_INVALID_PARAMS);
});
it("rejects calls with an unknown tool name", async () => {
const r = await routeMcpRequest(
{
jsonrpc: "2.0",
id: 4,
method: "tools/call",
params: { name: "no_such_tool", arguments: {} },
},
{ ctx, tools }
);
if (!r || "result" in r) throw new Error("expected error");
expect(r.error.code).toBe(ERR_METHOD_NOT_FOUND);
});
});
describe("argString / argNumber pure helpers", () => {
it("returns the trimmed string when present", () => {
expect(__test.argString({ x: " hi " }, "x")).toBe("hi");
});
it("falls back when missing", () => {
expect(__test.argString({}, "x", "default")).toBe("default");
});
it("throws McpError when missing without fallback", () => {
expect(() => __test.argString({}, "x")).toThrow(McpError);
});
it("argNumber accepts numeric strings", () => {
expect(__test.argNumber({ n: "42" }, "n")).toBe(42);
});
it("argNumber falls back on non-numeric input", () => {
expect(__test.argNumber({ n: "abc" }, "n", 7)).toBe(7);
});
});
describe("tool manifest shape", () => {
for (const handler of Object.values(tools)) {
it(`${handler.tool.name} — required fields populated`, () => {
expect(handler.tool.name).toMatch(/^gluecron_/);
expect(typeof handler.tool.description).toBe("string");
expect(handler.tool.description.length).toBeGreaterThan(10);
expect(handler.tool.inputSchema.type).toBe("object");
expect(typeof handler.tool.inputSchema.properties).toBe("object");
});
}
});
describe("HTTP route — GET /mcp discovery", () => {
it("returns server info + tool count", async () => {
const res = await app.request("/mcp");
expect(res.status).toBe(200);
const body = (await res.json()) as any;
expect(body.protocolVersion).toBe(MCP_PROTOCOL_VERSION);
expect(body.serverInfo.name).toBe(MCP_SERVER_NAME);
expect(body.toolCount).toBeGreaterThanOrEqual(4);
});
});
describe("HTTP route — POST /mcp JSON-RPC", () => {
it("answers initialize over the wire", async () => {
const res = await app.request("/mcp", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
jsonrpc: "2.0",
id: 1,
method: "initialize",
}),
});
expect(res.status).toBe(200);
const body = (await res.json()) as any;
expect(body.jsonrpc).toBe("2.0");
expect(body.id).toBe(1);
expect(body.result.protocolVersion).toBe(MCP_PROTOCOL_VERSION);
});
it("returns 400 on invalid JSON", async () => {
const res = await app.request("/mcp", {
method: "POST",
headers: { "content-type": "application/json" },
body: "not json",
});
expect(res.status).toBe(400);
});
it("returns 204 for a single notification (no `id`)", async () => {
const res = await app.request("/mcp", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
jsonrpc: "2.0",
method: "notifications/initialized",
}),
});
expect(res.status).toBe(204);
});
it("supports a batched request", async () => {
const res = await app.request("/mcp", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify([
{ jsonrpc: "2.0", id: 1, method: "initialize" },
{ jsonrpc: "2.0", id: 2, method: "tools/list" },
]),
});
expect(res.status).toBe(200);
const body = (await res.json()) as any[];
expect(body.length).toBe(2);
expect(body[0].id).toBe(1);
expect(body[1].id).toBe(2);
expect(body[1].result.tools.length).toBeGreaterThanOrEqual(4);
});
});
describe("HTTP route — POST /mcp OAuth challenge", () => {
it("challenges an anonymous tools/call with 401 + WWW-Authenticate", async () => {
const res = await app.request("/mcp", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
jsonrpc: "2.0",
id: 7,
method: "tools/call",
params: { name: "gluecron_repo_search", arguments: { q: "x" } },
}),
});
expect(res.status).toBe(401);
const wwwAuth = res.headers.get("www-authenticate") || "";
expect(wwwAuth).toContain("Bearer");
expect(wwwAuth).toContain("resource_metadata=");
expect(wwwAuth).toContain("/.well-known/oauth-protected-resource");
const body = (await res.json()) as any;
expect(body.error.code).toBe(-32001);
expect(body.id).toBe(7);
});
it("challenges when a batch contains a tools/call while anonymous", async () => {
const res = await app.request("/mcp", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify([
{ jsonrpc: "2.0", id: 1, method: "initialize" },
{
jsonrpc: "2.0",
id: 2,
method: "tools/call",
params: { name: "gluecron_repo_search", arguments: { q: "x" } },
},
]),
});
expect(res.status).toBe(401);
expect(res.headers.get("www-authenticate") || "").toContain("resource_metadata=");
});
it("still allows the anonymous handshake (no challenge on tools/list)", async () => {
const res = await app.request("/mcp", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/list" }),
});
expect(res.status).toBe(200);
});
});
|