agents 0.7.5 → 0.7.6

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.
@@ -1,2 +1,2 @@
1
- import { A as MCPClientManager, F as MCPDiscoverResult, I as MCPOAuthCallbackResult, L as MCPServerOptions, M as MCPClientOAuthCallbackConfig, N as MCPClientOAuthResult, P as MCPConnectionResult, R as RegisterServerOptions, j as MCPClientManagerOptions, z as getNamespacedData } from "../index-p1XLNvwQ.js";
1
+ import { B as RegisterServerOptions, F as MCPClientOAuthResult, I as MCPConnectionResult, L as MCPDiscoverResult, M as MCPClientManager, N as MCPClientManagerOptions, P as MCPClientOAuthCallbackConfig, R as MCPOAuthCallbackResult, V as getNamespacedData, z as MCPServerOptions } from "../index-WBy5hmm3.js";
2
2
  export { MCPClientManager, MCPClientManagerOptions, MCPClientOAuthCallbackConfig, MCPClientOAuthResult, MCPConnectionResult, MCPDiscoverResult, MCPOAuthCallbackResult, MCPServerOptions, RegisterServerOptions, getNamespacedData };
@@ -1,2 +1,2 @@
1
- import { n as getNamespacedData, t as MCPClientManager } from "../client-CgXIwdcc.js";
1
+ import { n as getNamespacedData, t as MCPClientManager } from "../client-K8Z-u76l.js";
2
2
  export { MCPClientManager, getNamespacedData };
@@ -1,2 +1,2 @@
1
- import { $ as getMcpAuthContext, B as RPCClientTransport, F as MCPDiscoverResult, G as ElicitRequest, H as RPCServerTransport, J as McpAgent, K as ElicitRequestSchema, L as MCPServerOptions, M as MCPClientOAuthCallbackConfig, N as MCPClientOAuthResult, P as MCPConnectionResult, Q as McpAuthContext, U as RPCServerTransportOptions, V as RPCClientTransportOptions, W as RPC_DO_PREFIX, X as createMcpHandler, Y as CreateMcpHandlerOptions, Z as experimental_createMcpHandler, at as McpClientOptions, et as TransportState, it as StreamableHTTPEdgeClientTransport, nt as WorkerTransportOptions, q as ElicitResult, rt as SSEEdgeClientTransport, tt as WorkerTransport } from "../index-p1XLNvwQ.js";
1
+ import { $ as experimental_createMcpHandler, F as MCPClientOAuthResult, G as RPCServerTransportOptions, H as RPCClientTransport, I as MCPConnectionResult, J as ElicitRequestSchema, K as RPC_DO_PREFIX, L as MCPDiscoverResult, P as MCPClientOAuthCallbackConfig, Q as createMcpHandler, U as RPCClientTransportOptions, W as RPCServerTransport, X as McpAgent, Y as ElicitResult, Z as CreateMcpHandlerOptions, at as SSEEdgeClientTransport, et as McpAuthContext, it as WorkerTransportOptions, nt as TransportState, ot as StreamableHTTPEdgeClientTransport, q as ElicitRequest, rt as WorkerTransport, st as McpClientOptions, tt as getMcpAuthContext, z as MCPServerOptions } from "../index-WBy5hmm3.js";
2
2
  export { CreateMcpHandlerOptions, ElicitRequest, ElicitRequestSchema, ElicitResult, MCPClientOAuthCallbackConfig, MCPClientOAuthResult, MCPConnectionResult, MCPDiscoverResult, MCPServerOptions, McpAgent, McpAuthContext, McpClientOptions, RPCClientTransport, RPCClientTransportOptions, RPCServerTransport, RPCServerTransportOptions, RPC_DO_PREFIX, SSEEdgeClientTransport, StreamableHTTPEdgeClientTransport, TransportState, WorkerTransport, WorkerTransportOptions, createMcpHandler, experimental_createMcpHandler, getMcpAuthContext };
package/dist/mcp/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { MessageType } from "../types.js";
2
- import { a as RPCServerTransport, i as RPCClientTransport, o as RPC_DO_PREFIX } from "../client-CgXIwdcc.js";
2
+ import { a as RPCServerTransport, i as RPCClientTransport, o as RPC_DO_PREFIX } from "../client-K8Z-u76l.js";
3
3
  import { Agent, getAgentByName, getCurrentAgent } from "../index.js";
4
4
  import { AsyncLocalStorage } from "node:async_hooks";
5
5
  import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
@@ -1275,6 +1275,10 @@ function experimental_createMcpHandler(server, options = {}) {
1275
1275
  //#endregion
1276
1276
  //#region src/mcp/index.ts
1277
1277
  var McpAgent = class McpAgent extends Agent {
1278
+ constructor(..._args) {
1279
+ super(..._args);
1280
+ this._pendingElicitations = /* @__PURE__ */ new Map();
1281
+ }
1278
1282
  shouldSendProtocolMessages(_connection, ctx) {
1279
1283
  return !ctx.request.headers.get(MCP_HTTP_METHOD_HEADER);
1280
1284
  }
@@ -1334,8 +1338,8 @@ var McpAgent = class McpAgent extends Agent {
1334
1338
  case "sse": return new McpSSETransport();
1335
1339
  case "streamable-http": {
1336
1340
  const transport = new StreamableHTTPServerTransport({});
1337
- transport.messageInterceptor = async (message) => {
1338
- return this._handleElicitationResponse(message);
1341
+ transport.messageInterceptor = (message) => {
1342
+ return Promise.resolve(this._handleElicitationResponse(message));
1339
1343
  };
1340
1344
  return transport;
1341
1345
  }
@@ -1354,7 +1358,7 @@ var McpAgent = class McpAgent extends Agent {
1354
1358
  /** Sets up the MCP transport and server every time the Agent is started.*/
1355
1359
  async onStart(props) {
1356
1360
  if (props) await this.updateProps(props);
1357
- this.props = await this.ctx.storage.get("props");
1361
+ else this.props = await this.ctx.storage.get("props");
1358
1362
  await this.init();
1359
1363
  const server = await this.server;
1360
1364
  this._transport = this.initTransport();
@@ -1402,7 +1406,7 @@ var McpAgent = class McpAgent extends Agent {
1402
1406
  this._transport?.onerror?.(error);
1403
1407
  throw error;
1404
1408
  }
1405
- if (await this._handleElicitationResponse(parsedMessage)) return null;
1409
+ if (this._handleElicitationResponse(parsedMessage)) return null;
1406
1410
  this._transport?.onmessage?.(parsedMessage, extraInfo);
1407
1411
  return null;
1408
1412
  } catch (error) {
@@ -1414,11 +1418,6 @@ var McpAgent = class McpAgent extends Agent {
1414
1418
  /** Elicit user input with a message and schema */
1415
1419
  async elicitInput(params) {
1416
1420
  const requestId = `elicit_${Math.random().toString(36).substring(2, 11)}`;
1417
- await this.ctx.storage.put(`elicitation:${requestId}`, {
1418
- message: params.message,
1419
- requestedSchema: params.requestedSchema,
1420
- timestamp: Date.now()
1421
- });
1422
1421
  const elicitRequest = {
1423
1422
  jsonrpc: "2.0",
1424
1423
  id: requestId,
@@ -1428,60 +1427,71 @@ var McpAgent = class McpAgent extends Agent {
1428
1427
  requestedSchema: params.requestedSchema
1429
1428
  }
1430
1429
  };
1431
- if (this._transport) await this._transport.send(elicitRequest);
1432
- else {
1433
- const connections = this.getConnections();
1434
- if (!connections || Array.from(connections).length === 0) {
1435
- await this.ctx.storage.delete(`elicitation:${requestId}`);
1436
- throw new Error("No active connections available for elicitation");
1437
- }
1438
- const connectionList = Array.from(connections);
1439
- for (const connection of connectionList) try {
1440
- connection.send(JSON.stringify(elicitRequest));
1430
+ let timeoutId;
1431
+ const responsePromise = new Promise((resolve, reject) => {
1432
+ timeoutId = setTimeout(() => {
1433
+ this._pendingElicitations.delete(requestId);
1434
+ reject(/* @__PURE__ */ new Error("Elicitation request timed out"));
1435
+ }, 6e4);
1436
+ this._pendingElicitations.set(requestId, {
1437
+ resolve: (result) => {
1438
+ clearTimeout(timeoutId);
1439
+ this._pendingElicitations.delete(requestId);
1440
+ resolve(result);
1441
+ },
1442
+ reject: (err) => {
1443
+ clearTimeout(timeoutId);
1444
+ this._pendingElicitations.delete(requestId);
1445
+ reject(err);
1446
+ }
1447
+ });
1448
+ });
1449
+ const cleanup = () => {
1450
+ clearTimeout(timeoutId);
1451
+ this._pendingElicitations.delete(requestId);
1452
+ };
1453
+ return this.keepAliveWhile(async () => {
1454
+ if (this._transport) try {
1455
+ await this._transport.send(elicitRequest);
1441
1456
  } catch (error) {
1442
- console.error("Failed to send elicitation request:", error);
1457
+ cleanup();
1458
+ throw error;
1443
1459
  }
1444
- }
1445
- return this._waitForElicitationResponse(requestId);
1446
- }
1447
- /** Wait for elicitation response through storage polling */
1448
- async _waitForElicitationResponse(requestId) {
1449
- const startTime = Date.now();
1450
- const timeout = 6e4;
1451
- try {
1452
- while (Date.now() - startTime < timeout) {
1453
- const response = await this.ctx.storage.get(`elicitation:response:${requestId}`);
1454
- if (response) {
1455
- await this.ctx.storage.delete(`elicitation:${requestId}`);
1456
- await this.ctx.storage.delete(`elicitation:response:${requestId}`);
1457
- return response;
1460
+ else {
1461
+ const connections = this.getConnections();
1462
+ if (!connections || Array.from(connections).length === 0) {
1463
+ cleanup();
1464
+ throw new Error("No active connections available for elicitation");
1465
+ }
1466
+ const connectionList = Array.from(connections);
1467
+ for (const connection of connectionList) try {
1468
+ connection.send(JSON.stringify(elicitRequest));
1469
+ } catch (error) {
1470
+ console.error("Failed to send elicitation request:", error);
1458
1471
  }
1459
- await new Promise((resolve) => setTimeout(resolve, 100));
1460
1472
  }
1461
- throw new Error("Elicitation request timed out");
1462
- } finally {
1463
- await this.ctx.storage.delete(`elicitation:${requestId}`);
1464
- await this.ctx.storage.delete(`elicitation:response:${requestId}`);
1465
- }
1473
+ return responsePromise;
1474
+ });
1466
1475
  }
1467
- /** Handle elicitation responses */
1468
- async _handleElicitationResponse(message) {
1476
+ /** Handle elicitation responses via in-memory resolver */
1477
+ _handleElicitationResponse(message) {
1469
1478
  if (isJSONRPCResultResponse(message) && message.result) {
1470
1479
  const requestId = message.id?.toString();
1471
1480
  if (!requestId || !requestId.startsWith("elicit_")) return false;
1472
- if (!await this.ctx.storage.get(`elicitation:${requestId}`)) return false;
1473
- await this.ctx.storage.put(`elicitation:response:${requestId}`, message.result);
1481
+ const pending = this._pendingElicitations.get(requestId);
1482
+ if (!pending) return false;
1483
+ pending.resolve(message.result);
1474
1484
  return true;
1475
1485
  }
1476
1486
  if (isJSONRPCErrorResponse(message)) {
1477
1487
  const requestId = message.id?.toString();
1478
1488
  if (!requestId || !requestId.startsWith("elicit_")) return false;
1479
- if (!await this.ctx.storage.get(`elicitation:${requestId}`)) return false;
1480
- const errorResult = {
1489
+ const pending = this._pendingElicitations.get(requestId);
1490
+ if (!pending) return false;
1491
+ pending.resolve({
1481
1492
  action: "cancel",
1482
1493
  content: { error: message.error.message || "Elicitation request failed" }
1483
- };
1484
- await this.ctx.storage.put(`elicitation:response:${requestId}`, errorResult);
1494
+ });
1485
1495
  return true;
1486
1496
  }
1487
1497
  return false;