@donghanh/hono 0.3.5 → 0.4.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/LICENSE +21 -0
- package/package.json +7 -4
- package/src/catalog.test.ts +525 -0
- package/src/catalog.ts +212 -0
- package/src/chat.test.ts +20 -0
- package/src/chat.ts +13 -3
- package/src/exposure.test.ts +137 -0
- package/src/exposure.ts +93 -0
- package/src/gpt.test.ts +30 -1
- package/src/gpt.ts +7 -1
- package/src/index.ts +12 -1
- package/src/mcp.test.ts +514 -3
- package/src/mcp.ts +450 -131
- package/src/openapi.test.ts +1 -1
- package/src/openapi.ts +2 -0
- package/src/protocol.ts +18 -0
package/src/mcp.test.ts
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
1
|
import type {
|
|
3
2
|
ChatNode,
|
|
4
3
|
Executor,
|
|
5
4
|
OperationComponent,
|
|
6
5
|
OperationConfig,
|
|
7
6
|
} from "@donghanh/core";
|
|
8
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
Action,
|
|
9
|
+
App,
|
|
10
|
+
Brief,
|
|
11
|
+
buildRegistry,
|
|
12
|
+
Display,
|
|
13
|
+
Message,
|
|
14
|
+
} from "@donghanh/core";
|
|
15
|
+
import { describe, expect, test, vi } from "vitest";
|
|
9
16
|
import { mcpRoutes } from "./mcp";
|
|
10
17
|
|
|
11
18
|
function makeOp(
|
|
@@ -58,7 +65,7 @@ describe("mcpRoutes", () => {
|
|
|
58
65
|
test("initialize returns protocol version + capabilities", async () => {
|
|
59
66
|
const app = mcpRoutes({ ...baseConfig, registry: makeApp({}) });
|
|
60
67
|
const { result } = await rpc(app, "initialize");
|
|
61
|
-
expect(result.protocolVersion).toBe("
|
|
68
|
+
expect(result.protocolVersion).toBe("2025-11-25");
|
|
62
69
|
expect(result.capabilities.tools).toEqual({});
|
|
63
70
|
});
|
|
64
71
|
|
|
@@ -84,6 +91,7 @@ describe("mcpRoutes", () => {
|
|
|
84
91
|
readOnlyHint: true,
|
|
85
92
|
openWorldHint: false,
|
|
86
93
|
destructiveHint: false,
|
|
94
|
+
idempotentHint: false,
|
|
87
95
|
});
|
|
88
96
|
});
|
|
89
97
|
|
|
@@ -103,6 +111,7 @@ describe("mcpRoutes", () => {
|
|
|
103
111
|
readOnlyHint: false,
|
|
104
112
|
openWorldHint: true,
|
|
105
113
|
destructiveHint: true,
|
|
114
|
+
idempotentHint: false,
|
|
106
115
|
});
|
|
107
116
|
});
|
|
108
117
|
|
|
@@ -259,6 +268,87 @@ describe("mcpRoutes", () => {
|
|
|
259
268
|
expect(result.structuredContent.display).toEqual({ x: 1 });
|
|
260
269
|
});
|
|
261
270
|
|
|
271
|
+
test("actions are appended to the text as a Next block", async () => {
|
|
272
|
+
const list = makeOp("list", {}, () =>
|
|
273
|
+
Brief({
|
|
274
|
+
children: [
|
|
275
|
+
Message({ children: "3 items." }),
|
|
276
|
+
Action({ operation: "add_item", label: "Add an item" }),
|
|
277
|
+
],
|
|
278
|
+
}),
|
|
279
|
+
);
|
|
280
|
+
const app = mcpRoutes({
|
|
281
|
+
...baseConfig,
|
|
282
|
+
registry: makeApp({ list, add_item: makeOp("add_item") }),
|
|
283
|
+
});
|
|
284
|
+
const { result } = await rpc(app, "tools/call", {
|
|
285
|
+
name: "list",
|
|
286
|
+
arguments: {},
|
|
287
|
+
});
|
|
288
|
+
expect(result.content[0].text).toBe(
|
|
289
|
+
"3 items.\n\nNext:\n- Add an item (add_item)",
|
|
290
|
+
);
|
|
291
|
+
expect(result.structuredContent.actions).toHaveLength(1);
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
test("actionsInText:false keeps the text unchanged", async () => {
|
|
295
|
+
const list = makeOp("list", {}, () =>
|
|
296
|
+
Brief({
|
|
297
|
+
children: [
|
|
298
|
+
Message({ children: "3 items." }),
|
|
299
|
+
Action({ operation: "add_item", label: "Add an item" }),
|
|
300
|
+
],
|
|
301
|
+
}),
|
|
302
|
+
);
|
|
303
|
+
const app = mcpRoutes({
|
|
304
|
+
...baseConfig,
|
|
305
|
+
actionsInText: false,
|
|
306
|
+
registry: makeApp({ list, add_item: makeOp("add_item") }),
|
|
307
|
+
});
|
|
308
|
+
const { result } = await rpc(app, "tools/call", {
|
|
309
|
+
name: "list",
|
|
310
|
+
arguments: {},
|
|
311
|
+
});
|
|
312
|
+
expect(result.content[0].text).toBe("3 items.");
|
|
313
|
+
expect(result.structuredContent.actions).toHaveLength(1);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
test("no Next block when there are no actions", async () => {
|
|
317
|
+
const app = mcpRoutes({
|
|
318
|
+
...baseConfig,
|
|
319
|
+
registry: makeApp({
|
|
320
|
+
list: makeOp("list", {}, () =>
|
|
321
|
+
Brief({ children: [Message({ children: "3 items." })] }),
|
|
322
|
+
),
|
|
323
|
+
}),
|
|
324
|
+
});
|
|
325
|
+
const { result } = await rpc(app, "tools/call", {
|
|
326
|
+
name: "list",
|
|
327
|
+
arguments: {},
|
|
328
|
+
});
|
|
329
|
+
expect(result.content[0].text).toBe("3 items.");
|
|
330
|
+
expect(result.structuredContent.actions).toBeUndefined();
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
test("actions without a message give a bare Next block", async () => {
|
|
334
|
+
const app = mcpRoutes({
|
|
335
|
+
...baseConfig,
|
|
336
|
+
registry: makeApp({
|
|
337
|
+
list: makeOp("list", {}, () =>
|
|
338
|
+
Brief({
|
|
339
|
+
children: [Action({ operation: "add_item", label: "Add an item" })],
|
|
340
|
+
}),
|
|
341
|
+
),
|
|
342
|
+
add_item: makeOp("add_item"),
|
|
343
|
+
}),
|
|
344
|
+
});
|
|
345
|
+
const { result } = await rpc(app, "tools/call", {
|
|
346
|
+
name: "list",
|
|
347
|
+
arguments: {},
|
|
348
|
+
});
|
|
349
|
+
expect(result.content[0].text).toBe("Next:\n- Add an item (add_item)");
|
|
350
|
+
});
|
|
351
|
+
|
|
262
352
|
test("tools/call: enrichResponse can add fields to structuredContent", async () => {
|
|
263
353
|
const app = mcpRoutes({
|
|
264
354
|
...baseConfig,
|
|
@@ -310,4 +400,425 @@ describe("mcpRoutes", () => {
|
|
|
310
400
|
expect(body.authorization_servers).toEqual(["https://auth.example.com"]);
|
|
311
401
|
expect(body.scopes_supported).toEqual(["read", "write"]);
|
|
312
402
|
});
|
|
403
|
+
|
|
404
|
+
test("initialize echoes each supported version", async () => {
|
|
405
|
+
const app = mcpRoutes({ ...baseConfig, registry: makeApp({}) });
|
|
406
|
+
for (const v of ["2025-11-25", "2025-06-18", "2025-03-26", "2024-11-05"]) {
|
|
407
|
+
const { result } = await rpc(app, "initialize", { protocolVersion: v });
|
|
408
|
+
expect(result.protocolVersion).toBe(v);
|
|
409
|
+
}
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
test("initialize answers unknown or missing versions with the newest", async () => {
|
|
413
|
+
const app = mcpRoutes({ ...baseConfig, registry: makeApp({}) });
|
|
414
|
+
expect(
|
|
415
|
+
(await rpc(app, "initialize", { protocolVersion: "2099-01-01" })).result
|
|
416
|
+
.protocolVersion,
|
|
417
|
+
).toBe("2025-11-25");
|
|
418
|
+
expect((await rpc(app, "initialize")).result.protocolVersion).toBe(
|
|
419
|
+
"2025-11-25",
|
|
420
|
+
);
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
test("initialize carries app instructions plus the endpoint suffix", async () => {
|
|
424
|
+
const reg = buildRegistry(App({ operations: {}, instructions: "Play." }));
|
|
425
|
+
const app = mcpRoutes({
|
|
426
|
+
...baseConfig,
|
|
427
|
+
registry: reg,
|
|
428
|
+
instructionsSuffix: " Use find_operations.",
|
|
429
|
+
});
|
|
430
|
+
expect((await rpc(app, "initialize")).result.instructions).toBe(
|
|
431
|
+
"Play. Use find_operations.",
|
|
432
|
+
);
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
test("no instructions key when the app has none", async () => {
|
|
436
|
+
const app = mcpRoutes({
|
|
437
|
+
...baseConfig,
|
|
438
|
+
registry: makeApp({}),
|
|
439
|
+
instructionsSuffix: " Suffix.",
|
|
440
|
+
});
|
|
441
|
+
const { result } = await rpc(app, "initialize");
|
|
442
|
+
expect("instructions" in result).toBe(false);
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
test("warns once at startup above 4096 characters", async () => {
|
|
446
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
447
|
+
try {
|
|
448
|
+
const reg = buildRegistry(
|
|
449
|
+
App({ operations: {}, instructions: "x".repeat(4097) }),
|
|
450
|
+
);
|
|
451
|
+
const app = mcpRoutes({ ...baseConfig, registry: reg });
|
|
452
|
+
await rpc(app, "initialize");
|
|
453
|
+
await rpc(app, "initialize");
|
|
454
|
+
expect(warn).toHaveBeenCalledTimes(1);
|
|
455
|
+
warn.mockClear();
|
|
456
|
+
mcpRoutes({
|
|
457
|
+
...baseConfig,
|
|
458
|
+
registry: buildRegistry(
|
|
459
|
+
App({ operations: {}, instructions: "x".repeat(4096) }),
|
|
460
|
+
),
|
|
461
|
+
});
|
|
462
|
+
expect(warn).toHaveBeenCalledTimes(0);
|
|
463
|
+
} finally {
|
|
464
|
+
warn.mockRestore();
|
|
465
|
+
}
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
test("ping answers an empty result", async () => {
|
|
469
|
+
const app = mcpRoutes({ ...baseConfig, registry: makeApp({}) });
|
|
470
|
+
expect(await rpc(app, "ping")).toEqual<unknown>({
|
|
471
|
+
jsonrpc: "2.0",
|
|
472
|
+
id: 1,
|
|
473
|
+
result: {},
|
|
474
|
+
});
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
test("templates and prompts list empty", async () => {
|
|
478
|
+
const app = mcpRoutes({ ...baseConfig, registry: makeApp({}) });
|
|
479
|
+
expect(
|
|
480
|
+
(await rpc(app, "resources/templates/list")).result.resourceTemplates,
|
|
481
|
+
).toEqual([]);
|
|
482
|
+
expect((await rpc(app, "prompts/list")).result.prompts).toEqual([]);
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
test("any notification is 202 with an empty body", async () => {
|
|
486
|
+
const app = mcpRoutes({ ...baseConfig, registry: makeApp({}) });
|
|
487
|
+
for (const method of [
|
|
488
|
+
"notifications/initialized",
|
|
489
|
+
"notifications/roots/list_changed",
|
|
490
|
+
]) {
|
|
491
|
+
const res = await app.request("/mcp", {
|
|
492
|
+
method: "POST",
|
|
493
|
+
headers: { "content-type": "application/json" },
|
|
494
|
+
body: JSON.stringify({ jsonrpc: "2.0", method }),
|
|
495
|
+
});
|
|
496
|
+
expect(res.status).toBe(202);
|
|
497
|
+
expect(await res.text()).toBe("");
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
test("unknown method, tool and resource are -32601 with HTTP 200", async () => {
|
|
502
|
+
const app = mcpRoutes({ ...baseConfig, registry: makeApp({}) });
|
|
503
|
+
const calls: Array<[string, Record<string, unknown>?]> = [
|
|
504
|
+
["nope/method"],
|
|
505
|
+
["tools/call", { name: "nope" }],
|
|
506
|
+
["resources/read", { uri: "ui://nope" }],
|
|
507
|
+
];
|
|
508
|
+
for (const [method, params] of calls) {
|
|
509
|
+
const res = await app.request("/mcp", {
|
|
510
|
+
method: "POST",
|
|
511
|
+
headers: { "content-type": "application/json" },
|
|
512
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }),
|
|
513
|
+
});
|
|
514
|
+
expect(res.status).toBe(200);
|
|
515
|
+
expect(((await res.json()) as any).error.code).toBe(-32601);
|
|
516
|
+
}
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
test("path mounts the endpoint elsewhere; metadata:false skips the metadata route", async () => {
|
|
520
|
+
const app = mcpRoutes({
|
|
521
|
+
...baseConfig,
|
|
522
|
+
registry: makeApp({}),
|
|
523
|
+
path: "/agent",
|
|
524
|
+
metadata: false,
|
|
525
|
+
});
|
|
526
|
+
const post = (p: string) =>
|
|
527
|
+
app.request(p, {
|
|
528
|
+
method: "POST",
|
|
529
|
+
headers: { "content-type": "application/json" },
|
|
530
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "ping" }),
|
|
531
|
+
});
|
|
532
|
+
expect((await post("/agent")).status).toBe(200);
|
|
533
|
+
expect((await post("/mcp")).status).toBe(404);
|
|
534
|
+
expect((await app.request("/agent")).status).toBe(405);
|
|
535
|
+
expect(
|
|
536
|
+
(await app.request("/.well-known/oauth-protected-resource")).status,
|
|
537
|
+
).toBe(404);
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
describe("tool listing", () => {
|
|
541
|
+
const schema = { type: "object", properties: { n: { type: "number" } } };
|
|
542
|
+
const withResponse = (cfg: Record<string, unknown> = {}) =>
|
|
543
|
+
buildRegistry(App({ operations: { op: makeOp("op", cfg) } }), {
|
|
544
|
+
op: schema,
|
|
545
|
+
});
|
|
546
|
+
const list = async (config: Record<string, unknown>) =>
|
|
547
|
+
(await rpc(mcpRoutes({ ...baseConfig, ...config } as any), "tools/list"))
|
|
548
|
+
.result.tools[0];
|
|
549
|
+
|
|
550
|
+
test("an operation with empty input still lists an object schema", async () => {
|
|
551
|
+
const tool = await list({
|
|
552
|
+
registry: makeApp({ op: makeOp("op", { input: {} }) }),
|
|
553
|
+
});
|
|
554
|
+
expect(tool.inputSchema.type).toBe("object");
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
test("tools carry title, idempotentHint and operation meta", async () => {
|
|
558
|
+
const op = makeOp("keep", {
|
|
559
|
+
type: "mutation",
|
|
560
|
+
title: "Keep candidate",
|
|
561
|
+
idempotent: true,
|
|
562
|
+
meta: { "x/y": 1 },
|
|
563
|
+
});
|
|
564
|
+
const tool = await list({ registry: makeApp({ op }) });
|
|
565
|
+
expect(tool.title).toBe("Keep candidate");
|
|
566
|
+
expect(tool.annotations).toEqual({
|
|
567
|
+
readOnlyHint: false,
|
|
568
|
+
destructiveHint: false,
|
|
569
|
+
openWorldHint: false,
|
|
570
|
+
idempotentHint: true,
|
|
571
|
+
});
|
|
572
|
+
expect(tool._meta).toMatchObject({ "x/y": 1 });
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
test("widget keys win over operation meta", async () => {
|
|
576
|
+
const op = makeOp("ui", {
|
|
577
|
+
widget: "boards",
|
|
578
|
+
meta: { ui: "mine", "x/y": 1 },
|
|
579
|
+
});
|
|
580
|
+
const tool = await list({
|
|
581
|
+
registry: makeApp({ op }),
|
|
582
|
+
widgets: { boards: { uri: "ui://w/b.html", html: "<div/>" } },
|
|
583
|
+
});
|
|
584
|
+
expect(tool._meta.ui.resourceUri).toBe("ui://w/b.html");
|
|
585
|
+
expect(tool._meta["x/y"]).toBe(1);
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
test("outputSchema wraps the response schema under responseKey", async () => {
|
|
589
|
+
const tool = await list({ registry: withResponse() });
|
|
590
|
+
expect(tool.outputSchema).toEqual({
|
|
591
|
+
type: "object",
|
|
592
|
+
properties: { op: schema, actions: { type: "array" }, display: {} },
|
|
593
|
+
required: ["op"],
|
|
594
|
+
});
|
|
595
|
+
});
|
|
596
|
+
|
|
597
|
+
test("no outputSchema when enrichResponse is set without an outputSchema option", async () => {
|
|
598
|
+
const tool = await list({
|
|
599
|
+
registry: withResponse(),
|
|
600
|
+
enrichResponse: async () => {},
|
|
601
|
+
});
|
|
602
|
+
expect(tool.outputSchema).toBeUndefined();
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
test("the outputSchema option replaces or removes the schema", async () => {
|
|
606
|
+
const custom = { type: "object", properties: {} };
|
|
607
|
+
const replaced = await list({
|
|
608
|
+
registry: withResponse(),
|
|
609
|
+
enrichResponse: async () => {},
|
|
610
|
+
outputSchema: () => custom,
|
|
611
|
+
});
|
|
612
|
+
expect(replaced.outputSchema).toEqual(custom);
|
|
613
|
+
const removed = await list({
|
|
614
|
+
registry: withResponse(),
|
|
615
|
+
outputSchema: () => undefined,
|
|
616
|
+
});
|
|
617
|
+
expect(removed.outputSchema).toBeUndefined();
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
test("no outputSchema without a response schema", async () => {
|
|
621
|
+
const tool = await list({ registry: makeApp({ op: makeOp("op") }) });
|
|
622
|
+
expect(tool.outputSchema).toBeUndefined();
|
|
623
|
+
expect(tool._meta).toBeUndefined();
|
|
624
|
+
});
|
|
625
|
+
});
|
|
626
|
+
});
|
|
627
|
+
|
|
628
|
+
describe("mcpRoutes exposure", () => {
|
|
629
|
+
const ops = () =>
|
|
630
|
+
makeApp({
|
|
631
|
+
p: makeOp("p"),
|
|
632
|
+
q: makeOp("q"),
|
|
633
|
+
a: makeOp("a", { appCallable: true, meta: { ui: { x: 1 } } }),
|
|
634
|
+
m2: makeOp("m2", { type: "mutation" }),
|
|
635
|
+
d2: makeOp("d2", { type: "mutation", destructive: true }),
|
|
636
|
+
});
|
|
637
|
+
const exposed = (path = "/mcp") =>
|
|
638
|
+
mcpRoutes({
|
|
639
|
+
...baseConfig,
|
|
640
|
+
registry: ops(),
|
|
641
|
+
path,
|
|
642
|
+
exposure: { default: "catalog", pin: ["p"] },
|
|
643
|
+
});
|
|
644
|
+
|
|
645
|
+
test("hybrid: catalog queries leave tools/list; pins get alwaysLoad; app-only get visibility", async () => {
|
|
646
|
+
const { result } = await rpc(exposed(), "tools/list");
|
|
647
|
+
const tools = result.tools as any[];
|
|
648
|
+
const names = tools.map((t) => t.name);
|
|
649
|
+
expect(names).not.toContain("q");
|
|
650
|
+
expect(names).not.toContain("m2");
|
|
651
|
+
expect(
|
|
652
|
+
tools.find((t) => t.name === "p")._meta["anthropic/alwaysLoad"],
|
|
653
|
+
).toBe(true);
|
|
654
|
+
expect(tools.find((t) => t.name === "a")._meta.ui).toEqual({
|
|
655
|
+
x: 1,
|
|
656
|
+
visibility: ["app"],
|
|
657
|
+
});
|
|
658
|
+
});
|
|
659
|
+
|
|
660
|
+
test("two mounts with different exposure serve one registry", async () => {
|
|
661
|
+
const registry = ops();
|
|
662
|
+
const full = mcpRoutes({ ...baseConfig, registry });
|
|
663
|
+
const cat = mcpRoutes({
|
|
664
|
+
...baseConfig,
|
|
665
|
+
registry,
|
|
666
|
+
path: "/mcp/catalog",
|
|
667
|
+
exposure: { default: "catalog" },
|
|
668
|
+
});
|
|
669
|
+
const a = await rpc(full, "tools/list");
|
|
670
|
+
expect(a.result.tools.map((t: any) => t.name)).toContain("q");
|
|
671
|
+
const res = await cat.request("/mcp/catalog", {
|
|
672
|
+
method: "POST",
|
|
673
|
+
headers: { "content-type": "application/json" },
|
|
674
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/list" }),
|
|
675
|
+
});
|
|
676
|
+
const b = (await res.json()) as any;
|
|
677
|
+
expect(b.result.tools.map((t: any) => t.name)).not.toContain("q");
|
|
678
|
+
});
|
|
679
|
+
|
|
680
|
+
test("a catalog query is callable by name", async () => {
|
|
681
|
+
const { result } = await rpc(exposed(), "tools/call", {
|
|
682
|
+
name: "q",
|
|
683
|
+
arguments: {},
|
|
684
|
+
});
|
|
685
|
+
expect(result.structuredContent).toBeDefined();
|
|
686
|
+
expect(result.isError).toBeUndefined();
|
|
687
|
+
});
|
|
688
|
+
|
|
689
|
+
test("an unlisted catalog mutation is refused by name with a pointer", async () => {
|
|
690
|
+
const { result, error } = await rpc(exposed(), "tools/call", {
|
|
691
|
+
name: "m2",
|
|
692
|
+
arguments: {},
|
|
693
|
+
});
|
|
694
|
+
expect(result).toBeUndefined();
|
|
695
|
+
expect(error.code).toBe(-32601);
|
|
696
|
+
expect(error.message).toMatch(/run_write/);
|
|
697
|
+
const d = await rpc(exposed(), "tools/call", { name: "d2", arguments: {} });
|
|
698
|
+
expect(d.error.message).toMatch(/run_destructive/);
|
|
699
|
+
});
|
|
700
|
+
|
|
701
|
+
test("unknown pin throws at construction", () => {
|
|
702
|
+
expect(() =>
|
|
703
|
+
mcpRoutes({
|
|
704
|
+
...baseConfig,
|
|
705
|
+
registry: ops(),
|
|
706
|
+
exposure: { pin: ["nope"] },
|
|
707
|
+
}),
|
|
708
|
+
).toThrow(/nope/);
|
|
709
|
+
});
|
|
710
|
+
});
|
|
711
|
+
|
|
712
|
+
describe("invalid method", () => {
|
|
713
|
+
test("a non-string method is Invalid Request, not a 500", async () => {
|
|
714
|
+
const app = mcpRoutes({
|
|
715
|
+
resource: "https://app.example.com",
|
|
716
|
+
authorizationServers: ["https://auth.example.com"],
|
|
717
|
+
scopes: ["read"],
|
|
718
|
+
authenticate: async () => ({ userId: "u1" }) as any,
|
|
719
|
+
registry: buildRegistry(App({ operations: {} })),
|
|
720
|
+
executor: (async () => ({})) as any,
|
|
721
|
+
});
|
|
722
|
+
const res = await app.request("/mcp", {
|
|
723
|
+
method: "POST",
|
|
724
|
+
headers: { "content-type": "application/json" },
|
|
725
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: 123 }),
|
|
726
|
+
});
|
|
727
|
+
expect(res.status).toBe(400);
|
|
728
|
+
expect(((await res.json()) as any).error.code).toBe(-32600);
|
|
729
|
+
});
|
|
730
|
+
});
|
|
731
|
+
|
|
732
|
+
describe("Next block formats", () => {
|
|
733
|
+
const nextText = async (exposure?: any) => {
|
|
734
|
+
const list = makeOp("list", {}, () =>
|
|
735
|
+
Brief({
|
|
736
|
+
children: [
|
|
737
|
+
Message({ children: "Done." }),
|
|
738
|
+
Action({ operation: "q", label: "Read", variables: { a: 1 } }),
|
|
739
|
+
Action({ operation: "m2", label: "Write" }),
|
|
740
|
+
Action({ operation: "d2", label: "Drop", variables: { id: "x" } }),
|
|
741
|
+
Action({ operation: "p", label: "Tool", variables: { b: "c" } }),
|
|
742
|
+
],
|
|
743
|
+
}),
|
|
744
|
+
);
|
|
745
|
+
const registry = makeApp({
|
|
746
|
+
list,
|
|
747
|
+
p: makeOp("p"),
|
|
748
|
+
q: makeOp("q"),
|
|
749
|
+
m2: makeOp("m2", { type: "mutation" }),
|
|
750
|
+
d2: makeOp("d2", { type: "mutation", destructive: true }),
|
|
751
|
+
});
|
|
752
|
+
const app = mcpRoutes({ ...baseConfig, registry, exposure });
|
|
753
|
+
const { result } = await rpc(app, "tools/call", {
|
|
754
|
+
name: "list",
|
|
755
|
+
arguments: {},
|
|
756
|
+
});
|
|
757
|
+
return result.content[0].text as string;
|
|
758
|
+
};
|
|
759
|
+
|
|
760
|
+
test("listed tools carry compact variables inside the parentheses", async () => {
|
|
761
|
+
const text = await nextText();
|
|
762
|
+
expect(text).toContain('- Read (q {"a":1})');
|
|
763
|
+
expect(text).toContain("- Write (m2)");
|
|
764
|
+
expect(text).toContain('- Tool (p {"b":"c"})');
|
|
765
|
+
});
|
|
766
|
+
|
|
767
|
+
test("catalog-placed operations name their run_* tool", async () => {
|
|
768
|
+
const text = await nextText({ default: "catalog", pin: ["p", "list"] });
|
|
769
|
+
expect(text).toContain('- Read (run_read id="q" variables={"a":1})');
|
|
770
|
+
expect(text).toContain('- Write (run_write id="m2")');
|
|
771
|
+
expect(text).toContain(
|
|
772
|
+
'- Drop (run_destructive id="d2" variables={"id":"x"})',
|
|
773
|
+
);
|
|
774
|
+
expect(text).toContain('- Tool (p {"b":"c"})');
|
|
775
|
+
});
|
|
776
|
+
});
|
|
777
|
+
|
|
778
|
+
describe("request body shape", () => {
|
|
779
|
+
const post = async (body: unknown) => {
|
|
780
|
+
const app = mcpRoutes({
|
|
781
|
+
...baseConfig,
|
|
782
|
+
registry: makeApp({ q: makeOp("q") }),
|
|
783
|
+
});
|
|
784
|
+
const res = await app.request("/mcp", {
|
|
785
|
+
method: "POST",
|
|
786
|
+
headers: { "content-type": "application/json" },
|
|
787
|
+
body: JSON.stringify(body),
|
|
788
|
+
});
|
|
789
|
+
return { status: res.status, json: (await res.json()) as any };
|
|
790
|
+
};
|
|
791
|
+
|
|
792
|
+
for (const [name, body] of [
|
|
793
|
+
["null", null],
|
|
794
|
+
["an array", [{ jsonrpc: "2.0", id: 1, method: "ping" }]],
|
|
795
|
+
["a number", 5],
|
|
796
|
+
["a string", "x"],
|
|
797
|
+
] as const) {
|
|
798
|
+
test(`${name} is -32600 Invalid Request with HTTP 400`, async () => {
|
|
799
|
+
const r = await post(body);
|
|
800
|
+
expect(r.status).toBe(400);
|
|
801
|
+
expect(r.json.error.code).toBe(-32600);
|
|
802
|
+
});
|
|
803
|
+
}
|
|
804
|
+
});
|
|
805
|
+
|
|
806
|
+
describe("widget meta merge", () => {
|
|
807
|
+
test("tools/list keeps an existing meta.ui next to the widget uri", async () => {
|
|
808
|
+
const app = mcpRoutes({
|
|
809
|
+
...baseConfig,
|
|
810
|
+
registry: makeApp({
|
|
811
|
+
ui: makeOp("ui", {
|
|
812
|
+
widget: "boards",
|
|
813
|
+
meta: { ui: { visibility: ["app"], resourceUri: "ui://old" } },
|
|
814
|
+
}),
|
|
815
|
+
}),
|
|
816
|
+
widgets: { boards: { uri: "ui://w/b.html", html: "<div/>" } },
|
|
817
|
+
});
|
|
818
|
+
const { result } = await rpc(app, "tools/list");
|
|
819
|
+
expect(result.tools[0]._meta.ui).toEqual({
|
|
820
|
+
visibility: ["app"],
|
|
821
|
+
resourceUri: "ui://w/b.html",
|
|
822
|
+
});
|
|
823
|
+
});
|
|
313
824
|
});
|