agents 0.0.79 → 0.0.81
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/ai-chat-agent.d.ts +27 -1
- package/dist/ai-chat-agent.js +100 -103
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.d.ts +11 -0
- package/dist/ai-react.js +1 -1
- package/dist/chunk-BZXOAZUX.js +106 -0
- package/dist/chunk-BZXOAZUX.js.map +1 -0
- package/dist/{chunk-HMLY7DHA.js → chunk-NOUFNU2O.js} +1 -5
- package/dist/{chunk-JR3NW4A7.js → chunk-NPGUKHFR.js} +245 -93
- package/dist/chunk-NPGUKHFR.js.map +1 -0
- package/dist/chunk-QSGN3REV.js +123 -0
- package/dist/chunk-QSGN3REV.js.map +1 -0
- package/dist/{chunk-7VFQNJFK.js → chunk-Y67CHZBI.js} +25 -23
- package/dist/chunk-Y67CHZBI.js.map +1 -0
- package/dist/client.d.ts +9 -1
- package/dist/client.js +7 -126
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +60 -3
- package/dist/index.js +5 -3
- package/dist/mcp/client.d.ts +13 -15
- package/dist/mcp/client.js +2 -2
- package/dist/mcp/do-oauth-client-provider.d.ts +3 -3
- package/dist/mcp/do-oauth-client-provider.js +4 -103
- package/dist/mcp/do-oauth-client-provider.js.map +1 -1
- package/dist/mcp/index.d.ts +10 -1
- package/dist/mcp/index.js +86 -122
- package/dist/mcp/index.js.map +1 -1
- package/dist/react.d.ts +14 -0
- package/dist/react.js +5 -1
- package/dist/react.js.map +1 -1
- package/dist/schedule.js +1 -1
- package/package.json +1 -1
- package/src/index.ts +330 -43
- package/dist/chunk-7VFQNJFK.js.map +0 -1
- package/dist/chunk-JR3NW4A7.js.map +0 -1
- /package/dist/{chunk-HMLY7DHA.js.map → chunk-NOUFNU2O.js.map} +0 -0
package/dist/mcp/client.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as zod from 'zod';
|
|
2
|
-
import {
|
|
2
|
+
import { Tool, Prompt, Resource, ResourceTemplate, ServerCapabilities, CallToolRequest, CallToolResultSchema, CompatibilityCallToolResultSchema, ReadResourceRequest, GetPromptRequest } from '@modelcontextprotocol/sdk/types.js';
|
|
3
3
|
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
4
4
|
import { SSEClientTransportOptions } from '@modelcontextprotocol/sdk/client/sse.js';
|
|
5
5
|
import { AgentsOAuthProvider } from './do-oauth-client-provider.js';
|
|
@@ -15,7 +15,6 @@ declare class MCPClientConnection {
|
|
|
15
15
|
authProvider?: AgentsOAuthProvider;
|
|
16
16
|
};
|
|
17
17
|
client: ConstructorParameters<typeof Client>[1];
|
|
18
|
-
capabilities: ClientCapabilities;
|
|
19
18
|
};
|
|
20
19
|
client: Client;
|
|
21
20
|
connectionState: "authenticating" | "connecting" | "ready" | "discovering" | "failed";
|
|
@@ -30,7 +29,6 @@ declare class MCPClientConnection {
|
|
|
30
29
|
authProvider?: AgentsOAuthProvider;
|
|
31
30
|
};
|
|
32
31
|
client: ConstructorParameters<typeof Client>[1];
|
|
33
|
-
capabilities: ClientCapabilities;
|
|
34
32
|
});
|
|
35
33
|
/**
|
|
36
34
|
* Initialize a client connection
|
|
@@ -38,7 +36,7 @@ declare class MCPClientConnection {
|
|
|
38
36
|
* @param code Optional OAuth code to initialize the connection with if auth hasn't been initialized
|
|
39
37
|
* @returns
|
|
40
38
|
*/
|
|
41
|
-
init(code?: string
|
|
39
|
+
init(code?: string): Promise<void>;
|
|
42
40
|
/**
|
|
43
41
|
* Notification handler registration
|
|
44
42
|
*/
|
|
@@ -68,10 +66,10 @@ declare class MCPClientConnection {
|
|
|
68
66
|
}[]>;
|
|
69
67
|
fetchResources(): Promise<{
|
|
70
68
|
[x: string]: unknown;
|
|
71
|
-
uri: string;
|
|
72
69
|
name: string;
|
|
73
|
-
|
|
70
|
+
uri: string;
|
|
74
71
|
description?: string | undefined;
|
|
72
|
+
mimeType?: string | undefined;
|
|
75
73
|
}[]>;
|
|
76
74
|
fetchPrompts(): Promise<{
|
|
77
75
|
[x: string]: unknown;
|
|
@@ -88,8 +86,8 @@ declare class MCPClientConnection {
|
|
|
88
86
|
[x: string]: unknown;
|
|
89
87
|
name: string;
|
|
90
88
|
uriTemplate: string;
|
|
91
|
-
mimeType?: string | undefined;
|
|
92
89
|
description?: string | undefined;
|
|
90
|
+
mimeType?: string | undefined;
|
|
93
91
|
}[]>;
|
|
94
92
|
}
|
|
95
93
|
|
|
@@ -97,16 +95,16 @@ declare class MCPClientConnection {
|
|
|
97
95
|
* Utility class that aggregates multiple MCP clients into one
|
|
98
96
|
*/
|
|
99
97
|
declare class MCPClientManager {
|
|
100
|
-
private
|
|
101
|
-
private
|
|
98
|
+
private _name;
|
|
99
|
+
private _version;
|
|
102
100
|
mcpConnections: Record<string, MCPClientConnection>;
|
|
103
|
-
private
|
|
101
|
+
private _callbackUrls;
|
|
104
102
|
/**
|
|
105
|
-
* @param
|
|
106
|
-
* @param
|
|
103
|
+
* @param _name Name of the MCP client
|
|
104
|
+
* @param _version Version of the MCP Client
|
|
107
105
|
* @param auth Auth paramters if being used to create a DurableObjectOAuthClientProvider
|
|
108
106
|
*/
|
|
109
|
-
constructor(
|
|
107
|
+
constructor(_name: string, _version: string);
|
|
110
108
|
/**
|
|
111
109
|
* Connect to and register an MCP server
|
|
112
110
|
*
|
|
@@ -124,10 +122,10 @@ declare class MCPClientManager {
|
|
|
124
122
|
authProvider?: AgentsOAuthProvider;
|
|
125
123
|
};
|
|
126
124
|
client?: ConstructorParameters<typeof Client>[1];
|
|
127
|
-
capabilities?: ClientCapabilities;
|
|
128
125
|
}): Promise<{
|
|
129
126
|
id: string;
|
|
130
|
-
authUrl
|
|
127
|
+
authUrl?: string;
|
|
128
|
+
clientId?: string;
|
|
131
129
|
}>;
|
|
132
130
|
isCallbackRequest(req: Request): boolean;
|
|
133
131
|
handleCallbackRequest(req: Request): Promise<{
|
package/dist/mcp/client.js
CHANGED
|
@@ -10,9 +10,9 @@ declare class DurableObjectOAuthClientProvider implements AgentsOAuthProvider {
|
|
|
10
10
|
storage: DurableObjectStorage;
|
|
11
11
|
clientName: string;
|
|
12
12
|
baseRedirectUrl: string;
|
|
13
|
-
private
|
|
14
|
-
private
|
|
15
|
-
private
|
|
13
|
+
private _authUrl_;
|
|
14
|
+
private _serverId_;
|
|
15
|
+
private _clientId_;
|
|
16
16
|
constructor(storage: DurableObjectStorage, clientName: string, baseRedirectUrl: string);
|
|
17
17
|
get clientMetadata(): OAuthClientMetadata;
|
|
18
18
|
get redirectUrl(): string;
|
|
@@ -1,106 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
constructor(storage, clientName, baseRedirectUrl) {
|
|
6
|
-
this.storage = storage;
|
|
7
|
-
this.clientName = clientName;
|
|
8
|
-
this.baseRedirectUrl = baseRedirectUrl;
|
|
9
|
-
}
|
|
10
|
-
get clientMetadata() {
|
|
11
|
-
return {
|
|
12
|
-
redirect_uris: [this.redirectUrl],
|
|
13
|
-
token_endpoint_auth_method: "none",
|
|
14
|
-
grant_types: ["authorization_code", "refresh_token"],
|
|
15
|
-
response_types: ["code"],
|
|
16
|
-
client_name: this.clientName,
|
|
17
|
-
client_uri: "example.com"
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
get redirectUrl() {
|
|
21
|
-
return `${this.baseRedirectUrl}/${this.serverId}`;
|
|
22
|
-
}
|
|
23
|
-
get clientId() {
|
|
24
|
-
if (!this.clientId_) {
|
|
25
|
-
throw new Error("Trying to access clientId before it was set");
|
|
26
|
-
}
|
|
27
|
-
return this.clientId_;
|
|
28
|
-
}
|
|
29
|
-
set clientId(clientId_) {
|
|
30
|
-
this.clientId_ = clientId_;
|
|
31
|
-
}
|
|
32
|
-
get serverId() {
|
|
33
|
-
if (!this.serverId_) {
|
|
34
|
-
throw new Error("Trying to access serverId before it was set");
|
|
35
|
-
}
|
|
36
|
-
return this.serverId_;
|
|
37
|
-
}
|
|
38
|
-
set serverId(serverId_) {
|
|
39
|
-
this.serverId_ = serverId_;
|
|
40
|
-
}
|
|
41
|
-
keyPrefix(clientId) {
|
|
42
|
-
return `/${this.clientName}/${this.serverId}/${clientId}`;
|
|
43
|
-
}
|
|
44
|
-
clientInfoKey(clientId) {
|
|
45
|
-
return `${this.keyPrefix(clientId)}/client_info/`;
|
|
46
|
-
}
|
|
47
|
-
async clientInformation() {
|
|
48
|
-
if (!this.clientId_) {
|
|
49
|
-
return void 0;
|
|
50
|
-
}
|
|
51
|
-
return await this.storage.get(
|
|
52
|
-
this.clientInfoKey(this.clientId)
|
|
53
|
-
) ?? void 0;
|
|
54
|
-
}
|
|
55
|
-
async saveClientInformation(clientInformation) {
|
|
56
|
-
await this.storage.put(
|
|
57
|
-
this.clientInfoKey(clientInformation.client_id),
|
|
58
|
-
clientInformation
|
|
59
|
-
);
|
|
60
|
-
this.clientId = clientInformation.client_id;
|
|
61
|
-
}
|
|
62
|
-
tokenKey(clientId) {
|
|
63
|
-
return `${this.keyPrefix(clientId)}/token`;
|
|
64
|
-
}
|
|
65
|
-
async tokens() {
|
|
66
|
-
if (!this.clientId_) {
|
|
67
|
-
return void 0;
|
|
68
|
-
}
|
|
69
|
-
return await this.storage.get(this.tokenKey(this.clientId)) ?? void 0;
|
|
70
|
-
}
|
|
71
|
-
async saveTokens(tokens) {
|
|
72
|
-
await this.storage.put(this.tokenKey(this.clientId), tokens);
|
|
73
|
-
}
|
|
74
|
-
get authUrl() {
|
|
75
|
-
return this.authUrl_;
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Because this operates on the server side (but we need browser auth), we send this url back to the user
|
|
79
|
-
* and require user interact to initiate the redirect flow
|
|
80
|
-
*/
|
|
81
|
-
async redirectToAuthorization(authUrl) {
|
|
82
|
-
const client_id = authUrl.searchParams.get("client_id");
|
|
83
|
-
if (client_id) {
|
|
84
|
-
authUrl.searchParams.append("state", client_id);
|
|
85
|
-
}
|
|
86
|
-
this.authUrl_ = authUrl.toString();
|
|
87
|
-
}
|
|
88
|
-
codeVerifierKey(clientId) {
|
|
89
|
-
return `${this.keyPrefix(clientId)}/code_verifier`;
|
|
90
|
-
}
|
|
91
|
-
async saveCodeVerifier(verifier) {
|
|
92
|
-
await this.storage.put(this.codeVerifierKey(this.clientId), verifier);
|
|
93
|
-
}
|
|
94
|
-
async codeVerifier() {
|
|
95
|
-
const codeVerifier = await this.storage.get(
|
|
96
|
-
this.codeVerifierKey(this.clientId)
|
|
97
|
-
);
|
|
98
|
-
if (!codeVerifier) {
|
|
99
|
-
throw new Error("No code verifier found");
|
|
100
|
-
}
|
|
101
|
-
return codeVerifier;
|
|
102
|
-
}
|
|
103
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
DurableObjectOAuthClientProvider
|
|
3
|
+
} from "../chunk-BZXOAZUX.js";
|
|
4
|
+
import "../chunk-NOUFNU2O.js";
|
|
104
5
|
export {
|
|
105
6
|
DurableObjectOAuthClientProvider
|
|
106
7
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/mcp/index.d.ts
CHANGED
|
@@ -22,7 +22,15 @@ interface CORSOptions {
|
|
|
22
22
|
}
|
|
23
23
|
type MaybePromise<T> = T | Promise<T>;
|
|
24
24
|
declare abstract class McpAgent<Env = unknown, State = unknown, Props extends Record<string, unknown> = Record<string, unknown>> extends DurableObject<Env> {
|
|
25
|
-
|
|
25
|
+
private _status;
|
|
26
|
+
private _transport?;
|
|
27
|
+
private _transportType;
|
|
28
|
+
private _requestIdToConnectionId;
|
|
29
|
+
/**
|
|
30
|
+
* Since McpAgent's _aren't_ yet real "Agents", let's only expose a couple of the methods
|
|
31
|
+
* to the outer class: initialState/state/setState/onStateUpdate/sql
|
|
32
|
+
*/
|
|
33
|
+
private _agent;
|
|
26
34
|
get mcp(): MCPClientManager;
|
|
27
35
|
protected constructor(ctx: DurableObjectState, env: Env);
|
|
28
36
|
/**
|
|
@@ -44,6 +52,7 @@ declare abstract class McpAgent<Env = unknown, State = unknown, Props extends Re
|
|
|
44
52
|
_init(props: Props): Promise<void>;
|
|
45
53
|
setInitialized(): Promise<void>;
|
|
46
54
|
isInitialized(): Promise<boolean>;
|
|
55
|
+
private _initialize;
|
|
47
56
|
fetch(request: Request): Promise<Response>;
|
|
48
57
|
getWebSocket(): WebSocket | null;
|
|
49
58
|
getWebSocketForResponseID(id: string): WebSocket | null;
|
package/dist/mcp/index.js
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Agent
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
__privateMethod,
|
|
9
|
-
__privateSet
|
|
10
|
-
} from "../chunk-HMLY7DHA.js";
|
|
3
|
+
} from "../chunk-NPGUKHFR.js";
|
|
4
|
+
import "../chunk-BZXOAZUX.js";
|
|
5
|
+
import "../chunk-QSGN3REV.js";
|
|
6
|
+
import "../chunk-Y67CHZBI.js";
|
|
7
|
+
import "../chunk-NOUFNU2O.js";
|
|
11
8
|
|
|
12
9
|
// src/mcp/index.ts
|
|
13
10
|
import { DurableObject } from "cloudflare:workers";
|
|
@@ -36,24 +33,22 @@ function handleCORS(request, corsOptions) {
|
|
|
36
33
|
}
|
|
37
34
|
return null;
|
|
38
35
|
}
|
|
39
|
-
var _getWebSocket, _started;
|
|
40
36
|
var McpSSETransport = class {
|
|
41
37
|
constructor(getWebSocket) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
__privateSet(this, _getWebSocket, getWebSocket);
|
|
38
|
+
this._started = false;
|
|
39
|
+
this._getWebSocket = getWebSocket;
|
|
45
40
|
}
|
|
46
41
|
async start() {
|
|
47
|
-
if (
|
|
42
|
+
if (this._started) {
|
|
48
43
|
throw new Error("Transport already started");
|
|
49
44
|
}
|
|
50
|
-
|
|
45
|
+
this._started = true;
|
|
51
46
|
}
|
|
52
47
|
async send(message) {
|
|
53
|
-
if (!
|
|
48
|
+
if (!this._started) {
|
|
54
49
|
throw new Error("Transport not started");
|
|
55
50
|
}
|
|
56
|
-
const websocket =
|
|
51
|
+
const websocket = this._getWebSocket();
|
|
57
52
|
if (!websocket) {
|
|
58
53
|
throw new Error("WebSocket not connected");
|
|
59
54
|
}
|
|
@@ -68,52 +63,40 @@ var McpSSETransport = class {
|
|
|
68
63
|
this.onclose?.();
|
|
69
64
|
}
|
|
70
65
|
};
|
|
71
|
-
_getWebSocket = new WeakMap();
|
|
72
|
-
_started = new WeakMap();
|
|
73
|
-
var _getWebSocketForGetRequest, _getWebSocketForMessageID, _notifyResponseIdSent, _started2;
|
|
74
66
|
var McpStreamableHttpTransport = class {
|
|
75
67
|
constructor(getWebSocketForMessageID, notifyResponseIdSent) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
__privateAdd(this, _getWebSocketForMessageID);
|
|
81
|
-
// Notify the server that a response has been sent for a given message id
|
|
82
|
-
// so that it may clean up it's mapping of message ids to connections
|
|
83
|
-
// once they are no longer needed
|
|
84
|
-
__privateAdd(this, _notifyResponseIdSent);
|
|
85
|
-
__privateAdd(this, _started2, false);
|
|
86
|
-
__privateSet(this, _getWebSocketForMessageID, getWebSocketForMessageID);
|
|
87
|
-
__privateSet(this, _notifyResponseIdSent, notifyResponseIdSent);
|
|
88
|
-
__privateSet(this, _getWebSocketForGetRequest, () => null);
|
|
68
|
+
this._started = false;
|
|
69
|
+
this._getWebSocketForMessageID = getWebSocketForMessageID;
|
|
70
|
+
this._notifyResponseIdSent = notifyResponseIdSent;
|
|
71
|
+
this._getWebSocketForGetRequest = () => null;
|
|
89
72
|
}
|
|
90
73
|
async start() {
|
|
91
|
-
if (
|
|
74
|
+
if (this._started) {
|
|
92
75
|
throw new Error("Transport already started");
|
|
93
76
|
}
|
|
94
|
-
|
|
77
|
+
this._started = true;
|
|
95
78
|
}
|
|
96
79
|
async send(message) {
|
|
97
|
-
if (!
|
|
80
|
+
if (!this._started) {
|
|
98
81
|
throw new Error("Transport not started");
|
|
99
82
|
}
|
|
100
83
|
let websocket = null;
|
|
101
84
|
if (isJSONRPCResponse(message) || isJSONRPCError(message)) {
|
|
102
|
-
websocket =
|
|
85
|
+
websocket = this._getWebSocketForMessageID(message.id.toString());
|
|
103
86
|
if (!websocket) {
|
|
104
87
|
throw new Error(
|
|
105
88
|
`Could not find WebSocket for message id: ${message.id}`
|
|
106
89
|
);
|
|
107
90
|
}
|
|
108
91
|
} else if (isJSONRPCRequest(message)) {
|
|
109
|
-
websocket =
|
|
92
|
+
websocket = this._getWebSocketForGetRequest();
|
|
110
93
|
} else if (isJSONRPCNotification(message)) {
|
|
111
94
|
websocket = null;
|
|
112
95
|
}
|
|
113
96
|
try {
|
|
114
97
|
websocket?.send(JSON.stringify(message));
|
|
115
98
|
if (isJSONRPCResponse(message)) {
|
|
116
|
-
|
|
99
|
+
this._notifyResponseIdSent(message.id.toString());
|
|
117
100
|
}
|
|
118
101
|
} catch (error) {
|
|
119
102
|
this.onerror?.(error);
|
|
@@ -124,28 +107,16 @@ var McpStreamableHttpTransport = class {
|
|
|
124
107
|
this.onclose?.();
|
|
125
108
|
}
|
|
126
109
|
};
|
|
127
|
-
|
|
128
|
-
_getWebSocketForMessageID = new WeakMap();
|
|
129
|
-
_notifyResponseIdSent = new WeakMap();
|
|
130
|
-
_started2 = new WeakMap();
|
|
131
|
-
var _status, _transport, _transportType, _requestIdToConnectionId, _agent, _McpAgent_instances, initialize_fn;
|
|
132
|
-
var _McpAgent = class _McpAgent extends DurableObject {
|
|
110
|
+
var McpAgent = class _McpAgent extends DurableObject {
|
|
133
111
|
constructor(ctx, env) {
|
|
134
112
|
var _a;
|
|
135
113
|
super(ctx, env);
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
__privateAdd(this, _transportType, "unset");
|
|
140
|
-
__privateAdd(this, _requestIdToConnectionId, /* @__PURE__ */ new Map());
|
|
141
|
-
/**
|
|
142
|
-
* Since McpAgent's _aren't_ yet real "Agents", let's only expose a couple of the methods
|
|
143
|
-
* to the outer class: initialState/state/setState/onStateUpdate/sql
|
|
144
|
-
*/
|
|
145
|
-
__privateAdd(this, _agent);
|
|
114
|
+
this._status = "zero";
|
|
115
|
+
this._transportType = "unset";
|
|
116
|
+
this._requestIdToConnectionId = /* @__PURE__ */ new Map();
|
|
146
117
|
this.initRun = false;
|
|
147
118
|
const self = this;
|
|
148
|
-
|
|
119
|
+
this._agent = new (_a = class extends Agent {
|
|
149
120
|
onStateUpdate(state, source) {
|
|
150
121
|
return self.onStateUpdate(state, source);
|
|
151
122
|
}
|
|
@@ -154,26 +125,26 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
154
125
|
}
|
|
155
126
|
}, _a.options = {
|
|
156
127
|
hibernate: true
|
|
157
|
-
}, _a)(ctx, env)
|
|
128
|
+
}, _a)(ctx, env);
|
|
158
129
|
}
|
|
159
130
|
get mcp() {
|
|
160
|
-
return
|
|
131
|
+
return this._agent.mcp;
|
|
161
132
|
}
|
|
162
133
|
get state() {
|
|
163
|
-
return
|
|
134
|
+
return this._agent.state;
|
|
164
135
|
}
|
|
165
136
|
sql(strings, ...values) {
|
|
166
|
-
return
|
|
137
|
+
return this._agent.sql(strings, ...values);
|
|
167
138
|
}
|
|
168
139
|
setState(state) {
|
|
169
|
-
return
|
|
140
|
+
return this._agent.setState(state);
|
|
170
141
|
}
|
|
171
142
|
onStateUpdate(state, source) {
|
|
172
143
|
}
|
|
173
144
|
async onStart() {
|
|
174
145
|
var _a;
|
|
175
146
|
const self = this;
|
|
176
|
-
|
|
147
|
+
this._agent = new (_a = class extends Agent {
|
|
177
148
|
constructor() {
|
|
178
149
|
super(...arguments);
|
|
179
150
|
this.initialState = self.initialState;
|
|
@@ -186,22 +157,22 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
186
157
|
}
|
|
187
158
|
}, _a.options = {
|
|
188
159
|
hibernate: true
|
|
189
|
-
}, _a)(this.ctx, this.env)
|
|
160
|
+
}, _a)(this.ctx, this.env);
|
|
190
161
|
this.props = await this.ctx.storage.get("props");
|
|
191
|
-
|
|
162
|
+
this._transportType = await this.ctx.storage.get(
|
|
192
163
|
"transportType"
|
|
193
|
-
)
|
|
164
|
+
);
|
|
194
165
|
await this._init(this.props);
|
|
195
166
|
const server = await this.server;
|
|
196
|
-
if (
|
|
197
|
-
|
|
198
|
-
await server.connect(
|
|
199
|
-
} else if (
|
|
200
|
-
|
|
167
|
+
if (this._transportType === "sse") {
|
|
168
|
+
this._transport = new McpSSETransport(() => this.getWebSocket());
|
|
169
|
+
await server.connect(this._transport);
|
|
170
|
+
} else if (this._transportType === "streamable-http") {
|
|
171
|
+
this._transport = new McpStreamableHttpTransport(
|
|
201
172
|
(id) => this.getWebSocketForResponseID(id),
|
|
202
|
-
(id) =>
|
|
203
|
-
)
|
|
204
|
-
await server.connect(
|
|
173
|
+
(id) => this._requestIdToConnectionId.delete(id)
|
|
174
|
+
);
|
|
175
|
+
await server.connect(this._transport);
|
|
205
176
|
}
|
|
206
177
|
}
|
|
207
178
|
async _init(props) {
|
|
@@ -221,10 +192,17 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
221
192
|
async isInitialized() {
|
|
222
193
|
return await this.ctx.storage.get("initialized") === true;
|
|
223
194
|
}
|
|
195
|
+
async _initialize() {
|
|
196
|
+
await this.ctx.blockConcurrencyWhile(async () => {
|
|
197
|
+
this._status = "starting";
|
|
198
|
+
await this.onStart();
|
|
199
|
+
this._status = "started";
|
|
200
|
+
});
|
|
201
|
+
}
|
|
224
202
|
// Allow the worker to fetch a websocket connection to the agent
|
|
225
203
|
async fetch(request) {
|
|
226
|
-
if (
|
|
227
|
-
await
|
|
204
|
+
if (this._status !== "started") {
|
|
205
|
+
await this._initialize();
|
|
228
206
|
}
|
|
229
207
|
if (request.headers.get("Upgrade") !== "websocket") {
|
|
230
208
|
return new Response("Expected WebSocket Upgrade request", {
|
|
@@ -241,24 +219,24 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
241
219
|
return new Response("Websocket already connected", { status: 400 });
|
|
242
220
|
}
|
|
243
221
|
await this.ctx.storage.put("transportType", "sse");
|
|
244
|
-
|
|
245
|
-
if (!
|
|
246
|
-
|
|
247
|
-
await server.connect(
|
|
222
|
+
this._transportType = "sse";
|
|
223
|
+
if (!this._transport) {
|
|
224
|
+
this._transport = new McpSSETransport(() => this.getWebSocket());
|
|
225
|
+
await server.connect(this._transport);
|
|
248
226
|
}
|
|
249
|
-
return
|
|
227
|
+
return this._agent.fetch(request);
|
|
250
228
|
}
|
|
251
229
|
case "/streamable-http": {
|
|
252
|
-
if (!
|
|
253
|
-
|
|
230
|
+
if (!this._transport) {
|
|
231
|
+
this._transport = new McpStreamableHttpTransport(
|
|
254
232
|
(id) => this.getWebSocketForResponseID(id),
|
|
255
|
-
(id) =>
|
|
256
|
-
)
|
|
257
|
-
await server.connect(
|
|
233
|
+
(id) => this._requestIdToConnectionId.delete(id)
|
|
234
|
+
);
|
|
235
|
+
await server.connect(this._transport);
|
|
258
236
|
}
|
|
259
237
|
await this.ctx.storage.put("transportType", "streamable-http");
|
|
260
|
-
|
|
261
|
-
return
|
|
238
|
+
this._transportType = "streamable-http";
|
|
239
|
+
return this._agent.fetch(request);
|
|
262
240
|
}
|
|
263
241
|
default:
|
|
264
242
|
return new Response(
|
|
@@ -277,19 +255,19 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
277
255
|
return websockets[0];
|
|
278
256
|
}
|
|
279
257
|
getWebSocketForResponseID(id) {
|
|
280
|
-
const connectionId =
|
|
258
|
+
const connectionId = this._requestIdToConnectionId.get(id);
|
|
281
259
|
if (connectionId === void 0) {
|
|
282
260
|
return null;
|
|
283
261
|
}
|
|
284
|
-
return
|
|
262
|
+
return this._agent.getConnection(connectionId) ?? null;
|
|
285
263
|
}
|
|
286
264
|
// All messages received here. This is currently never called
|
|
287
265
|
async onMessage(connection, event) {
|
|
288
|
-
if (
|
|
266
|
+
if (this._transportType !== "streamable-http") {
|
|
289
267
|
const err = new Error(
|
|
290
268
|
"Internal Server Error: Expected streamable-http protocol"
|
|
291
269
|
);
|
|
292
|
-
|
|
270
|
+
this._transport?.onerror?.(err);
|
|
293
271
|
return;
|
|
294
272
|
}
|
|
295
273
|
let message;
|
|
@@ -297,21 +275,21 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
297
275
|
const data = typeof event === "string" ? event : new TextDecoder().decode(event);
|
|
298
276
|
message = JSONRPCMessageSchema.parse(JSON.parse(data));
|
|
299
277
|
} catch (error) {
|
|
300
|
-
|
|
278
|
+
this._transport?.onerror?.(error);
|
|
301
279
|
return;
|
|
302
280
|
}
|
|
303
281
|
if (isJSONRPCRequest(message)) {
|
|
304
|
-
|
|
282
|
+
this._requestIdToConnectionId.set(message.id.toString(), connection.id);
|
|
305
283
|
}
|
|
306
|
-
|
|
284
|
+
this._transport?.onmessage?.(message);
|
|
307
285
|
}
|
|
308
286
|
// All messages received over SSE after the initial connection has been established
|
|
309
287
|
// will be passed here
|
|
310
288
|
async onSSEMcpMessage(sessionId, request) {
|
|
311
|
-
if (
|
|
312
|
-
await
|
|
289
|
+
if (this._status !== "started") {
|
|
290
|
+
await this._initialize();
|
|
313
291
|
}
|
|
314
|
-
if (
|
|
292
|
+
if (this._transportType !== "sse") {
|
|
315
293
|
return new Error("Internal Server Error: Expected SSE protocol");
|
|
316
294
|
}
|
|
317
295
|
try {
|
|
@@ -320,36 +298,36 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
320
298
|
try {
|
|
321
299
|
parsedMessage = JSONRPCMessageSchema.parse(message);
|
|
322
300
|
} catch (error) {
|
|
323
|
-
|
|
301
|
+
this._transport?.onerror?.(error);
|
|
324
302
|
throw error;
|
|
325
303
|
}
|
|
326
|
-
|
|
304
|
+
this._transport?.onmessage?.(parsedMessage);
|
|
327
305
|
return null;
|
|
328
306
|
} catch (error) {
|
|
329
307
|
console.error("Error forwarding message to SSE:", error);
|
|
330
|
-
|
|
308
|
+
this._transport?.onerror?.(error);
|
|
331
309
|
return error;
|
|
332
310
|
}
|
|
333
311
|
}
|
|
334
312
|
// Delegate all websocket events to the underlying agent
|
|
335
313
|
async webSocketMessage(ws, event) {
|
|
336
|
-
if (
|
|
337
|
-
await
|
|
314
|
+
if (this._status !== "started") {
|
|
315
|
+
await this._initialize();
|
|
338
316
|
}
|
|
339
|
-
return await
|
|
317
|
+
return await this._agent.webSocketMessage(ws, event);
|
|
340
318
|
}
|
|
341
319
|
// WebSocket event handlers for hibernation support
|
|
342
320
|
async webSocketError(ws, error) {
|
|
343
|
-
if (
|
|
344
|
-
await
|
|
321
|
+
if (this._status !== "started") {
|
|
322
|
+
await this._initialize();
|
|
345
323
|
}
|
|
346
|
-
return await
|
|
324
|
+
return await this._agent.webSocketError(ws, error);
|
|
347
325
|
}
|
|
348
326
|
async webSocketClose(ws, code, reason, wasClean) {
|
|
349
|
-
if (
|
|
350
|
-
await
|
|
327
|
+
if (this._status !== "started") {
|
|
328
|
+
await this._initialize();
|
|
351
329
|
}
|
|
352
|
-
return await
|
|
330
|
+
return await this._agent.webSocketClose(ws, code, reason, wasClean);
|
|
353
331
|
}
|
|
354
332
|
static mount(path, {
|
|
355
333
|
binding = "MCP_OBJECT",
|
|
@@ -796,20 +774,6 @@ data: ${JSON.stringify(result.data)}
|
|
|
796
774
|
};
|
|
797
775
|
}
|
|
798
776
|
};
|
|
799
|
-
_status = new WeakMap();
|
|
800
|
-
_transport = new WeakMap();
|
|
801
|
-
_transportType = new WeakMap();
|
|
802
|
-
_requestIdToConnectionId = new WeakMap();
|
|
803
|
-
_agent = new WeakMap();
|
|
804
|
-
_McpAgent_instances = new WeakSet();
|
|
805
|
-
initialize_fn = async function() {
|
|
806
|
-
await this.ctx.blockConcurrencyWhile(async () => {
|
|
807
|
-
__privateSet(this, _status, "starting");
|
|
808
|
-
await this.onStart();
|
|
809
|
-
__privateSet(this, _status, "started");
|
|
810
|
-
});
|
|
811
|
-
};
|
|
812
|
-
var McpAgent = _McpAgent;
|
|
813
777
|
export {
|
|
814
778
|
McpAgent
|
|
815
779
|
};
|