cencori 1.12.0 → 1.13.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/dist/agents/index.d.mts +119 -1
- package/dist/agents/index.d.ts +119 -1
- package/dist/agents/index.js +365 -44
- package/dist/agents/index.js.map +1 -1
- package/dist/agents/index.mjs +353 -44
- package/dist/agents/index.mjs.map +1 -1
- package/dist/react/index.d.mts +9 -3
- package/dist/react/index.d.ts +9 -3
- package/dist/react/index.js +191 -76
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +192 -77
- package/dist/react/index.mjs.map +1 -1
- package/package.json +1 -6
package/dist/agents/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __esm = (fn, res) => function __init() {
|
|
7
9
|
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
@@ -18,6 +20,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
20
|
}
|
|
19
21
|
return to;
|
|
20
22
|
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
+
mod
|
|
30
|
+
));
|
|
21
31
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
22
32
|
|
|
23
33
|
// src/agents-sdk/schema.ts
|
|
@@ -198,6 +208,304 @@ var init_tool = __esm({
|
|
|
198
208
|
}
|
|
199
209
|
});
|
|
200
210
|
|
|
211
|
+
// src/agents-sdk/errors.ts
|
|
212
|
+
var AgentsError, MaxTurnsExceededError, GuardrailTripwireTriggered, CencoriRunError;
|
|
213
|
+
var init_errors = __esm({
|
|
214
|
+
"src/agents-sdk/errors.ts"() {
|
|
215
|
+
"use strict";
|
|
216
|
+
AgentsError = class extends Error {
|
|
217
|
+
constructor(message) {
|
|
218
|
+
super(message);
|
|
219
|
+
this.name = "AgentsError";
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
MaxTurnsExceededError = class extends AgentsError {
|
|
223
|
+
constructor(maxTurns, sessionId) {
|
|
224
|
+
super(`The run exceeded maxTurns (${maxTurns}) without a final answer. Raise maxTurns or check for a tool loop.`);
|
|
225
|
+
this.name = "MaxTurnsExceededError";
|
|
226
|
+
this.sessionId = sessionId;
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
GuardrailTripwireTriggered = class extends AgentsError {
|
|
230
|
+
constructor(kind, guardrail, outputInfo) {
|
|
231
|
+
super(`${kind === "input" ? "Input" : "Output"} guardrail "${guardrail}" tripped.`);
|
|
232
|
+
this.name = kind === "input" ? "InputGuardrailTripwireTriggered" : "OutputGuardrailTripwireTriggered";
|
|
233
|
+
this.guardrail = guardrail;
|
|
234
|
+
this.kind = kind;
|
|
235
|
+
this.outputInfo = outputInfo;
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
CencoriRunError = class extends AgentsError {
|
|
239
|
+
constructor(message, details = {}) {
|
|
240
|
+
super(message);
|
|
241
|
+
this.name = "CencoriRunError";
|
|
242
|
+
this.status = details.status;
|
|
243
|
+
this.code = details.code;
|
|
244
|
+
this.sessionId = details.sessionId;
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// src/agents-sdk/mcp.ts
|
|
251
|
+
function fromEventStream(text, id) {
|
|
252
|
+
let fallback = null;
|
|
253
|
+
for (const block of text.split(/\r?\n\r?\n/)) {
|
|
254
|
+
const data = block.split(/\r?\n/).filter((l) => l.startsWith("data:")).map((l) => l.slice(5).trim()).join("\n");
|
|
255
|
+
if (!data) continue;
|
|
256
|
+
try {
|
|
257
|
+
const message = JSON.parse(data);
|
|
258
|
+
if (message.id === id) return message;
|
|
259
|
+
if ("result" in message || "error" in message) fallback ?? (fallback = message);
|
|
260
|
+
} catch {
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if (fallback) return fallback;
|
|
264
|
+
throw new RpcError("MCP response stream carried no result");
|
|
265
|
+
}
|
|
266
|
+
function legacyOnly(error) {
|
|
267
|
+
if (!(error instanceof RpcError)) return false;
|
|
268
|
+
if (error.httpStatus && [400, 404, 405, 501].includes(error.httpStatus)) return true;
|
|
269
|
+
return error.code === -32601 || error.code === -32600 || error.code === -32022;
|
|
270
|
+
}
|
|
271
|
+
function safeHost(url) {
|
|
272
|
+
try {
|
|
273
|
+
return new URL(url).hostname.split(".").slice(-2, -1)[0] || "mcp";
|
|
274
|
+
} catch {
|
|
275
|
+
return "mcp";
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
function mcpResultText(result) {
|
|
279
|
+
if (result?.structuredContent !== void 0 && !(result.content ?? []).some((c) => c.type === "text")) {
|
|
280
|
+
return JSON.stringify(result.structuredContent);
|
|
281
|
+
}
|
|
282
|
+
const parts = (result?.content ?? []).map((c) => {
|
|
283
|
+
if (c.type === "text" && typeof c.text === "string") return c.text;
|
|
284
|
+
if (c.type === "resource" && c.resource && typeof c.resource.text === "string") return c.resource.text;
|
|
285
|
+
return `[${c.type} content]`;
|
|
286
|
+
});
|
|
287
|
+
const text = parts.join("\n") || (result?.structuredContent !== void 0 ? JSON.stringify(result.structuredContent) : "");
|
|
288
|
+
return result?.isError ? `Error: ${text || "the tool failed."}` : text;
|
|
289
|
+
}
|
|
290
|
+
function mcpNeedsApproval(server, tool2) {
|
|
291
|
+
const rule = server.options.requireApproval;
|
|
292
|
+
if (rule === false) return false;
|
|
293
|
+
if (rule === true) return true;
|
|
294
|
+
if (Array.isArray(rule)) return rule.includes(tool2.name);
|
|
295
|
+
if (typeof rule === "function") return rule(tool2);
|
|
296
|
+
return tool2.annotations?.destructiveHint === true;
|
|
297
|
+
}
|
|
298
|
+
var MODERN_PROTOCOL, LEGACY_PROTOCOL, CLIENT_INFO, RpcError, nextId, MCPServerStreamableHttp, MCPServerStdio;
|
|
299
|
+
var init_mcp = __esm({
|
|
300
|
+
"src/agents-sdk/mcp.ts"() {
|
|
301
|
+
"use strict";
|
|
302
|
+
init_errors();
|
|
303
|
+
MODERN_PROTOCOL = "2026-07-28";
|
|
304
|
+
LEGACY_PROTOCOL = "2025-11-25";
|
|
305
|
+
CLIENT_INFO = { name: "cencori-agents", version: "1" };
|
|
306
|
+
RpcError = class extends Error {
|
|
307
|
+
constructor(message, code, httpStatus) {
|
|
308
|
+
super(message);
|
|
309
|
+
this.code = code;
|
|
310
|
+
this.httpStatus = httpStatus;
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
nextId = 1;
|
|
314
|
+
MCPServerStreamableHttp = class {
|
|
315
|
+
constructor(options) {
|
|
316
|
+
this.mode = null;
|
|
317
|
+
this.sessionHeaders = null;
|
|
318
|
+
this.tools = null;
|
|
319
|
+
if (!options?.url) throw new TypeError("MCPServerStreamableHttp needs a url.");
|
|
320
|
+
this.options = options;
|
|
321
|
+
this.name = options.name ?? safeHost(options.url);
|
|
322
|
+
}
|
|
323
|
+
async connect() {
|
|
324
|
+
await this.listTools();
|
|
325
|
+
}
|
|
326
|
+
async listTools() {
|
|
327
|
+
if (this.tools && this.options.cacheToolsList !== false) return this.tools;
|
|
328
|
+
const result = await this.request("tools/list", {});
|
|
329
|
+
this.tools = Array.isArray(result?.tools) ? result.tools : [];
|
|
330
|
+
return this.tools;
|
|
331
|
+
}
|
|
332
|
+
async callTool(name, args) {
|
|
333
|
+
return await this.request("tools/call", { name, arguments: args }, name);
|
|
334
|
+
}
|
|
335
|
+
async close() {
|
|
336
|
+
const session = this.sessionHeaders?.["mcp-session-id"];
|
|
337
|
+
this.mode = null;
|
|
338
|
+
this.sessionHeaders = null;
|
|
339
|
+
if (session) {
|
|
340
|
+
await (this.options.fetch ?? fetch)(this.options.url, { method: "DELETE", headers: { ...this.options.headers, "mcp-session-id": session } }).catch(() => {
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
async request(method, params, toolName) {
|
|
345
|
+
if (this.mode !== "legacy") {
|
|
346
|
+
try {
|
|
347
|
+
const result = await this.post(
|
|
348
|
+
{ ...this.options.headers, "MCP-Protocol-Version": MODERN_PROTOCOL, "Mcp-Method": method, ...toolName ? { "Mcp-Name": toolName } : {} },
|
|
349
|
+
method,
|
|
350
|
+
{ ...params, _meta: { "io.modelcontextprotocol/clientInfo": CLIENT_INFO } }
|
|
351
|
+
);
|
|
352
|
+
this.mode = "modern";
|
|
353
|
+
return result.result;
|
|
354
|
+
} catch (error) {
|
|
355
|
+
if (this.mode === "modern" || !legacyOnly(error)) throw error;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if (!this.sessionHeaders) await this.handshake();
|
|
359
|
+
try {
|
|
360
|
+
return (await this.post(this.sessionHeaders, method, params)).result;
|
|
361
|
+
} catch (error) {
|
|
362
|
+
if (error instanceof RpcError && error.httpStatus === 404) {
|
|
363
|
+
await this.handshake();
|
|
364
|
+
return (await this.post(this.sessionHeaders, method, params)).result;
|
|
365
|
+
}
|
|
366
|
+
throw error;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
async handshake() {
|
|
370
|
+
const base = { ...this.options.headers };
|
|
371
|
+
const { result, headers } = await this.post(base, "initialize", { protocolVersion: LEGACY_PROTOCOL, capabilities: {}, clientInfo: CLIENT_INFO });
|
|
372
|
+
const info = result ?? {};
|
|
373
|
+
const session = headers.get("mcp-session-id") ?? info.sessionId;
|
|
374
|
+
this.sessionHeaders = { ...base, "MCP-Protocol-Version": info.protocolVersion ?? LEGACY_PROTOCOL, ...session ? { "mcp-session-id": session } : {} };
|
|
375
|
+
this.mode = "legacy";
|
|
376
|
+
await this.post(this.sessionHeaders, "notifications/initialized", {}, true).catch(() => {
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
async post(headers, method, params, notification = false) {
|
|
380
|
+
const id = nextId++;
|
|
381
|
+
const res = await (this.options.fetch ?? fetch)(this.options.url, {
|
|
382
|
+
method: "POST",
|
|
383
|
+
headers: { "Content-Type": "application/json", Accept: "application/json, text/event-stream", ...headers },
|
|
384
|
+
body: JSON.stringify(notification ? { jsonrpc: "2.0", method, params } : { jsonrpc: "2.0", id, method, params }),
|
|
385
|
+
signal: AbortSignal.timeout(this.options.timeoutMs ?? 3e4)
|
|
386
|
+
});
|
|
387
|
+
if (!res.ok) {
|
|
388
|
+
const text2 = await res.text().catch(() => "");
|
|
389
|
+
throw new RpcError(`MCP ${method} failed (${res.status}): ${text2.slice(0, 300)}`, void 0, res.status);
|
|
390
|
+
}
|
|
391
|
+
if (notification || res.status === 202) return { result: void 0, headers: res.headers };
|
|
392
|
+
const text = await res.text();
|
|
393
|
+
const message = (res.headers.get("content-type") ?? "").includes("text/event-stream") ? fromEventStream(text, id) : JSON.parse(text);
|
|
394
|
+
if (message.error) throw new RpcError(`MCP ${method}: ${message.error.message ?? "error"}`, message.error.code);
|
|
395
|
+
return { result: message.result, headers: res.headers };
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
MCPServerStdio = class {
|
|
399
|
+
constructor(options) {
|
|
400
|
+
this.child = null;
|
|
401
|
+
this.starting = null;
|
|
402
|
+
this.pending = /* @__PURE__ */ new Map();
|
|
403
|
+
this.tools = null;
|
|
404
|
+
this.stderr = "";
|
|
405
|
+
if (!options?.command) throw new TypeError("MCPServerStdio needs a command.");
|
|
406
|
+
this.options = options;
|
|
407
|
+
this.name = options.name ?? (options.args?.find((a) => !a.startsWith("-")) ?? options.command).split("/").pop().replace(/^@[^/]*\//, "");
|
|
408
|
+
}
|
|
409
|
+
connect() {
|
|
410
|
+
this.starting ?? (this.starting = this.start());
|
|
411
|
+
return this.starting;
|
|
412
|
+
}
|
|
413
|
+
async listTools() {
|
|
414
|
+
if (this.tools && this.options.cacheToolsList !== false) return this.tools;
|
|
415
|
+
await this.connect();
|
|
416
|
+
const result = await this.request("tools/list", {});
|
|
417
|
+
this.tools = Array.isArray(result?.tools) ? result.tools : [];
|
|
418
|
+
return this.tools;
|
|
419
|
+
}
|
|
420
|
+
async callTool(name, args) {
|
|
421
|
+
await this.connect();
|
|
422
|
+
return await this.request("tools/call", { name, arguments: args });
|
|
423
|
+
}
|
|
424
|
+
async close() {
|
|
425
|
+
for (const p of this.pending.values()) {
|
|
426
|
+
clearTimeout(p.timer);
|
|
427
|
+
p.reject(new AgentsError(`MCP server ${this.name} closed.`));
|
|
428
|
+
}
|
|
429
|
+
this.pending.clear();
|
|
430
|
+
try {
|
|
431
|
+
this.child?.stdin.end();
|
|
432
|
+
this.child?.kill();
|
|
433
|
+
} catch {
|
|
434
|
+
}
|
|
435
|
+
this.child = null;
|
|
436
|
+
this.starting = null;
|
|
437
|
+
}
|
|
438
|
+
async start() {
|
|
439
|
+
let spawn;
|
|
440
|
+
try {
|
|
441
|
+
({ spawn } = await import("child_process"));
|
|
442
|
+
} catch {
|
|
443
|
+
throw new AgentsError("MCPServerStdio needs Node.js. In other runtimes use MCPServerStreamableHttp.");
|
|
444
|
+
}
|
|
445
|
+
const child = spawn(this.options.command, this.options.args ?? [], {
|
|
446
|
+
cwd: this.options.cwd,
|
|
447
|
+
env: { ...process.env, ...this.options.env },
|
|
448
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
449
|
+
});
|
|
450
|
+
this.child = child;
|
|
451
|
+
let buffer = "";
|
|
452
|
+
child.stdout.setEncoding("utf8");
|
|
453
|
+
child.stdout.on("data", (chunk) => {
|
|
454
|
+
buffer += chunk;
|
|
455
|
+
let nl;
|
|
456
|
+
while ((nl = buffer.indexOf("\n")) !== -1) {
|
|
457
|
+
const line = buffer.slice(0, nl).trim();
|
|
458
|
+
buffer = buffer.slice(nl + 1);
|
|
459
|
+
if (!line) continue;
|
|
460
|
+
try {
|
|
461
|
+
const message = JSON.parse(line);
|
|
462
|
+
const waiter = typeof message.id === "number" ? this.pending.get(message.id) : void 0;
|
|
463
|
+
if (!waiter) continue;
|
|
464
|
+
clearTimeout(waiter.timer);
|
|
465
|
+
this.pending.delete(message.id);
|
|
466
|
+
if (message.error) waiter.reject(new AgentsError(`MCP ${this.name}: ${message.error.message ?? "error"}`));
|
|
467
|
+
else waiter.resolve(message.result);
|
|
468
|
+
} catch {
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
child.stderr.setEncoding("utf8");
|
|
473
|
+
child.stderr.on("data", (chunk) => {
|
|
474
|
+
this.stderr = (this.stderr + chunk).slice(-2e3);
|
|
475
|
+
});
|
|
476
|
+
const failAll = (why) => {
|
|
477
|
+
for (const p of this.pending.values()) {
|
|
478
|
+
clearTimeout(p.timer);
|
|
479
|
+
p.reject(new AgentsError(why));
|
|
480
|
+
}
|
|
481
|
+
this.pending.clear();
|
|
482
|
+
this.child = null;
|
|
483
|
+
this.starting = null;
|
|
484
|
+
};
|
|
485
|
+
child.on("error", (e2) => failAll(`MCP server ${this.name} didn't start: ${e2.message}`));
|
|
486
|
+
child.on("exit", (code) => failAll(`MCP server ${this.name} exited (${code ?? "signal"})${this.stderr ? `: ${this.stderr.trim().slice(-300)}` : ""}`));
|
|
487
|
+
await this.request("initialize", { protocolVersion: LEGACY_PROTOCOL, capabilities: {}, clientInfo: CLIENT_INFO });
|
|
488
|
+
this.child?.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", method: "notifications/initialized", params: {} })}
|
|
489
|
+
`);
|
|
490
|
+
}
|
|
491
|
+
request(method, params) {
|
|
492
|
+
const child = this.child;
|
|
493
|
+
if (!child) return Promise.reject(new AgentsError(`MCP server ${this.name} isn't running.`));
|
|
494
|
+
const id = nextId++;
|
|
495
|
+
return new Promise((resolve, reject) => {
|
|
496
|
+
const timer = setTimeout(() => {
|
|
497
|
+
this.pending.delete(id);
|
|
498
|
+
reject(new AgentsError(`MCP ${this.name} ${method} timed out.`));
|
|
499
|
+
}, this.options.timeoutMs ?? 3e4);
|
|
500
|
+
this.pending.set(id, { resolve, reject, timer });
|
|
501
|
+
child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id, method, params })}
|
|
502
|
+
`);
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
|
|
201
509
|
// src/widgets/spec.ts
|
|
202
510
|
function safeUrl(v, images) {
|
|
203
511
|
if (images && /^data:image\/(png|jpe?g|gif|webp|svg\+xml);base64,/i.test(v)) return v.length <= 2e5;
|
|
@@ -676,45 +984,6 @@ var init_partial = __esm({
|
|
|
676
984
|
}
|
|
677
985
|
});
|
|
678
986
|
|
|
679
|
-
// src/agents-sdk/errors.ts
|
|
680
|
-
var AgentsError, MaxTurnsExceededError, GuardrailTripwireTriggered, CencoriRunError;
|
|
681
|
-
var init_errors = __esm({
|
|
682
|
-
"src/agents-sdk/errors.ts"() {
|
|
683
|
-
"use strict";
|
|
684
|
-
AgentsError = class extends Error {
|
|
685
|
-
constructor(message) {
|
|
686
|
-
super(message);
|
|
687
|
-
this.name = "AgentsError";
|
|
688
|
-
}
|
|
689
|
-
};
|
|
690
|
-
MaxTurnsExceededError = class extends AgentsError {
|
|
691
|
-
constructor(maxTurns, sessionId) {
|
|
692
|
-
super(`The run exceeded maxTurns (${maxTurns}) without a final answer. Raise maxTurns or check for a tool loop.`);
|
|
693
|
-
this.name = "MaxTurnsExceededError";
|
|
694
|
-
this.sessionId = sessionId;
|
|
695
|
-
}
|
|
696
|
-
};
|
|
697
|
-
GuardrailTripwireTriggered = class extends AgentsError {
|
|
698
|
-
constructor(kind, guardrail, outputInfo) {
|
|
699
|
-
super(`${kind === "input" ? "Input" : "Output"} guardrail "${guardrail}" tripped.`);
|
|
700
|
-
this.name = kind === "input" ? "InputGuardrailTripwireTriggered" : "OutputGuardrailTripwireTriggered";
|
|
701
|
-
this.guardrail = guardrail;
|
|
702
|
-
this.kind = kind;
|
|
703
|
-
this.outputInfo = outputInfo;
|
|
704
|
-
}
|
|
705
|
-
};
|
|
706
|
-
CencoriRunError = class extends AgentsError {
|
|
707
|
-
constructor(message, details = {}) {
|
|
708
|
-
super(message);
|
|
709
|
-
this.name = "CencoriRunError";
|
|
710
|
-
this.status = details.status;
|
|
711
|
-
this.code = details.code;
|
|
712
|
-
this.sessionId = details.sessionId;
|
|
713
|
-
}
|
|
714
|
-
};
|
|
715
|
-
}
|
|
716
|
-
});
|
|
717
|
-
|
|
718
987
|
// src/agents-sdk/transport.ts
|
|
719
988
|
function env(name) {
|
|
720
989
|
try {
|
|
@@ -903,8 +1172,8 @@ function collectAgents(start) {
|
|
|
903
1172
|
visit(start);
|
|
904
1173
|
return seen;
|
|
905
1174
|
}
|
|
906
|
-
function wireTools(agent) {
|
|
907
|
-
const tools =
|
|
1175
|
+
function wireTools(agent, resolved = agent.tools) {
|
|
1176
|
+
const tools = resolved.map((t) => ({
|
|
908
1177
|
type: "function",
|
|
909
1178
|
function: { name: t.name, description: t.description, parameters: t.parameters },
|
|
910
1179
|
// Only an unconditional approval is the server's to hold and record; a
|
|
@@ -1024,10 +1293,19 @@ async function* runLoop(startAgent, input, options) {
|
|
|
1024
1293
|
}
|
|
1025
1294
|
return nextForLocal(agent2, sessionId2);
|
|
1026
1295
|
};
|
|
1296
|
+
const resolvedTools = /* @__PURE__ */ new Map();
|
|
1297
|
+
const toolsOf = (agent2) => {
|
|
1298
|
+
let tools = resolvedTools.get(agent2);
|
|
1299
|
+
if (!tools) {
|
|
1300
|
+
tools = agent2.resolveTools();
|
|
1301
|
+
resolvedTools.set(agent2, tools);
|
|
1302
|
+
}
|
|
1303
|
+
return tools;
|
|
1304
|
+
};
|
|
1027
1305
|
const nextForLocal = async (agent2, sessionId2) => ({
|
|
1028
1306
|
instructions: await agent2.resolveInstructions(contextFor(agent2, sessionId2)) ?? "",
|
|
1029
1307
|
model: agent2.model,
|
|
1030
|
-
tools: wireTools(agent2),
|
|
1308
|
+
tools: wireTools(agent2, await toolsOf(agent2)),
|
|
1031
1309
|
metadata: { sdk: "cencori/agents", agent: agent2.name }
|
|
1032
1310
|
});
|
|
1033
1311
|
let agent;
|
|
@@ -1058,7 +1336,7 @@ async function* runLoop(startAgent, input, options) {
|
|
|
1058
1336
|
}
|
|
1059
1337
|
yield { type: "agent_updated", agent };
|
|
1060
1338
|
turnsUsed++;
|
|
1061
|
-
const firstTools = wireTools(agent);
|
|
1339
|
+
const firstTools = wireTools(agent, await toolsOf(agent));
|
|
1062
1340
|
const firstTurn = {
|
|
1063
1341
|
input,
|
|
1064
1342
|
model: agent.model,
|
|
@@ -1118,7 +1396,7 @@ async function* runLoop(startAgent, input, options) {
|
|
|
1118
1396
|
}
|
|
1119
1397
|
if (!pending) throw new AgentsError("Nothing to continue.");
|
|
1120
1398
|
const runContext = contextFor(agent, sessionId);
|
|
1121
|
-
const toolsByName = new Map(agent.
|
|
1399
|
+
const toolsByName = new Map((await toolsOf(agent)).map((t) => [t.name, t]));
|
|
1122
1400
|
const handoffsByName = new Map(agent.handoffs.map((a) => [Agent.handoffToolName(a), a]));
|
|
1123
1401
|
const interruptions = [];
|
|
1124
1402
|
for (const action of pending.actions) {
|
|
@@ -1318,6 +1596,8 @@ var init_agent = __esm({
|
|
|
1318
1596
|
"src/agents-sdk/agent.ts"() {
|
|
1319
1597
|
"use strict";
|
|
1320
1598
|
init_tool();
|
|
1599
|
+
init_errors();
|
|
1600
|
+
init_mcp();
|
|
1321
1601
|
DEFAULT_MODEL = "gpt-oss-120b";
|
|
1322
1602
|
Agent = class _Agent {
|
|
1323
1603
|
constructor(options) {
|
|
@@ -1335,6 +1615,7 @@ var init_agent = __esm({
|
|
|
1335
1615
|
this.outputGuardrails = options.outputGuardrails ?? [];
|
|
1336
1616
|
this.widgets = options.widgets ?? false;
|
|
1337
1617
|
this.cencoriAgentId = options.cencoriAgentId;
|
|
1618
|
+
this.mcpServers = options.mcpServers ?? [];
|
|
1338
1619
|
const names = /* @__PURE__ */ new Set();
|
|
1339
1620
|
for (const t of [...this.tools.map((t2) => t2.name), ...this.handoffs.map((a) => _Agent.handoffToolName(a))]) {
|
|
1340
1621
|
if (names.has(t)) throw new TypeError(`Agent "${this.name}" has two tools named "${t}".`);
|
|
@@ -1356,6 +1637,7 @@ var init_agent = __esm({
|
|
|
1356
1637
|
outputGuardrails: this.outputGuardrails,
|
|
1357
1638
|
widgets: this.widgets,
|
|
1358
1639
|
cencoriAgentId: this.cencoriAgentId,
|
|
1640
|
+
mcpServers: this.mcpServers,
|
|
1359
1641
|
...overrides
|
|
1360
1642
|
});
|
|
1361
1643
|
}
|
|
@@ -1373,6 +1655,40 @@ var init_agent = __esm({
|
|
|
1373
1655
|
if (!agentId || typeof agentId !== "string") throw new TypeError("Agent.fromCencori needs the stored agent's id.");
|
|
1374
1656
|
return new _Agent({ ...options, name: options.name ?? "Agent", cencoriAgentId: agentId });
|
|
1375
1657
|
}
|
|
1658
|
+
/**
|
|
1659
|
+
* Every tool this agent can call: its own, then its MCP servers' (fetched
|
|
1660
|
+
* from each server, once per server unless it says otherwise). An MCP tool
|
|
1661
|
+
* whose name is taken gets its server's name in front.
|
|
1662
|
+
*/
|
|
1663
|
+
async resolveTools() {
|
|
1664
|
+
if (this.mcpServers.length === 0) return this.tools;
|
|
1665
|
+
const taken = /* @__PURE__ */ new Set([...this.tools.map((t) => t.name), ...this.handoffs.map((a) => _Agent.handoffToolName(a))]);
|
|
1666
|
+
const out = [...this.tools];
|
|
1667
|
+
for (const server of this.mcpServers) {
|
|
1668
|
+
let listed;
|
|
1669
|
+
try {
|
|
1670
|
+
listed = await server.listTools();
|
|
1671
|
+
} catch (error) {
|
|
1672
|
+
throw new AgentsError(`Couldn't list the tools of MCP server "${server.name}": ${error instanceof Error ? error.message : String(error)}`);
|
|
1673
|
+
}
|
|
1674
|
+
const allowed = server.options.allowedTools;
|
|
1675
|
+
for (const t of listed) {
|
|
1676
|
+
if (allowed && !allowed.includes(t.name)) continue;
|
|
1677
|
+
let name = toolSafe(t.name).slice(0, 64);
|
|
1678
|
+
if (taken.has(name)) name = `${toolSafe(server.name)}_${toolSafe(t.name)}`.slice(0, 64);
|
|
1679
|
+
if (taken.has(name) || !TOOL_NAME_PATTERN.test(name)) continue;
|
|
1680
|
+
taken.add(name);
|
|
1681
|
+
out.push(tool({
|
|
1682
|
+
name,
|
|
1683
|
+
description: (t.description ?? `${t.name} (from ${server.name})`).slice(0, 1024),
|
|
1684
|
+
parameters: t.inputSchema && typeof t.inputSchema === "object" ? t.inputSchema : { type: "object", properties: {} },
|
|
1685
|
+
needsApproval: mcpNeedsApproval(server, t),
|
|
1686
|
+
execute: async (args) => mcpResultText(await server.callTool(t.name, args ?? {}))
|
|
1687
|
+
}));
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
return out;
|
|
1691
|
+
}
|
|
1376
1692
|
/** The tool name a handoff to this agent goes by. */
|
|
1377
1693
|
static handoffToolName(agent) {
|
|
1378
1694
|
const name = `transfer_to_${toolSafe(agent.name)}`.slice(0, 64);
|
|
@@ -1431,6 +1747,8 @@ __export(agents_sdk_exports, {
|
|
|
1431
1747
|
CencoriRunError: () => CencoriRunError,
|
|
1432
1748
|
DEFAULT_MODEL: () => DEFAULT_MODEL,
|
|
1433
1749
|
GuardrailTripwireTriggered: () => GuardrailTripwireTriggered,
|
|
1750
|
+
MCPServerStdio: () => MCPServerStdio,
|
|
1751
|
+
MCPServerStreamableHttp: () => MCPServerStreamableHttp,
|
|
1434
1752
|
MaxTurnsExceededError: () => MaxTurnsExceededError,
|
|
1435
1753
|
RunState: () => RunState,
|
|
1436
1754
|
SchemaValidationError: () => SchemaValidationError,
|
|
@@ -1622,6 +1940,7 @@ function env2(name) {
|
|
|
1622
1940
|
|
|
1623
1941
|
// src/agents-sdk/index.ts
|
|
1624
1942
|
init_spec();
|
|
1943
|
+
init_mcp();
|
|
1625
1944
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1626
1945
|
0 && (module.exports = {
|
|
1627
1946
|
Agent,
|
|
@@ -1629,6 +1948,8 @@ init_spec();
|
|
|
1629
1948
|
CencoriRunError,
|
|
1630
1949
|
DEFAULT_MODEL,
|
|
1631
1950
|
GuardrailTripwireTriggered,
|
|
1951
|
+
MCPServerStdio,
|
|
1952
|
+
MCPServerStreamableHttp,
|
|
1632
1953
|
MaxTurnsExceededError,
|
|
1633
1954
|
RunState,
|
|
1634
1955
|
SchemaValidationError,
|