@ftrdotdev/pi-delegates 0.0.0-stage → 0.1.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/CHANGELOG.md +77 -0
- package/LICENSE +21 -0
- package/OPERATIONS.md +57 -0
- package/README.md +448 -2
- package/ROADMAP.md +158 -0
- package/TESTING.md +1061 -0
- package/agents.ts +8 -0
- package/examples/README.md +114 -0
- package/examples/gated.ts +28 -0
- package/index.ts +9 -0
- package/package.json +93 -4
- package/skills/pi-delegates/SKILL.md +147 -0
- package/skills/pi-delegates/references/control.md +159 -0
- package/skills/pi-delegates/references/definitions.md +163 -0
- package/skills/pi-delegates/references/gates.md +175 -0
- package/skills/pi-delegates/references/launch.md +156 -0
- package/src/domain/definitions.test.ts +41 -0
- package/src/domain/definitions.ts +161 -0
- package/src/domain/ids.ts +13 -0
- package/src/domain/names.ts +35 -0
- package/src/domain/orchestration/admission.ts +78 -0
- package/src/domain/orchestration/control.test.ts +1298 -0
- package/src/domain/orchestration/control.ts +436 -0
- package/src/domain/orchestration/fanout.test.ts +419 -0
- package/src/domain/orchestration/fanout.ts +334 -0
- package/src/domain/orchestration/gates.test.ts +686 -0
- package/src/domain/orchestration/gates.ts +360 -0
- package/src/domain/orchestration/launch.test.ts +173 -0
- package/src/domain/orchestration/launch.ts +581 -0
- package/src/domain/orchestration/requests.test.ts +700 -0
- package/src/domain/orchestration/requests.ts +769 -0
- package/src/domain/orchestration/review.test.ts +214 -0
- package/src/domain/orchestration/review.ts +168 -0
- package/src/domain/orchestration/run-wait.test.ts +262 -0
- package/src/domain/orchestration/run-wait.ts +256 -0
- package/src/domain/orchestration/workflow-section.test.ts +62 -0
- package/src/domain/orchestration/workflow-section.ts +99 -0
- package/src/domain/orchestration/workflow.test.ts +1425 -0
- package/src/domain/orchestration/workflow.ts +705 -0
- package/src/domain/run-records.test.ts +27 -0
- package/src/domain/run-records.ts +527 -0
- package/src/domain/shapes.test.ts +165 -0
- package/src/domain/skills.test.ts +466 -0
- package/src/domain/skills.ts +97 -0
- package/src/domain/tool-schema.test.ts +46 -0
- package/src/domain/tool-schema.ts +20 -0
- package/src/domain/toolkit.test.ts +201 -0
- package/src/domain/toolkit.ts +379 -0
- package/src/domain/view.test.ts +739 -0
- package/src/domain/view.ts +459 -0
- package/src/example.test.ts +15 -0
- package/src/extension/commands.test.ts +481 -0
- package/src/extension/commands.ts +227 -0
- package/src/extension/entry.test.ts +1118 -0
- package/src/extension/entry.ts +342 -0
- package/src/extension/gate-alerts.test.ts +235 -0
- package/src/extension/gate-alerts.ts +143 -0
- package/src/extension/guard.test.ts +48 -0
- package/src/extension/guard.ts +31 -0
- package/src/extension/host-signals.test.ts +20 -0
- package/src/extension/host-signals.ts +13 -0
- package/src/extension/inspector-command.test.ts +1191 -0
- package/src/extension/inspector-command.ts +638 -0
- package/src/extension/prompt.test.ts +110 -0
- package/src/extension/prompt.ts +50 -0
- package/src/extension/request-alerts.test.ts +183 -0
- package/src/extension/request-alerts.ts +181 -0
- package/src/extension/revocation.test.ts +81 -0
- package/src/extension/revocation.ts +84 -0
- package/src/extension/shortcut.test.ts +64 -0
- package/src/extension/shortcut.ts +105 -0
- package/src/extension/tools.test.ts +993 -0
- package/src/extension/tools.ts +505 -0
- package/src/extension/version.test.ts +123 -0
- package/src/extension/version.ts +97 -0
- package/src/harness-policy.test.ts +74 -0
- package/src/package-manifest.test.ts +157 -0
- package/src/presentation/actions.test.ts +221 -0
- package/src/presentation/actions.ts +85 -0
- package/src/presentation/activity.test.ts +387 -0
- package/src/presentation/activity.ts +66 -0
- package/src/presentation/close.test.ts +361 -0
- package/src/presentation/inspector.test.ts +679 -0
- package/src/presentation/inspector.ts +440 -0
- package/src/presentation/overlay.test.ts +555 -0
- package/src/presentation/overlay.ts +229 -0
- package/src/presentation/repaint.test.ts +329 -0
- package/src/presentation/shortcut.ts +2 -0
- package/src/presentation/snapshot.test.ts +530 -0
- package/src/presentation/snapshot.ts +306 -0
- package/src/presentation/strip.test.ts +196 -0
- package/src/presentation/strip.ts +74 -0
- package/src/presentation/wording.test.ts +238 -0
- package/src/presentation/wording.ts +222 -0
- package/src/runtime/bridge.test.ts +131 -0
- package/src/runtime/bridge.ts +134 -0
- package/src/runtime/config.test.ts +44 -0
- package/src/runtime/config.ts +87 -0
- package/src/runtime/context.test.ts +19 -0
- package/src/runtime/context.ts +9 -0
- package/src/runtime/execution-env.test.ts +35 -0
- package/src/runtime/execution-env.ts +15 -0
- package/src/runtime/extensions.test.ts +86 -0
- package/src/runtime/extensions.ts +140 -0
- package/src/runtime/log.test.ts +128 -0
- package/src/runtime/log.ts +68 -0
- package/src/runtime/mcp.test.ts +1453 -0
- package/src/runtime/mcp.ts +938 -0
- package/src/runtime/models.test.ts +145 -0
- package/src/runtime/models.ts +259 -0
- package/src/runtime/observe.test.ts +50 -0
- package/src/runtime/observe.ts +150 -0
- package/src/runtime/session.test.ts +359 -0
- package/src/runtime/session.ts +425 -0
- package/src/runtime/timers.test.ts +31 -0
- package/src/runtime/timers.ts +10 -0
|
@@ -0,0 +1,1453 @@
|
|
|
1
|
+
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
2
|
+
import type { McpExposure, McpServerEntry } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { AgentDoc, type ToolRegistration } from "@earendil-works/pi-durable";
|
|
4
|
+
import type { CallToolResult, Tool } from "@earendil-works/pi-mcp";
|
|
5
|
+
import { MCP_OUTPUT_MAX_BYTES } from "@earendil-works/pi-mcp";
|
|
6
|
+
import { Effect } from "effect";
|
|
7
|
+
import { readFileSync } from "node:fs";
|
|
8
|
+
import { mkdir, mkdtemp, writeFile } from "node:fs/promises";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { assert, it } from "@effect/vitest";
|
|
12
|
+
import { rejects } from "../../test-support/assertions.ts";
|
|
13
|
+
import { waitFor } from "../../test-support/wait.ts";
|
|
14
|
+
import { makeStubPi } from "../../test-support/stub-pi.ts";
|
|
15
|
+
import { stubHostSource } from "../../test-support/stub-model-source.ts";
|
|
16
|
+
import { createCatalogue, type AgentDefinition } from "../domain/definitions.ts";
|
|
17
|
+
import type { AdmissionHost } from "../domain/orchestration/admission.ts";
|
|
18
|
+
import type { HostDefaults } from "../domain/orchestration/launch.ts";
|
|
19
|
+
import { launch } from "../domain/orchestration/launch.ts";
|
|
20
|
+
import { refusingSandbox } from "../domain/orchestration/workflow.ts";
|
|
21
|
+
import { resolverFor, RunIndexDoc } from "../domain/run-records.ts";
|
|
22
|
+
import type { SkillsService } from "../domain/skills.ts";
|
|
23
|
+
import { bindToolkit, ToolkitUnresolved, type ToolkitResolver } from "../domain/toolkit.ts";
|
|
24
|
+
import { viewRun, viewSession } from "../domain/view.ts";
|
|
25
|
+
import { createDelegatesExtension } from "../extension/entry.ts";
|
|
26
|
+
import { createDelegatesRegistry } from "./extensions.ts";
|
|
27
|
+
import type { LogFields, LogLevel, LogSink } from "./log.ts";
|
|
28
|
+
import { nullLogSink } from "./log.ts";
|
|
29
|
+
import {
|
|
30
|
+
connectServers,
|
|
31
|
+
readMcpConfig,
|
|
32
|
+
type McpClientFactory,
|
|
33
|
+
type McpClientPort,
|
|
34
|
+
type McpHost,
|
|
35
|
+
} from "./mcp.ts";
|
|
36
|
+
import { HostModelAdapter } from "./models.ts";
|
|
37
|
+
import {
|
|
38
|
+
openReadOnlyRuntime,
|
|
39
|
+
openSessionRuntime,
|
|
40
|
+
storePathFor,
|
|
41
|
+
type SessionIdentity,
|
|
42
|
+
type SessionRuntime,
|
|
43
|
+
} from "./session.ts";
|
|
44
|
+
import { systemTimers, type Timers } from "./timers.ts";
|
|
45
|
+
|
|
46
|
+
/** A workspace with an agent directory and a project directory, both under one temp root. */
|
|
47
|
+
const tempWorkspace = async (options: {
|
|
48
|
+
readonly global?: unknown;
|
|
49
|
+
readonly project?: unknown;
|
|
50
|
+
readonly projectTrusted?: boolean;
|
|
51
|
+
}): Promise<{
|
|
52
|
+
readonly agentDir: string;
|
|
53
|
+
readonly cwd: string;
|
|
54
|
+
readonly projectPath: string;
|
|
55
|
+
readonly read: (projectTrusted?: boolean) => ReturnType<typeof readMcpConfig>;
|
|
56
|
+
}> => {
|
|
57
|
+
const root = await mkdtemp(join(tmpdir(), "delegates-mcp-"));
|
|
58
|
+
const agentDir = join(root, "agent");
|
|
59
|
+
const cwd = join(root, "repo");
|
|
60
|
+
await mkdir(agentDir, { recursive: true });
|
|
61
|
+
await mkdir(cwd, { recursive: true });
|
|
62
|
+
const projectPath = join(cwd, ".pi", "mcp.json");
|
|
63
|
+
if (options.global !== undefined) {
|
|
64
|
+
await writeFile(join(agentDir, "mcp.json"), textOf(options.global));
|
|
65
|
+
}
|
|
66
|
+
if (options.project !== undefined) {
|
|
67
|
+
await mkdir(join(cwd, ".pi"), { recursive: true });
|
|
68
|
+
await writeFile(projectPath, textOf(options.project));
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
agentDir,
|
|
72
|
+
cwd,
|
|
73
|
+
projectPath,
|
|
74
|
+
read: (projectTrusted = options.projectTrusted ?? false) =>
|
|
75
|
+
readMcpConfig({ agentDir, cwd, projectTrusted }),
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/** A string is written as raw file text, so a malformed file needs no special case. */
|
|
80
|
+
const textOf = (value: unknown): string =>
|
|
81
|
+
typeof value === "string" ? value : JSON.stringify(value);
|
|
82
|
+
|
|
83
|
+
const codes = (result: ReturnType<typeof readMcpConfig>): readonly string[] =>
|
|
84
|
+
result.diagnostics.map((diagnostic) => diagnostic.code);
|
|
85
|
+
|
|
86
|
+
it("a global entry is read and validated", async () => {
|
|
87
|
+
const workspace = await tempWorkspace({
|
|
88
|
+
global: { mcpServers: { demo: { command: "node", args: ["server.js"] } } },
|
|
89
|
+
});
|
|
90
|
+
const result = workspace.read();
|
|
91
|
+
assert.strictEqual(result.servers.length, 1);
|
|
92
|
+
const entry = result.servers[0]!;
|
|
93
|
+
assert.strictEqual(entry.name, "demo");
|
|
94
|
+
assert.strictEqual(entry.scope, "global");
|
|
95
|
+
assert.strictEqual((entry.config as { command?: string }).command, "node");
|
|
96
|
+
assert.deepStrictEqual(result.diagnostics, []);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("a project entry replaces a global entry of the same name", async () => {
|
|
100
|
+
const workspace = await tempWorkspace({
|
|
101
|
+
global: { mcpServers: { demo: { command: "a" } } },
|
|
102
|
+
project: { mcpServers: { demo: { command: "b" } } },
|
|
103
|
+
projectTrusted: true,
|
|
104
|
+
});
|
|
105
|
+
const result = workspace.read();
|
|
106
|
+
assert.strictEqual(result.servers.length, 1);
|
|
107
|
+
const entry = result.servers[0]!;
|
|
108
|
+
assert.strictEqual((entry.config as { command?: string }).command, "b");
|
|
109
|
+
assert.strictEqual(entry.scope, "project");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("an override-only entry keeps the global config", async () => {
|
|
113
|
+
const workspace = await tempWorkspace({
|
|
114
|
+
global: { mcpServers: { demo: { command: "a" } } },
|
|
115
|
+
project: { mcpServers: { demo: { enabled: false } } },
|
|
116
|
+
projectTrusted: true,
|
|
117
|
+
});
|
|
118
|
+
const result = workspace.read();
|
|
119
|
+
assert.strictEqual(result.servers.length, 1);
|
|
120
|
+
const entry = result.servers[0]!;
|
|
121
|
+
assert.strictEqual((entry.config as { enabled?: boolean }).enabled, false);
|
|
122
|
+
assert.strictEqual((entry.config as { command?: string }).command, "a");
|
|
123
|
+
assert.strictEqual(entry.override, workspace.projectPath);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("the project file is ignored without trust", async () => {
|
|
127
|
+
const workspace = await tempWorkspace({
|
|
128
|
+
global: { mcpServers: { demo: { command: "a" } } },
|
|
129
|
+
project: { mcpServers: { demo: { enabled: false } } },
|
|
130
|
+
});
|
|
131
|
+
const result = workspace.read(false);
|
|
132
|
+
assert.strictEqual(result.servers.length, 1);
|
|
133
|
+
assert.notStrictEqual((result.servers[0]!.config as { enabled?: boolean }).enabled, false);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("a malformed file is a diagnostic, not a throw", async () => {
|
|
137
|
+
const truncated = await tempWorkspace({ global: '{"mcpServers": ' });
|
|
138
|
+
const first = truncated.read();
|
|
139
|
+
assert.deepStrictEqual(first.servers, []);
|
|
140
|
+
assert.deepStrictEqual(codes(first), ["mcp.config_invalid"]);
|
|
141
|
+
|
|
142
|
+
const mixed = await tempWorkspace({
|
|
143
|
+
global: {
|
|
144
|
+
mcpServers: {
|
|
145
|
+
good: { command: "a" },
|
|
146
|
+
bad: { args: ["no command and no url"] },
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
const second = mixed.read();
|
|
151
|
+
assert.deepStrictEqual(
|
|
152
|
+
second.servers.map((entry) => entry.name),
|
|
153
|
+
["good"],
|
|
154
|
+
);
|
|
155
|
+
assert.deepStrictEqual(codes(second), ["mcp.server_invalid"]);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("a disabled server is kept with enabled false", async () => {
|
|
159
|
+
const workspace = await tempWorkspace({
|
|
160
|
+
global: { mcpServers: { demo: { command: "node", enabled: false } } },
|
|
161
|
+
});
|
|
162
|
+
const result = workspace.read();
|
|
163
|
+
assert.strictEqual(result.servers.length, 1);
|
|
164
|
+
assert.strictEqual((result.servers[0]!.config as { enabled?: boolean }).enabled, false);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("a disabled server writes no diagnostic", async () => {
|
|
168
|
+
const workspace = await tempWorkspace({
|
|
169
|
+
global: { mcpServers: { demo: { command: "node", enabled: false } } },
|
|
170
|
+
});
|
|
171
|
+
assert.deepStrictEqual(workspace.read().diagnostics, []);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("two servers that share a namespace are diagnosed", async () => {
|
|
175
|
+
const workspace = await tempWorkspace({
|
|
176
|
+
global: { mcpServers: { "a-b": { command: "a" }, a_b: { command: "b" } } },
|
|
177
|
+
});
|
|
178
|
+
const result = workspace.read();
|
|
179
|
+
assert.deepStrictEqual(codes(result), ["mcp.namespace_collision"]);
|
|
180
|
+
assert.deepStrictEqual(
|
|
181
|
+
result.servers.map((entry) => entry.name),
|
|
182
|
+
["a-b"],
|
|
183
|
+
);
|
|
184
|
+
const detail = result.diagnostics[0]!.detail;
|
|
185
|
+
assert.ok(detail.includes("a-b"), detail);
|
|
186
|
+
assert.ok(detail.includes("a_b"), detail);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("an override with an extra key is refused and the global entry stands", async () => {
|
|
190
|
+
const workspace = await tempWorkspace({
|
|
191
|
+
global: { mcpServers: { demo: { command: "a" } } },
|
|
192
|
+
project: { mcpServers: { demo: { enabled: false, headers: { A: "b" } } } },
|
|
193
|
+
projectTrusted: true,
|
|
194
|
+
});
|
|
195
|
+
const result = workspace.read();
|
|
196
|
+
assert.deepStrictEqual(codes(result), ["mcp.server_invalid"]);
|
|
197
|
+
const entry = result.servers[0]!;
|
|
198
|
+
assert.strictEqual((entry.config as { command?: string }).command, "a");
|
|
199
|
+
assert.notStrictEqual((entry.config as { enabled?: boolean }).enabled, false);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("a project entry with auth is refused", async () => {
|
|
203
|
+
const workspace = await tempWorkspace({
|
|
204
|
+
project: {
|
|
205
|
+
mcpServers: { docs: { url: "https://example.test/mcp", auth: { provider: "x" } } },
|
|
206
|
+
},
|
|
207
|
+
projectTrusted: true,
|
|
208
|
+
});
|
|
209
|
+
const result = workspace.read();
|
|
210
|
+
assert.deepStrictEqual(result.servers, []);
|
|
211
|
+
assert.deepStrictEqual(codes(result), ["mcp.server_invalid"]);
|
|
212
|
+
assert.ok(result.diagnostics[0]!.detail.includes("auth"), result.diagnostics[0]!.detail);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it("a project stdio entry may set auth, which no transport reads", async () => {
|
|
216
|
+
const workspace = await tempWorkspace({
|
|
217
|
+
project: { mcpServers: { local: { command: "node", auth: { provider: "x" } } } },
|
|
218
|
+
projectTrusted: true,
|
|
219
|
+
});
|
|
220
|
+
const result = workspace.read();
|
|
221
|
+
// The host refuses auth only beside `url`; a stdio entry keeps the inert field.
|
|
222
|
+
assert.deepStrictEqual(codes(result), []);
|
|
223
|
+
assert.deepStrictEqual(
|
|
224
|
+
result.servers.map((entry) => entry.name),
|
|
225
|
+
["local"],
|
|
226
|
+
);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("a project override without a global server is refused", async () => {
|
|
230
|
+
const workspace = await tempWorkspace({
|
|
231
|
+
project: { mcpServers: { demo: { enabled: false } } },
|
|
232
|
+
projectTrusted: true,
|
|
233
|
+
});
|
|
234
|
+
const result = workspace.read();
|
|
235
|
+
assert.deepStrictEqual(result.servers, []);
|
|
236
|
+
assert.deepStrictEqual(codes(result), ["mcp.override_without_server"]);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("a malformed container is a diagnostic", async () => {
|
|
240
|
+
const shapes = await tempWorkspace({ global: { mcpServers: [] } });
|
|
241
|
+
const first = shapes.read();
|
|
242
|
+
assert.deepStrictEqual(first.servers, []);
|
|
243
|
+
assert.deepStrictEqual(codes(first), ["mcp.config_invalid"]);
|
|
244
|
+
|
|
245
|
+
const toggle = await tempWorkspace({ global: { autoEnableCodemode: "yes" } });
|
|
246
|
+
const second = toggle.read();
|
|
247
|
+
assert.deepStrictEqual(second.servers, []);
|
|
248
|
+
assert.deepStrictEqual(codes(second), ["mcp.config_invalid"]);
|
|
249
|
+
assert.ok(second.diagnostics[0]!.detail.includes("autoEnableCodemode"));
|
|
250
|
+
|
|
251
|
+
const nullToggle = await tempWorkspace({ global: { autoEnableCodemode: null } });
|
|
252
|
+
assert.deepStrictEqual(codes(nullToggle.read()), ["mcp.config_invalid"]);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("a malformed entry never throws", async () => {
|
|
256
|
+
const workspace = await tempWorkspace({
|
|
257
|
+
global: {
|
|
258
|
+
mcpServers: {
|
|
259
|
+
text: "not an object",
|
|
260
|
+
list: ["a"],
|
|
261
|
+
empty: null,
|
|
262
|
+
number: 3,
|
|
263
|
+
good: { command: "node" },
|
|
264
|
+
},
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
const result = workspace.read();
|
|
268
|
+
assert.deepStrictEqual(
|
|
269
|
+
result.servers.map((entry) => entry.name),
|
|
270
|
+
["good"],
|
|
271
|
+
);
|
|
272
|
+
assert.deepStrictEqual(codes(result), Array<string>(4).fill("mcp.server_invalid"));
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it("a malformed exposure value is refused without throwing", async () => {
|
|
276
|
+
const workspace = await tempWorkspace({
|
|
277
|
+
global: {
|
|
278
|
+
mcpServers: {
|
|
279
|
+
numberExposure: { command: "node", exposure: 42 },
|
|
280
|
+
listToolExposure: { command: "node", toolExposure: ["echo"] },
|
|
281
|
+
mixedToolExposure: { command: "node", toolExposure: { echo: 7, add: null, ok: "direct" } },
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
});
|
|
285
|
+
const result = workspace.read();
|
|
286
|
+
const byName = new Map(result.servers.map((entry) => [entry.name, entry.config]));
|
|
287
|
+
assert.strictEqual(byName.get("numberExposure")?.exposure, "codemode");
|
|
288
|
+
assert.strictEqual(byName.get("listToolExposure")?.toolExposure, undefined);
|
|
289
|
+
assert.deepStrictEqual(byName.get("mixedToolExposure")?.toolExposure, { ok: "direct" });
|
|
290
|
+
assert.ok(result.diagnostics.every((diagnostic) => diagnostic.code === "mcp.server_invalid"));
|
|
291
|
+
// One for the number, one for the array, and one per bad tool value.
|
|
292
|
+
assert.strictEqual(result.diagnostics.length, 4);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it("a non-string env value is a diagnostic", async () => {
|
|
296
|
+
const workspace = await tempWorkspace({
|
|
297
|
+
global: { mcpServers: { demo: { command: "node", env: { TOKEN: 5 } } } },
|
|
298
|
+
});
|
|
299
|
+
const result = workspace.read();
|
|
300
|
+
assert.deepStrictEqual(result.servers, []);
|
|
301
|
+
assert.deepStrictEqual(codes(result), ["mcp.server_invalid"]);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it("a merged override is re-validated", async () => {
|
|
305
|
+
const workspace = await tempWorkspace({
|
|
306
|
+
global: { mcpServers: { demo: { command: "a" } } },
|
|
307
|
+
project: { mcpServers: { demo: { enabled: "yes" } } },
|
|
308
|
+
projectTrusted: true,
|
|
309
|
+
});
|
|
310
|
+
const result = workspace.read();
|
|
311
|
+
assert.deepStrictEqual(codes(result), ["mcp.server_invalid"]);
|
|
312
|
+
assert.strictEqual(result.servers[0]!.name, "demo");
|
|
313
|
+
assert.strictEqual((result.servers[0]!.config as { enabled?: unknown }).enabled, undefined);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it("a missing mcpServers key is empty", async () => {
|
|
317
|
+
const workspace = await tempWorkspace({ global: {} });
|
|
318
|
+
const result = workspace.read();
|
|
319
|
+
assert.deepStrictEqual(result.servers, []);
|
|
320
|
+
assert.deepStrictEqual(result.diagnostics, []);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it("an exposure value is normalized", async () => {
|
|
324
|
+
const workspace = await tempWorkspace({
|
|
325
|
+
global: {
|
|
326
|
+
mcpServers: {
|
|
327
|
+
alias: { command: "a", exposure: "codemode-deferred" },
|
|
328
|
+
plain: { command: "b" },
|
|
329
|
+
bad: { command: "c", exposure: "hiddenn" },
|
|
330
|
+
tools: {
|
|
331
|
+
command: "d",
|
|
332
|
+
toolExposure: { echo: "codemode-deferred", add: "hiddenn" },
|
|
333
|
+
},
|
|
334
|
+
},
|
|
335
|
+
},
|
|
336
|
+
});
|
|
337
|
+
const result = workspace.read();
|
|
338
|
+
const byName = new Map(result.servers.map((entry) => [entry.name, entry.config]));
|
|
339
|
+
assert.strictEqual((byName.get("alias") as { exposure?: string }).exposure, "deferred");
|
|
340
|
+
assert.strictEqual((byName.get("plain") as { exposure?: string }).exposure, "codemode");
|
|
341
|
+
const tools = byName.get("tools") as { toolExposure?: Record<string, string> };
|
|
342
|
+
assert.deepStrictEqual(tools.toolExposure, { echo: "deferred" });
|
|
343
|
+
assert.deepStrictEqual(codes(result), ["mcp.server_invalid", "mcp.server_invalid"]);
|
|
344
|
+
const details = result.diagnostics.map((diagnostic) => diagnostic.detail).join(" ");
|
|
345
|
+
assert.ok(details.includes("hiddenn"), details);
|
|
346
|
+
assert.ok(details.includes("toolExposure"), details);
|
|
347
|
+
assert.ok(details.includes("add"), details);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
interface FakeClient {
|
|
351
|
+
readonly client: McpClientPort;
|
|
352
|
+
readonly closed: () => number;
|
|
353
|
+
readonly closeListeners: (() => void)[];
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const fakeClient = (
|
|
357
|
+
options: {
|
|
358
|
+
readonly tools?: readonly Tool[];
|
|
359
|
+
readonly callTool?: (name: string, args: Record<string, unknown>) => Promise<CallToolResult>;
|
|
360
|
+
readonly listTools?: () => Promise<Tool[]>;
|
|
361
|
+
} = {},
|
|
362
|
+
): FakeClient => {
|
|
363
|
+
let closed = 0;
|
|
364
|
+
const closeListeners: (() => void)[] = [];
|
|
365
|
+
const client = {
|
|
366
|
+
listTools: options.listTools ?? (async () => [...(options.tools ?? [])]),
|
|
367
|
+
callTool:
|
|
368
|
+
options.callTool ?? (async () => ({ content: [{ type: "text" as const, text: "hi" }] })),
|
|
369
|
+
onClose: (listener: () => void) => {
|
|
370
|
+
closeListeners.push(listener);
|
|
371
|
+
return () => {};
|
|
372
|
+
},
|
|
373
|
+
close: async () => {
|
|
374
|
+
closed += 1;
|
|
375
|
+
},
|
|
376
|
+
};
|
|
377
|
+
return { client, closed: () => closed, closeListeners };
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
const fakeFactory = (
|
|
381
|
+
make: (name: string) => Promise<McpClientPort>,
|
|
382
|
+
): {
|
|
383
|
+
readonly factory: McpClientFactory;
|
|
384
|
+
readonly asked: string[];
|
|
385
|
+
readonly entries: McpServerEntry[];
|
|
386
|
+
} => {
|
|
387
|
+
const asked: string[] = [];
|
|
388
|
+
const entries: McpServerEntry[] = [];
|
|
389
|
+
return {
|
|
390
|
+
asked,
|
|
391
|
+
entries,
|
|
392
|
+
factory: {
|
|
393
|
+
connect: async (entry) => {
|
|
394
|
+
asked.push(entry.name);
|
|
395
|
+
entries.push(entry);
|
|
396
|
+
return make(entry.name);
|
|
397
|
+
},
|
|
398
|
+
},
|
|
399
|
+
};
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
/** The fields a test entry sets, in pi's own types; the transport fields stay per helper. */
|
|
403
|
+
type EntryFields = {
|
|
404
|
+
readonly enabled?: boolean;
|
|
405
|
+
readonly exposure?: McpExposure;
|
|
406
|
+
readonly toolExposure?: Record<string, McpExposure>;
|
|
407
|
+
readonly timeout?: number;
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
const entryOf = (
|
|
411
|
+
name: string,
|
|
412
|
+
config: EntryFields & { readonly env?: Record<string, string> } = {},
|
|
413
|
+
): McpServerEntry => ({
|
|
414
|
+
name,
|
|
415
|
+
config: { command: "node", ...config },
|
|
416
|
+
source: "/tmp/mcp.json",
|
|
417
|
+
scope: "global",
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
const httpEntryOf = (
|
|
421
|
+
name: string,
|
|
422
|
+
config: EntryFields & { readonly headers?: Record<string, string> } = {},
|
|
423
|
+
): McpServerEntry => ({
|
|
424
|
+
name,
|
|
425
|
+
config: { url: "https://example.test/mcp", ...config },
|
|
426
|
+
source: "/tmp/mcp.json",
|
|
427
|
+
scope: "global",
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
const hostFor = async (
|
|
431
|
+
entries: readonly McpServerEntry[],
|
|
432
|
+
options: {
|
|
433
|
+
readonly factory?: McpClientFactory;
|
|
434
|
+
readonly timers?: Timers;
|
|
435
|
+
readonly log?: LogSink;
|
|
436
|
+
readonly referenced?: ReadonlySet<string>;
|
|
437
|
+
readonly env?: Record<string, string | undefined>;
|
|
438
|
+
readonly onLost?: (server: string) => void;
|
|
439
|
+
readonly sessionDir?: string;
|
|
440
|
+
} = {},
|
|
441
|
+
): Promise<McpHost> => {
|
|
442
|
+
const sessionDir = options.sessionDir ?? (await mkdtemp(join(tmpdir(), "delegates-mcp-")));
|
|
443
|
+
return connectServers(entries, options.log ?? nullLogSink, {
|
|
444
|
+
referenced: options.referenced ?? new Set(entries.map((entry) => entry.name)),
|
|
445
|
+
timers: options.timers ?? systemTimers,
|
|
446
|
+
sessionDir,
|
|
447
|
+
...(options.factory === undefined ? {} : { factory: options.factory }),
|
|
448
|
+
...(options.env === undefined ? {} : { env: options.env }),
|
|
449
|
+
...(options.onLost === undefined ? {} : { onLost: options.onLost }),
|
|
450
|
+
});
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
const registryFor = (
|
|
454
|
+
mcp: McpHost,
|
|
455
|
+
options: { readonly log?: LogSink; readonly timers?: Timers } = {},
|
|
456
|
+
) =>
|
|
457
|
+
createDelegatesRegistry({
|
|
458
|
+
admission,
|
|
459
|
+
skills,
|
|
460
|
+
sandbox: refusingSandbox(),
|
|
461
|
+
mcp,
|
|
462
|
+
...(options.log === undefined ? {} : { log: options.log }),
|
|
463
|
+
...(options.timers === undefined ? {} : { timers: options.timers }),
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
const toolIn = (
|
|
467
|
+
mcp: McpHost,
|
|
468
|
+
name: string,
|
|
469
|
+
options: { readonly log?: LogSink; readonly timers?: Timers } = {},
|
|
470
|
+
): ToolRegistration => {
|
|
471
|
+
const found = registryFor(mcp, options)
|
|
472
|
+
.registry.snapshot()
|
|
473
|
+
.tools()
|
|
474
|
+
.find((entry) => entry.tool.name === name);
|
|
475
|
+
assert.ok(found !== undefined, `tool ${name} is not installed`);
|
|
476
|
+
return found.tool;
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
const outputText = (result: {
|
|
480
|
+
readonly output?: readonly { readonly type: string; readonly text?: string }[];
|
|
481
|
+
}): string => (result.output ?? []).map((item) => item.text ?? "").join("");
|
|
482
|
+
|
|
483
|
+
const skills: SkillsService = {
|
|
484
|
+
all: () => [],
|
|
485
|
+
find: () => undefined,
|
|
486
|
+
resolve: () => [],
|
|
487
|
+
render: () => "",
|
|
488
|
+
diagnostics: () => [],
|
|
489
|
+
reload: Effect.void,
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
const admission: AdmissionHost = {
|
|
493
|
+
inspect: () => Promise.reject(new Error("the install must not inspect the harness")),
|
|
494
|
+
caps: () => ({ laneCap: 1, sessionCeiling: 1 }),
|
|
495
|
+
pollMs: 10,
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
/** A timer source this test fires by hand, so no test waits ten seconds. */
|
|
499
|
+
const manualTimers = (): {
|
|
500
|
+
readonly timers: Timers;
|
|
501
|
+
readonly fire: () => void;
|
|
502
|
+
readonly pending: () => number;
|
|
503
|
+
} => {
|
|
504
|
+
const armed = new Map<number, () => void>();
|
|
505
|
+
let next = 1;
|
|
506
|
+
return {
|
|
507
|
+
timers: {
|
|
508
|
+
setTimeout: (callback) => {
|
|
509
|
+
const handle = next++;
|
|
510
|
+
armed.set(handle, callback);
|
|
511
|
+
return handle;
|
|
512
|
+
},
|
|
513
|
+
clearTimeout: (handle) => {
|
|
514
|
+
armed.delete(handle as number);
|
|
515
|
+
},
|
|
516
|
+
},
|
|
517
|
+
fire: () => {
|
|
518
|
+
for (const [handle, callback] of Array.from(armed.entries())) {
|
|
519
|
+
armed.delete(handle);
|
|
520
|
+
callback();
|
|
521
|
+
}
|
|
522
|
+
},
|
|
523
|
+
pending: () => armed.size,
|
|
524
|
+
};
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
const recordingSink = (): {
|
|
528
|
+
readonly sink: LogSink;
|
|
529
|
+
readonly entries: { level: LogLevel; code: string; fields: LogFields }[];
|
|
530
|
+
} => {
|
|
531
|
+
const entries: { level: LogLevel; code: string; fields: LogFields }[] = [];
|
|
532
|
+
return {
|
|
533
|
+
entries,
|
|
534
|
+
sink: {
|
|
535
|
+
write: (level, code, fields = {}) => {
|
|
536
|
+
entries.push({ level, code, fields });
|
|
537
|
+
},
|
|
538
|
+
},
|
|
539
|
+
};
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
it("a connected server's tools are registered under its namespace", async () => {
|
|
543
|
+
const fake = fakeClient({
|
|
544
|
+
tools: [{ name: "echo", description: "Echoes text", inputSchema: {} }],
|
|
545
|
+
});
|
|
546
|
+
const host = await hostFor([entryOf("demo")], {
|
|
547
|
+
factory: fakeFactory(async () => fake.client).factory,
|
|
548
|
+
});
|
|
549
|
+
try {
|
|
550
|
+
const snapshot = registryFor(host).registry.snapshot();
|
|
551
|
+
const extension = snapshot.extension("mcp:demo");
|
|
552
|
+
assert.ok(extension !== undefined);
|
|
553
|
+
assert.strictEqual(extension.namespace?.name, "mcp__demo");
|
|
554
|
+
const tool = snapshot.tools().find((entry) => entry.tool.name === "mcp__demo__echo")?.tool;
|
|
555
|
+
assert.ok(tool !== undefined);
|
|
556
|
+
// MCP servers may omit `type` and `properties`; the harness validates against both.
|
|
557
|
+
const parameters = tool.parameters as { readonly type?: string; readonly properties?: unknown };
|
|
558
|
+
assert.strictEqual(parameters.type, "object");
|
|
559
|
+
assert.deepStrictEqual(parameters.properties, {});
|
|
560
|
+
} finally {
|
|
561
|
+
await host.close();
|
|
562
|
+
}
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
it("a tool call returns the server's text", async () => {
|
|
566
|
+
const fake = fakeClient({ tools: [{ name: "echo", inputSchema: {} }] });
|
|
567
|
+
const host = await hostFor([entryOf("demo")], {
|
|
568
|
+
factory: fakeFactory(async () => fake.client).factory,
|
|
569
|
+
});
|
|
570
|
+
try {
|
|
571
|
+
const result = await toolIn(host, "mcp__demo__echo").execute(
|
|
572
|
+
{ text: "hi" },
|
|
573
|
+
{} as never,
|
|
574
|
+
BACKGROUND_CONTEXT,
|
|
575
|
+
);
|
|
576
|
+
assert.notStrictEqual(result.isError, true);
|
|
577
|
+
assert.deepStrictEqual(result.output, [{ type: "text", text: "hi" }]);
|
|
578
|
+
} finally {
|
|
579
|
+
await host.close();
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
it("a server that cannot connect degrades", async () => {
|
|
584
|
+
const factory = fakeFactory(async () => {
|
|
585
|
+
throw new Error("spawn ENOENT");
|
|
586
|
+
}).factory;
|
|
587
|
+
const host = await hostFor([entryOf("demo")], { factory });
|
|
588
|
+
try {
|
|
589
|
+
assert.deepStrictEqual(host.connections, []);
|
|
590
|
+
assert.deepStrictEqual(host.presence.unconnected, ["demo"]);
|
|
591
|
+
assert.deepStrictEqual(host.failures, [{ server: "demo", code: "mcp.connect_failed" }]);
|
|
592
|
+
assert.strictEqual(host.diagnostics.length, 1);
|
|
593
|
+
assert.strictEqual(host.diagnostics[0]?.code, "mcp.connect_failed");
|
|
594
|
+
assert.ok(host.diagnostics[0]?.detail.includes("demo"), host.diagnostics[0]?.detail);
|
|
595
|
+
assert.ok(host.diagnostics[0]?.detail.includes("spawn ENOENT"), host.diagnostics[0]?.detail);
|
|
596
|
+
} finally {
|
|
597
|
+
await host.close();
|
|
598
|
+
}
|
|
599
|
+
});
|
|
600
|
+
|
|
601
|
+
it("a server that cannot list its tools degrades", async () => {
|
|
602
|
+
const fake = fakeClient({ listTools: () => Promise.reject(new Error("tools/list refused")) });
|
|
603
|
+
const host = await hostFor([entryOf("demo")], {
|
|
604
|
+
factory: fakeFactory(async () => fake.client).factory,
|
|
605
|
+
});
|
|
606
|
+
try {
|
|
607
|
+
assert.deepStrictEqual(host.connections, []);
|
|
608
|
+
assert.deepStrictEqual(host.failures, [{ server: "demo", code: "mcp.list_tools_failed" }]);
|
|
609
|
+
assert.strictEqual(host.diagnostics[0]?.code, "mcp.list_tools_failed");
|
|
610
|
+
assert.ok(host.diagnostics[0]?.detail.includes("tools/list refused"));
|
|
611
|
+
// The client is closed, so a server whose tools cannot be listed keeps no process.
|
|
612
|
+
assert.strictEqual(fake.closed(), 1);
|
|
613
|
+
} finally {
|
|
614
|
+
await host.close();
|
|
615
|
+
}
|
|
616
|
+
});
|
|
617
|
+
|
|
618
|
+
it("a disabled server connects nothing", async () => {
|
|
619
|
+
const fake = fakeClient();
|
|
620
|
+
const { factory, asked } = fakeFactory(async () => fake.client);
|
|
621
|
+
const host = await hostFor([entryOf("demo", { enabled: false })], { factory });
|
|
622
|
+
try {
|
|
623
|
+
assert.deepStrictEqual(asked, []);
|
|
624
|
+
assert.deepStrictEqual(host.diagnostics, []);
|
|
625
|
+
assert.deepStrictEqual(host.failures, []);
|
|
626
|
+
} finally {
|
|
627
|
+
await host.close();
|
|
628
|
+
}
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
it("close closes every connection", async () => {
|
|
632
|
+
const first = fakeClient();
|
|
633
|
+
const second = fakeClient();
|
|
634
|
+
const host = await hostFor([entryOf("a"), entryOf("b")], {
|
|
635
|
+
factory: fakeFactory(async (name) => (name === "a" ? first.client : second.client)).factory,
|
|
636
|
+
});
|
|
637
|
+
await host.close();
|
|
638
|
+
assert.strictEqual(first.closed(), 1);
|
|
639
|
+
assert.strictEqual(second.closed(), 1);
|
|
640
|
+
await host.close();
|
|
641
|
+
assert.strictEqual(first.closed(), 1);
|
|
642
|
+
});
|
|
643
|
+
|
|
644
|
+
it("a connect that never answers degrades after 10 s", async () => {
|
|
645
|
+
const timers = manualTimers();
|
|
646
|
+
const hanging = new Promise<McpClientPort>(() => {});
|
|
647
|
+
const host = hostFor([entryOf("demo")], {
|
|
648
|
+
factory: fakeFactory(async () => hanging).factory,
|
|
649
|
+
timers: timers.timers,
|
|
650
|
+
});
|
|
651
|
+
await waitFor(() => timers.pending() === 1, "the connect deadline is armed");
|
|
652
|
+
timers.fire();
|
|
653
|
+
const connected = await host;
|
|
654
|
+
assert.deepStrictEqual(connected.connections, []);
|
|
655
|
+
assert.deepStrictEqual(connected.failures, [{ server: "demo", code: "mcp.connect_timeout" }]);
|
|
656
|
+
assert.strictEqual(connected.diagnostics[0]?.code, "mcp.connect_timeout");
|
|
657
|
+
assert.ok(connected.diagnostics[0]?.detail.includes("demo"));
|
|
658
|
+
assert.strictEqual(timers.pending(), 0);
|
|
659
|
+
});
|
|
660
|
+
|
|
661
|
+
it("a toolExposure entry overrides the server exposure", async () => {
|
|
662
|
+
const fake = fakeClient({
|
|
663
|
+
tools: [
|
|
664
|
+
{ name: "echo", inputSchema: {} },
|
|
665
|
+
{ name: "add", inputSchema: {} },
|
|
666
|
+
{ name: "secret", inputSchema: {} },
|
|
667
|
+
],
|
|
668
|
+
});
|
|
669
|
+
const host = await hostFor(
|
|
670
|
+
[entryOf("demo", { exposure: "hidden", toolExposure: { echo: "direct", add: "codemode" } })],
|
|
671
|
+
{ factory: fakeFactory(async () => fake.client).factory },
|
|
672
|
+
);
|
|
673
|
+
try {
|
|
674
|
+
assert.strictEqual(host.connections.length, 1);
|
|
675
|
+
const snapshot = registryFor(host).registry.snapshot();
|
|
676
|
+
const tools = snapshot.tools();
|
|
677
|
+
const callers = (name: string) => tools.find((entry) => entry.tool.name === name)?.tool.callers;
|
|
678
|
+
assert.deepStrictEqual(callers("mcp__demo__echo"), ["model", "tools"]);
|
|
679
|
+
assert.deepStrictEqual(callers("mcp__demo__add"), ["tools"]);
|
|
680
|
+
assert.ok(!tools.some((entry) => entry.tool.name === "mcp__demo__secret"));
|
|
681
|
+
} finally {
|
|
682
|
+
await host.close();
|
|
683
|
+
}
|
|
684
|
+
});
|
|
685
|
+
|
|
686
|
+
it("a pattern toolExposure applies only after an exact name misses", async () => {
|
|
687
|
+
const fake = fakeClient({
|
|
688
|
+
tools: [
|
|
689
|
+
{ name: "echo", inputSchema: {} },
|
|
690
|
+
{ name: "echoes", inputSchema: {} },
|
|
691
|
+
],
|
|
692
|
+
});
|
|
693
|
+
const host = await hostFor(
|
|
694
|
+
[entryOf("demo", { toolExposure: { "ec*": "deferred", echo: "direct" } })],
|
|
695
|
+
{
|
|
696
|
+
factory: fakeFactory(async () => fake.client).factory,
|
|
697
|
+
},
|
|
698
|
+
);
|
|
699
|
+
try {
|
|
700
|
+
const tools = registryFor(host).registry.snapshot().tools();
|
|
701
|
+
const exact = tools.find((entry) => entry.tool.name === "mcp__demo__echo")?.tool;
|
|
702
|
+
const pattern = tools.find((entry) => entry.tool.name === "mcp__demo__echoes")?.tool;
|
|
703
|
+
assert.deepStrictEqual(exact?.callers, ["model", "tools"]);
|
|
704
|
+
assert.strictEqual(exact?.deferred, undefined);
|
|
705
|
+
assert.deepStrictEqual(pattern?.callers, ["model", "tools"]);
|
|
706
|
+
assert.strictEqual(pattern?.deferred, true);
|
|
707
|
+
} finally {
|
|
708
|
+
await host.close();
|
|
709
|
+
}
|
|
710
|
+
});
|
|
711
|
+
|
|
712
|
+
it("connects run concurrently", async () => {
|
|
713
|
+
const asked: string[] = [];
|
|
714
|
+
let release = (): void => {};
|
|
715
|
+
const both = new Promise<void>((resolve) => {
|
|
716
|
+
release = resolve;
|
|
717
|
+
});
|
|
718
|
+
const clients = new Map([
|
|
719
|
+
["a", fakeClient()],
|
|
720
|
+
["b", fakeClient()],
|
|
721
|
+
]);
|
|
722
|
+
const factory: McpClientFactory = {
|
|
723
|
+
connect: async (entry) => {
|
|
724
|
+
asked.push(entry.name);
|
|
725
|
+
if (asked.length === 2) release();
|
|
726
|
+
await both;
|
|
727
|
+
return clients.get(entry.name)!.client;
|
|
728
|
+
},
|
|
729
|
+
};
|
|
730
|
+
const host = await hostFor([entryOf("a"), entryOf("b")], { factory });
|
|
731
|
+
try {
|
|
732
|
+
assert.deepStrictEqual(asked, ["a", "b"]);
|
|
733
|
+
assert.deepStrictEqual(
|
|
734
|
+
host.connections.map((connection) => connection.server),
|
|
735
|
+
["a", "b"],
|
|
736
|
+
);
|
|
737
|
+
} finally {
|
|
738
|
+
await host.close();
|
|
739
|
+
}
|
|
740
|
+
});
|
|
741
|
+
|
|
742
|
+
it("a call is bounded by the server timeout", async () => {
|
|
743
|
+
const timers = manualTimers();
|
|
744
|
+
const { sink, entries } = recordingSink();
|
|
745
|
+
const fake = fakeClient({
|
|
746
|
+
tools: [{ name: "slow", inputSchema: {} }],
|
|
747
|
+
callTool: () => new Promise(() => {}),
|
|
748
|
+
});
|
|
749
|
+
const host = await hostFor([entryOf("demo", { timeout: 1 })], {
|
|
750
|
+
factory: fakeFactory(async () => fake.client).factory,
|
|
751
|
+
timers: timers.timers,
|
|
752
|
+
log: sink,
|
|
753
|
+
});
|
|
754
|
+
try {
|
|
755
|
+
const pending = toolIn(host, "mcp__demo__slow", {
|
|
756
|
+
log: sink,
|
|
757
|
+
timers: timers.timers,
|
|
758
|
+
}).execute({}, {} as never, BACKGROUND_CONTEXT);
|
|
759
|
+
await Promise.resolve();
|
|
760
|
+
timers.fire();
|
|
761
|
+
const result = await pending;
|
|
762
|
+
assert.strictEqual(result.isError, true);
|
|
763
|
+
assert.ok(outputText(result).includes("demo"), outputText(result));
|
|
764
|
+
assert.deepStrictEqual(
|
|
765
|
+
entries.filter((entry) => entry.code.startsWith("mcp.")).map((entry) => entry.code),
|
|
766
|
+
["mcp.call_timeout"],
|
|
767
|
+
);
|
|
768
|
+
assert.strictEqual(entries[0]?.fields.server, "demo");
|
|
769
|
+
} finally {
|
|
770
|
+
await host.close();
|
|
771
|
+
}
|
|
772
|
+
});
|
|
773
|
+
|
|
774
|
+
it("a call stops when the run is aborted", async () => {
|
|
775
|
+
const timers = manualTimers();
|
|
776
|
+
const { sink, entries } = recordingSink();
|
|
777
|
+
const fake = fakeClient({
|
|
778
|
+
tools: [{ name: "slow", inputSchema: {} }],
|
|
779
|
+
callTool: () => new Promise(() => {}),
|
|
780
|
+
});
|
|
781
|
+
const host = await hostFor([entryOf("demo")], {
|
|
782
|
+
factory: fakeFactory(async () => fake.client).factory,
|
|
783
|
+
timers: timers.timers,
|
|
784
|
+
log: sink,
|
|
785
|
+
});
|
|
786
|
+
try {
|
|
787
|
+
const controller = new AbortController();
|
|
788
|
+
const context = {
|
|
789
|
+
...BACKGROUND_CONTEXT,
|
|
790
|
+
abortSignal: controller.signal,
|
|
791
|
+
} as typeof BACKGROUND_CONTEXT;
|
|
792
|
+
const pending = toolIn(host, "mcp__demo__slow", {
|
|
793
|
+
log: sink,
|
|
794
|
+
timers: timers.timers,
|
|
795
|
+
}).execute({}, {} as never, context);
|
|
796
|
+
await Promise.resolve();
|
|
797
|
+
controller.abort();
|
|
798
|
+
const result = await pending;
|
|
799
|
+
assert.strictEqual(result.isError, true);
|
|
800
|
+
assert.deepStrictEqual(
|
|
801
|
+
entries.filter((entry) => entry.code === "mcp.call_timeout"),
|
|
802
|
+
[],
|
|
803
|
+
);
|
|
804
|
+
assert.strictEqual(timers.pending(), 0);
|
|
805
|
+
} finally {
|
|
806
|
+
await host.close();
|
|
807
|
+
}
|
|
808
|
+
});
|
|
809
|
+
|
|
810
|
+
const sessionIdentity = async (sessionId: string): Promise<SessionIdentity> => {
|
|
811
|
+
const sessionDir = await mkdtemp(join(tmpdir(), "delegates-mcp-session-"));
|
|
812
|
+
return { sessionId, sessionDir, cwd: sessionDir, agentDir: sessionDir };
|
|
813
|
+
};
|
|
814
|
+
|
|
815
|
+
const models = (): HostModelAdapter => new HostModelAdapter(stubHostSource());
|
|
816
|
+
|
|
817
|
+
const definitionOf = (name: string, tools: readonly string[]): AgentDefinition => ({
|
|
818
|
+
name,
|
|
819
|
+
role: name,
|
|
820
|
+
prompt: { role: name },
|
|
821
|
+
tools,
|
|
822
|
+
extensions: [],
|
|
823
|
+
approval: { gates: [] },
|
|
824
|
+
});
|
|
825
|
+
|
|
826
|
+
const writeMcpJson = async (dir: string, servers: Record<string, unknown>): Promise<void> => {
|
|
827
|
+
await writeFile(join(dir, "mcp.json"), JSON.stringify({ mcpServers: servers }));
|
|
828
|
+
};
|
|
829
|
+
|
|
830
|
+
it("only a referenced server connects", async () => {
|
|
831
|
+
const identity = await sessionIdentity("s-1");
|
|
832
|
+
await writeMcpJson(identity.agentDir, { demo: { command: "node" }, other: { command: "node" } });
|
|
833
|
+
const fake = fakeClient({ tools: [{ name: "echo", inputSchema: {} }] });
|
|
834
|
+
const { factory, asked } = fakeFactory(async () => fake.client);
|
|
835
|
+
const runtime = await openSessionRuntime({
|
|
836
|
+
identity,
|
|
837
|
+
models: models(),
|
|
838
|
+
catalogue: createCatalogue([definitionOf("echoer", ["read", "mcp__demo__*"])]),
|
|
839
|
+
mcpFactory: factory,
|
|
840
|
+
sandbox: refusingSandbox(),
|
|
841
|
+
});
|
|
842
|
+
try {
|
|
843
|
+
assert.deepStrictEqual(asked, ["demo"]);
|
|
844
|
+
const installed = runtime.registry
|
|
845
|
+
.snapshot()
|
|
846
|
+
.installed()
|
|
847
|
+
.map((extension) => extension.name);
|
|
848
|
+
assert.ok(installed.includes("mcp:demo"));
|
|
849
|
+
assert.ok(!installed.includes("mcp:other"));
|
|
850
|
+
assert.deepStrictEqual(runtime.mcpPresence.unconnected, ["other"]);
|
|
851
|
+
} finally {
|
|
852
|
+
await runtime.close();
|
|
853
|
+
}
|
|
854
|
+
});
|
|
855
|
+
|
|
856
|
+
it("an exact tool name references its server", async () => {
|
|
857
|
+
const identity = await sessionIdentity("s-1");
|
|
858
|
+
await writeMcpJson(identity.agentDir, { demo: { command: "node" } });
|
|
859
|
+
const fake = fakeClient({ tools: [{ name: "echo", inputSchema: {} }] });
|
|
860
|
+
const { factory, asked } = fakeFactory(async () => fake.client);
|
|
861
|
+
const runtime = await openSessionRuntime({
|
|
862
|
+
identity,
|
|
863
|
+
models: models(),
|
|
864
|
+
catalogue: createCatalogue([definitionOf("echoer", ["mcp__demo__echo"])]),
|
|
865
|
+
mcpFactory: factory,
|
|
866
|
+
sandbox: refusingSandbox(),
|
|
867
|
+
});
|
|
868
|
+
try {
|
|
869
|
+
assert.deepStrictEqual(asked, ["demo"]);
|
|
870
|
+
const bound = await Effect.runPromise(
|
|
871
|
+
bindToolkit(runtime.catalogue.find("echoer")!, resolverOf(runtime)),
|
|
872
|
+
);
|
|
873
|
+
assert.deepStrictEqual(bound.names.tools, ["mcp__demo__echo"]);
|
|
874
|
+
assert.ok(bound.names.extensions.includes("mcp:demo"));
|
|
875
|
+
} finally {
|
|
876
|
+
await runtime.close();
|
|
877
|
+
}
|
|
878
|
+
});
|
|
879
|
+
|
|
880
|
+
it("a runtime with no MCP reference connects nothing", async () => {
|
|
881
|
+
const identity = await sessionIdentity("s-1");
|
|
882
|
+
await writeMcpJson(identity.agentDir, { demo: { command: "node" } });
|
|
883
|
+
const fake = fakeClient();
|
|
884
|
+
const { factory, asked } = fakeFactory(async () => fake.client);
|
|
885
|
+
const runtime = await openSessionRuntime({
|
|
886
|
+
identity,
|
|
887
|
+
models: models(),
|
|
888
|
+
catalogue: createCatalogue([definitionOf("reader", ["read"])]),
|
|
889
|
+
mcpFactory: factory,
|
|
890
|
+
sandbox: refusingSandbox(),
|
|
891
|
+
});
|
|
892
|
+
try {
|
|
893
|
+
assert.deepStrictEqual(asked, []);
|
|
894
|
+
const installed = runtime.registry
|
|
895
|
+
.snapshot()
|
|
896
|
+
.installed()
|
|
897
|
+
.map((extension) => extension.name);
|
|
898
|
+
assert.ok(!installed.some((name) => name.startsWith("mcp:")));
|
|
899
|
+
} finally {
|
|
900
|
+
await runtime.close();
|
|
901
|
+
}
|
|
902
|
+
});
|
|
903
|
+
|
|
904
|
+
it("the runtime reports unconnected servers", async () => {
|
|
905
|
+
const identity = await sessionIdentity("s-1");
|
|
906
|
+
await writeMcpJson(identity.agentDir, {
|
|
907
|
+
demo: { command: "node" },
|
|
908
|
+
other: { command: "node" },
|
|
909
|
+
third: { command: "node", enabled: false },
|
|
910
|
+
});
|
|
911
|
+
const factory: McpClientFactory = {
|
|
912
|
+
connect: async (entry) => {
|
|
913
|
+
if (entry.name === "other") throw new Error("spawn ENOENT");
|
|
914
|
+
return fakeClient({ tools: [{ name: "echo", inputSchema: {} }] }).client;
|
|
915
|
+
},
|
|
916
|
+
};
|
|
917
|
+
const runtime = await openSessionRuntime({
|
|
918
|
+
identity,
|
|
919
|
+
models: models(),
|
|
920
|
+
catalogue: createCatalogue([
|
|
921
|
+
definitionOf("echoer", ["read", "mcp__demo__*", "mcp__other__*", "mcp__third__*"]),
|
|
922
|
+
]),
|
|
923
|
+
mcpFactory: factory,
|
|
924
|
+
sandbox: refusingSandbox(),
|
|
925
|
+
});
|
|
926
|
+
try {
|
|
927
|
+
assert.deepStrictEqual(runtime.mcpPresence.unconnected, ["other", "third"]);
|
|
928
|
+
} finally {
|
|
929
|
+
await runtime.close();
|
|
930
|
+
}
|
|
931
|
+
});
|
|
932
|
+
|
|
933
|
+
it("session start shows one aggregated notice", async () => {
|
|
934
|
+
const sessionDir = await mkdtemp(join(tmpdir(), "delegates-mcp-entry-"));
|
|
935
|
+
const agentDir = await mkdtemp(join(tmpdir(), "delegates-mcp-agent-"));
|
|
936
|
+
await writeMcpJson(agentDir, {
|
|
937
|
+
demo: { command: "/nonexistent/delegates-mcp-demo" },
|
|
938
|
+
other: { command: "/nonexistent/delegates-mcp-other" },
|
|
939
|
+
off: { command: "node", enabled: false },
|
|
940
|
+
unused: { command: "node" },
|
|
941
|
+
});
|
|
942
|
+
const stub = makeStubPi({
|
|
943
|
+
sessionId: "s-1",
|
|
944
|
+
sessionDir,
|
|
945
|
+
cwd: sessionDir,
|
|
946
|
+
modelRegistry: models(),
|
|
947
|
+
});
|
|
948
|
+
createDelegatesExtension({
|
|
949
|
+
agentDir,
|
|
950
|
+
definitions: [definitionOf("echoer", ["read", "mcp__demo__*", "mcp__other__*", "mcp__off__*"])],
|
|
951
|
+
})(stub.api);
|
|
952
|
+
try {
|
|
953
|
+
await stub.fire("session_start", { type: "session_start", reason: "startup" });
|
|
954
|
+
assert.strictEqual(stub.notices.length, 1);
|
|
955
|
+
const notice = stub.notices[0]!;
|
|
956
|
+
assert.strictEqual(notice.severity, "warning");
|
|
957
|
+
assert.ok(notice.message.includes("demo"), notice.message);
|
|
958
|
+
assert.ok(notice.message.includes("other"), notice.message);
|
|
959
|
+
assert.ok(!notice.message.includes("off"), notice.message);
|
|
960
|
+
assert.ok(!notice.message.includes("unused"), notice.message);
|
|
961
|
+
const lines = readFileSync(join(sessionDir, "delegates.log"), "utf8")
|
|
962
|
+
.trim()
|
|
963
|
+
.split("\n")
|
|
964
|
+
.map((line) => JSON.parse(line) as { code: string; detail?: string });
|
|
965
|
+
const failures = lines.filter((line) => line.code === "mcp.connect_failed");
|
|
966
|
+
assert.strictEqual(failures.length, 2);
|
|
967
|
+
} finally {
|
|
968
|
+
await stub.fire("session_shutdown", { type: "session_shutdown" });
|
|
969
|
+
}
|
|
970
|
+
});
|
|
971
|
+
|
|
972
|
+
const hostDefaults: HostDefaults = {
|
|
973
|
+
model: { provider: "stub", modelId: "stub-1" },
|
|
974
|
+
thinkingLevel: "low",
|
|
975
|
+
};
|
|
976
|
+
|
|
977
|
+
const openWithMcp = async (options: {
|
|
978
|
+
readonly definitions: readonly AgentDefinition[];
|
|
979
|
+
readonly servers: Record<string, unknown>;
|
|
980
|
+
readonly factory: McpClientFactory;
|
|
981
|
+
}): Promise<{ readonly runtime: SessionRuntime; readonly identity: SessionIdentity }> => {
|
|
982
|
+
const identity = await sessionIdentity("s-1");
|
|
983
|
+
await writeMcpJson(identity.agentDir, options.servers);
|
|
984
|
+
const runtime = await openSessionRuntime({
|
|
985
|
+
identity,
|
|
986
|
+
models: models(),
|
|
987
|
+
catalogue: createCatalogue(options.definitions),
|
|
988
|
+
mcpFactory: options.factory,
|
|
989
|
+
sandbox: refusingSandbox(),
|
|
990
|
+
});
|
|
991
|
+
return { runtime, identity };
|
|
992
|
+
};
|
|
993
|
+
|
|
994
|
+
const resolverOf = (runtime: SessionRuntime): ToolkitResolver =>
|
|
995
|
+
resolverFor({
|
|
996
|
+
registry: runtime.registry.snapshot(),
|
|
997
|
+
skills: runtime.skills,
|
|
998
|
+
presence: runtime.mcpPresence,
|
|
999
|
+
});
|
|
1000
|
+
|
|
1001
|
+
const validHost = (runtime: SessionRuntime) => ({
|
|
1002
|
+
runtime,
|
|
1003
|
+
catalogue: runtime.catalogue,
|
|
1004
|
+
host: hostDefaults,
|
|
1005
|
+
waitMs: 5_000,
|
|
1006
|
+
});
|
|
1007
|
+
|
|
1008
|
+
it("a definition selects a server's tools by pattern", async () => {
|
|
1009
|
+
const fake = fakeClient({
|
|
1010
|
+
tools: [
|
|
1011
|
+
{ name: "echo", inputSchema: {} },
|
|
1012
|
+
{ name: "add", inputSchema: {} },
|
|
1013
|
+
],
|
|
1014
|
+
});
|
|
1015
|
+
const { runtime } = await openWithMcp({
|
|
1016
|
+
definitions: [definitionOf("echoer", ["read", "mcp__demo__*"])],
|
|
1017
|
+
servers: { demo: { command: "node" } },
|
|
1018
|
+
factory: fakeFactory(async () => fake.client).factory,
|
|
1019
|
+
});
|
|
1020
|
+
try {
|
|
1021
|
+
const bound = await Effect.runPromise(
|
|
1022
|
+
bindToolkit(runtime.catalogue.find("echoer")!, resolverOf(runtime)),
|
|
1023
|
+
);
|
|
1024
|
+
assert.ok(bound.names.tools.includes("mcp__demo__echo"), String(bound.names.tools));
|
|
1025
|
+
assert.ok(bound.names.tools.includes("mcp__demo__add"));
|
|
1026
|
+
assert.ok(bound.names.extensions.includes("mcp:demo"));
|
|
1027
|
+
// A codemode-exposed tool needs codemode, and only codemode: `runs` stays opt-in.
|
|
1028
|
+
assert.ok(bound.names.extensions.includes("codemode"), String(bound.names.extensions));
|
|
1029
|
+
assert.ok(!bound.names.extensions.includes("runs"));
|
|
1030
|
+
} finally {
|
|
1031
|
+
await runtime.close();
|
|
1032
|
+
}
|
|
1033
|
+
});
|
|
1034
|
+
|
|
1035
|
+
it("an exact MCP tool name selects its server extension", async () => {
|
|
1036
|
+
const fake = fakeClient({ tools: [{ name: "echo", inputSchema: {} }] });
|
|
1037
|
+
const { runtime } = await openWithMcp({
|
|
1038
|
+
definitions: [definitionOf("echoer", ["mcp__demo__echo"])],
|
|
1039
|
+
servers: { demo: { command: "node" } },
|
|
1040
|
+
factory: fakeFactory(async () => fake.client).factory,
|
|
1041
|
+
});
|
|
1042
|
+
try {
|
|
1043
|
+
const bound = await Effect.runPromise(
|
|
1044
|
+
bindToolkit(runtime.catalogue.find("echoer")!, resolverOf(runtime)),
|
|
1045
|
+
);
|
|
1046
|
+
assert.deepStrictEqual(bound.names.tools, ["mcp__demo__echo"]);
|
|
1047
|
+
assert.ok(bound.names.extensions.includes("mcp:demo"));
|
|
1048
|
+
} finally {
|
|
1049
|
+
await runtime.close();
|
|
1050
|
+
}
|
|
1051
|
+
});
|
|
1052
|
+
|
|
1053
|
+
it("an unknown namespace fails the launch", async () => {
|
|
1054
|
+
const fake = fakeClient({ tools: [{ name: "echo", inputSchema: {} }] });
|
|
1055
|
+
const factory: McpClientFactory = {
|
|
1056
|
+
connect: async (entry) => {
|
|
1057
|
+
if (entry.name === "absent") throw new Error("spawn ENOENT");
|
|
1058
|
+
return fake.client;
|
|
1059
|
+
},
|
|
1060
|
+
};
|
|
1061
|
+
const { runtime } = await openWithMcp({
|
|
1062
|
+
definitions: [
|
|
1063
|
+
definitionOf("echoer", ["mcp__demo__*"]),
|
|
1064
|
+
definitionOf("absentees", ["mcp__absent__*"]),
|
|
1065
|
+
],
|
|
1066
|
+
servers: { demo: { command: "node" }, absent: { command: "node" } },
|
|
1067
|
+
factory,
|
|
1068
|
+
});
|
|
1069
|
+
try {
|
|
1070
|
+
await rejects(
|
|
1071
|
+
launch({ kind: "single", agent: "absentees", task: "go" }, validHost(runtime)),
|
|
1072
|
+
(error: unknown) =>
|
|
1073
|
+
error instanceof ToolkitUnresolved &&
|
|
1074
|
+
error.message.includes("mcp__absent__") &&
|
|
1075
|
+
error.message.includes("mcp__demo") &&
|
|
1076
|
+
error.message.includes("absent") &&
|
|
1077
|
+
error.message.includes("not connected"),
|
|
1078
|
+
);
|
|
1079
|
+
assert.strictEqual(await runtime.snapshot(RunIndexDoc), undefined);
|
|
1080
|
+
} finally {
|
|
1081
|
+
await runtime.close();
|
|
1082
|
+
}
|
|
1083
|
+
});
|
|
1084
|
+
|
|
1085
|
+
it("a pattern that matches no tool of a known server fails", async () => {
|
|
1086
|
+
const fake = fakeClient({ tools: [{ name: "echo", inputSchema: {} }] });
|
|
1087
|
+
const { runtime } = await openWithMcp({
|
|
1088
|
+
definitions: [definitionOf("echoer", ["mcp__demo__zzz*"])],
|
|
1089
|
+
servers: { demo: { command: "node" } },
|
|
1090
|
+
factory: fakeFactory(async () => fake.client).factory,
|
|
1091
|
+
});
|
|
1092
|
+
try {
|
|
1093
|
+
await rejects(
|
|
1094
|
+
launch({ kind: "single", agent: "echoer", task: "go" }, validHost(runtime)),
|
|
1095
|
+
(error: unknown) =>
|
|
1096
|
+
error instanceof ToolkitUnresolved &&
|
|
1097
|
+
error.message.includes("mcp__demo__zzz*") &&
|
|
1098
|
+
error.message.includes("mcp__demo__echo"),
|
|
1099
|
+
);
|
|
1100
|
+
} finally {
|
|
1101
|
+
await runtime.close();
|
|
1102
|
+
}
|
|
1103
|
+
});
|
|
1104
|
+
|
|
1105
|
+
it("a cross-server pattern is refused", async () => {
|
|
1106
|
+
const fake = fakeClient({ tools: [{ name: "echo", inputSchema: {} }] });
|
|
1107
|
+
const { runtime } = await openWithMcp({
|
|
1108
|
+
definitions: [
|
|
1109
|
+
definitionOf("echoer", ["mcp__demo__*"]),
|
|
1110
|
+
definitionOf("every", ["mcp__*"]),
|
|
1111
|
+
definitionOf("partial", ["mcp__d*"]),
|
|
1112
|
+
definitionOf("plain", ["read*"]),
|
|
1113
|
+
],
|
|
1114
|
+
servers: { demo: { command: "node" } },
|
|
1115
|
+
factory: fakeFactory(async () => fake.client).factory,
|
|
1116
|
+
});
|
|
1117
|
+
try {
|
|
1118
|
+
for (const agent of ["every", "partial", "plain"]) {
|
|
1119
|
+
await rejects(
|
|
1120
|
+
launch({ kind: "single", agent, task: "go" }, validHost(runtime)),
|
|
1121
|
+
(error: unknown) =>
|
|
1122
|
+
error instanceof ToolkitUnresolved &&
|
|
1123
|
+
error.message.includes("mcp__<server>__*") &&
|
|
1124
|
+
error.message.includes("mcp__demo"),
|
|
1125
|
+
);
|
|
1126
|
+
}
|
|
1127
|
+
assert.strictEqual(await runtime.snapshot(RunIndexDoc), undefined);
|
|
1128
|
+
} finally {
|
|
1129
|
+
await runtime.close();
|
|
1130
|
+
}
|
|
1131
|
+
});
|
|
1132
|
+
|
|
1133
|
+
it("a deferred tool selects the tool-search extension", async () => {
|
|
1134
|
+
const fake = fakeClient({
|
|
1135
|
+
tools: [
|
|
1136
|
+
{ name: "echo", inputSchema: {} },
|
|
1137
|
+
{ name: "add", inputSchema: {} },
|
|
1138
|
+
],
|
|
1139
|
+
});
|
|
1140
|
+
const { runtime } = await openWithMcp({
|
|
1141
|
+
definitions: [definitionOf("echoer", ["mcp__demo__*"])],
|
|
1142
|
+
servers: { demo: { command: "node", exposure: "deferred" } },
|
|
1143
|
+
factory: fakeFactory(async () => fake.client).factory,
|
|
1144
|
+
});
|
|
1145
|
+
try {
|
|
1146
|
+
const bound = await Effect.runPromise(
|
|
1147
|
+
bindToolkit(runtime.catalogue.find("echoer")!, resolverOf(runtime)),
|
|
1148
|
+
);
|
|
1149
|
+
assert.ok(bound.names.extensions.includes("tool-search"), String(bound.names.extensions));
|
|
1150
|
+
assert.ok(!bound.names.extensions.includes("tool_search"));
|
|
1151
|
+
assert.ok(
|
|
1152
|
+
runtime.registry
|
|
1153
|
+
.snapshot()
|
|
1154
|
+
.tools()
|
|
1155
|
+
.some((entry) => entry.tool.name === "tool_search"),
|
|
1156
|
+
);
|
|
1157
|
+
assert.ok(bound.names.tools.includes("mcp__demo__echo"));
|
|
1158
|
+
} finally {
|
|
1159
|
+
await runtime.close();
|
|
1160
|
+
}
|
|
1161
|
+
});
|
|
1162
|
+
|
|
1163
|
+
it("a direct tool does not select tool-search", async () => {
|
|
1164
|
+
const fake = fakeClient({ tools: [{ name: "echo", inputSchema: {} }] });
|
|
1165
|
+
const { runtime } = await openWithMcp({
|
|
1166
|
+
definitions: [definitionOf("echoer", ["mcp__demo__*"])],
|
|
1167
|
+
servers: { demo: { command: "node", exposure: "direct" } },
|
|
1168
|
+
factory: fakeFactory(async () => fake.client).factory,
|
|
1169
|
+
});
|
|
1170
|
+
try {
|
|
1171
|
+
const bound = await Effect.runPromise(
|
|
1172
|
+
bindToolkit(runtime.catalogue.find("echoer")!, resolverOf(runtime)),
|
|
1173
|
+
);
|
|
1174
|
+
assert.ok(!bound.names.extensions.includes("tool-search"));
|
|
1175
|
+
} finally {
|
|
1176
|
+
await runtime.close();
|
|
1177
|
+
}
|
|
1178
|
+
});
|
|
1179
|
+
|
|
1180
|
+
const launchWithMcp = async () => {
|
|
1181
|
+
const fake = fakeClient({
|
|
1182
|
+
tools: [
|
|
1183
|
+
{ name: "echo", inputSchema: {} },
|
|
1184
|
+
{ name: "add", inputSchema: {} },
|
|
1185
|
+
],
|
|
1186
|
+
});
|
|
1187
|
+
const opened = await openWithMcp({
|
|
1188
|
+
definitions: [definitionOf("echoer", ["mcp__demo__*"])],
|
|
1189
|
+
servers: { demo: { command: "node" } },
|
|
1190
|
+
factory: fakeFactory(async () => fake.client).factory,
|
|
1191
|
+
});
|
|
1192
|
+
const result = await launch(
|
|
1193
|
+
{ kind: "single", agent: "echoer", task: "say hi" },
|
|
1194
|
+
validHost(opened.runtime),
|
|
1195
|
+
);
|
|
1196
|
+
return { ...opened, result };
|
|
1197
|
+
};
|
|
1198
|
+
|
|
1199
|
+
it("the document records expanded names, not the pattern", async () => {
|
|
1200
|
+
const { runtime, result } = await launchWithMcp();
|
|
1201
|
+
try {
|
|
1202
|
+
const agent = await runtime.run((context) =>
|
|
1203
|
+
runtime.harness.snapshot(AgentDoc, result.view.conversationId as never, context),
|
|
1204
|
+
);
|
|
1205
|
+
assert.deepStrictEqual(agent?.tools, ["mcp__demo__echo", "mcp__demo__add"]);
|
|
1206
|
+
const viewed = await viewRun(result.view.runId, runtime);
|
|
1207
|
+
assert.deepStrictEqual(viewed.missing, []);
|
|
1208
|
+
} finally {
|
|
1209
|
+
await runtime.close();
|
|
1210
|
+
}
|
|
1211
|
+
});
|
|
1212
|
+
|
|
1213
|
+
it("a viewer does not report MCP names missing", async () => {
|
|
1214
|
+
const { runtime, identity, result } = await launchWithMcp();
|
|
1215
|
+
await runtime.close();
|
|
1216
|
+
const viewer = await openReadOnlyRuntime({
|
|
1217
|
+
storePath: storePathFor(identity),
|
|
1218
|
+
models: models(),
|
|
1219
|
+
});
|
|
1220
|
+
try {
|
|
1221
|
+
const view = await viewSession(viewer);
|
|
1222
|
+
const run = view.runs.find((entry) => entry.runId === result.view.runId);
|
|
1223
|
+
assert.ok(run !== undefined);
|
|
1224
|
+
assert.deepStrictEqual(run.missing, []);
|
|
1225
|
+
} finally {
|
|
1226
|
+
await viewer.close();
|
|
1227
|
+
}
|
|
1228
|
+
});
|
|
1229
|
+
|
|
1230
|
+
it("an http entry builds an http transport", async () => {
|
|
1231
|
+
const fake = fakeClient({ tools: [{ name: "echo", inputSchema: {} }] });
|
|
1232
|
+
const { factory, asked, entries } = fakeFactory(async () => fake.client);
|
|
1233
|
+
const host = await hostFor(
|
|
1234
|
+
[
|
|
1235
|
+
httpEntryOf("docs", {
|
|
1236
|
+
headers: { Authorization: "Bearer ${TOKEN}" },
|
|
1237
|
+
}),
|
|
1238
|
+
],
|
|
1239
|
+
{ factory, env: { TOKEN: "secret" } },
|
|
1240
|
+
);
|
|
1241
|
+
try {
|
|
1242
|
+
assert.deepStrictEqual(asked, ["docs"]);
|
|
1243
|
+
assert.strictEqual(host.connections.length, 1);
|
|
1244
|
+
const config = entries[0]!.config as {
|
|
1245
|
+
readonly url?: string;
|
|
1246
|
+
readonly headers?: Record<string, string>;
|
|
1247
|
+
};
|
|
1248
|
+
assert.strictEqual(config.url, "https://example.test/mcp");
|
|
1249
|
+
assert.deepStrictEqual(config.headers, { Authorization: "Bearer secret" });
|
|
1250
|
+
} finally {
|
|
1251
|
+
await host.close();
|
|
1252
|
+
}
|
|
1253
|
+
});
|
|
1254
|
+
|
|
1255
|
+
it("a missing variable leaves the literal and writes a diagnostic", async () => {
|
|
1256
|
+
const fake = fakeClient();
|
|
1257
|
+
const { factory } = fakeFactory(async () => fake.client);
|
|
1258
|
+
const host = await hostFor(
|
|
1259
|
+
[
|
|
1260
|
+
httpEntryOf("docs", {
|
|
1261
|
+
headers: { Authorization: "Bearer ${DEFINITELY_UNSET_TOKEN}" },
|
|
1262
|
+
}),
|
|
1263
|
+
],
|
|
1264
|
+
{ factory, env: {} },
|
|
1265
|
+
);
|
|
1266
|
+
try {
|
|
1267
|
+
const diagnostic = host.diagnostics[0]!;
|
|
1268
|
+
assert.strictEqual(diagnostic.code, "mcp.env_missing");
|
|
1269
|
+
assert.ok(diagnostic.detail.includes("DEFINITELY_UNSET_TOKEN"), diagnostic.detail);
|
|
1270
|
+
assert.ok(diagnostic.detail.includes("docs"), diagnostic.detail);
|
|
1271
|
+
assert.strictEqual(host.connections.length, 1);
|
|
1272
|
+
} finally {
|
|
1273
|
+
await host.close();
|
|
1274
|
+
}
|
|
1275
|
+
});
|
|
1276
|
+
|
|
1277
|
+
it("a command substitution is refused", async () => {
|
|
1278
|
+
const fake = fakeClient();
|
|
1279
|
+
const { factory, asked } = fakeFactory(async () => fake.client);
|
|
1280
|
+
const host = await hostFor([entryOf("demo", { env: { TOKEN: "!op read op://vault/token" } })], {
|
|
1281
|
+
factory,
|
|
1282
|
+
env: {},
|
|
1283
|
+
});
|
|
1284
|
+
try {
|
|
1285
|
+
assert.deepStrictEqual(asked, []);
|
|
1286
|
+
assert.deepStrictEqual(host.connections, []);
|
|
1287
|
+
assert.strictEqual(host.diagnostics.length, 1);
|
|
1288
|
+
assert.strictEqual(host.diagnostics[0]?.code, "mcp.env_unsupported");
|
|
1289
|
+
assert.ok(host.diagnostics[0]?.detail.includes("demo"));
|
|
1290
|
+
assert.ok(!(host.diagnostics[0]?.detail.includes("op://vault/token") ?? false));
|
|
1291
|
+
} finally {
|
|
1292
|
+
await host.close();
|
|
1293
|
+
}
|
|
1294
|
+
});
|
|
1295
|
+
|
|
1296
|
+
it("a large result is bounded and saved", async () => {
|
|
1297
|
+
const full = "x".repeat(MCP_OUTPUT_MAX_BYTES + 1);
|
|
1298
|
+
const fake = fakeClient({
|
|
1299
|
+
tools: [{ name: "long", inputSchema: {} }],
|
|
1300
|
+
callTool: async () => ({ content: [{ type: "text", text: full }] }),
|
|
1301
|
+
});
|
|
1302
|
+
const sessionDir = await mkdtemp(join(tmpdir(), "delegates-mcp-output-"));
|
|
1303
|
+
const host = await hostFor([entryOf("demo")], {
|
|
1304
|
+
factory: fakeFactory(async () => fake.client).factory,
|
|
1305
|
+
sessionDir,
|
|
1306
|
+
});
|
|
1307
|
+
try {
|
|
1308
|
+
const result = await toolIn(host, "mcp__demo__long").execute(
|
|
1309
|
+
{},
|
|
1310
|
+
{} as never,
|
|
1311
|
+
BACKGROUND_CONTEXT,
|
|
1312
|
+
);
|
|
1313
|
+
const text = outputText(result);
|
|
1314
|
+
assert.ok(text.includes("truncated output"), text.slice(0, 120));
|
|
1315
|
+
assert.ok(text.includes("chars truncated"), text.slice(0, 120));
|
|
1316
|
+
const diagnostic = result.diagnostics?.find((entry) => entry.code === "mcp.output_saved");
|
|
1317
|
+
assert.ok(diagnostic !== undefined);
|
|
1318
|
+
const match = / saved at (.+)\.$/.exec(diagnostic.message);
|
|
1319
|
+
assert.ok(match !== null, diagnostic.message);
|
|
1320
|
+
const path = match[1]!;
|
|
1321
|
+
assert.ok(path.startsWith(join(sessionDir, "mcp")), path);
|
|
1322
|
+
const saved = readFileSync(path);
|
|
1323
|
+
assert.strictEqual(saved.length, full.length);
|
|
1324
|
+
} finally {
|
|
1325
|
+
await host.close();
|
|
1326
|
+
}
|
|
1327
|
+
});
|
|
1328
|
+
|
|
1329
|
+
it("a connection that drops mid-session notifies once", async () => {
|
|
1330
|
+
const fake = fakeClient({ tools: [{ name: "echo", inputSchema: {} }] });
|
|
1331
|
+
const notified: string[] = [];
|
|
1332
|
+
const { sink, entries } = recordingSink();
|
|
1333
|
+
const host = await hostFor([entryOf("demo")], {
|
|
1334
|
+
factory: fakeFactory(async () => fake.client).factory,
|
|
1335
|
+
log: sink,
|
|
1336
|
+
onLost: (server) => notified.push(server),
|
|
1337
|
+
});
|
|
1338
|
+
try {
|
|
1339
|
+
for (const listener of fake.closeListeners) listener();
|
|
1340
|
+
for (const listener of fake.closeListeners) listener();
|
|
1341
|
+
assert.deepStrictEqual(notified, ["demo"]);
|
|
1342
|
+
assert.deepStrictEqual(
|
|
1343
|
+
entries.filter((entry) => entry.code === "mcp.connection_lost").length,
|
|
1344
|
+
1,
|
|
1345
|
+
);
|
|
1346
|
+
const result = await toolIn(host, "mcp__demo__echo", { log: sink }).execute(
|
|
1347
|
+
{},
|
|
1348
|
+
{} as never,
|
|
1349
|
+
BACKGROUND_CONTEXT,
|
|
1350
|
+
);
|
|
1351
|
+
assert.strictEqual(result.isError, true);
|
|
1352
|
+
assert.ok(outputText(result).includes("demo"), outputText(result));
|
|
1353
|
+
} finally {
|
|
1354
|
+
await host.close();
|
|
1355
|
+
}
|
|
1356
|
+
});
|
|
1357
|
+
|
|
1358
|
+
it("presence drops a server whose connection closed", async () => {
|
|
1359
|
+
const fake = fakeClient();
|
|
1360
|
+
const host = await hostFor([entryOf("demo")], {
|
|
1361
|
+
factory: fakeFactory(async () => fake.client).factory,
|
|
1362
|
+
});
|
|
1363
|
+
try {
|
|
1364
|
+
assert.deepStrictEqual(host.presence.unconnected, []);
|
|
1365
|
+
for (const listener of fake.closeListeners) listener();
|
|
1366
|
+
assert.deepStrictEqual(host.presence.unconnected, ["demo"]);
|
|
1367
|
+
} finally {
|
|
1368
|
+
await host.close();
|
|
1369
|
+
}
|
|
1370
|
+
});
|
|
1371
|
+
|
|
1372
|
+
it("shutdown closes connections after the harness", async () => {
|
|
1373
|
+
const identity = await sessionIdentity("s-1");
|
|
1374
|
+
const events: string[] = [];
|
|
1375
|
+
const mcp: McpHost = {
|
|
1376
|
+
connections: [],
|
|
1377
|
+
diagnostics: [],
|
|
1378
|
+
failures: [],
|
|
1379
|
+
presence: { unconnected: [] },
|
|
1380
|
+
close: async () => {
|
|
1381
|
+
events.push("mcp");
|
|
1382
|
+
},
|
|
1383
|
+
};
|
|
1384
|
+
const runtime = await openSessionRuntime({
|
|
1385
|
+
identity,
|
|
1386
|
+
models: models(),
|
|
1387
|
+
mcp,
|
|
1388
|
+
sandbox: refusingSandbox(),
|
|
1389
|
+
});
|
|
1390
|
+
void runtime.harness.closed.then(() => events.push("harness"));
|
|
1391
|
+
await runtime.close();
|
|
1392
|
+
assert.deepStrictEqual(events, ["harness", "mcp"]);
|
|
1393
|
+
});
|
|
1394
|
+
|
|
1395
|
+
it("a drop notifies once through the runtime", async () => {
|
|
1396
|
+
const identity = await sessionIdentity("s-1");
|
|
1397
|
+
await writeMcpJson(identity.agentDir, { demo: { command: "node" } });
|
|
1398
|
+
const fake = fakeClient({ tools: [{ name: "echo", inputSchema: {} }] });
|
|
1399
|
+
const notices: string[] = [];
|
|
1400
|
+
const { sink, entries } = recordingSink();
|
|
1401
|
+
const runtime = await openSessionRuntime({
|
|
1402
|
+
identity,
|
|
1403
|
+
models: models(),
|
|
1404
|
+
log: sink,
|
|
1405
|
+
notify: (message) => notices.push(message),
|
|
1406
|
+
catalogue: createCatalogue([definitionOf("echoer", ["mcp__demo__*"])]),
|
|
1407
|
+
mcpFactory: fakeFactory(async () => fake.client).factory,
|
|
1408
|
+
sandbox: refusingSandbox(),
|
|
1409
|
+
});
|
|
1410
|
+
try {
|
|
1411
|
+
for (const listener of fake.closeListeners) listener();
|
|
1412
|
+
for (const listener of fake.closeListeners) listener();
|
|
1413
|
+
assert.strictEqual(notices.length, 1, notices.join(" | "));
|
|
1414
|
+
assert.ok(notices[0]!.includes("demo"), notices[0]);
|
|
1415
|
+
assert.deepStrictEqual(
|
|
1416
|
+
entries.filter((entry) => entry.code === "mcp.connection_lost").length,
|
|
1417
|
+
1,
|
|
1418
|
+
);
|
|
1419
|
+
} finally {
|
|
1420
|
+
await runtime.close();
|
|
1421
|
+
}
|
|
1422
|
+
});
|
|
1423
|
+
|
|
1424
|
+
it("a viewer opens no connection", async () => {
|
|
1425
|
+
const identity = await sessionIdentity("s-1");
|
|
1426
|
+
const { runtime } = await openWithMcp({
|
|
1427
|
+
definitions: [definitionOf("echoer", ["mcp__demo__*"])],
|
|
1428
|
+
servers: { demo: { command: "node" } },
|
|
1429
|
+
factory: fakeFactory(
|
|
1430
|
+
async () => fakeClient({ tools: [{ name: "echo", inputSchema: {} }] }).client,
|
|
1431
|
+
).factory,
|
|
1432
|
+
});
|
|
1433
|
+
await runtime.close();
|
|
1434
|
+
const viewer = await openReadOnlyRuntime({
|
|
1435
|
+
storePath: storePathFor(identity),
|
|
1436
|
+
models: models(),
|
|
1437
|
+
});
|
|
1438
|
+
try {
|
|
1439
|
+
const installed = viewer.registry
|
|
1440
|
+
.snapshot()
|
|
1441
|
+
.installed()
|
|
1442
|
+
.map((extension) => extension.name);
|
|
1443
|
+
assert.ok(!installed.some((name) => name.startsWith("mcp:")));
|
|
1444
|
+
assert.strictEqual(viewer.mcpPresence, undefined);
|
|
1445
|
+
// The read-only path has no factory to spy on: it builds no MCP layer, which
|
|
1446
|
+
// the resolver the viewer uses shows as no servers and no missing-name signal.
|
|
1447
|
+
const resolver = resolverFor({ registry: viewer.registry.snapshot(), skills: viewer.skills });
|
|
1448
|
+
assert.deepStrictEqual(resolver.mcpServers, []);
|
|
1449
|
+
assert.strictEqual(resolver.unconnected, undefined);
|
|
1450
|
+
} finally {
|
|
1451
|
+
await viewer.close();
|
|
1452
|
+
}
|
|
1453
|
+
});
|