@milaboratories/pl-mcp-server 53.1.0 → 55.0.0
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/README.md +30 -37
- package/dist/_virtual/_rolldown/runtime.cjs +22 -0
- package/dist/agent-tables.cjs +116 -0
- package/dist/agent-tables.cjs.map +1 -0
- package/dist/agent-tables.js +115 -0
- package/dist/agent-tables.js.map +1 -0
- package/dist/instructions.cjs +16 -12
- package/dist/instructions.cjs.map +1 -1
- package/dist/instructions.js +16 -12
- package/dist/instructions.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/project-holds.cjs +122 -0
- package/dist/project-holds.cjs.map +1 -0
- package/dist/project-holds.js +122 -0
- package/dist/project-holds.js.map +1 -0
- package/dist/project-list.cjs +16 -0
- package/dist/project-list.cjs.map +1 -0
- package/dist/project-list.js +16 -0
- package/dist/project-list.js.map +1 -0
- package/dist/server-log.cjs +48 -18
- package/dist/server-log.cjs.map +1 -1
- package/dist/server-log.js +48 -19
- package/dist/server-log.js.map +1 -1
- package/dist/server.cjs +71 -18
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.ts +37 -24
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +71 -19
- package/dist/server.js.map +1 -1
- package/dist/tools/agent-tables.cjs +424 -0
- package/dist/tools/agent-tables.cjs.map +1 -0
- package/dist/tools/agent-tables.js +417 -0
- package/dist/tools/agent-tables.js.map +1 -0
- package/dist/tools/await.cjs +216 -72
- package/dist/tools/await.cjs.map +1 -1
- package/dist/tools/await.js +212 -74
- package/dist/tools/await.js.map +1 -1
- package/dist/tools/block-action-context.cjs +14 -9
- package/dist/tools/block-action-context.cjs.map +1 -1
- package/dist/tools/block-action-context.js +14 -9
- package/dist/tools/block-action-context.js.map +1 -1
- package/dist/tools/block-api.cjs +17 -2
- package/dist/tools/block-api.cjs.map +1 -1
- package/dist/tools/block-api.js +17 -3
- package/dist/tools/block-api.js.map +1 -1
- package/dist/tools/block-params.cjs +275 -6
- package/dist/tools/block-params.cjs.map +1 -1
- package/dist/tools/block-params.js +273 -8
- package/dist/tools/block-params.js.map +1 -1
- package/dist/tools/block-state.cjs +343 -57
- package/dist/tools/block-state.cjs.map +1 -1
- package/dist/tools/block-state.js +337 -61
- package/dist/tools/block-state.js.map +1 -1
- package/dist/tools/block-storage.cjs +153 -0
- package/dist/tools/block-storage.cjs.map +1 -0
- package/dist/tools/block-storage.js +147 -0
- package/dist/tools/block-storage.js.map +1 -0
- package/dist/tools/block-updates.cjs +405 -0
- package/dist/tools/block-updates.cjs.map +1 -0
- package/dist/tools/block-updates.js +397 -0
- package/dist/tools/block-updates.js.map +1 -0
- package/dist/tools/blocks.cjs +199 -76
- package/dist/tools/blocks.cjs.map +1 -1
- package/dist/tools/blocks.js +193 -76
- package/dist/tools/blocks.js.map +1 -1
- package/dist/tools/data-query.cjs +470 -101
- package/dist/tools/data-query.cjs.map +1 -1
- package/dist/tools/data-query.js +465 -106
- package/dist/tools/data-query.js.map +1 -1
- package/dist/tools/errors.cjs +21 -0
- package/dist/tools/errors.cjs.map +1 -0
- package/dist/tools/errors.js +20 -0
- package/dist/tools/errors.js.map +1 -0
- package/dist/tools/files.cjs +643 -0
- package/dist/tools/files.cjs.map +1 -0
- package/dist/tools/files.js +613 -0
- package/dist/tools/files.js.map +1 -0
- package/dist/tools/ids.cjs +72 -0
- package/dist/tools/ids.cjs.map +1 -1
- package/dist/tools/ids.js +63 -2
- package/dist/tools/ids.js.map +1 -1
- package/dist/tools/imports.cjs +195 -0
- package/dist/tools/imports.cjs.map +1 -0
- package/dist/tools/imports.js +186 -0
- package/dist/tools/imports.js.map +1 -0
- package/dist/tools/logs.cjs +111 -30
- package/dist/tools/logs.cjs.map +1 -1
- package/dist/tools/logs.js +108 -31
- package/dist/tools/logs.js.map +1 -1
- package/dist/tools/paging.cjs +27 -0
- package/dist/tools/paging.cjs.map +1 -0
- package/dist/tools/paging.js +26 -0
- package/dist/tools/paging.js.map +1 -0
- package/dist/tools/params-merge.cjs +106 -0
- package/dist/tools/params-merge.cjs.map +1 -0
- package/dist/tools/params-merge.js +106 -0
- package/dist/tools/params-merge.js.map +1 -0
- package/dist/tools/project-template.cjs +75 -0
- package/dist/tools/project-template.cjs.map +1 -0
- package/dist/tools/project-template.js +72 -0
- package/dist/tools/project-template.js.map +1 -0
- package/dist/tools/projects.cjs +68 -38
- package/dist/tools/projects.cjs.map +1 -1
- package/dist/tools/projects.js +69 -40
- package/dist/tools/projects.js.map +1 -1
- package/dist/tools/readiness.cjs +104 -0
- package/dist/tools/readiness.cjs.map +1 -0
- package/dist/tools/readiness.js +99 -0
- package/dist/tools/readiness.js.map +1 -0
- package/dist/tools/result-pool.cjs +260 -0
- package/dist/tools/result-pool.cjs.map +1 -0
- package/dist/tools/result-pool.js +252 -0
- package/dist/tools/result-pool.js.map +1 -0
- package/dist/tools/sandbox.cjs +112 -14
- package/dist/tools/sandbox.cjs.map +1 -1
- package/dist/tools/sandbox.js +110 -15
- package/dist/tools/sandbox.js.map +1 -1
- package/dist/tools/sdk-types.cjs +24 -0
- package/dist/tools/sdk-types.cjs.map +1 -0
- package/dist/tools/sdk-types.js +24 -0
- package/dist/tools/sdk-types.js.map +1 -0
- package/dist/tools/settle.cjs +155 -0
- package/dist/tools/settle.cjs.map +1 -0
- package/dist/tools/settle.js +148 -0
- package/dist/tools/settle.js.map +1 -0
- package/dist/tools/tokens.cjs +49 -8
- package/dist/tools/tokens.cjs.map +1 -1
- package/dist/tools/tokens.js +45 -9
- package/dist/tools/tokens.js.map +1 -1
- package/dist/tools/types.cjs +62 -11
- package/dist/tools/types.cjs.map +1 -1
- package/dist/tools/types.js +60 -12
- package/dist/tools/types.js.map +1 -1
- package/dist/tools/value-outline.cjs +119 -0
- package/dist/tools/value-outline.cjs.map +1 -0
- package/dist/tools/value-outline.js +117 -0
- package/dist/tools/value-outline.js.map +1 -0
- package/package.json +9 -8
- package/src/agent-tables.test.ts +246 -0
- package/src/agent-tables.ts +145 -0
- package/src/instructions.ts +16 -12
- package/src/project-holds.test.ts +362 -0
- package/src/project-holds.ts +207 -0
- package/src/project-list.test.ts +38 -0
- package/src/project-list.ts +25 -0
- package/src/server-log.test.ts +71 -6
- package/src/server-log.ts +58 -27
- package/src/server.logging.test.ts +46 -51
- package/src/server.test.ts +166 -22
- package/src/server.ts +97 -37
- package/src/tools/agent-tables.test.ts +650 -0
- package/src/tools/agent-tables.ts +673 -0
- package/src/tools/await.test.ts +287 -0
- package/src/tools/await.ts +393 -122
- package/src/tools/block-action-context.ts +17 -13
- package/src/tools/block-api.ts +19 -4
- package/src/tools/block-params.test.ts +828 -0
- package/src/tools/block-params.ts +626 -10
- package/src/tools/block-state.test.ts +480 -0
- package/src/tools/block-state.ts +678 -83
- package/src/tools/block-storage.test.ts +270 -0
- package/src/tools/block-storage.ts +195 -0
- package/src/tools/block-updates.test.ts +605 -0
- package/src/tools/block-updates.ts +751 -0
- package/src/tools/blocks.test.ts +169 -22
- package/src/tools/blocks.ts +354 -113
- package/src/tools/data-query.test.ts +641 -0
- package/src/tools/data-query.ts +839 -145
- package/src/tools/errors.test.ts +33 -0
- package/src/tools/errors.ts +23 -0
- package/src/tools/files.test.ts +842 -0
- package/src/tools/files.ts +990 -0
- package/src/tools/ids.test.ts +87 -0
- package/src/tools/ids.ts +89 -1
- package/src/tools/imports.ts +304 -0
- package/src/tools/logs.test.ts +70 -0
- package/src/tools/logs.ts +154 -44
- package/src/tools/paging.test.ts +39 -0
- package/src/tools/paging.ts +39 -0
- package/src/tools/params-merge.test.ts +274 -0
- package/src/tools/params-merge.ts +149 -0
- package/src/tools/project-template.test.ts +230 -0
- package/src/tools/project-template.ts +135 -0
- package/src/tools/projects.test.ts +122 -0
- package/src/tools/projects.ts +136 -48
- package/src/tools/readiness.test.ts +195 -0
- package/src/tools/readiness.ts +189 -0
- package/src/tools/result-pool.test.ts +377 -0
- package/src/tools/result-pool.ts +483 -0
- package/src/tools/sandbox.test.ts +128 -10
- package/src/tools/sandbox.ts +132 -18
- package/src/tools/sdk-types.ts +54 -0
- package/src/tools/settle.test.ts +340 -0
- package/src/tools/settle.ts +312 -0
- package/src/tools/tokens.ts +74 -9
- package/src/tools/types.ts +84 -12
- package/src/tools/value-outline.ts +143 -0
- package/dist/tools/screenshot.cjs +0 -44
- package/dist/tools/screenshot.cjs.map +0 -1
- package/dist/tools/screenshot.js +0 -44
- package/dist/tools/screenshot.js.map +0 -1
- package/dist/tools/ui-interaction.cjs +0 -140
- package/dist/tools/ui-interaction.cjs.map +0 -1
- package/dist/tools/ui-interaction.js +0 -140
- package/dist/tools/ui-interaction.js.map +0 -1
- package/src/tools/screenshot.ts +0 -61
- package/src/tools/ui-interaction.ts +0 -184
package/src/server-log.test.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { MiLogger } from "@milaboratories/helpers";
|
|
2
|
+
import type { ProjectId } from "@milaboratories/pl-model-common";
|
|
2
3
|
import { inspect } from "node:util";
|
|
3
4
|
import { describe, expect, test } from "vitest";
|
|
4
5
|
import type { McpInstanceId } from "./server";
|
|
@@ -15,6 +16,7 @@ import {
|
|
|
15
16
|
} from "./tools/types";
|
|
16
17
|
|
|
17
18
|
const instanceId = "mcp-1a2b3c4d" as McpInstanceId;
|
|
19
|
+
const P1 = "p-1" as ProjectId;
|
|
18
20
|
|
|
19
21
|
/** A log writing to a recording logger, on a clock the test moves by hand. */
|
|
20
22
|
function logOnManualClock(options: Partial<ServerLogOptions> = {}) {
|
|
@@ -102,7 +104,11 @@ describe("a call that succeeds", () => {
|
|
|
102
104
|
|
|
103
105
|
test("takes an id only from a string argument", async () => {
|
|
104
106
|
const { log, at, returning } = logOnManualClock();
|
|
105
|
-
await log.run(
|
|
107
|
+
await log.run(
|
|
108
|
+
"get_project_overview",
|
|
109
|
+
{ projectId: 5, blockId: { id: "b" } },
|
|
110
|
+
returning(textResult(1)),
|
|
111
|
+
);
|
|
106
112
|
|
|
107
113
|
expect(parse(at("info")[0] ?? "")).not.toHaveProperty("project");
|
|
108
114
|
expect(parse(at("info")[0] ?? "")).not.toHaveProperty("block");
|
|
@@ -129,7 +135,7 @@ describe("a call that succeeds", () => {
|
|
|
129
135
|
],
|
|
130
136
|
};
|
|
131
137
|
|
|
132
|
-
await log.run("
|
|
138
|
+
await log.run("read_file", {}, returning(result));
|
|
133
139
|
await log.run("ping", undefined, returning("not a tool result"));
|
|
134
140
|
|
|
135
141
|
const [first, second] = at("info").map(parse);
|
|
@@ -280,6 +286,39 @@ describe("a slow call", () => {
|
|
|
280
286
|
|
|
281
287
|
expect(lines.map((line) => line.level)).toEqual(["info"]);
|
|
282
288
|
});
|
|
289
|
+
|
|
290
|
+
test("of a write asked to wait for the block is not reported; one that was not is", async () => {
|
|
291
|
+
const { log, lines, returning } = logOnManualClock();
|
|
292
|
+
|
|
293
|
+
// set_block_data and set_block_params wait by default; add_block only with `wait: true`.
|
|
294
|
+
await log.run("set_block_data", { projectId: "p-1" }, returning(textResult(1), 30_000));
|
|
295
|
+
await log.run("set_block_params", { projectId: "p-1" }, returning(textResult(1), 30_000));
|
|
296
|
+
await log.run("add_block", { projectId: "p-1", wait: true }, returning(textResult(1), 30_000));
|
|
297
|
+
expect(lines.map((line) => line.level)).toEqual(["info", "info", "info"]);
|
|
298
|
+
|
|
299
|
+
await log.run(
|
|
300
|
+
"set_block_data",
|
|
301
|
+
{ projectId: "p-1", wait: false },
|
|
302
|
+
returning(textResult(1), 30_000),
|
|
303
|
+
);
|
|
304
|
+
await log.run(
|
|
305
|
+
"set_block_params",
|
|
306
|
+
{ projectId: "p-1", wait: false },
|
|
307
|
+
returning(textResult(1), 30_000),
|
|
308
|
+
);
|
|
309
|
+
await log.run("add_block", { projectId: "p-1" }, returning(textResult(1), 30_000));
|
|
310
|
+
expect(lines.map((line) => line.level)).toEqual([
|
|
311
|
+
"info",
|
|
312
|
+
"info",
|
|
313
|
+
"info",
|
|
314
|
+
"info",
|
|
315
|
+
"warn",
|
|
316
|
+
"info",
|
|
317
|
+
"warn",
|
|
318
|
+
"info",
|
|
319
|
+
"warn",
|
|
320
|
+
]);
|
|
321
|
+
});
|
|
283
322
|
});
|
|
284
323
|
|
|
285
324
|
describe("a wait that ran out of time", () => {
|
|
@@ -447,8 +486,12 @@ describe("argument values", () => {
|
|
|
447
486
|
const { log, at, returning } = logOnManualClock();
|
|
448
487
|
const odd = 'p 1"\noutcome=ok';
|
|
449
488
|
|
|
450
|
-
await log.run("
|
|
451
|
-
await log.run(
|
|
489
|
+
await log.run("get_project_overview", { projectId: odd }, returning(textResult(1)));
|
|
490
|
+
await log.run(
|
|
491
|
+
"get_project_overview",
|
|
492
|
+
{ projectId: "x".repeat(5000) },
|
|
493
|
+
returning(textResult(1)),
|
|
494
|
+
);
|
|
452
495
|
|
|
453
496
|
const [quoted, long] = at("info");
|
|
454
497
|
expect(quoted).not.toContain("\n");
|
|
@@ -635,7 +678,11 @@ describe("a secret given to the log", () => {
|
|
|
635
678
|
secrets: [secret],
|
|
636
679
|
});
|
|
637
680
|
|
|
638
|
-
await log.run(
|
|
681
|
+
await log.run(
|
|
682
|
+
"get_project_overview",
|
|
683
|
+
{ projectId: secret },
|
|
684
|
+
returning(errorResult(`at /${secret}/`)),
|
|
685
|
+
);
|
|
639
686
|
await expect(
|
|
640
687
|
log.run("ping", undefined, throwing(new Error(`http://127.0.0.1:1/${secret}/mcp`))),
|
|
641
688
|
).rejects.toThrow(secret);
|
|
@@ -656,7 +703,7 @@ describe("a secret given to the log", () => {
|
|
|
656
703
|
log.requestFailed("POST", 500, new Error(message));
|
|
657
704
|
await expect(log.run("ping", undefined, throwing(new Error(message)))).rejects.toThrow();
|
|
658
705
|
await log.run(
|
|
659
|
-
"
|
|
706
|
+
"get_project_overview",
|
|
660
707
|
{ projectId: `${"y".repeat(190)}${secret}` },
|
|
661
708
|
returning(textResult(1)),
|
|
662
709
|
);
|
|
@@ -857,3 +904,21 @@ describe("fields", () => {
|
|
|
857
904
|
).toBe('call=mcp-1a2b3c4d#57 project=NG:0x35B1EB ms=0 error="not \\"found\\"\\there" keys=""');
|
|
858
905
|
});
|
|
859
906
|
});
|
|
907
|
+
|
|
908
|
+
describe("projects the server holds", () => {
|
|
909
|
+
test("a hold kept past the idle time and a hold given back write their lines", () => {
|
|
910
|
+
const { log, at } = logOnManualClock();
|
|
911
|
+
log.projectKept(P1, "imports", 3);
|
|
912
|
+
log.projectReleased(P1, "idle");
|
|
913
|
+
const [kept, released] = at("info");
|
|
914
|
+
expect(kept.split(" ", 1)[0]).toBe("mcp-project-keep");
|
|
915
|
+
expect(parse(kept)).toEqual({
|
|
916
|
+
id: instanceId,
|
|
917
|
+
project: "p-1",
|
|
918
|
+
reason: "imports",
|
|
919
|
+
pending: "3",
|
|
920
|
+
});
|
|
921
|
+
expect(released.split(" ", 1)[0]).toBe("mcp-project-release");
|
|
922
|
+
expect(parse(released)).toEqual({ id: instanceId, project: "p-1", reason: "idle" });
|
|
923
|
+
});
|
|
924
|
+
});
|
package/src/server-log.ts
CHANGED
|
@@ -1,17 +1,36 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { isPlainObject, type MiLogger } from "@milaboratories/helpers";
|
|
2
|
+
import type { ProjectId } from "@milaboratories/pl-model-common";
|
|
2
3
|
import { inspect } from "node:util";
|
|
4
|
+
import { isPromise } from "es-toolkit";
|
|
3
5
|
import type { McpInstanceId } from "./server";
|
|
4
6
|
import type { ToolRegistrar } from "./tools/types";
|
|
5
7
|
import { isTimedOutResult } from "./tools/types";
|
|
6
8
|
|
|
7
9
|
/**
|
|
8
|
-
* A call taking longer than this many milliseconds is reported as slow, unless
|
|
9
|
-
* {@link
|
|
10
|
+
* A call taking longer than this many milliseconds is reported as slow, unless it was asked to
|
|
11
|
+
* wait ({@link waitsByDesign}).
|
|
10
12
|
*/
|
|
11
13
|
export const SLOW_CALL_MS = 5000;
|
|
12
14
|
|
|
13
|
-
/**
|
|
14
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Whether a call of `tool` with `args` was asked to wait, so that taking long is its job and not
|
|
17
|
+
* slowness: `await_block_done` always; `set_block_data` and `set_block_params` unless
|
|
18
|
+
* `wait: false` (they wait by default); `add_block` with `wait: true`.
|
|
19
|
+
*/
|
|
20
|
+
export function waitsByDesign(tool: string, args: unknown): boolean {
|
|
21
|
+
const wait = typeof args === "object" && args !== null && "wait" in args ? args.wait : undefined;
|
|
22
|
+
switch (tool) {
|
|
23
|
+
case "await_block_done":
|
|
24
|
+
return true;
|
|
25
|
+
case "set_block_data":
|
|
26
|
+
case "set_block_params":
|
|
27
|
+
return wait !== false;
|
|
28
|
+
case "add_block":
|
|
29
|
+
return wait === true;
|
|
30
|
+
default:
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
15
34
|
|
|
16
35
|
/** Longest id written to a line; a longer one is cut, so that a line stays a line. */
|
|
17
36
|
const MAX_ID_LENGTH = 200;
|
|
@@ -77,7 +96,8 @@ type StartedCall = {
|
|
|
77
96
|
id: string;
|
|
78
97
|
/**
|
|
79
98
|
* The arguments, as the handler got them. They are not written: they are read only to find the
|
|
80
|
-
* values of secret arguments (see {@link SECRET_ARGUMENTS})
|
|
99
|
+
* values of secret arguments (see {@link SECRET_ARGUMENTS}) when the call failed, and to tell
|
|
100
|
+
* whether it was asked to wait ({@link waitsByDesign}).
|
|
81
101
|
*/
|
|
82
102
|
args: unknown;
|
|
83
103
|
tool: string;
|
|
@@ -194,6 +214,26 @@ export class ServerLog {
|
|
|
194
214
|
);
|
|
195
215
|
}
|
|
196
216
|
|
|
217
|
+
/** The server gave back its hold on a project it had kept loaded between calls. */
|
|
218
|
+
projectReleased(projectId: ProjectId, reason: string): void {
|
|
219
|
+
this.write(
|
|
220
|
+
"info",
|
|
221
|
+
() => `mcp-project-release ${fields({ id: this.instanceId, project: projectId, reason })}`,
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* The server kept its hold on a project past the idle time: the project has `pending` pieces of
|
|
227
|
+
* work that need it loaded (`reason`: `imports`, files still uploading).
|
|
228
|
+
*/
|
|
229
|
+
projectKept(projectId: ProjectId, reason: string, pending: number): void {
|
|
230
|
+
this.write(
|
|
231
|
+
"info",
|
|
232
|
+
() =>
|
|
233
|
+
`mcp-project-keep ${fields({ id: this.instanceId, project: projectId, reason, pending })}`,
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
|
|
197
237
|
/**
|
|
198
238
|
* An HTTP request failed outside the MCP handler. `status` is what it is answered with, absent
|
|
199
239
|
* when the answer had already begun.
|
|
@@ -239,7 +279,7 @@ export class ServerLog {
|
|
|
239
279
|
attempt(() => {
|
|
240
280
|
call.projectId = stringProperty(args, "projectId");
|
|
241
281
|
call.blockId = stringProperty(args, "blockId");
|
|
242
|
-
call.argKeys =
|
|
282
|
+
call.argKeys = isPlainObject(args) ? Object.keys(args) : [];
|
|
243
283
|
call.argsBytes = jsonBytes(args);
|
|
244
284
|
});
|
|
245
285
|
attempt(() => {
|
|
@@ -304,7 +344,11 @@ export class ServerLog {
|
|
|
304
344
|
if (!finish.thrown && attempt(() => isTimedOutResult(finish.result)) === true)
|
|
305
345
|
this.write("warn", () => `mcp-call-timeout ${fields({ ...subject, ms })}`);
|
|
306
346
|
|
|
307
|
-
if (
|
|
347
|
+
if (
|
|
348
|
+
ms !== undefined &&
|
|
349
|
+
ms > this.slowCallMs &&
|
|
350
|
+
attempt(() => waitsByDesign(call.tool, call.args)) !== true
|
|
351
|
+
)
|
|
308
352
|
this.write(
|
|
309
353
|
"warn",
|
|
310
354
|
() => `mcp-call-slow ${fields({ ...subject, ms, thresholdMs: this.slowCallMs })}`,
|
|
@@ -320,7 +364,7 @@ export class ServerLog {
|
|
|
320
364
|
// The texts in a line were redacted before they were cut; this pass over the whole line is
|
|
321
365
|
// for anything that still holds a secret.
|
|
322
366
|
const returned: unknown = this.logger[level](this.secrets.redact(line()));
|
|
323
|
-
if (
|
|
367
|
+
if (isPromise(returned)) void returned.then(undefined, () => {});
|
|
324
368
|
} catch {
|
|
325
369
|
// A line is lost. The call it describes must not be.
|
|
326
370
|
}
|
|
@@ -336,21 +380,8 @@ function attempt<T>(fn: () => T): T | undefined {
|
|
|
336
380
|
}
|
|
337
381
|
}
|
|
338
382
|
|
|
339
|
-
function isThenable(value: unknown): value is PromiseLike<unknown> {
|
|
340
|
-
return (
|
|
341
|
-
(typeof value === "object" || typeof value === "function") &&
|
|
342
|
-
value !== null &&
|
|
343
|
-
"then" in value &&
|
|
344
|
-
typeof value.then === "function"
|
|
345
|
-
);
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
349
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
350
|
-
}
|
|
351
|
-
|
|
352
383
|
function stringProperty(value: unknown, key: string): string | undefined {
|
|
353
|
-
if (!
|
|
384
|
+
if (!isPlainObject(value)) return undefined;
|
|
354
385
|
const property = value[key];
|
|
355
386
|
return typeof property === "string" ? property : undefined;
|
|
356
387
|
}
|
|
@@ -369,13 +400,13 @@ function jsonBytes(value: unknown): number {
|
|
|
369
400
|
* not a tool result.
|
|
370
401
|
*/
|
|
371
402
|
function contentBytes(result: unknown): number | undefined {
|
|
372
|
-
if (!
|
|
403
|
+
if (!isPlainObject(result) || !Array.isArray(result.content)) return undefined;
|
|
373
404
|
const textBytes = (text: string | undefined) =>
|
|
374
405
|
text === undefined ? 0 : Buffer.byteLength(text);
|
|
375
406
|
const dataLength = (data: string | undefined) => data?.length ?? 0;
|
|
376
407
|
let bytes = 0;
|
|
377
408
|
for (const item of result.content) {
|
|
378
|
-
const resource =
|
|
409
|
+
const resource = isPlainObject(item) ? item.resource : undefined;
|
|
379
410
|
bytes +=
|
|
380
411
|
textBytes(stringProperty(item, "text")) +
|
|
381
412
|
dataLength(stringProperty(item, "data")) +
|
|
@@ -387,7 +418,7 @@ function contentBytes(result: unknown): number | undefined {
|
|
|
387
418
|
|
|
388
419
|
/** The values of the secret arguments of a call (see {@link SECRET_ARGUMENTS}) that are strings. */
|
|
389
420
|
function secretArgumentValues(args: unknown): string[] {
|
|
390
|
-
if (!
|
|
421
|
+
if (!isPlainObject(args)) return [];
|
|
391
422
|
return Object.entries(args)
|
|
392
423
|
.filter(([key]) => SECRET_ARGUMENTS.has(key))
|
|
393
424
|
.map(([, value]) => value)
|
|
@@ -467,7 +498,7 @@ export function fields(values: Record<string, string | number | undefined>): str
|
|
|
467
498
|
function failureOf(finish: Finish): string | undefined {
|
|
468
499
|
if (finish.thrown) return errorText(finish.error);
|
|
469
500
|
const result = finish.result;
|
|
470
|
-
if (!
|
|
501
|
+
if (!isPlainObject(result) || result.isError !== true) return undefined;
|
|
471
502
|
const content = Array.isArray(result.content) ? result.content : [];
|
|
472
503
|
return content
|
|
473
504
|
.map((item: unknown) => stringProperty(item, "text"))
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { tmpdir } from "node:os";
|
|
2
|
-
import { join } from "node:path";
|
|
3
1
|
import { Client, StreamableHTTPClientTransport } from "@modelcontextprotocol/client";
|
|
4
2
|
import { afterEach, describe, expect, test } from "vitest";
|
|
5
3
|
import type { McpSecret, PlMcpServerCallbacks, PlMcpServerOptions } from "./server";
|
|
@@ -115,18 +113,16 @@ describe.each(eras)("a tool call of a $era client", (era) => {
|
|
|
115
113
|
const { connect, lines } = await startLogged();
|
|
116
114
|
const client = await connect(era);
|
|
117
115
|
|
|
118
|
-
const result = await client.callTool({ name: "
|
|
116
|
+
const result = await client.callTool({ name: "get_app_log", arguments: {} });
|
|
119
117
|
|
|
120
118
|
expect(result.isError).toBe(true);
|
|
121
119
|
const text = JSON.stringify(result.content);
|
|
122
120
|
const [info, error, ...rest] = callLines({ lines });
|
|
123
121
|
expect(rest).toEqual([]);
|
|
124
|
-
expect(info?.text).toMatch(/ tool=
|
|
122
|
+
expect(info?.text).toMatch(/ tool=get_app_log argsBytes=\d+ ms=\d+ outcome=error /);
|
|
125
123
|
expect(error?.level).toBe("error");
|
|
126
124
|
const [head, ...details] = (error?.text ?? "").split("\n");
|
|
127
|
-
expect(head).toMatch(
|
|
128
|
-
/^mcp-call-failed call=\S+ tool=capture_screenshot outcome=error argKeys=/,
|
|
129
|
-
);
|
|
125
|
+
expect(head).toMatch(/^mcp-call-failed call=\S+ tool=get_app_log outcome=error argKeys=/);
|
|
130
126
|
expect(text).toContain(JSON.stringify(details.join("\n")).slice(1, -1));
|
|
131
127
|
});
|
|
132
128
|
});
|
|
@@ -160,31 +156,6 @@ describe("the arguments of a call", () => {
|
|
|
160
156
|
});
|
|
161
157
|
});
|
|
162
158
|
|
|
163
|
-
describe("a screenshot that cannot be saved", () => {
|
|
164
|
-
test("is reported with the error of the file system, to the agent and in the log", async () => {
|
|
165
|
-
const { connect, lines, at } = await startLogged({
|
|
166
|
-
captureScreenshot: async () => Buffer.from("not a png").toString("base64"),
|
|
167
|
-
});
|
|
168
|
-
const client = await connect();
|
|
169
|
-
const folder = join(tmpdir(), `missing-folder-${process.pid}-${Date.now()}`);
|
|
170
|
-
|
|
171
|
-
const result = await client.callTool({
|
|
172
|
-
name: "capture_screenshot",
|
|
173
|
-
arguments: { savePath: join(folder, "result.png") },
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
expect(result.isError).toBe(true);
|
|
177
|
-
expect(result.content).toEqual([
|
|
178
|
-
{
|
|
179
|
-
type: "text",
|
|
180
|
-
text: expect.stringMatching(/^Could not save the screenshot: ENOENT: .*result\.png/),
|
|
181
|
-
},
|
|
182
|
-
]);
|
|
183
|
-
expect(callLines({ lines }).map((line) => line.level)).toEqual(["info", "error"]);
|
|
184
|
-
expect(at("error")[0]).toContain("ENOENT");
|
|
185
|
-
});
|
|
186
|
-
});
|
|
187
|
-
|
|
188
159
|
describe("the tools of the server", () => {
|
|
189
160
|
/** Arguments that pass a tool's schema where plain values are enough for it. */
|
|
190
161
|
function sampleArguments(schema: {
|
|
@@ -192,15 +163,27 @@ describe("the tools of the server", () => {
|
|
|
192
163
|
required?: string[];
|
|
193
164
|
}): Record<string, unknown> {
|
|
194
165
|
const samples: Record<string, unknown> = { string: "x", number: 1, integer: 1, boolean: false };
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
166
|
+
const typeOf = (property: unknown): unknown =>
|
|
167
|
+
typeof property === "object" && property !== null && "type" in property
|
|
168
|
+
? property.type
|
|
169
|
+
: undefined;
|
|
170
|
+
/** A plain sample of the property, or of the first plain option of an `anyOf`. */
|
|
171
|
+
const sampleOf = (property: unknown): unknown => {
|
|
172
|
+
const type = typeOf(property);
|
|
173
|
+
if (typeof type === "string" && type in samples) return samples[type];
|
|
174
|
+
const options =
|
|
175
|
+
typeof property === "object" && property !== null && "anyOf" in property
|
|
176
|
+
? property.anyOf
|
|
201
177
|
: undefined;
|
|
202
|
-
|
|
203
|
-
|
|
178
|
+
if (!Array.isArray(options)) return {};
|
|
179
|
+
for (const option of options) {
|
|
180
|
+
const optionType = typeOf(option);
|
|
181
|
+
if (typeof optionType === "string" && optionType in samples) return samples[optionType];
|
|
182
|
+
}
|
|
183
|
+
return {};
|
|
184
|
+
};
|
|
185
|
+
const args: Record<string, unknown> = {};
|
|
186
|
+
for (const name of schema.required ?? []) args[name] = sampleOf(schema.properties?.[name]);
|
|
204
187
|
return args;
|
|
205
188
|
}
|
|
206
189
|
|
|
@@ -226,16 +209,28 @@ describe("the tools of the server", () => {
|
|
|
226
209
|
.filter((line) => line.level === "info")
|
|
227
210
|
.map((line) => / tool=(\S+) /.exec(line.text)?.[1]);
|
|
228
211
|
expect(logged).toEqual(reached);
|
|
229
|
-
// Only the tools whose arguments are more than plain values are not reached this way
|
|
230
|
-
|
|
231
|
-
expect(
|
|
212
|
+
// Only the tools whose arguments are more than plain values are not reached this way; a pool
|
|
213
|
+
// column id is parsed, and "x" is not one.
|
|
214
|
+
expect(refused).toEqual([
|
|
215
|
+
"add_block",
|
|
216
|
+
"update_block",
|
|
217
|
+
"reorder_blocks",
|
|
218
|
+
"get_block_contract",
|
|
219
|
+
"get_file_handles",
|
|
220
|
+
"get_pool_column",
|
|
221
|
+
"create_table",
|
|
222
|
+
]);
|
|
223
|
+
expect(reached).toHaveLength(tools.length - 7);
|
|
232
224
|
});
|
|
233
225
|
|
|
234
226
|
test("a call refused before its handler writes no line", async () => {
|
|
235
227
|
const { connect, lines } = await startLogged();
|
|
236
228
|
const client = await connect();
|
|
237
229
|
|
|
238
|
-
const result = await client.callTool({
|
|
230
|
+
const result = await client.callTool({
|
|
231
|
+
name: "get_project_overview",
|
|
232
|
+
arguments: { projectId: 5 },
|
|
233
|
+
});
|
|
239
234
|
|
|
240
235
|
expect(text(result)).toContain("Input validation error");
|
|
241
236
|
expect(callLines({ lines })).toEqual([]);
|
|
@@ -291,9 +286,9 @@ describe("a logger that throws", () => {
|
|
|
291
286
|
|
|
292
287
|
for (const call of [
|
|
293
288
|
{ name: "ping", arguments: {} },
|
|
294
|
-
{ name: "
|
|
295
|
-
{ name: "
|
|
296
|
-
{ name: "
|
|
289
|
+
{ name: "get_app_log", arguments: {} },
|
|
290
|
+
{ name: "get_project_overview", arguments: { projectId: "p-1" } },
|
|
291
|
+
{ name: "get_project_overview", arguments: { projectId: 5 } },
|
|
297
292
|
]) {
|
|
298
293
|
const answer = await broken.callTool(call);
|
|
299
294
|
expect(answer).toEqual(await silent.callTool(call));
|
|
@@ -312,8 +307,8 @@ describe("a server given no logger", () => {
|
|
|
312
307
|
|
|
313
308
|
for (const call of [
|
|
314
309
|
{ name: "ping", arguments: {} },
|
|
315
|
-
{ name: "
|
|
316
|
-
{ name: "
|
|
310
|
+
{ name: "get_app_log", arguments: {} },
|
|
311
|
+
{ name: "get_project_overview", arguments: { projectId: "p-1" } },
|
|
317
312
|
])
|
|
318
313
|
expect(await without.callTool(call)).toEqual(await withLog.callTool(call));
|
|
319
314
|
expect(callLines(logged)).toHaveLength(5);
|
|
@@ -351,8 +346,8 @@ describe("the server's own lines", () => {
|
|
|
351
346
|
const client = await connect();
|
|
352
347
|
|
|
353
348
|
await client.callTool({ name: "ping", arguments: {} });
|
|
354
|
-
await client.callTool({ name: "
|
|
355
|
-
await client.callTool({ name: "
|
|
349
|
+
await client.callTool({ name: "get_project_overview", arguments: { projectId: secret } });
|
|
350
|
+
await client.callTool({ name: "get_app_log", arguments: {} });
|
|
356
351
|
await fetch(server.url, { method: "POST", headers: notJson, body: "ping" });
|
|
357
352
|
await fetch(server.url, { method: "GET" });
|
|
358
353
|
await client.close();
|