@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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rezza Hub
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@donghanh/hono",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Hono routes for
|
|
3
|
+
"version": "0.4.1",
|
|
4
|
+
"description": "Hono routes for Apps SDK, LLM chat and GPT Store (deprecated) — renderers for ChatGPT and OpenAI-compatible APIs",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"exports": {
|
|
@@ -13,7 +13,10 @@
|
|
|
13
13
|
"directory": "packages/hono"
|
|
14
14
|
},
|
|
15
15
|
"peerDependencies": {
|
|
16
|
-
"@donghanh/core": "^0.
|
|
16
|
+
"@donghanh/core": "^0.4.0",
|
|
17
17
|
"hono": "^4.7.0"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@donghanh/core": "0.4.1"
|
|
18
21
|
}
|
|
19
|
-
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Executor,
|
|
3
|
+
OperationComponent,
|
|
4
|
+
OperationConfig,
|
|
5
|
+
} from "@donghanh/core";
|
|
6
|
+
import { App, Brief, buildRegistry, Message } from "@donghanh/core";
|
|
7
|
+
import { describe, expect, test } from "vitest";
|
|
8
|
+
import { mcpRoutes } from "./mcp";
|
|
9
|
+
|
|
10
|
+
function makeOp(
|
|
11
|
+
id: string,
|
|
12
|
+
overrides: Partial<OperationConfig> = {},
|
|
13
|
+
): OperationComponent {
|
|
14
|
+
const op = (() =>
|
|
15
|
+
Brief({
|
|
16
|
+
children: [Message({ children: "ok" })],
|
|
17
|
+
})) as unknown as OperationComponent;
|
|
18
|
+
op.operationConfig = {
|
|
19
|
+
id,
|
|
20
|
+
type: "query",
|
|
21
|
+
description: `${id} desc`,
|
|
22
|
+
instruction: `call ${id}`,
|
|
23
|
+
input: { type: "object", properties: {} },
|
|
24
|
+
responseKey: id,
|
|
25
|
+
...overrides,
|
|
26
|
+
};
|
|
27
|
+
return op;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const reg = (ops: Record<string, OperationComponent>) =>
|
|
31
|
+
buildRegistry(App({ operations: ops }));
|
|
32
|
+
|
|
33
|
+
const baseConfig = {
|
|
34
|
+
resource: "https://app.example.com",
|
|
35
|
+
authorizationServers: ["https://auth.example.com"],
|
|
36
|
+
scopes: ["read"],
|
|
37
|
+
authenticate: async () => ({ userId: "u1" }) as any,
|
|
38
|
+
executor: (async (opId) => ({ opId })) as Executor,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
async function rpc(
|
|
42
|
+
app: ReturnType<typeof mcpRoutes>,
|
|
43
|
+
method: string,
|
|
44
|
+
params?: Record<string, unknown>,
|
|
45
|
+
) {
|
|
46
|
+
const res = await app.request("/mcp", {
|
|
47
|
+
method: "POST",
|
|
48
|
+
headers: { "content-type": "application/json" },
|
|
49
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }),
|
|
50
|
+
});
|
|
51
|
+
return res.json() as Promise<{ result?: any; error?: any }>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const call = (
|
|
55
|
+
app: ReturnType<typeof mcpRoutes>,
|
|
56
|
+
name: string,
|
|
57
|
+
args: Record<string, unknown>,
|
|
58
|
+
) => rpc(app, "tools/call", { name, arguments: args });
|
|
59
|
+
|
|
60
|
+
const catalogApp = (
|
|
61
|
+
ops: Record<string, OperationComponent>,
|
|
62
|
+
extra: Record<string, unknown> = {},
|
|
63
|
+
) =>
|
|
64
|
+
mcpRoutes({
|
|
65
|
+
...baseConfig,
|
|
66
|
+
registry: reg(ops),
|
|
67
|
+
exposure: { default: "catalog" },
|
|
68
|
+
...extra,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const names = async (app: ReturnType<typeof mcpRoutes>) =>
|
|
72
|
+
((await rpc(app, "tools/list")).result.tools as any[]).map((t) => t.name);
|
|
73
|
+
|
|
74
|
+
describe("catalog meta-tools", () => {
|
|
75
|
+
test("only applicable meta-tools are listed", async () => {
|
|
76
|
+
const app = catalogApp({ q: makeOp("q"), r: makeOp("r") });
|
|
77
|
+
expect(await names(app)).toEqual([
|
|
78
|
+
"find_operations",
|
|
79
|
+
"get_operation",
|
|
80
|
+
"run_read",
|
|
81
|
+
]);
|
|
82
|
+
const all = catalogApp({
|
|
83
|
+
q: makeOp("q"),
|
|
84
|
+
m: makeOp("m", { type: "mutation" }),
|
|
85
|
+
d: makeOp("d", { type: "mutation", destructive: true }),
|
|
86
|
+
});
|
|
87
|
+
expect(await names(all)).toEqual([
|
|
88
|
+
"find_operations",
|
|
89
|
+
"get_operation",
|
|
90
|
+
"run_read",
|
|
91
|
+
"run_write",
|
|
92
|
+
"run_destructive",
|
|
93
|
+
]);
|
|
94
|
+
const none = mcpRoutes({
|
|
95
|
+
...baseConfig,
|
|
96
|
+
registry: reg({ q: makeOp("q") }),
|
|
97
|
+
});
|
|
98
|
+
expect(await names(none)).toEqual(["q"]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("meta-tool hints", async () => {
|
|
102
|
+
const app = catalogApp({
|
|
103
|
+
q: makeOp("q"),
|
|
104
|
+
m: makeOp("m", { type: "mutation" }),
|
|
105
|
+
d: makeOp("d", { type: "mutation", destructive: true }),
|
|
106
|
+
});
|
|
107
|
+
const tools = (await rpc(app, "tools/list")).result.tools as any[];
|
|
108
|
+
const by = (n: string) => tools.find((t) => t.name === n);
|
|
109
|
+
for (const n of ["find_operations", "get_operation", "run_read"]) {
|
|
110
|
+
expect(by(n).annotations).toEqual({
|
|
111
|
+
readOnlyHint: true,
|
|
112
|
+
destructiveHint: false,
|
|
113
|
+
openWorldHint: false,
|
|
114
|
+
idempotentHint: true,
|
|
115
|
+
});
|
|
116
|
+
expect(typeof by(n).title).toBe("string");
|
|
117
|
+
}
|
|
118
|
+
expect(by("run_write").annotations).toEqual({
|
|
119
|
+
readOnlyHint: false,
|
|
120
|
+
destructiveHint: false,
|
|
121
|
+
openWorldHint: false,
|
|
122
|
+
idempotentHint: false,
|
|
123
|
+
});
|
|
124
|
+
expect(by("run_destructive").annotations.destructiveHint).toBe(true);
|
|
125
|
+
expect(by("run_write").inputSchema.required).toEqual([
|
|
126
|
+
"id",
|
|
127
|
+
"variables",
|
|
128
|
+
"intent",
|
|
129
|
+
]);
|
|
130
|
+
expect(by("run_write").inputSchema.properties.intent.minLength).toBe(1);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("securitySchemes is the union over the class's operations", async () => {
|
|
134
|
+
const app = catalogApp({
|
|
135
|
+
a: makeOp("a", { auth: "none" }),
|
|
136
|
+
b: makeOp("b", { auth: "required", scopes: ["s"] }),
|
|
137
|
+
});
|
|
138
|
+
const tools = (await rpc(app, "tools/list")).result.tools as any[];
|
|
139
|
+
const schemes = tools.find((t) => t.name === "run_read").securitySchemes;
|
|
140
|
+
expect(schemes).toContainEqual({ type: "noauth" });
|
|
141
|
+
const oauth = schemes.find((s: any) => s.type === "oauth2");
|
|
142
|
+
expect(oauth.scopes).toContain("s");
|
|
143
|
+
expect(oauth.scopes).toContain("read");
|
|
144
|
+
expect(schemes.filter((s: any) => s.type === "oauth2")).toHaveLength(1);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test("find_operations hides required operations from anonymous callers", async () => {
|
|
148
|
+
const app = catalogApp(
|
|
149
|
+
{
|
|
150
|
+
open: makeOp("open", { auth: "none" }),
|
|
151
|
+
opt: makeOp("opt", { auth: "optional" }),
|
|
152
|
+
req: makeOp("req", { auth: "required" }),
|
|
153
|
+
},
|
|
154
|
+
{ authenticate: async () => ({ error: "no" }) },
|
|
155
|
+
);
|
|
156
|
+
const { result } = await call(app, "find_operations", {});
|
|
157
|
+
const ids = result.structuredContent.operations.map((o: any) => o.id);
|
|
158
|
+
expect(ids.sort()).toEqual(["open", "opt"]);
|
|
159
|
+
expect(result.structuredContent.hint).toBe(
|
|
160
|
+
"Call get_operation with an id before running it.",
|
|
161
|
+
);
|
|
162
|
+
expect(JSON.parse(result.content[0].text)).toEqual(
|
|
163
|
+
result.structuredContent,
|
|
164
|
+
);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test("find_operations matches id, title, description and tags case-insensitively", async () => {
|
|
168
|
+
const app = catalogApp({
|
|
169
|
+
alpha: makeOp("alpha"),
|
|
170
|
+
b: makeOp("b", { title: "Zebra Finder" }),
|
|
171
|
+
c: makeOp("c", { description: "Counts PANDAS" }),
|
|
172
|
+
d: makeOp("d", { tags: ["Mammal"] }),
|
|
173
|
+
});
|
|
174
|
+
const find = async (args: Record<string, unknown>) =>
|
|
175
|
+
(
|
|
176
|
+
(await call(app, "find_operations", args)).result.structuredContent
|
|
177
|
+
.operations as any[]
|
|
178
|
+
).map((o) => o.id);
|
|
179
|
+
expect(await find({ query: "ALPH" })).toEqual(["alpha"]);
|
|
180
|
+
expect(await find({ query: "zebra" })).toEqual(["b"]);
|
|
181
|
+
expect(await find({ query: "pandas" })).toEqual(["c"]);
|
|
182
|
+
expect(await find({ query: "mammal" })).toEqual(["d"]);
|
|
183
|
+
expect(await find({ tags: ["mammal"] })).toEqual(["d"]);
|
|
184
|
+
expect((await find({})).length).toBe(4);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test("get_operation returns input, instruction, response and siblings", async () => {
|
|
188
|
+
const app = catalogApp({
|
|
189
|
+
q: makeOp("q", { input: { type: "object", properties: { x: {} } } }),
|
|
190
|
+
r: makeOp("r"),
|
|
191
|
+
});
|
|
192
|
+
const { result } = await call(app, "get_operation", { id: "q" });
|
|
193
|
+
const s = result.structuredContent;
|
|
194
|
+
expect(s.id).toBe("q");
|
|
195
|
+
expect(s.instruction).toBe("call q");
|
|
196
|
+
expect(s.input.properties.x).toEqual({});
|
|
197
|
+
expect(s.operations.map((o: any) => o.id)).toEqual(["q", "r"]);
|
|
198
|
+
expect(JSON.parse(result.content[0].text)).toEqual(s);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test("get_operation errors for unknown and listed-tool ids", async () => {
|
|
202
|
+
const app = mcpRoutes({
|
|
203
|
+
...baseConfig,
|
|
204
|
+
registry: reg({ q: makeOp("q"), t: makeOp("t") }),
|
|
205
|
+
exposure: { catalog: { ids: ["q"] } },
|
|
206
|
+
});
|
|
207
|
+
const u = (await call(app, "get_operation", { id: "zz" })).result;
|
|
208
|
+
expect(u.isError).toBe(true);
|
|
209
|
+
expect(u.content[0].text).toBe(
|
|
210
|
+
'Unknown operation "zz". Call find_operations to list available operations.',
|
|
211
|
+
);
|
|
212
|
+
const t = (await call(app, "get_operation", { id: "t" })).result;
|
|
213
|
+
expect(t.isError).toBe(true);
|
|
214
|
+
expect(t.content[0].text).toBe(
|
|
215
|
+
'"t" is a tool on this endpoint; call it directly.',
|
|
216
|
+
);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test("run_read runs a catalog query like a direct call", async () => {
|
|
220
|
+
const seen: any[] = [];
|
|
221
|
+
const app = catalogApp(
|
|
222
|
+
{ q: makeOp("q") },
|
|
223
|
+
{
|
|
224
|
+
executor: (async (opId: string, vars: any) => {
|
|
225
|
+
seen.push([opId, vars]);
|
|
226
|
+
return { n: 1 };
|
|
227
|
+
}) as Executor,
|
|
228
|
+
enrichResponse: async (s: any, o: any) => {
|
|
229
|
+
s.enrichedFor = o.operationId;
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
);
|
|
233
|
+
const { result } = await call(app, "run_read", {
|
|
234
|
+
id: "q",
|
|
235
|
+
variables: { a: 1 },
|
|
236
|
+
});
|
|
237
|
+
expect(seen).toEqual([["q", { a: 1 }]]);
|
|
238
|
+
expect(result.structuredContent.q).toEqual({ n: 1 });
|
|
239
|
+
expect(result.structuredContent.enrichedFor).toBe("q");
|
|
240
|
+
expect(result.isError).toBeUndefined();
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
test("run_* refuse the wrong class and name the right tool", async () => {
|
|
244
|
+
const app = catalogApp({
|
|
245
|
+
q: makeOp("q"),
|
|
246
|
+
m: makeOp("m", { type: "mutation" }),
|
|
247
|
+
d: makeOp("d", { type: "mutation", destructive: true }),
|
|
248
|
+
});
|
|
249
|
+
const a = (await call(app, "run_read", { id: "m", variables: {} })).result;
|
|
250
|
+
expect(a.isError).toBe(true);
|
|
251
|
+
expect(a.content[0].text).toBe('"m" is a mutation; use run_write.');
|
|
252
|
+
const b = (
|
|
253
|
+
await call(app, "run_write", { id: "d", variables: {}, intent: "x" })
|
|
254
|
+
).result;
|
|
255
|
+
expect(b.content[0].text).toBe(
|
|
256
|
+
'"d" is a destructive mutation; use run_destructive.',
|
|
257
|
+
);
|
|
258
|
+
const c = (
|
|
259
|
+
await call(app, "run_destructive", {
|
|
260
|
+
id: "q",
|
|
261
|
+
variables: {},
|
|
262
|
+
intent: "x",
|
|
263
|
+
})
|
|
264
|
+
).result;
|
|
265
|
+
expect(c.content[0].text).toBe('"q" is a query; use run_read.');
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
test("run_write requires intent and passes it to the executor", async () => {
|
|
269
|
+
const ctxs: any[] = [];
|
|
270
|
+
const app = catalogApp(
|
|
271
|
+
{ m: makeOp("m", { type: "mutation" }) },
|
|
272
|
+
{
|
|
273
|
+
executor: (async (_o: string, _v: any, ctx: any) => {
|
|
274
|
+
ctxs.push(ctx);
|
|
275
|
+
return {};
|
|
276
|
+
}) as Executor,
|
|
277
|
+
},
|
|
278
|
+
);
|
|
279
|
+
for (const args of [
|
|
280
|
+
{ id: "m", variables: {} },
|
|
281
|
+
{ id: "m", variables: {}, intent: "" },
|
|
282
|
+
]) {
|
|
283
|
+
const r = (await call(app, "run_write", args)).result;
|
|
284
|
+
expect(r.isError).toBe(true);
|
|
285
|
+
expect(r.content[0].text).toBe(
|
|
286
|
+
"intent is required: one sentence saying what this call does for the user.",
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
expect(ctxs).toHaveLength(0);
|
|
290
|
+
const ok = (
|
|
291
|
+
await call(app, "run_write", {
|
|
292
|
+
id: "m",
|
|
293
|
+
variables: {},
|
|
294
|
+
intent: "Keep it",
|
|
295
|
+
})
|
|
296
|
+
).result;
|
|
297
|
+
expect(ok.isError).toBeUndefined();
|
|
298
|
+
expect(ctxs[0].intent).toBe("Keep it");
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
test("run_read with an unknown id points to find_operations", async () => {
|
|
302
|
+
const app = catalogApp({ q: makeOp("q") });
|
|
303
|
+
const r = (await call(app, "run_read", { id: "nope", variables: {} }))
|
|
304
|
+
.result;
|
|
305
|
+
expect(r.isError).toBe(true);
|
|
306
|
+
expect(r.content[0].text).toMatch(/find_operations/);
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
test("an app operation named like a generated meta-tool throws at startup", () => {
|
|
310
|
+
expect(() =>
|
|
311
|
+
catalogApp({ run_read: makeOp("run_read"), q: makeOp("q") }),
|
|
312
|
+
).toThrow(/run_read/);
|
|
313
|
+
// not generated (no catalog ops): no conflict
|
|
314
|
+
expect(() =>
|
|
315
|
+
mcpRoutes({
|
|
316
|
+
...baseConfig,
|
|
317
|
+
registry: reg({ run_read: makeOp("run_read") }),
|
|
318
|
+
}),
|
|
319
|
+
).not.toThrow();
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
test("run_* applies the target operation's auth challenge", async () => {
|
|
323
|
+
const app = catalogApp(
|
|
324
|
+
{ q: makeOp("q", { auth: "required" }) },
|
|
325
|
+
{ authenticate: async () => ({ error: "no" }) },
|
|
326
|
+
);
|
|
327
|
+
const res = await app.request("/mcp", {
|
|
328
|
+
method: "POST",
|
|
329
|
+
headers: { "content-type": "application/json" },
|
|
330
|
+
body: JSON.stringify({
|
|
331
|
+
jsonrpc: "2.0",
|
|
332
|
+
id: 1,
|
|
333
|
+
method: "tools/call",
|
|
334
|
+
params: { name: "run_read", arguments: { id: "q", variables: {} } },
|
|
335
|
+
}),
|
|
336
|
+
});
|
|
337
|
+
expect(res.headers.get("WWW-Authenticate")).toMatch(/resource_metadata/);
|
|
338
|
+
const body = (await res.json()) as any;
|
|
339
|
+
expect(body.result.isError).toBe(true);
|
|
340
|
+
expect(body.result._meta["mcp/www_authenticate"]).toHaveLength(1);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
test("a second mount with default catalog lists find_operations", async () => {
|
|
344
|
+
const registry = reg({ q: makeOp("q") });
|
|
345
|
+
const cat = mcpRoutes({
|
|
346
|
+
...baseConfig,
|
|
347
|
+
registry,
|
|
348
|
+
path: "/mcp/catalog",
|
|
349
|
+
exposure: { default: "catalog" },
|
|
350
|
+
});
|
|
351
|
+
const res = await cat.request("/mcp/catalog", {
|
|
352
|
+
method: "POST",
|
|
353
|
+
headers: { "content-type": "application/json" },
|
|
354
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/list" }),
|
|
355
|
+
});
|
|
356
|
+
const b = (await res.json()) as any;
|
|
357
|
+
expect(b.result.tools.map((t: any) => t.name)).toContain("find_operations");
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
describe("catalog meta-tools: final review fixes", () => {
|
|
362
|
+
const anon = { authenticate: async () => ({ error: "no" }) };
|
|
363
|
+
|
|
364
|
+
test("run_* reject a non-object variables and do not call the executor", async () => {
|
|
365
|
+
let calls = 0;
|
|
366
|
+
const app = catalogApp(
|
|
367
|
+
{
|
|
368
|
+
q: makeOp("q"),
|
|
369
|
+
m: makeOp("m", { type: "mutation" }),
|
|
370
|
+
d: makeOp("d", { type: "mutation", destructive: true }),
|
|
371
|
+
},
|
|
372
|
+
{
|
|
373
|
+
executor: (async () => {
|
|
374
|
+
calls++;
|
|
375
|
+
return {};
|
|
376
|
+
}) as Executor,
|
|
377
|
+
},
|
|
378
|
+
);
|
|
379
|
+
const cases: [string, string][] = [
|
|
380
|
+
["run_read", "q"],
|
|
381
|
+
["run_write", "m"],
|
|
382
|
+
["run_destructive", "d"],
|
|
383
|
+
];
|
|
384
|
+
for (const [tool, id] of cases) {
|
|
385
|
+
for (const variables of ['{"a":1}', [1]]) {
|
|
386
|
+
const r = (await call(app, tool, { id, variables, intent: "x" }))
|
|
387
|
+
.result;
|
|
388
|
+
expect(r.isError).toBe(true);
|
|
389
|
+
expect(r.content[0].text).toBe("variables must be an object.");
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
expect(calls).toBe(0);
|
|
393
|
+
for (const [tool, id] of cases) {
|
|
394
|
+
for (const variables of [undefined, null]) {
|
|
395
|
+
const r = (await call(app, tool, { id, variables, intent: "x" }))
|
|
396
|
+
.result;
|
|
397
|
+
expect(r.isError).toBeUndefined();
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
expect(calls).toBe(6);
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
test("run_write/run_destructive openWorldHint follows reachable external operations", async () => {
|
|
404
|
+
const hints = async (ops: Record<string, OperationComponent>) => {
|
|
405
|
+
const tools = (await rpc(catalogApp(ops), "tools/list")).result
|
|
406
|
+
.tools as any[];
|
|
407
|
+
const by = (n: string) => tools.find((t) => t.name === n);
|
|
408
|
+
return [
|
|
409
|
+
by("run_write")?.annotations.openWorldHint,
|
|
410
|
+
by("run_destructive")?.annotations.openWorldHint,
|
|
411
|
+
];
|
|
412
|
+
};
|
|
413
|
+
expect(
|
|
414
|
+
await hints({
|
|
415
|
+
m: makeOp("m", { type: "mutation" }),
|
|
416
|
+
d: makeOp("d", { type: "mutation", destructive: true }),
|
|
417
|
+
}),
|
|
418
|
+
).toEqual([false, false]);
|
|
419
|
+
expect(
|
|
420
|
+
await hints({
|
|
421
|
+
m: makeOp("m", { type: "mutation", external: true }),
|
|
422
|
+
d: makeOp("d", { type: "mutation", destructive: true }),
|
|
423
|
+
}),
|
|
424
|
+
).toEqual([true, false]);
|
|
425
|
+
expect(
|
|
426
|
+
await hints({
|
|
427
|
+
m: makeOp("m", { type: "mutation" }),
|
|
428
|
+
d: makeOp("d", {
|
|
429
|
+
type: "mutation",
|
|
430
|
+
destructive: true,
|
|
431
|
+
external: true,
|
|
432
|
+
}),
|
|
433
|
+
}),
|
|
434
|
+
).toEqual([false, true]);
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
test("get_operation hides required siblings from anonymous callers", async () => {
|
|
438
|
+
const app = catalogApp(
|
|
439
|
+
{
|
|
440
|
+
open: makeOp("open", { auth: "none" }),
|
|
441
|
+
req: makeOp("req", { auth: "required" }),
|
|
442
|
+
},
|
|
443
|
+
anon,
|
|
444
|
+
);
|
|
445
|
+
const { result } = await call(app, "get_operation", { id: "open" });
|
|
446
|
+
expect(result.isError).toBeUndefined();
|
|
447
|
+
expect(result.structuredContent.operations.map((o: any) => o.id)).toEqual([
|
|
448
|
+
"open",
|
|
449
|
+
]);
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
test("get_operation on a required operation challenges an anonymous caller", async () => {
|
|
453
|
+
const app = catalogApp({ req: makeOp("req", { auth: "required" }) }, anon);
|
|
454
|
+
const { result } = await call(app, "get_operation", { id: "req" });
|
|
455
|
+
expect(result.isError).toBe(true);
|
|
456
|
+
expect(result._meta["mcp/www_authenticate"]).toHaveLength(1);
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
test("find_operations challenges when hiding removed every operation", async () => {
|
|
460
|
+
const app = catalogApp({ req: makeOp("req", { auth: "required" }) }, anon);
|
|
461
|
+
const { result } = await call(app, "find_operations", {});
|
|
462
|
+
expect(result.isError).toBe(true);
|
|
463
|
+
expect(result._meta["mcp/www_authenticate"]).toHaveLength(1);
|
|
464
|
+
const mixed = catalogApp(
|
|
465
|
+
{
|
|
466
|
+
req: makeOp("req", { auth: "required" }),
|
|
467
|
+
open: makeOp("open", { auth: "none" }),
|
|
468
|
+
},
|
|
469
|
+
anon,
|
|
470
|
+
);
|
|
471
|
+
const ok = (await call(mixed, "find_operations", {})).result;
|
|
472
|
+
expect(ok.isError).toBeUndefined();
|
|
473
|
+
expect(ok.structuredContent.operations.map((o: any) => o.id)).toEqual([
|
|
474
|
+
"open",
|
|
475
|
+
]);
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
test("find_operations matches any whitespace-separated token", async () => {
|
|
479
|
+
const app = catalogApp({
|
|
480
|
+
del: makeOp("del", {
|
|
481
|
+
title: "Delete a candidate",
|
|
482
|
+
description: "Delete a candidate from project",
|
|
483
|
+
}),
|
|
484
|
+
other: makeOp("other"),
|
|
485
|
+
});
|
|
486
|
+
const find = async (query: string) =>
|
|
487
|
+
(
|
|
488
|
+
(await call(app, "find_operations", { query })).result.structuredContent
|
|
489
|
+
.operations as any[]
|
|
490
|
+
).map((o) => o.id);
|
|
491
|
+
expect(await find("delete candidate")).toEqual(["del"]);
|
|
492
|
+
expect(await find(" CANDIDATE zzz ")).toEqual(["del"]);
|
|
493
|
+
expect(await find("zzz qqq")).toEqual([]);
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
test("an appCallable catalog mutation is callable by name and listed app-only", async () => {
|
|
497
|
+
const app = catalogApp({
|
|
498
|
+
m: makeOp("m", { type: "mutation", appCallable: true }),
|
|
499
|
+
q: makeOp("q"),
|
|
500
|
+
});
|
|
501
|
+
const tools = (await rpc(app, "tools/list")).result.tools as any[];
|
|
502
|
+
expect(tools.find((t) => t.name === "m")._meta.ui.visibility).toEqual([
|
|
503
|
+
"app",
|
|
504
|
+
]);
|
|
505
|
+
const r = (await call(app, "m", {})).result;
|
|
506
|
+
expect(r.isError).toBeUndefined();
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
test("run_* on an optional-auth operation runs anonymously with userId null", async () => {
|
|
510
|
+
const users: unknown[] = [];
|
|
511
|
+
const app = catalogApp(
|
|
512
|
+
{ q: makeOp("q", { auth: "optional" }) },
|
|
513
|
+
{
|
|
514
|
+
...anon,
|
|
515
|
+
executor: (async (_o: string, _v: any, ctx: any) => {
|
|
516
|
+
users.push(ctx.userId);
|
|
517
|
+
return {};
|
|
518
|
+
}) as Executor,
|
|
519
|
+
},
|
|
520
|
+
);
|
|
521
|
+
const r = (await call(app, "run_read", { id: "q", variables: {} })).result;
|
|
522
|
+
expect(r.isError).toBeUndefined();
|
|
523
|
+
expect(users).toEqual([null]);
|
|
524
|
+
});
|
|
525
|
+
});
|