agents 0.2.21 → 0.2.22

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,23 +1,59 @@
1
- import { c as MaybePromise, d as StreamableHTTPEdgeClientTransport, f as SSEEdgeClientTransport, l as ServeOptions, n as MCPClientOAuthCallbackConfig, o as BaseTransportType, r as MCPClientOAuthResult, s as CORSOptions } from "../client-BEMdXPYJ.js";
1
+ import { c as MaybePromise, l as ServeOptions, n as MCPClientOAuthCallbackConfig, o as BaseTransportType, r as MCPClientOAuthResult, s as CORSOptions } from "../client-GfgZTqrS.js";
2
2
  import "../mcp-Dw5vDrY8.js";
3
3
  import "../do-oauth-client-provider-DGc5pP0l.js";
4
4
  import "../index-DhJCaDWd.js";
5
5
  import "../ai-types-D5YoPrBZ.js";
6
- import { c as ConnectionContext, s as Connection, t as Agent } from "../index-BXG3v10y.js";
6
+ import { c as ConnectionContext, s as Connection, t as Agent } from "../index-BQLBXqnQ.js";
7
+ import { SSEClientTransport, SSEClientTransportOptions } from "@modelcontextprotocol/sdk/client/sse.js";
8
+ import { StreamableHTTPClientTransport, StreamableHTTPClientTransportOptions } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
7
9
  import { ElicitRequest, ElicitRequestSchema, ElicitResult, ElicitResult as ElicitResult$1, JSONRPCMessage, MessageExtraInfo } from "@modelcontextprotocol/sdk/types.js";
8
10
  import { Server } from "@modelcontextprotocol/sdk/server/index.js";
9
11
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
10
12
  import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
11
13
 
14
+ //#region src/mcp/client-transports.d.ts
15
+ /**
16
+ * @deprecated Use SSEClientTransport from @modelcontextprotocol/sdk/client/sse.js instead. This alias will be removed in the next major version.
17
+ */
18
+ declare class SSEEdgeClientTransport extends SSEClientTransport {
19
+ constructor(url: URL, options: SSEClientTransportOptions);
20
+ }
21
+ /**
22
+ * @deprecated Use StreamableHTTPClientTransport from @modelcontextprotocol/sdk/client/streamableHttp.js instead. This alias will be removed in the next major version.
23
+ */
24
+ declare class StreamableHTTPEdgeClientTransport extends StreamableHTTPClientTransport {
25
+ constructor(url: URL, options: StreamableHTTPClientTransportOptions);
26
+ }
27
+ //#endregion
12
28
  //#region src/mcp/worker-transport.d.ts
29
+ declare const SUPPORTED_PROTOCOL_VERSIONS: readonly ["2025-03-26", "2025-06-18"];
30
+ type ProtocolVersion = (typeof SUPPORTED_PROTOCOL_VERSIONS)[number];
31
+ interface MCPStorageApi {
32
+ get(): Promise<TransportState | undefined> | TransportState | undefined;
33
+ set(state: TransportState): Promise<void> | void;
34
+ }
35
+ interface TransportState {
36
+ sessionId?: string;
37
+ initialized: boolean;
38
+ protocolVersion?: ProtocolVersion;
39
+ }
13
40
  interface WorkerTransportOptions {
14
41
  sessionIdGenerator?: () => string;
42
+ /**
43
+ * Enable traditional Request/Response mode, this will disable streaming.
44
+ */
15
45
  enableJsonResponse?: boolean;
16
46
  onsessioninitialized?: (sessionId: string) => void;
17
47
  corsOptions?: CORSOptions;
48
+ /**
49
+ * Optional storage api for persisting transport state.
50
+ * Use this to store session state in Durable Object/Agent storage
51
+ * so it survives hibernation/restart.
52
+ */
53
+ storage?: MCPStorageApi;
18
54
  }
19
55
  declare class WorkerTransport implements Transport {
20
- private started;
56
+ started: boolean;
21
57
  private initialized;
22
58
  private sessionIdGenerator?;
23
59
  private enableJsonResponse;
@@ -28,11 +64,22 @@ declare class WorkerTransport implements Transport {
28
64
  private requestResponseMap;
29
65
  private corsOptions?;
30
66
  private protocolVersion?;
67
+ private storage?;
68
+ private stateRestored;
31
69
  sessionId?: string;
32
70
  onclose?: () => void;
33
71
  onerror?: (error: Error) => void;
34
72
  onmessage?: (message: JSONRPCMessage, extra?: MessageExtraInfo) => void;
35
73
  constructor(options?: WorkerTransportOptions);
74
+ /**
75
+ * Restore transport state from persistent storage.
76
+ * This is automatically called on start.
77
+ */
78
+ private restoreState;
79
+ /**
80
+ * Persist current transport state to storage.
81
+ */
82
+ private saveState;
36
83
  start(): Promise<void>;
37
84
  private validateProtocolVersion;
38
85
  private getHeaders;
@@ -47,6 +94,12 @@ declare class WorkerTransport implements Transport {
47
94
  send(message: JSONRPCMessage): Promise<void>;
48
95
  }
49
96
  //#endregion
97
+ //#region src/mcp/auth-context.d.ts
98
+ interface McpAuthContext {
99
+ props: Record<string, unknown>;
100
+ }
101
+ declare function getMcpAuthContext(): McpAuthContext | undefined;
102
+ //#endregion
50
103
  //#region src/mcp/handler.d.ts
51
104
  interface CreateMcpHandlerOptions extends WorkerTransportOptions {
52
105
  /**
@@ -56,36 +109,22 @@ interface CreateMcpHandlerOptions extends WorkerTransportOptions {
56
109
  */
57
110
  route?: string;
58
111
  /**
59
- * CORS configuration options for handling cross-origin requests.
60
- * These options are passed to the WorkerTransport which handles adding
61
- * CORS headers to all responses.
62
- *
63
- * Default values are:
64
- * - origin: "*"
65
- * - headers: "Content-Type, Accept, Authorization, mcp-session-id, MCP-Protocol-Version"
66
- * - methods: "GET, POST, DELETE, OPTIONS"
67
- * - exposeHeaders: "mcp-session-id"
68
- * - maxAge: 86400
69
- *
70
- * Provided options will overwrite the defaults.
112
+ * An optional auth context to use for handling MCP requests.
113
+ * If not provided, the handler will look for props in the execution context.
71
114
  */
72
- corsOptions?: CORSOptions;
115
+ authContext?: McpAuthContext;
116
+ /**
117
+ * An optional transport to use for handling MCP requests.
118
+ * If not provided, a WorkerTransport will be created with the provided WorkerTransportOptions.
119
+ */
120
+ transport?: WorkerTransport;
73
121
  }
74
- type OAuthExecutionContext = ExecutionContext & {
75
- props?: Record<string, unknown>;
76
- };
77
122
  declare function createMcpHandler(server: McpServer | Server, options?: CreateMcpHandlerOptions): (request: Request, env: unknown, ctx: ExecutionContext) => Promise<Response>;
78
123
  /**
79
124
  * @deprecated This has been renamed to createMcpHandler, and experimental_createMcpHandler will be removed in the next major version
80
125
  */
81
126
  declare function experimental_createMcpHandler(server: McpServer | Server, options?: CreateMcpHandlerOptions): (request: Request, env: unknown, ctx: ExecutionContext) => Promise<Response>;
82
127
  //#endregion
83
- //#region src/mcp/auth-context.d.ts
84
- interface McpAuthContext {
85
- props: Record<string, unknown>;
86
- }
87
- declare function getMcpAuthContext(): McpAuthContext | undefined;
88
- //#endregion
89
128
  //#region src/mcp/index.d.ts
90
129
  declare abstract class McpAgent<Env = unknown, State = unknown, Props extends Record<string, unknown> = Record<string, unknown>> extends Agent<Env, State, Props> {
91
130
  private _transport?;
@@ -150,5 +189,5 @@ declare abstract class McpAgent<Env = unknown, State = unknown, Props extends Re
150
189
  };
151
190
  }
152
191
  //#endregion
153
- export { type CreateMcpHandlerOptions, type ElicitRequest, ElicitRequestSchema, type ElicitResult, type MCPClientOAuthCallbackConfig, type MCPClientOAuthResult, McpAgent, type McpAuthContext, type OAuthExecutionContext, SSEEdgeClientTransport, StreamableHTTPEdgeClientTransport, WorkerTransport, type WorkerTransportOptions, createMcpHandler, experimental_createMcpHandler, getMcpAuthContext };
192
+ export { type CreateMcpHandlerOptions, type ElicitRequest, ElicitRequestSchema, type ElicitResult, type MCPClientOAuthCallbackConfig, type MCPClientOAuthResult, McpAgent, type McpAuthContext, SSEEdgeClientTransport, StreamableHTTPEdgeClientTransport, type TransportState, WorkerTransport, type WorkerTransportOptions, createMcpHandler, experimental_createMcpHandler, getMcpAuthContext };
154
193
  //# sourceMappingURL=index.d.ts.map
package/dist/mcp/index.js CHANGED
@@ -1,9 +1,11 @@
1
1
  import { t as MessageType } from "../ai-types-B3aQaFv3.js";
2
2
  import "../client-BfiZ3HQd.js";
3
- import { a as SSEEdgeClientTransport, i as StreamableHTTPEdgeClientTransport } from "../client-JMskg2fw.js";
3
+ import "../client-DZhjV_XA.js";
4
4
  import "../do-oauth-client-provider-CswoD5Lu.js";
5
- import { c as getCurrentAgent, s as getAgentByName, t as Agent } from "../src-nFNV3Ttx.js";
5
+ import { c as getCurrentAgent, s as getAgentByName, t as Agent } from "../src-CwIW7tU2.js";
6
6
  import { AsyncLocalStorage } from "node:async_hooks";
7
+ import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
8
+ import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
7
9
  import { ElicitRequestSchema, InitializeRequestSchema, JSONRPCMessageSchema, isInitializeRequest, isJSONRPCError, isJSONRPCNotification, isJSONRPCRequest, isJSONRPCResponse } from "@modelcontextprotocol/sdk/types.js";
8
10
 
9
11
  //#region src/mcp/utils.ts
@@ -637,6 +639,35 @@ var StreamableHTTPServerTransport = class {
637
639
  }
638
640
  };
639
641
 
642
+ //#endregion
643
+ //#region src/mcp/client-transports.ts
644
+ let didWarnAboutSSEEdgeClientTransport = false;
645
+ /**
646
+ * @deprecated Use SSEClientTransport from @modelcontextprotocol/sdk/client/sse.js instead. This alias will be removed in the next major version.
647
+ */
648
+ var SSEEdgeClientTransport = class extends SSEClientTransport {
649
+ constructor(url, options) {
650
+ super(url, options);
651
+ if (!didWarnAboutSSEEdgeClientTransport) {
652
+ didWarnAboutSSEEdgeClientTransport = true;
653
+ console.warn("SSEEdgeClientTransport is deprecated. Use SSEClientTransport from @modelcontextprotocol/sdk/client/sse.js instead. SSEEdgeClientTransport will be removed in the next major version.");
654
+ }
655
+ }
656
+ };
657
+ let didWarnAboutStreamableHTTPEdgeClientTransport = false;
658
+ /**
659
+ * @deprecated Use StreamableHTTPClientTransport from @modelcontextprotocol/sdk/client/streamableHttp.js instead. This alias will be removed in the next major version.
660
+ */
661
+ var StreamableHTTPEdgeClientTransport = class extends StreamableHTTPClientTransport {
662
+ constructor(url, options) {
663
+ super(url, options);
664
+ if (!didWarnAboutStreamableHTTPEdgeClientTransport) {
665
+ didWarnAboutStreamableHTTPEdgeClientTransport = true;
666
+ console.warn("StreamableHTTPEdgeClientTransport is deprecated. Use StreamableHTTPClientTransport from @modelcontextprotocol/sdk/client/streamableHttp.js instead. StreamableHTTPEdgeClientTransport will be removed in the next major version.");
667
+ }
668
+ }
669
+ };
670
+
640
671
  //#endregion
641
672
  //#region src/mcp/worker-transport.ts
642
673
  const SUPPORTED_PROTOCOL_VERSIONS = ["2025-03-26", "2025-06-18"];
@@ -651,10 +682,38 @@ var WorkerTransport = class {
651
682
  this.streamMapping = /* @__PURE__ */ new Map();
652
683
  this.requestToStreamMapping = /* @__PURE__ */ new Map();
653
684
  this.requestResponseMap = /* @__PURE__ */ new Map();
685
+ this.stateRestored = false;
654
686
  this.sessionIdGenerator = options?.sessionIdGenerator;
655
687
  this.enableJsonResponse = options?.enableJsonResponse ?? false;
656
688
  this.onsessioninitialized = options?.onsessioninitialized;
657
689
  this.corsOptions = options?.corsOptions;
690
+ this.storage = options?.storage;
691
+ }
692
+ /**
693
+ * Restore transport state from persistent storage.
694
+ * This is automatically called on start.
695
+ */
696
+ async restoreState() {
697
+ if (!this.storage || this.stateRestored) return;
698
+ const state = await Promise.resolve(this.storage.get());
699
+ if (state) {
700
+ this.sessionId = state.sessionId;
701
+ this.initialized = state.initialized;
702
+ this.protocolVersion = state.protocolVersion;
703
+ }
704
+ this.stateRestored = true;
705
+ }
706
+ /**
707
+ * Persist current transport state to storage.
708
+ */
709
+ async saveState() {
710
+ if (!this.storage) return;
711
+ const state = {
712
+ sessionId: this.sessionId,
713
+ initialized: this.initialized,
714
+ protocolVersion: this.protocolVersion
715
+ };
716
+ await Promise.resolve(this.storage.set(state));
658
717
  }
659
718
  async start() {
660
719
  if (this.started) throw new Error("Transport already started");
@@ -729,6 +788,7 @@ var WorkerTransport = class {
729
788
  };
730
789
  }
731
790
  async handleRequest(request, parsedBody) {
791
+ await this.restoreState();
732
792
  switch (request.method) {
733
793
  case "OPTIONS": return this.handleOptionsRequest(request);
734
794
  case "GET": return this.handleGetRequest(request);
@@ -907,6 +967,7 @@ var WorkerTransport = class {
907
967
  }
908
968
  this.sessionId = this.sessionIdGenerator?.();
909
969
  this.initialized = true;
970
+ await this.saveState();
910
971
  if (this.sessionId && this.onsessioninitialized) this.onsessioninitialized(this.sessionId);
911
972
  }
912
973
  if (!isInitializationRequest) {
@@ -1103,13 +1164,22 @@ function createMcpHandler(server, options = {}) {
1103
1164
  return async (request, _env, ctx) => {
1104
1165
  const url = new URL(request.url);
1105
1166
  if (route && url.pathname !== route) return new Response("Not Found", { status: 404 });
1106
- const oauthCtx = ctx;
1107
- const authContext = oauthCtx.props ? { props: oauthCtx.props } : void 0;
1108
- const transport = new WorkerTransport(options);
1109
- await server.connect(transport);
1167
+ const transport = options.transport ?? new WorkerTransport({
1168
+ sessionIdGenerator: options.sessionIdGenerator,
1169
+ enableJsonResponse: options.enableJsonResponse,
1170
+ onsessioninitialized: options.onsessioninitialized,
1171
+ corsOptions: options.corsOptions,
1172
+ storage: options.storage
1173
+ });
1174
+ const buildAuthContext = () => {
1175
+ if (options.authContext) return options.authContext;
1176
+ if (ctx.props && Object.keys(ctx.props).length > 0) return { props: ctx.props };
1177
+ };
1110
1178
  const handleRequest = async () => {
1111
1179
  return await transport.handleRequest(request);
1112
1180
  };
1181
+ const authContext = buildAuthContext();
1182
+ if (!transport.started) await server.connect(transport);
1113
1183
  try {
1114
1184
  if (authContext) return await runWithAuthContext(authContext, handleRequest);
1115
1185
  else return await handleRequest();