agents 0.5.1 → 0.7.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/mcp/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { MessageType } from "../types.js";
2
- import "../client-connection-CGMuV62J.js";
2
+ import { a as RPC_DO_PREFIX, i as RPCServerTransport, r as RPCClientTransport } from "../client-connection-D3Wcd6Q6.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";
@@ -161,7 +161,6 @@ const createStreamingHttpHandler = (basePath, namespace, options = {}) => {
161
161
  [MCP_MESSAGE_HEADER]: Buffer.from(JSON.stringify(messages)).toString("base64"),
162
162
  Upgrade: "websocket"
163
163
  } });
164
- if (ctx.props) agent.updateProps(ctx.props);
165
164
  const ws = (await agent.fetch(req)).webSocket;
166
165
  if (!ws) {
167
166
  console.error("Failed to establish WebSocket connection");
@@ -263,7 +262,6 @@ const createStreamingHttpHandler = (basePath, namespace, options = {}) => {
263
262
  request.headers.forEach((v, k) => {
264
263
  existingHeaders[k] = v;
265
264
  });
266
- if (ctx.props) agent.updateProps(ctx.props);
267
265
  const ws = (await agent.fetch(new Request(request.url, { headers: {
268
266
  ...existingHeaders,
269
267
  [MCP_HTTP_METHOD_HEADER]: "GET",
@@ -371,9 +369,9 @@ const createLegacySseHandler = (basePath, namespace, options = {}) => {
371
369
  request.headers.forEach((value, key) => {
372
370
  existingHeaders[key] = value;
373
371
  });
374
- if (ctx.props) agent.updateProps(ctx.props);
375
372
  const ws = (await agent.fetch(new Request(request.url, { headers: {
376
373
  ...existingHeaders,
374
+ [MCP_HTTP_METHOD_HEADER]: "SSE",
377
375
  Upgrade: "websocket"
378
376
  } }))).webSocket;
379
377
  if (!ws) {
@@ -458,11 +456,12 @@ const createLegacySseHandler = (basePath, namespace, options = {}) => {
458
456
  };
459
457
  };
460
458
  function corsHeaders(_request, corsOptions = {}) {
461
- const origin = "*";
459
+ const origin = corsOptions.origin || "*";
460
+ const headers = corsOptions.headers || "Content-Type, Accept, Authorization, mcp-session-id, mcp-protocol-version";
462
461
  return {
463
- "Access-Control-Allow-Headers": corsOptions.headers || "Content-Type, Accept, mcp-session-id, mcp-protocol-version",
462
+ "Access-Control-Allow-Headers": headers,
464
463
  "Access-Control-Allow-Methods": corsOptions.methods || "GET, POST, DELETE, OPTIONS",
465
- "Access-Control-Allow-Origin": corsOptions.origin || origin,
464
+ "Access-Control-Allow-Origin": origin,
466
465
  "Access-Control-Expose-Headers": corsOptions.exposeHeaders || "mcp-session-id",
467
466
  "Access-Control-Max-Age": (corsOptions.maxAge || 86400).toString()
468
467
  };
@@ -1283,6 +1282,9 @@ function experimental_createMcpHandler(server, options = {}) {
1283
1282
  //#endregion
1284
1283
  //#region src/mcp/index.ts
1285
1284
  var McpAgent = class McpAgent extends Agent {
1285
+ shouldSendProtocolMessages(_connection, ctx) {
1286
+ return !ctx.request.headers.get(MCP_HTTP_METHOD_HEADER);
1287
+ }
1286
1288
  async setInitializeRequest(initializeRequest) {
1287
1289
  await this.ctx.storage.put("initializeRequest", initializeRequest);
1288
1290
  }
@@ -1290,14 +1292,15 @@ var McpAgent = class McpAgent extends Agent {
1290
1292
  return this.ctx.storage.get("initializeRequest");
1291
1293
  }
1292
1294
  /** Read the transport type for this agent.
1293
- * This relies on the naming scheme being `sse:${sessionId}`
1294
- * or `streamable-http:${sessionId}`.
1295
+ * This relies on the naming scheme being `sse:${sessionId}`,
1296
+ * `streamable-http:${sessionId}`, or `rpc:${sessionId}`.
1295
1297
  */
1296
1298
  getTransportType() {
1297
1299
  const [t, ..._] = this.name.split(":");
1298
1300
  switch (t) {
1299
1301
  case "sse": return "sse";
1300
1302
  case "streamable-http": return "streamable-http";
1303
+ case "rpc": return "rpc";
1301
1304
  default: throw new Error("Invalid transport type. McpAgent must be addressed with a valid protocol.");
1302
1305
  }
1303
1306
  }
@@ -1316,6 +1319,22 @@ var McpAgent = class McpAgent extends Agent {
1316
1319
  if (websockets.length === 0) return null;
1317
1320
  return websockets[0];
1318
1321
  }
1322
+ /**
1323
+ * Returns options for configuring the RPC server transport.
1324
+ * Override this method to customize RPC transport behavior (e.g., timeout).
1325
+ *
1326
+ * @example
1327
+ * ```typescript
1328
+ * class MyMCP extends McpAgent {
1329
+ * protected getRpcTransportOptions() {
1330
+ * return { timeout: 120000 }; // 2 minutes
1331
+ * }
1332
+ * }
1333
+ * ```
1334
+ */
1335
+ getRpcTransportOptions() {
1336
+ return {};
1337
+ }
1319
1338
  /** Returns a new transport matching the type of the Agent. */
1320
1339
  initTransport() {
1321
1340
  switch (this.getTransportType()) {
@@ -1327,6 +1346,7 @@ var McpAgent = class McpAgent extends Agent {
1327
1346
  };
1328
1347
  return transport;
1329
1348
  }
1349
+ case "rpc": return new RPCServerTransport(this.getRpcTransportOptions());
1330
1350
  }
1331
1351
  }
1332
1352
  /** Update and store the props */
@@ -1345,6 +1365,7 @@ var McpAgent = class McpAgent extends Agent {
1345
1365
  await this.init();
1346
1366
  const server = await this.server;
1347
1367
  this._transport = this.initTransport();
1368
+ if (!this._transport) throw new Error("Failed to initialize transport");
1348
1369
  await server.connect(this._transport);
1349
1370
  await this.reinitializeServer();
1350
1371
  }
@@ -1472,6 +1493,25 @@ var McpAgent = class McpAgent extends Agent {
1472
1493
  }
1473
1494
  return false;
1474
1495
  }
1496
+ /**
1497
+ * Handle an RPC message for MCP
1498
+ * This method is called by the RPC stub to process MCP messages
1499
+ * @param message The JSON-RPC message(s) to handle
1500
+ * @returns The response message(s) or undefined
1501
+ */
1502
+ async handleMcpMessage(message) {
1503
+ if (!this._transport) {
1504
+ this.props = await this.ctx.storage.get("props");
1505
+ await this.init();
1506
+ const server = await this.server;
1507
+ this._transport = this.initTransport();
1508
+ if (!this._transport) throw new Error("Failed to initialize transport");
1509
+ await server.connect(this._transport);
1510
+ await this.reinitializeServer();
1511
+ }
1512
+ if (!(this._transport instanceof RPCServerTransport)) throw new Error("Expected RPC transport");
1513
+ return await this._transport.handle(message);
1514
+ }
1475
1515
  /** Return a handler for the given path for this MCP.
1476
1516
  * Defaults to Streamable HTTP transport.
1477
1517
  */
@@ -1511,5 +1551,5 @@ var McpAgent = class McpAgent extends Agent {
1511
1551
  };
1512
1552
 
1513
1553
  //#endregion
1514
- export { ElicitRequestSchema, McpAgent, SSEEdgeClientTransport, StreamableHTTPEdgeClientTransport, WorkerTransport, createMcpHandler, experimental_createMcpHandler, getMcpAuthContext };
1554
+ export { ElicitRequestSchema, McpAgent, RPCClientTransport, RPCServerTransport, RPC_DO_PREFIX, SSEEdgeClientTransport, StreamableHTTPEdgeClientTransport, WorkerTransport, createMcpHandler, experimental_createMcpHandler, getMcpAuthContext };
1515
1555
  //# sourceMappingURL=index.js.map