pi-harness-runtime 1.1.464 → 1.1.466
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 +85 -0
- package/harness/honcho-init.ts +136 -1
- package/harness/mcp-connector.ts +507 -0
- package/index.ts +186 -5
- package/package.json +1 -1
- package/packages/agent-policy/package.json +1 -1
- package/packages/auth/package.json +1 -1
- package/packages/autonomous-runtime/package.json +1 -1
- package/packages/checkpoint/package.json +1 -1
- package/packages/clipboard/package.json +1 -1
- package/packages/codex-watcher/package.json +1 -1
- package/packages/config-capture/package.json +1 -1
- package/packages/contracts/package.json +1 -1
- package/packages/cookie-sanitizer/package.json +1 -1
- package/packages/evaluation-engine/package.json +1 -1
- package/packages/event-bus/package.json +1 -1
- package/packages/event-store/package.json +1 -1
- package/packages/experience-replay/package.json +1 -1
- package/packages/file-copy-helper/package.json +1 -1
- package/packages/intent-analyzer/package.json +1 -1
- package/packages/jev-judge/package.json +1 -1
- package/packages/knowledge-retrieval/package.json +1 -1
- package/packages/learning-engine/package.json +1 -1
- package/packages/logging/package.json +1 -1
- package/packages/memory-engine/package.json +1 -1
- package/packages/notification/package.json +1 -1
- package/packages/paperclip-integration/package.json +1 -1
- package/packages/performance-optimizer/package.json +1 -1
- package/packages/privilege-broker/package.json +1 -1
- package/packages/prompt-compiler/package.json +1 -1
- package/packages/providers/package.json +1 -1
- package/packages/qdrant-skills/package.json +1 -1
- package/packages/quota-manager/package.json +1 -1
- package/packages/relay/package.json +1 -1
- package/packages/skill-mcp-client/package.json +1 -1
- package/packages/skills/package.json +1 -1
- package/packages/todo-bd-sync/package.json +1 -1
- package/packages/token-estimation/package.json +1 -1
- package/packages/tooling-preference/package.json +1 -1
- package/packages/trajectory/package.json +1 -1
- package/packages/tui/package.json +1 -1
- package/packages/two-phase-review/package.json +1 -1
- package/packages/types/package.json +1 -1
- package/packages/webmcp-tester/package.json +1 -1
- package/packages/wiki/package.json +1 -1
- package/packages/write-review/package.json +1 -1
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The fallback connector for the MCP servers that extensions register.
|
|
3
|
+
*
|
|
4
|
+
* Pi's built-in MCP extension connects the registered servers. While it is
|
|
5
|
+
* active, it does that job and this connector stays out of the way: it only
|
|
6
|
+
* connects a server that nobody else connected. The evidence of another
|
|
7
|
+
* connector is the tools of the server in the session - the same evidence
|
|
8
|
+
* the startup report of Honcho uses.
|
|
9
|
+
*
|
|
10
|
+
* The connector never claims a readiness it did not reach: a server it could
|
|
11
|
+
* not connect is reported with its reason, and its tools stay absent.
|
|
12
|
+
*
|
|
13
|
+
* The connector handles the change of the registered servers because Pi
|
|
14
|
+
* expects the extension that handles it to be the one that connects them.
|
|
15
|
+
* Handling the event is therefore a promise, and this module keeps it: a
|
|
16
|
+
* registered server is connected here unless another connector got there
|
|
17
|
+
* first.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { createHash } from "node:crypto";
|
|
21
|
+
|
|
22
|
+
/** A server registered by an extension. */
|
|
23
|
+
export interface RegisteredMcpServerLike {
|
|
24
|
+
name: string;
|
|
25
|
+
config: McpServerConfigLike;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The configuration of a registered server. */
|
|
29
|
+
export interface McpServerConfigLike {
|
|
30
|
+
/** The url of a streamable HTTP server. */
|
|
31
|
+
url?: string;
|
|
32
|
+
/** The headers of the request, for example the credential. */
|
|
33
|
+
headers?: Record<string, string>;
|
|
34
|
+
/** The command of a stdio server. */
|
|
35
|
+
command?: string;
|
|
36
|
+
/** The arguments of the command. */
|
|
37
|
+
args?: string[];
|
|
38
|
+
/** The environment of the command. */
|
|
39
|
+
env?: Record<string, string>;
|
|
40
|
+
/** The working directory of the command. */
|
|
41
|
+
cwd?: string;
|
|
42
|
+
/** How the model reaches the tools. Default: `codemode`. */
|
|
43
|
+
exposure?: string;
|
|
44
|
+
/** What the server offers, in a sentence. */
|
|
45
|
+
description?: string;
|
|
46
|
+
/** A server that is not enabled is kept without connecting. */
|
|
47
|
+
enabled?: boolean;
|
|
48
|
+
/** The timeout of a request, in seconds. */
|
|
49
|
+
timeout?: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A transport of Pi's MCP library. */
|
|
53
|
+
export interface McpTransportLike {
|
|
54
|
+
start?(): Promise<void>;
|
|
55
|
+
send?(message: unknown): Promise<void>;
|
|
56
|
+
close?(): Promise<void>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The part of Pi's client the connector uses. */
|
|
60
|
+
export interface McpClientLike {
|
|
61
|
+
connect(transport: unknown): Promise<unknown>;
|
|
62
|
+
listTools(): Promise<McpToolLike[]>;
|
|
63
|
+
callTool(
|
|
64
|
+
name: string,
|
|
65
|
+
args?: Record<string, unknown>,
|
|
66
|
+
options?: { signal?: AbortSignal; timeoutMs?: number },
|
|
67
|
+
): Promise<unknown>;
|
|
68
|
+
close(): Promise<void>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** A tool of an MCP server. */
|
|
72
|
+
export interface McpToolLike {
|
|
73
|
+
name: string;
|
|
74
|
+
description?: string;
|
|
75
|
+
inputSchema?: Record<string, unknown>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The transports and the client of Pi's MCP library. */
|
|
79
|
+
export interface McpClientLibrary {
|
|
80
|
+
McpClient: new (options: {
|
|
81
|
+
name: string;
|
|
82
|
+
version: string;
|
|
83
|
+
requestTimeoutMs?: number;
|
|
84
|
+
}) => McpClientLike;
|
|
85
|
+
StreamableHttpTransport: new (options: {
|
|
86
|
+
url: string;
|
|
87
|
+
headers?: Record<string, string>;
|
|
88
|
+
}) => McpTransportLike;
|
|
89
|
+
StdioTransport: new (options: {
|
|
90
|
+
command: string;
|
|
91
|
+
args?: readonly string[];
|
|
92
|
+
env?: Record<string, string>;
|
|
93
|
+
cwd?: string;
|
|
94
|
+
}) => McpTransportLike;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The result of a tool call, in the shape Pi expects. */
|
|
98
|
+
export interface McpToolResult {
|
|
99
|
+
content: Array<{ type: "text"; text: string }>;
|
|
100
|
+
isError?: boolean;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The tool the connector registers. */
|
|
104
|
+
export interface McpToolDefinition {
|
|
105
|
+
name: string;
|
|
106
|
+
label: string;
|
|
107
|
+
description: string;
|
|
108
|
+
parameters: Record<string, unknown>;
|
|
109
|
+
exposure: string;
|
|
110
|
+
namespace: { name: string; aliases?: string[] };
|
|
111
|
+
execute(
|
|
112
|
+
toolCallId: string,
|
|
113
|
+
params: Record<string, unknown>,
|
|
114
|
+
signal?: AbortSignal,
|
|
115
|
+
): Promise<McpToolResult>;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** The API of Pi that the connector needs. */
|
|
119
|
+
export interface McpConnectorApi {
|
|
120
|
+
/** The servers registered by the extensions. */
|
|
121
|
+
getMcpServers?: () => RegisteredMcpServerLike[];
|
|
122
|
+
/** The tools of the session. */
|
|
123
|
+
getAllTools?: () => Array<{ name?: unknown }>;
|
|
124
|
+
/** Registers a tool of the session. */
|
|
125
|
+
registerTool?: (tool: McpToolDefinition) => void;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** The options of the connector. */
|
|
129
|
+
export interface McpConnectorOptions {
|
|
130
|
+
/**
|
|
131
|
+
* How long the connector waits before it connects a server.
|
|
132
|
+
*
|
|
133
|
+
* Another connector may be at work: the built-in extension of Pi
|
|
134
|
+
* connects the registered servers in the background. The wait gives it
|
|
135
|
+
* the time, and the evidence of its work is the tools of the server.
|
|
136
|
+
*/
|
|
137
|
+
graceMs?: number;
|
|
138
|
+
/** The library of Pi that carries the client and the transports. */
|
|
139
|
+
loadLibrary: () => Promise<McpClientLibrary>;
|
|
140
|
+
/** The log of the connector. */
|
|
141
|
+
log?: (message: string) => void;
|
|
142
|
+
/** The wait, replaced by the tests. */
|
|
143
|
+
delay?: (ms: number) => Promise<void>;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** What the connector knows about a server. */
|
|
147
|
+
export interface McpServerStatus {
|
|
148
|
+
/**
|
|
149
|
+
* - "connected": the connector connected the server and registered its
|
|
150
|
+
* tools.
|
|
151
|
+
* - "skipped": another connector connected the server.
|
|
152
|
+
* - "failed": the connector could not connect the server, and the
|
|
153
|
+
* reason says why.
|
|
154
|
+
*/
|
|
155
|
+
state: "connected" | "skipped" | "failed";
|
|
156
|
+
/** The names of the tools of the server. */
|
|
157
|
+
tools: string[];
|
|
158
|
+
/** The reason of a failure or of a skip. */
|
|
159
|
+
reason?: string;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** The connector of the session. */
|
|
163
|
+
export interface McpConnector {
|
|
164
|
+
/** Connect the servers that nobody else connected. */
|
|
165
|
+
connectPending(): Promise<void>;
|
|
166
|
+
/** What the connector knows about every server it saw. */
|
|
167
|
+
readonly statuses: Map<string, McpServerStatus>;
|
|
168
|
+
/** Close the connections of the connector. */
|
|
169
|
+
close(): Promise<void>;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** The default wait before the connector connects a server. */
|
|
173
|
+
const DEFAULT_GRACE_MS = 1_500;
|
|
174
|
+
|
|
175
|
+
/** The longest name a tool may have, the same limit Pi uses. */
|
|
176
|
+
const MAX_TOOL_NAME_LENGTH = 64;
|
|
177
|
+
|
|
178
|
+
/** The timeout of a request of the connector, in milliseconds. */
|
|
179
|
+
const DEFAULT_REQUEST_TIMEOUT_MS = 60_000;
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* The name of a tool of an MCP server.
|
|
183
|
+
*
|
|
184
|
+
* The name follows the convention of Pi, so the tools of the connector and
|
|
185
|
+
* the tools of the built-in extension are the same tools and not two of
|
|
186
|
+
* them: the session keeps one entry per name. A name that is too long or
|
|
187
|
+
* that another tool already took gets the same hash suffix as in Pi, so the
|
|
188
|
+
* names of the two connectors agree.
|
|
189
|
+
*
|
|
190
|
+
* @param server The name of the server
|
|
191
|
+
* @param tool The name of the tool in the server
|
|
192
|
+
* @param isTaken Whether a name is already in use
|
|
193
|
+
* @returns The name of the tool in the session
|
|
194
|
+
*/
|
|
195
|
+
export function mcpToolName(
|
|
196
|
+
server: string,
|
|
197
|
+
tool: string,
|
|
198
|
+
isTaken: (candidate: string) => boolean = () => false,
|
|
199
|
+
): string {
|
|
200
|
+
const name = `mcp__${server}__${tool}`.replace(/[^A-Za-z0-9_]/g, "_");
|
|
201
|
+
if (name.length <= MAX_TOOL_NAME_LENGTH && !isTaken(name)) return name;
|
|
202
|
+
const hash = createHash("sha256")
|
|
203
|
+
.update(`${server}\0${tool}`)
|
|
204
|
+
.digest("hex")
|
|
205
|
+
.slice(0, 8);
|
|
206
|
+
return `${name.slice(0, MAX_TOOL_NAME_LENGTH - hash.length - 1)}_${hash}`;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* The namespace of the tools of a server.
|
|
211
|
+
*
|
|
212
|
+
* The namespace replaces a dash with an underscore, so a server and a
|
|
213
|
+
* namespace never collide.
|
|
214
|
+
*/
|
|
215
|
+
export function mcpNamespace(server: string): string {
|
|
216
|
+
return `mcp__${server.replace(/-/g, "_")}`;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* The tools of a server that the session has.
|
|
221
|
+
*
|
|
222
|
+
* The tools are the evidence of a connection: a server that nobody connected
|
|
223
|
+
* has none.
|
|
224
|
+
*
|
|
225
|
+
* @param api The API of Pi
|
|
226
|
+
* @param server The name of the server
|
|
227
|
+
* @returns The names of the tools of the server
|
|
228
|
+
*/
|
|
229
|
+
export function serverToolNames(
|
|
230
|
+
api: McpConnectorApi,
|
|
231
|
+
server: string,
|
|
232
|
+
): string[] {
|
|
233
|
+
if (typeof api.getAllTools !== "function") return [];
|
|
234
|
+
let tools: Array<{ name?: unknown }>;
|
|
235
|
+
try {
|
|
236
|
+
tools = api.getAllTools();
|
|
237
|
+
} catch {
|
|
238
|
+
return [];
|
|
239
|
+
}
|
|
240
|
+
if (!Array.isArray(tools)) return [];
|
|
241
|
+
const prefix = `${mcpNamespace(server)}__`;
|
|
242
|
+
const names: string[] = [];
|
|
243
|
+
for (const tool of tools) {
|
|
244
|
+
const name = typeof tool?.name === "string" ? tool.name : "";
|
|
245
|
+
if (name.startsWith(prefix) && !names.includes(name)) names.push(name);
|
|
246
|
+
}
|
|
247
|
+
return names;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* The parameter schema of a tool of a server.
|
|
252
|
+
*
|
|
253
|
+
* A tool of an MCP server describes its input with a JSON schema. The schema
|
|
254
|
+
* of a tool has to be an object, and some servers leave the type out.
|
|
255
|
+
*/
|
|
256
|
+
export function toParameters(
|
|
257
|
+
schema: Record<string, unknown> | undefined,
|
|
258
|
+
): Record<string, unknown> {
|
|
259
|
+
const value = schema ?? {};
|
|
260
|
+
return {
|
|
261
|
+
...value,
|
|
262
|
+
type: typeof value.type === "string" ? value.type : "object",
|
|
263
|
+
...(value.properties === undefined ? { properties: {} } : {}),
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* The result of a tool call of a server, in the shape Pi expects.
|
|
269
|
+
*
|
|
270
|
+
* The blocks that are not text become text, so the model always gets
|
|
271
|
+
* something it can read.
|
|
272
|
+
*/
|
|
273
|
+
export function toToolResult(result: unknown): McpToolResult {
|
|
274
|
+
const value = (result ?? {}) as {
|
|
275
|
+
content?: unknown;
|
|
276
|
+
structuredContent?: unknown;
|
|
277
|
+
isError?: boolean;
|
|
278
|
+
};
|
|
279
|
+
const blocks = Array.isArray(value.content) ? value.content : [];
|
|
280
|
+
const content = blocks.map((block) => {
|
|
281
|
+
const item = (block ?? {}) as { type?: string; text?: unknown };
|
|
282
|
+
if (item.type === "text" && typeof item.text === "string") {
|
|
283
|
+
return { type: "text" as const, text: item.text };
|
|
284
|
+
}
|
|
285
|
+
return { type: "text" as const, text: JSON.stringify(block) };
|
|
286
|
+
});
|
|
287
|
+
if (content.length === 0 && value.structuredContent !== undefined) {
|
|
288
|
+
content.push({ type: "text", text: JSON.stringify(value.structuredContent) });
|
|
289
|
+
}
|
|
290
|
+
return {
|
|
291
|
+
content,
|
|
292
|
+
...(value.isError === true ? { isError: true } : {}),
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* The transport of a registered server.
|
|
298
|
+
*
|
|
299
|
+
* A server is a streamable HTTP server or a stdio server. A server with
|
|
300
|
+
* neither cannot be connected, and the connector says so instead of guessing.
|
|
301
|
+
*/
|
|
302
|
+
export function createTransport(
|
|
303
|
+
library: McpClientLibrary,
|
|
304
|
+
config: McpServerConfigLike,
|
|
305
|
+
): McpTransportLike {
|
|
306
|
+
if (typeof config.url === "string" && config.url !== "") {
|
|
307
|
+
return new library.StreamableHttpTransport({
|
|
308
|
+
url: config.url,
|
|
309
|
+
...(config.headers ? { headers: config.headers } : {}),
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
if (typeof config.command === "string" && config.command !== "") {
|
|
313
|
+
return new library.StdioTransport({
|
|
314
|
+
command: config.command,
|
|
315
|
+
...(config.args ? { args: config.args } : {}),
|
|
316
|
+
...(config.env ? { env: config.env } : {}),
|
|
317
|
+
...(config.cwd ? { cwd: config.cwd } : {}),
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
throw new Error(
|
|
321
|
+
"the server has neither a url nor a command, so it cannot be connected",
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* The tool of a server, in the shape Pi expects.
|
|
327
|
+
*
|
|
328
|
+
* The tool calls the server through the connection of the connector, and the
|
|
329
|
+
* result of the server becomes the result of the tool.
|
|
330
|
+
*/
|
|
331
|
+
export function createToolDefinition(
|
|
332
|
+
server: RegisteredMcpServerLike,
|
|
333
|
+
tool: McpToolLike,
|
|
334
|
+
getClient: () => McpClientLike,
|
|
335
|
+
isTaken: (candidate: string) => boolean = () => false,
|
|
336
|
+
): McpToolDefinition {
|
|
337
|
+
const name = mcpToolName(server.name, tool.name, isTaken);
|
|
338
|
+
return {
|
|
339
|
+
name,
|
|
340
|
+
label: `${server.name}/${tool.name}`,
|
|
341
|
+
description:
|
|
342
|
+
tool.description?.trim() ||
|
|
343
|
+
`MCP tool ${tool.name} from server ${server.name}`,
|
|
344
|
+
parameters: toParameters(tool.inputSchema),
|
|
345
|
+
exposure: server.config?.exposure ?? "codemode",
|
|
346
|
+
namespace: { name: mcpNamespace(server.name), aliases: [server.name] },
|
|
347
|
+
async execute(
|
|
348
|
+
_toolCallId: string,
|
|
349
|
+
params: Record<string, unknown>,
|
|
350
|
+
signal?: AbortSignal,
|
|
351
|
+
): Promise<McpToolResult> {
|
|
352
|
+
const client = getClient();
|
|
353
|
+
const result = await client.callTool(tool.name, params ?? {}, {
|
|
354
|
+
...(signal ? { signal } : {}),
|
|
355
|
+
...(server.config?.timeout
|
|
356
|
+
? { timeoutMs: server.config.timeout * 1000 }
|
|
357
|
+
: {}),
|
|
358
|
+
});
|
|
359
|
+
return toToolResult(result);
|
|
360
|
+
},
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* The secrets of a configuration, removed from a text.
|
|
366
|
+
*
|
|
367
|
+
* The credential of a server travels in the headers of the request, and a
|
|
368
|
+
* server that quotes its own configuration in an error must not put the
|
|
369
|
+
* credential into the log of the harness.
|
|
370
|
+
*
|
|
371
|
+
* @param text The text that may hold a secret
|
|
372
|
+
* @param config The configuration of the server
|
|
373
|
+
* @returns The text without the secrets
|
|
374
|
+
*/
|
|
375
|
+
export function redactConfigSecrets(
|
|
376
|
+
text: string,
|
|
377
|
+
config: McpServerConfigLike | undefined,
|
|
378
|
+
): string {
|
|
379
|
+
let result = text;
|
|
380
|
+
const values = [
|
|
381
|
+
...Object.values(config?.headers ?? {}),
|
|
382
|
+
...Object.values(config?.env ?? {}),
|
|
383
|
+
];
|
|
384
|
+
for (const value of values) {
|
|
385
|
+
if (typeof value !== "string" || value.length < 8) continue;
|
|
386
|
+
result = result.split(value).join("[REDACTED]");
|
|
387
|
+
}
|
|
388
|
+
return result;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* The connector of the session.
|
|
393
|
+
*
|
|
394
|
+
* The connector connects the servers that no other connector connected, and
|
|
395
|
+
* it closes its own connections when the session ends.
|
|
396
|
+
*
|
|
397
|
+
* @param api The API of Pi
|
|
398
|
+
* @param options The options of the connector
|
|
399
|
+
* @returns The connector
|
|
400
|
+
*/
|
|
401
|
+
export function createMcpConnector(
|
|
402
|
+
api: McpConnectorApi,
|
|
403
|
+
options: McpConnectorOptions,
|
|
404
|
+
): McpConnector {
|
|
405
|
+
const graceMs = options.graceMs ?? DEFAULT_GRACE_MS;
|
|
406
|
+
const delay = options.delay ?? ((ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms)));
|
|
407
|
+
const log = options.log ?? ((): void => {});
|
|
408
|
+
const statuses = new Map<string, McpServerStatus>();
|
|
409
|
+
const clients = new Map<string, McpClientLike>();
|
|
410
|
+
|
|
411
|
+
async function connectServer(
|
|
412
|
+
server: RegisteredMcpServerLike,
|
|
413
|
+
): Promise<McpServerStatus> {
|
|
414
|
+
let client: McpClientLike | null = null;
|
|
415
|
+
try {
|
|
416
|
+
if (typeof api.registerTool !== "function") {
|
|
417
|
+
throw new Error(
|
|
418
|
+
"the runtime cannot register tools, so the tools of the server would stay invisible",
|
|
419
|
+
);
|
|
420
|
+
}
|
|
421
|
+
const library = await options.loadLibrary();
|
|
422
|
+
const transport = createTransport(library, server.config ?? {});
|
|
423
|
+
client = new library.McpClient({
|
|
424
|
+
name: "pi-harness-runtime",
|
|
425
|
+
version: "1.0.0",
|
|
426
|
+
requestTimeoutMs: DEFAULT_REQUEST_TIMEOUT_MS,
|
|
427
|
+
});
|
|
428
|
+
await client.connect(transport);
|
|
429
|
+
const tools = await client.listTools();
|
|
430
|
+
const names: string[] = [];
|
|
431
|
+
const taken = new Set<string>();
|
|
432
|
+
for (const tool of tools ?? []) {
|
|
433
|
+
if (!tool || typeof tool.name !== "string") continue;
|
|
434
|
+
const definition = createToolDefinition(
|
|
435
|
+
server,
|
|
436
|
+
tool,
|
|
437
|
+
() => client as McpClientLike,
|
|
438
|
+
(candidate) => taken.has(candidate),
|
|
439
|
+
);
|
|
440
|
+
taken.add(definition.name);
|
|
441
|
+
api.registerTool?.(definition);
|
|
442
|
+
names.push(definition.name);
|
|
443
|
+
}
|
|
444
|
+
clients.set(server.name, client);
|
|
445
|
+
log(`MCP server "${server.name}" connected by the harness (${names.length} tools)`);
|
|
446
|
+
return { state: "connected", tools: names };
|
|
447
|
+
} catch (error) {
|
|
448
|
+
const reason = redactConfigSecrets(
|
|
449
|
+
error instanceof Error ? error.message : String(error),
|
|
450
|
+
server.config,
|
|
451
|
+
);
|
|
452
|
+
try {
|
|
453
|
+
await client?.close();
|
|
454
|
+
} catch {
|
|
455
|
+
// The connection is already broken: the reason is the error.
|
|
456
|
+
}
|
|
457
|
+
log(`MCP server "${server.name}" could not be connected: ${reason}`);
|
|
458
|
+
return { state: "failed", tools: [], reason };
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
return {
|
|
463
|
+
statuses,
|
|
464
|
+
|
|
465
|
+
async connectPending(): Promise<void> {
|
|
466
|
+
const servers = api.getMcpServers?.() ?? [];
|
|
467
|
+
const wanted = servers.filter(
|
|
468
|
+
(server) => server?.name && server.config?.enabled !== false,
|
|
469
|
+
);
|
|
470
|
+
if (wanted.length === 0) return;
|
|
471
|
+
// Another connector may be at work. The wait gives it the time,
|
|
472
|
+
// and its work is visible in the tools of the session.
|
|
473
|
+
await delay(graceMs);
|
|
474
|
+
for (const server of wanted) {
|
|
475
|
+
const known = statuses.get(server.name);
|
|
476
|
+
// A server that was connected is done, and a server that
|
|
477
|
+
// another connector connected belongs to it. A server that
|
|
478
|
+
// failed may be tried again.
|
|
479
|
+
if (known && known.state !== "failed") continue;
|
|
480
|
+
const foreign = serverToolNames(api, server.name);
|
|
481
|
+
if (foreign.length > 0) {
|
|
482
|
+
statuses.set(server.name, {
|
|
483
|
+
state: "skipped",
|
|
484
|
+
tools: foreign,
|
|
485
|
+
reason: "another connector connected the server",
|
|
486
|
+
});
|
|
487
|
+
log(
|
|
488
|
+
`MCP server "${server.name}" was connected by another connector (${foreign.length} tools)`,
|
|
489
|
+
);
|
|
490
|
+
continue;
|
|
491
|
+
}
|
|
492
|
+
statuses.set(server.name, await connectServer(server));
|
|
493
|
+
}
|
|
494
|
+
},
|
|
495
|
+
|
|
496
|
+
async close(): Promise<void> {
|
|
497
|
+
for (const [name, client] of clients) {
|
|
498
|
+
try {
|
|
499
|
+
await client.close();
|
|
500
|
+
} catch {
|
|
501
|
+
// A broken connection needs no closing.
|
|
502
|
+
}
|
|
503
|
+
clients.delete(name);
|
|
504
|
+
}
|
|
505
|
+
},
|
|
506
|
+
};
|
|
507
|
+
}
|