agents 0.0.0-dd6a9e3 → 0.0.0-de168a2
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 +5 -1
- package/dist/ai-chat-agent.js +20 -19
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.js +16 -9
- package/dist/ai-react.js.map +1 -1
- package/dist/{chunk-X6BBKLSC.js → chunk-XG52S6YY.js} +95 -72
- package/dist/chunk-XG52S6YY.js.map +1 -0
- package/dist/client.js +16 -23
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +8 -8
- package/dist/index.js +5 -5
- package/dist/mcp/client.d.ts +763 -0
- package/dist/mcp/client.js +408 -0
- package/dist/mcp/client.js.map +1 -0
- package/dist/mcp/do-oauth-client-provider.d.ts +41 -0
- package/dist/mcp/do-oauth-client-provider.js +107 -0
- package/dist/mcp/do-oauth-client-provider.js.map +1 -0
- package/dist/mcp/index.d.ts +45 -0
- package/dist/mcp/index.js +349 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/react.js +34 -27
- package/dist/react.js.map +1 -1
- package/package.json +21 -8
- package/src/index.ts +116 -92
- package/dist/chunk-X6BBKLSC.js.map +0 -1
- package/dist/mcp.d.ts +0 -58
- package/dist/mcp.js +0 -945
- package/dist/mcp.js.map +0 -1
package/dist/mcp.js
DELETED
|
@@ -1,945 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Agent
|
|
3
|
-
} from "./chunk-X6BBKLSC.js";
|
|
4
|
-
import {
|
|
5
|
-
__privateAdd,
|
|
6
|
-
__privateGet,
|
|
7
|
-
__privateSet
|
|
8
|
-
} from "./chunk-HMLY7DHA.js";
|
|
9
|
-
|
|
10
|
-
// src/mcp.ts
|
|
11
|
-
import { DurableObject } from "cloudflare:workers";
|
|
12
|
-
|
|
13
|
-
// ../../node_modules/@modelcontextprotocol/sdk/dist/esm/types.js
|
|
14
|
-
import { z } from "zod";
|
|
15
|
-
var JSONRPC_VERSION = "2.0";
|
|
16
|
-
var ProgressTokenSchema = z.union([z.string(), z.number().int()]);
|
|
17
|
-
var CursorSchema = z.string();
|
|
18
|
-
var BaseRequestParamsSchema = z.object({
|
|
19
|
-
_meta: z.optional(z.object({
|
|
20
|
-
/**
|
|
21
|
-
* If specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications.
|
|
22
|
-
*/
|
|
23
|
-
progressToken: z.optional(ProgressTokenSchema)
|
|
24
|
-
}).passthrough())
|
|
25
|
-
}).passthrough();
|
|
26
|
-
var RequestSchema = z.object({
|
|
27
|
-
method: z.string(),
|
|
28
|
-
params: z.optional(BaseRequestParamsSchema)
|
|
29
|
-
});
|
|
30
|
-
var BaseNotificationParamsSchema = z.object({
|
|
31
|
-
/**
|
|
32
|
-
* This parameter name is reserved by MCP to allow clients and servers to attach additional metadata to their notifications.
|
|
33
|
-
*/
|
|
34
|
-
_meta: z.optional(z.object({}).passthrough())
|
|
35
|
-
}).passthrough();
|
|
36
|
-
var NotificationSchema = z.object({
|
|
37
|
-
method: z.string(),
|
|
38
|
-
params: z.optional(BaseNotificationParamsSchema)
|
|
39
|
-
});
|
|
40
|
-
var ResultSchema = z.object({
|
|
41
|
-
/**
|
|
42
|
-
* This result property is reserved by the protocol to allow clients and servers to attach additional metadata to their responses.
|
|
43
|
-
*/
|
|
44
|
-
_meta: z.optional(z.object({}).passthrough())
|
|
45
|
-
}).passthrough();
|
|
46
|
-
var RequestIdSchema = z.union([z.string(), z.number().int()]);
|
|
47
|
-
var JSONRPCRequestSchema = z.object({
|
|
48
|
-
jsonrpc: z.literal(JSONRPC_VERSION),
|
|
49
|
-
id: RequestIdSchema
|
|
50
|
-
}).merge(RequestSchema).strict();
|
|
51
|
-
var JSONRPCNotificationSchema = z.object({
|
|
52
|
-
jsonrpc: z.literal(JSONRPC_VERSION)
|
|
53
|
-
}).merge(NotificationSchema).strict();
|
|
54
|
-
var JSONRPCResponseSchema = z.object({
|
|
55
|
-
jsonrpc: z.literal(JSONRPC_VERSION),
|
|
56
|
-
id: RequestIdSchema,
|
|
57
|
-
result: ResultSchema
|
|
58
|
-
}).strict();
|
|
59
|
-
var ErrorCode;
|
|
60
|
-
(function(ErrorCode2) {
|
|
61
|
-
ErrorCode2[ErrorCode2["ConnectionClosed"] = -32e3] = "ConnectionClosed";
|
|
62
|
-
ErrorCode2[ErrorCode2["RequestTimeout"] = -32001] = "RequestTimeout";
|
|
63
|
-
ErrorCode2[ErrorCode2["ParseError"] = -32700] = "ParseError";
|
|
64
|
-
ErrorCode2[ErrorCode2["InvalidRequest"] = -32600] = "InvalidRequest";
|
|
65
|
-
ErrorCode2[ErrorCode2["MethodNotFound"] = -32601] = "MethodNotFound";
|
|
66
|
-
ErrorCode2[ErrorCode2["InvalidParams"] = -32602] = "InvalidParams";
|
|
67
|
-
ErrorCode2[ErrorCode2["InternalError"] = -32603] = "InternalError";
|
|
68
|
-
})(ErrorCode || (ErrorCode = {}));
|
|
69
|
-
var JSONRPCErrorSchema = z.object({
|
|
70
|
-
jsonrpc: z.literal(JSONRPC_VERSION),
|
|
71
|
-
id: RequestIdSchema,
|
|
72
|
-
error: z.object({
|
|
73
|
-
/**
|
|
74
|
-
* The error type that occurred.
|
|
75
|
-
*/
|
|
76
|
-
code: z.number().int(),
|
|
77
|
-
/**
|
|
78
|
-
* A short description of the error. The message SHOULD be limited to a concise single sentence.
|
|
79
|
-
*/
|
|
80
|
-
message: z.string(),
|
|
81
|
-
/**
|
|
82
|
-
* Additional information about the error. The value of this member is defined by the sender (e.g. detailed error information, nested errors etc.).
|
|
83
|
-
*/
|
|
84
|
-
data: z.optional(z.unknown())
|
|
85
|
-
})
|
|
86
|
-
}).strict();
|
|
87
|
-
var JSONRPCMessageSchema = z.union([
|
|
88
|
-
JSONRPCRequestSchema,
|
|
89
|
-
JSONRPCNotificationSchema,
|
|
90
|
-
JSONRPCResponseSchema,
|
|
91
|
-
JSONRPCErrorSchema
|
|
92
|
-
]);
|
|
93
|
-
var EmptyResultSchema = ResultSchema.strict();
|
|
94
|
-
var CancelledNotificationSchema = NotificationSchema.extend({
|
|
95
|
-
method: z.literal("notifications/cancelled"),
|
|
96
|
-
params: BaseNotificationParamsSchema.extend({
|
|
97
|
-
/**
|
|
98
|
-
* The ID of the request to cancel.
|
|
99
|
-
*
|
|
100
|
-
* This MUST correspond to the ID of a request previously issued in the same direction.
|
|
101
|
-
*/
|
|
102
|
-
requestId: RequestIdSchema,
|
|
103
|
-
/**
|
|
104
|
-
* An optional string describing the reason for the cancellation. This MAY be logged or presented to the user.
|
|
105
|
-
*/
|
|
106
|
-
reason: z.string().optional()
|
|
107
|
-
})
|
|
108
|
-
});
|
|
109
|
-
var ImplementationSchema = z.object({
|
|
110
|
-
name: z.string(),
|
|
111
|
-
version: z.string()
|
|
112
|
-
}).passthrough();
|
|
113
|
-
var ClientCapabilitiesSchema = z.object({
|
|
114
|
-
/**
|
|
115
|
-
* Experimental, non-standard capabilities that the client supports.
|
|
116
|
-
*/
|
|
117
|
-
experimental: z.optional(z.object({}).passthrough()),
|
|
118
|
-
/**
|
|
119
|
-
* Present if the client supports sampling from an LLM.
|
|
120
|
-
*/
|
|
121
|
-
sampling: z.optional(z.object({}).passthrough()),
|
|
122
|
-
/**
|
|
123
|
-
* Present if the client supports listing roots.
|
|
124
|
-
*/
|
|
125
|
-
roots: z.optional(z.object({
|
|
126
|
-
/**
|
|
127
|
-
* Whether the client supports issuing notifications for changes to the roots list.
|
|
128
|
-
*/
|
|
129
|
-
listChanged: z.optional(z.boolean())
|
|
130
|
-
}).passthrough())
|
|
131
|
-
}).passthrough();
|
|
132
|
-
var InitializeRequestSchema = RequestSchema.extend({
|
|
133
|
-
method: z.literal("initialize"),
|
|
134
|
-
params: BaseRequestParamsSchema.extend({
|
|
135
|
-
/**
|
|
136
|
-
* The latest version of the Model Context Protocol that the client supports. The client MAY decide to support older versions as well.
|
|
137
|
-
*/
|
|
138
|
-
protocolVersion: z.string(),
|
|
139
|
-
capabilities: ClientCapabilitiesSchema,
|
|
140
|
-
clientInfo: ImplementationSchema
|
|
141
|
-
})
|
|
142
|
-
});
|
|
143
|
-
var ServerCapabilitiesSchema = z.object({
|
|
144
|
-
/**
|
|
145
|
-
* Experimental, non-standard capabilities that the server supports.
|
|
146
|
-
*/
|
|
147
|
-
experimental: z.optional(z.object({}).passthrough()),
|
|
148
|
-
/**
|
|
149
|
-
* Present if the server supports sending log messages to the client.
|
|
150
|
-
*/
|
|
151
|
-
logging: z.optional(z.object({}).passthrough()),
|
|
152
|
-
/**
|
|
153
|
-
* Present if the server offers any prompt templates.
|
|
154
|
-
*/
|
|
155
|
-
prompts: z.optional(z.object({
|
|
156
|
-
/**
|
|
157
|
-
* Whether this server supports issuing notifications for changes to the prompt list.
|
|
158
|
-
*/
|
|
159
|
-
listChanged: z.optional(z.boolean())
|
|
160
|
-
}).passthrough()),
|
|
161
|
-
/**
|
|
162
|
-
* Present if the server offers any resources to read.
|
|
163
|
-
*/
|
|
164
|
-
resources: z.optional(z.object({
|
|
165
|
-
/**
|
|
166
|
-
* Whether this server supports clients subscribing to resource updates.
|
|
167
|
-
*/
|
|
168
|
-
subscribe: z.optional(z.boolean()),
|
|
169
|
-
/**
|
|
170
|
-
* Whether this server supports issuing notifications for changes to the resource list.
|
|
171
|
-
*/
|
|
172
|
-
listChanged: z.optional(z.boolean())
|
|
173
|
-
}).passthrough()),
|
|
174
|
-
/**
|
|
175
|
-
* Present if the server offers any tools to call.
|
|
176
|
-
*/
|
|
177
|
-
tools: z.optional(z.object({
|
|
178
|
-
/**
|
|
179
|
-
* Whether this server supports issuing notifications for changes to the tool list.
|
|
180
|
-
*/
|
|
181
|
-
listChanged: z.optional(z.boolean())
|
|
182
|
-
}).passthrough())
|
|
183
|
-
}).passthrough();
|
|
184
|
-
var InitializeResultSchema = ResultSchema.extend({
|
|
185
|
-
/**
|
|
186
|
-
* The version of the Model Context Protocol that the server wants to use. This may not match the version that the client requested. If the client cannot support this version, it MUST disconnect.
|
|
187
|
-
*/
|
|
188
|
-
protocolVersion: z.string(),
|
|
189
|
-
capabilities: ServerCapabilitiesSchema,
|
|
190
|
-
serverInfo: ImplementationSchema,
|
|
191
|
-
/**
|
|
192
|
-
* Instructions describing how to use the server and its features.
|
|
193
|
-
*
|
|
194
|
-
* This can be used by clients to improve the LLM's understanding of available tools, resources, etc. It can be thought of like a "hint" to the model. For example, this information MAY be added to the system prompt.
|
|
195
|
-
*/
|
|
196
|
-
instructions: z.optional(z.string())
|
|
197
|
-
});
|
|
198
|
-
var InitializedNotificationSchema = NotificationSchema.extend({
|
|
199
|
-
method: z.literal("notifications/initialized")
|
|
200
|
-
});
|
|
201
|
-
var PingRequestSchema = RequestSchema.extend({
|
|
202
|
-
method: z.literal("ping")
|
|
203
|
-
});
|
|
204
|
-
var ProgressSchema = z.object({
|
|
205
|
-
/**
|
|
206
|
-
* The progress thus far. This should increase every time progress is made, even if the total is unknown.
|
|
207
|
-
*/
|
|
208
|
-
progress: z.number(),
|
|
209
|
-
/**
|
|
210
|
-
* Total number of items to process (or total progress required), if known.
|
|
211
|
-
*/
|
|
212
|
-
total: z.optional(z.number())
|
|
213
|
-
}).passthrough();
|
|
214
|
-
var ProgressNotificationSchema = NotificationSchema.extend({
|
|
215
|
-
method: z.literal("notifications/progress"),
|
|
216
|
-
params: BaseNotificationParamsSchema.merge(ProgressSchema).extend({
|
|
217
|
-
/**
|
|
218
|
-
* The progress token which was given in the initial request, used to associate this notification with the request that is proceeding.
|
|
219
|
-
*/
|
|
220
|
-
progressToken: ProgressTokenSchema
|
|
221
|
-
})
|
|
222
|
-
});
|
|
223
|
-
var PaginatedRequestSchema = RequestSchema.extend({
|
|
224
|
-
params: BaseRequestParamsSchema.extend({
|
|
225
|
-
/**
|
|
226
|
-
* An opaque token representing the current pagination position.
|
|
227
|
-
* If provided, the server should return results starting after this cursor.
|
|
228
|
-
*/
|
|
229
|
-
cursor: z.optional(CursorSchema)
|
|
230
|
-
}).optional()
|
|
231
|
-
});
|
|
232
|
-
var PaginatedResultSchema = ResultSchema.extend({
|
|
233
|
-
/**
|
|
234
|
-
* An opaque token representing the pagination position after the last returned result.
|
|
235
|
-
* If present, there may be more results available.
|
|
236
|
-
*/
|
|
237
|
-
nextCursor: z.optional(CursorSchema)
|
|
238
|
-
});
|
|
239
|
-
var ResourceContentsSchema = z.object({
|
|
240
|
-
/**
|
|
241
|
-
* The URI of this resource.
|
|
242
|
-
*/
|
|
243
|
-
uri: z.string(),
|
|
244
|
-
/**
|
|
245
|
-
* The MIME type of this resource, if known.
|
|
246
|
-
*/
|
|
247
|
-
mimeType: z.optional(z.string())
|
|
248
|
-
}).passthrough();
|
|
249
|
-
var TextResourceContentsSchema = ResourceContentsSchema.extend({
|
|
250
|
-
/**
|
|
251
|
-
* The text of the item. This must only be set if the item can actually be represented as text (not binary data).
|
|
252
|
-
*/
|
|
253
|
-
text: z.string()
|
|
254
|
-
});
|
|
255
|
-
var BlobResourceContentsSchema = ResourceContentsSchema.extend({
|
|
256
|
-
/**
|
|
257
|
-
* A base64-encoded string representing the binary data of the item.
|
|
258
|
-
*/
|
|
259
|
-
blob: z.string().base64()
|
|
260
|
-
});
|
|
261
|
-
var ResourceSchema = z.object({
|
|
262
|
-
/**
|
|
263
|
-
* The URI of this resource.
|
|
264
|
-
*/
|
|
265
|
-
uri: z.string(),
|
|
266
|
-
/**
|
|
267
|
-
* A human-readable name for this resource.
|
|
268
|
-
*
|
|
269
|
-
* This can be used by clients to populate UI elements.
|
|
270
|
-
*/
|
|
271
|
-
name: z.string(),
|
|
272
|
-
/**
|
|
273
|
-
* A description of what this resource represents.
|
|
274
|
-
*
|
|
275
|
-
* This can be used by clients to improve the LLM's understanding of available resources. It can be thought of like a "hint" to the model.
|
|
276
|
-
*/
|
|
277
|
-
description: z.optional(z.string()),
|
|
278
|
-
/**
|
|
279
|
-
* The MIME type of this resource, if known.
|
|
280
|
-
*/
|
|
281
|
-
mimeType: z.optional(z.string())
|
|
282
|
-
}).passthrough();
|
|
283
|
-
var ResourceTemplateSchema = z.object({
|
|
284
|
-
/**
|
|
285
|
-
* A URI template (according to RFC 6570) that can be used to construct resource URIs.
|
|
286
|
-
*/
|
|
287
|
-
uriTemplate: z.string(),
|
|
288
|
-
/**
|
|
289
|
-
* A human-readable name for the type of resource this template refers to.
|
|
290
|
-
*
|
|
291
|
-
* This can be used by clients to populate UI elements.
|
|
292
|
-
*/
|
|
293
|
-
name: z.string(),
|
|
294
|
-
/**
|
|
295
|
-
* A description of what this template is for.
|
|
296
|
-
*
|
|
297
|
-
* This can be used by clients to improve the LLM's understanding of available resources. It can be thought of like a "hint" to the model.
|
|
298
|
-
*/
|
|
299
|
-
description: z.optional(z.string()),
|
|
300
|
-
/**
|
|
301
|
-
* The MIME type for all resources that match this template. This should only be included if all resources matching this template have the same type.
|
|
302
|
-
*/
|
|
303
|
-
mimeType: z.optional(z.string())
|
|
304
|
-
}).passthrough();
|
|
305
|
-
var ListResourcesRequestSchema = PaginatedRequestSchema.extend({
|
|
306
|
-
method: z.literal("resources/list")
|
|
307
|
-
});
|
|
308
|
-
var ListResourcesResultSchema = PaginatedResultSchema.extend({
|
|
309
|
-
resources: z.array(ResourceSchema)
|
|
310
|
-
});
|
|
311
|
-
var ListResourceTemplatesRequestSchema = PaginatedRequestSchema.extend({
|
|
312
|
-
method: z.literal("resources/templates/list")
|
|
313
|
-
});
|
|
314
|
-
var ListResourceTemplatesResultSchema = PaginatedResultSchema.extend({
|
|
315
|
-
resourceTemplates: z.array(ResourceTemplateSchema)
|
|
316
|
-
});
|
|
317
|
-
var ReadResourceRequestSchema = RequestSchema.extend({
|
|
318
|
-
method: z.literal("resources/read"),
|
|
319
|
-
params: BaseRequestParamsSchema.extend({
|
|
320
|
-
/**
|
|
321
|
-
* The URI of the resource to read. The URI can use any protocol; it is up to the server how to interpret it.
|
|
322
|
-
*/
|
|
323
|
-
uri: z.string()
|
|
324
|
-
})
|
|
325
|
-
});
|
|
326
|
-
var ReadResourceResultSchema = ResultSchema.extend({
|
|
327
|
-
contents: z.array(z.union([TextResourceContentsSchema, BlobResourceContentsSchema]))
|
|
328
|
-
});
|
|
329
|
-
var ResourceListChangedNotificationSchema = NotificationSchema.extend({
|
|
330
|
-
method: z.literal("notifications/resources/list_changed")
|
|
331
|
-
});
|
|
332
|
-
var SubscribeRequestSchema = RequestSchema.extend({
|
|
333
|
-
method: z.literal("resources/subscribe"),
|
|
334
|
-
params: BaseRequestParamsSchema.extend({
|
|
335
|
-
/**
|
|
336
|
-
* The URI of the resource to subscribe to. The URI can use any protocol; it is up to the server how to interpret it.
|
|
337
|
-
*/
|
|
338
|
-
uri: z.string()
|
|
339
|
-
})
|
|
340
|
-
});
|
|
341
|
-
var UnsubscribeRequestSchema = RequestSchema.extend({
|
|
342
|
-
method: z.literal("resources/unsubscribe"),
|
|
343
|
-
params: BaseRequestParamsSchema.extend({
|
|
344
|
-
/**
|
|
345
|
-
* The URI of the resource to unsubscribe from.
|
|
346
|
-
*/
|
|
347
|
-
uri: z.string()
|
|
348
|
-
})
|
|
349
|
-
});
|
|
350
|
-
var ResourceUpdatedNotificationSchema = NotificationSchema.extend({
|
|
351
|
-
method: z.literal("notifications/resources/updated"),
|
|
352
|
-
params: BaseNotificationParamsSchema.extend({
|
|
353
|
-
/**
|
|
354
|
-
* The URI of the resource that has been updated. This might be a sub-resource of the one that the client actually subscribed to.
|
|
355
|
-
*/
|
|
356
|
-
uri: z.string()
|
|
357
|
-
})
|
|
358
|
-
});
|
|
359
|
-
var PromptArgumentSchema = z.object({
|
|
360
|
-
/**
|
|
361
|
-
* The name of the argument.
|
|
362
|
-
*/
|
|
363
|
-
name: z.string(),
|
|
364
|
-
/**
|
|
365
|
-
* A human-readable description of the argument.
|
|
366
|
-
*/
|
|
367
|
-
description: z.optional(z.string()),
|
|
368
|
-
/**
|
|
369
|
-
* Whether this argument must be provided.
|
|
370
|
-
*/
|
|
371
|
-
required: z.optional(z.boolean())
|
|
372
|
-
}).passthrough();
|
|
373
|
-
var PromptSchema = z.object({
|
|
374
|
-
/**
|
|
375
|
-
* The name of the prompt or prompt template.
|
|
376
|
-
*/
|
|
377
|
-
name: z.string(),
|
|
378
|
-
/**
|
|
379
|
-
* An optional description of what this prompt provides
|
|
380
|
-
*/
|
|
381
|
-
description: z.optional(z.string()),
|
|
382
|
-
/**
|
|
383
|
-
* A list of arguments to use for templating the prompt.
|
|
384
|
-
*/
|
|
385
|
-
arguments: z.optional(z.array(PromptArgumentSchema))
|
|
386
|
-
}).passthrough();
|
|
387
|
-
var ListPromptsRequestSchema = PaginatedRequestSchema.extend({
|
|
388
|
-
method: z.literal("prompts/list")
|
|
389
|
-
});
|
|
390
|
-
var ListPromptsResultSchema = PaginatedResultSchema.extend({
|
|
391
|
-
prompts: z.array(PromptSchema)
|
|
392
|
-
});
|
|
393
|
-
var GetPromptRequestSchema = RequestSchema.extend({
|
|
394
|
-
method: z.literal("prompts/get"),
|
|
395
|
-
params: BaseRequestParamsSchema.extend({
|
|
396
|
-
/**
|
|
397
|
-
* The name of the prompt or prompt template.
|
|
398
|
-
*/
|
|
399
|
-
name: z.string(),
|
|
400
|
-
/**
|
|
401
|
-
* Arguments to use for templating the prompt.
|
|
402
|
-
*/
|
|
403
|
-
arguments: z.optional(z.record(z.string()))
|
|
404
|
-
})
|
|
405
|
-
});
|
|
406
|
-
var TextContentSchema = z.object({
|
|
407
|
-
type: z.literal("text"),
|
|
408
|
-
/**
|
|
409
|
-
* The text content of the message.
|
|
410
|
-
*/
|
|
411
|
-
text: z.string()
|
|
412
|
-
}).passthrough();
|
|
413
|
-
var ImageContentSchema = z.object({
|
|
414
|
-
type: z.literal("image"),
|
|
415
|
-
/**
|
|
416
|
-
* The base64-encoded image data.
|
|
417
|
-
*/
|
|
418
|
-
data: z.string().base64(),
|
|
419
|
-
/**
|
|
420
|
-
* The MIME type of the image. Different providers may support different image types.
|
|
421
|
-
*/
|
|
422
|
-
mimeType: z.string()
|
|
423
|
-
}).passthrough();
|
|
424
|
-
var EmbeddedResourceSchema = z.object({
|
|
425
|
-
type: z.literal("resource"),
|
|
426
|
-
resource: z.union([TextResourceContentsSchema, BlobResourceContentsSchema])
|
|
427
|
-
}).passthrough();
|
|
428
|
-
var PromptMessageSchema = z.object({
|
|
429
|
-
role: z.enum(["user", "assistant"]),
|
|
430
|
-
content: z.union([
|
|
431
|
-
TextContentSchema,
|
|
432
|
-
ImageContentSchema,
|
|
433
|
-
EmbeddedResourceSchema
|
|
434
|
-
])
|
|
435
|
-
}).passthrough();
|
|
436
|
-
var GetPromptResultSchema = ResultSchema.extend({
|
|
437
|
-
/**
|
|
438
|
-
* An optional description for the prompt.
|
|
439
|
-
*/
|
|
440
|
-
description: z.optional(z.string()),
|
|
441
|
-
messages: z.array(PromptMessageSchema)
|
|
442
|
-
});
|
|
443
|
-
var PromptListChangedNotificationSchema = NotificationSchema.extend({
|
|
444
|
-
method: z.literal("notifications/prompts/list_changed")
|
|
445
|
-
});
|
|
446
|
-
var ToolSchema = z.object({
|
|
447
|
-
/**
|
|
448
|
-
* The name of the tool.
|
|
449
|
-
*/
|
|
450
|
-
name: z.string(),
|
|
451
|
-
/**
|
|
452
|
-
* A human-readable description of the tool.
|
|
453
|
-
*/
|
|
454
|
-
description: z.optional(z.string()),
|
|
455
|
-
/**
|
|
456
|
-
* A JSON Schema object defining the expected parameters for the tool.
|
|
457
|
-
*/
|
|
458
|
-
inputSchema: z.object({
|
|
459
|
-
type: z.literal("object"),
|
|
460
|
-
properties: z.optional(z.object({}).passthrough())
|
|
461
|
-
}).passthrough()
|
|
462
|
-
}).passthrough();
|
|
463
|
-
var ListToolsRequestSchema = PaginatedRequestSchema.extend({
|
|
464
|
-
method: z.literal("tools/list")
|
|
465
|
-
});
|
|
466
|
-
var ListToolsResultSchema = PaginatedResultSchema.extend({
|
|
467
|
-
tools: z.array(ToolSchema)
|
|
468
|
-
});
|
|
469
|
-
var CallToolResultSchema = ResultSchema.extend({
|
|
470
|
-
content: z.array(z.union([TextContentSchema, ImageContentSchema, EmbeddedResourceSchema])),
|
|
471
|
-
isError: z.boolean().default(false).optional()
|
|
472
|
-
});
|
|
473
|
-
var CompatibilityCallToolResultSchema = CallToolResultSchema.or(ResultSchema.extend({
|
|
474
|
-
toolResult: z.unknown()
|
|
475
|
-
}));
|
|
476
|
-
var CallToolRequestSchema = RequestSchema.extend({
|
|
477
|
-
method: z.literal("tools/call"),
|
|
478
|
-
params: BaseRequestParamsSchema.extend({
|
|
479
|
-
name: z.string(),
|
|
480
|
-
arguments: z.optional(z.record(z.unknown()))
|
|
481
|
-
})
|
|
482
|
-
});
|
|
483
|
-
var ToolListChangedNotificationSchema = NotificationSchema.extend({
|
|
484
|
-
method: z.literal("notifications/tools/list_changed")
|
|
485
|
-
});
|
|
486
|
-
var LoggingLevelSchema = z.enum([
|
|
487
|
-
"debug",
|
|
488
|
-
"info",
|
|
489
|
-
"notice",
|
|
490
|
-
"warning",
|
|
491
|
-
"error",
|
|
492
|
-
"critical",
|
|
493
|
-
"alert",
|
|
494
|
-
"emergency"
|
|
495
|
-
]);
|
|
496
|
-
var SetLevelRequestSchema = RequestSchema.extend({
|
|
497
|
-
method: z.literal("logging/setLevel"),
|
|
498
|
-
params: BaseRequestParamsSchema.extend({
|
|
499
|
-
/**
|
|
500
|
-
* The level of logging that the client wants to receive from the server. The server should send all logs at this level and higher (i.e., more severe) to the client as notifications/logging/message.
|
|
501
|
-
*/
|
|
502
|
-
level: LoggingLevelSchema
|
|
503
|
-
})
|
|
504
|
-
});
|
|
505
|
-
var LoggingMessageNotificationSchema = NotificationSchema.extend({
|
|
506
|
-
method: z.literal("notifications/message"),
|
|
507
|
-
params: BaseNotificationParamsSchema.extend({
|
|
508
|
-
/**
|
|
509
|
-
* The severity of this log message.
|
|
510
|
-
*/
|
|
511
|
-
level: LoggingLevelSchema,
|
|
512
|
-
/**
|
|
513
|
-
* An optional name of the logger issuing this message.
|
|
514
|
-
*/
|
|
515
|
-
logger: z.optional(z.string()),
|
|
516
|
-
/**
|
|
517
|
-
* The data to be logged, such as a string message or an object. Any JSON serializable type is allowed here.
|
|
518
|
-
*/
|
|
519
|
-
data: z.unknown()
|
|
520
|
-
})
|
|
521
|
-
});
|
|
522
|
-
var ModelHintSchema = z.object({
|
|
523
|
-
/**
|
|
524
|
-
* A hint for a model name.
|
|
525
|
-
*/
|
|
526
|
-
name: z.string().optional()
|
|
527
|
-
}).passthrough();
|
|
528
|
-
var ModelPreferencesSchema = z.object({
|
|
529
|
-
/**
|
|
530
|
-
* Optional hints to use for model selection.
|
|
531
|
-
*/
|
|
532
|
-
hints: z.optional(z.array(ModelHintSchema)),
|
|
533
|
-
/**
|
|
534
|
-
* How much to prioritize cost when selecting a model.
|
|
535
|
-
*/
|
|
536
|
-
costPriority: z.optional(z.number().min(0).max(1)),
|
|
537
|
-
/**
|
|
538
|
-
* How much to prioritize sampling speed (latency) when selecting a model.
|
|
539
|
-
*/
|
|
540
|
-
speedPriority: z.optional(z.number().min(0).max(1)),
|
|
541
|
-
/**
|
|
542
|
-
* How much to prioritize intelligence and capabilities when selecting a model.
|
|
543
|
-
*/
|
|
544
|
-
intelligencePriority: z.optional(z.number().min(0).max(1))
|
|
545
|
-
}).passthrough();
|
|
546
|
-
var SamplingMessageSchema = z.object({
|
|
547
|
-
role: z.enum(["user", "assistant"]),
|
|
548
|
-
content: z.union([TextContentSchema, ImageContentSchema])
|
|
549
|
-
}).passthrough();
|
|
550
|
-
var CreateMessageRequestSchema = RequestSchema.extend({
|
|
551
|
-
method: z.literal("sampling/createMessage"),
|
|
552
|
-
params: BaseRequestParamsSchema.extend({
|
|
553
|
-
messages: z.array(SamplingMessageSchema),
|
|
554
|
-
/**
|
|
555
|
-
* An optional system prompt the server wants to use for sampling. The client MAY modify or omit this prompt.
|
|
556
|
-
*/
|
|
557
|
-
systemPrompt: z.optional(z.string()),
|
|
558
|
-
/**
|
|
559
|
-
* A request to include context from one or more MCP servers (including the caller), to be attached to the prompt. The client MAY ignore this request.
|
|
560
|
-
*/
|
|
561
|
-
includeContext: z.optional(z.enum(["none", "thisServer", "allServers"])),
|
|
562
|
-
temperature: z.optional(z.number()),
|
|
563
|
-
/**
|
|
564
|
-
* The maximum number of tokens to sample, as requested by the server. The client MAY choose to sample fewer tokens than requested.
|
|
565
|
-
*/
|
|
566
|
-
maxTokens: z.number().int(),
|
|
567
|
-
stopSequences: z.optional(z.array(z.string())),
|
|
568
|
-
/**
|
|
569
|
-
* Optional metadata to pass through to the LLM provider. The format of this metadata is provider-specific.
|
|
570
|
-
*/
|
|
571
|
-
metadata: z.optional(z.object({}).passthrough()),
|
|
572
|
-
/**
|
|
573
|
-
* The server's preferences for which model to select.
|
|
574
|
-
*/
|
|
575
|
-
modelPreferences: z.optional(ModelPreferencesSchema)
|
|
576
|
-
})
|
|
577
|
-
});
|
|
578
|
-
var CreateMessageResultSchema = ResultSchema.extend({
|
|
579
|
-
/**
|
|
580
|
-
* The name of the model that generated the message.
|
|
581
|
-
*/
|
|
582
|
-
model: z.string(),
|
|
583
|
-
/**
|
|
584
|
-
* The reason why sampling stopped.
|
|
585
|
-
*/
|
|
586
|
-
stopReason: z.optional(z.enum(["endTurn", "stopSequence", "maxTokens"]).or(z.string())),
|
|
587
|
-
role: z.enum(["user", "assistant"]),
|
|
588
|
-
content: z.discriminatedUnion("type", [
|
|
589
|
-
TextContentSchema,
|
|
590
|
-
ImageContentSchema
|
|
591
|
-
])
|
|
592
|
-
});
|
|
593
|
-
var ResourceReferenceSchema = z.object({
|
|
594
|
-
type: z.literal("ref/resource"),
|
|
595
|
-
/**
|
|
596
|
-
* The URI or URI template of the resource.
|
|
597
|
-
*/
|
|
598
|
-
uri: z.string()
|
|
599
|
-
}).passthrough();
|
|
600
|
-
var PromptReferenceSchema = z.object({
|
|
601
|
-
type: z.literal("ref/prompt"),
|
|
602
|
-
/**
|
|
603
|
-
* The name of the prompt or prompt template
|
|
604
|
-
*/
|
|
605
|
-
name: z.string()
|
|
606
|
-
}).passthrough();
|
|
607
|
-
var CompleteRequestSchema = RequestSchema.extend({
|
|
608
|
-
method: z.literal("completion/complete"),
|
|
609
|
-
params: BaseRequestParamsSchema.extend({
|
|
610
|
-
ref: z.union([PromptReferenceSchema, ResourceReferenceSchema]),
|
|
611
|
-
/**
|
|
612
|
-
* The argument's information
|
|
613
|
-
*/
|
|
614
|
-
argument: z.object({
|
|
615
|
-
/**
|
|
616
|
-
* The name of the argument
|
|
617
|
-
*/
|
|
618
|
-
name: z.string(),
|
|
619
|
-
/**
|
|
620
|
-
* The value of the argument to use for completion matching.
|
|
621
|
-
*/
|
|
622
|
-
value: z.string()
|
|
623
|
-
}).passthrough()
|
|
624
|
-
})
|
|
625
|
-
});
|
|
626
|
-
var CompleteResultSchema = ResultSchema.extend({
|
|
627
|
-
completion: z.object({
|
|
628
|
-
/**
|
|
629
|
-
* An array of completion values. Must not exceed 100 items.
|
|
630
|
-
*/
|
|
631
|
-
values: z.array(z.string()).max(100),
|
|
632
|
-
/**
|
|
633
|
-
* The total number of completion options available. This can exceed the number of values actually sent in the response.
|
|
634
|
-
*/
|
|
635
|
-
total: z.optional(z.number().int()),
|
|
636
|
-
/**
|
|
637
|
-
* Indicates whether there are additional completion options beyond those provided in the current response, even if the exact total is unknown.
|
|
638
|
-
*/
|
|
639
|
-
hasMore: z.optional(z.boolean())
|
|
640
|
-
}).passthrough()
|
|
641
|
-
});
|
|
642
|
-
var RootSchema = z.object({
|
|
643
|
-
/**
|
|
644
|
-
* The URI identifying the root. This *must* start with file:// for now.
|
|
645
|
-
*/
|
|
646
|
-
uri: z.string().startsWith("file://"),
|
|
647
|
-
/**
|
|
648
|
-
* An optional name for the root.
|
|
649
|
-
*/
|
|
650
|
-
name: z.optional(z.string())
|
|
651
|
-
}).passthrough();
|
|
652
|
-
var ListRootsRequestSchema = RequestSchema.extend({
|
|
653
|
-
method: z.literal("roots/list")
|
|
654
|
-
});
|
|
655
|
-
var ListRootsResultSchema = ResultSchema.extend({
|
|
656
|
-
roots: z.array(RootSchema)
|
|
657
|
-
});
|
|
658
|
-
var RootsListChangedNotificationSchema = NotificationSchema.extend({
|
|
659
|
-
method: z.literal("notifications/roots/list_changed")
|
|
660
|
-
});
|
|
661
|
-
var ClientRequestSchema = z.union([
|
|
662
|
-
PingRequestSchema,
|
|
663
|
-
InitializeRequestSchema,
|
|
664
|
-
CompleteRequestSchema,
|
|
665
|
-
SetLevelRequestSchema,
|
|
666
|
-
GetPromptRequestSchema,
|
|
667
|
-
ListPromptsRequestSchema,
|
|
668
|
-
ListResourcesRequestSchema,
|
|
669
|
-
ListResourceTemplatesRequestSchema,
|
|
670
|
-
ReadResourceRequestSchema,
|
|
671
|
-
SubscribeRequestSchema,
|
|
672
|
-
UnsubscribeRequestSchema,
|
|
673
|
-
CallToolRequestSchema,
|
|
674
|
-
ListToolsRequestSchema
|
|
675
|
-
]);
|
|
676
|
-
var ClientNotificationSchema = z.union([
|
|
677
|
-
CancelledNotificationSchema,
|
|
678
|
-
ProgressNotificationSchema,
|
|
679
|
-
InitializedNotificationSchema,
|
|
680
|
-
RootsListChangedNotificationSchema
|
|
681
|
-
]);
|
|
682
|
-
var ClientResultSchema = z.union([
|
|
683
|
-
EmptyResultSchema,
|
|
684
|
-
CreateMessageResultSchema,
|
|
685
|
-
ListRootsResultSchema
|
|
686
|
-
]);
|
|
687
|
-
var ServerRequestSchema = z.union([
|
|
688
|
-
PingRequestSchema,
|
|
689
|
-
CreateMessageRequestSchema,
|
|
690
|
-
ListRootsRequestSchema
|
|
691
|
-
]);
|
|
692
|
-
var ServerNotificationSchema = z.union([
|
|
693
|
-
CancelledNotificationSchema,
|
|
694
|
-
ProgressNotificationSchema,
|
|
695
|
-
LoggingMessageNotificationSchema,
|
|
696
|
-
ResourceUpdatedNotificationSchema,
|
|
697
|
-
ResourceListChangedNotificationSchema,
|
|
698
|
-
ToolListChangedNotificationSchema,
|
|
699
|
-
PromptListChangedNotificationSchema
|
|
700
|
-
]);
|
|
701
|
-
var ServerResultSchema = z.union([
|
|
702
|
-
EmptyResultSchema,
|
|
703
|
-
InitializeResultSchema,
|
|
704
|
-
CompleteResultSchema,
|
|
705
|
-
GetPromptResultSchema,
|
|
706
|
-
ListPromptsResultSchema,
|
|
707
|
-
ListResourcesResultSchema,
|
|
708
|
-
ListResourceTemplatesResultSchema,
|
|
709
|
-
ReadResourceResultSchema,
|
|
710
|
-
CallToolResultSchema,
|
|
711
|
-
ListToolsResultSchema
|
|
712
|
-
]);
|
|
713
|
-
|
|
714
|
-
// src/lib/sseEdge.ts
|
|
715
|
-
var MAXIMUM_MESSAGE_SIZE = 4 * 1024 * 1024;
|
|
716
|
-
var SSEEdgeTransport = class {
|
|
717
|
-
/**
|
|
718
|
-
* Creates a new EdgeSSETransport, which will direct the MPC client to POST messages to messageUrl
|
|
719
|
-
*/
|
|
720
|
-
constructor(messageUrl, sessionId) {
|
|
721
|
-
this.messageUrl = messageUrl;
|
|
722
|
-
this.sessionId = sessionId;
|
|
723
|
-
this.controller = null;
|
|
724
|
-
this.closed = false;
|
|
725
|
-
this.stream = new ReadableStream({
|
|
726
|
-
start: (controller) => {
|
|
727
|
-
this.controller = controller;
|
|
728
|
-
},
|
|
729
|
-
cancel: () => {
|
|
730
|
-
this.closed = true;
|
|
731
|
-
this.onclose?.();
|
|
732
|
-
}
|
|
733
|
-
});
|
|
734
|
-
}
|
|
735
|
-
async start() {
|
|
736
|
-
if (this.closed) {
|
|
737
|
-
throw new Error(
|
|
738
|
-
"SSE transport already closed! If using Server class, note that connect() calls start() automatically."
|
|
739
|
-
);
|
|
740
|
-
}
|
|
741
|
-
if (!this.controller) {
|
|
742
|
-
throw new Error("Stream controller not initialized");
|
|
743
|
-
}
|
|
744
|
-
const endpointMessage = `event: endpoint
|
|
745
|
-
data: ${encodeURI(this.messageUrl)}?sessionId=${this.sessionId}
|
|
746
|
-
|
|
747
|
-
`;
|
|
748
|
-
this.controller.enqueue(new TextEncoder().encode(endpointMessage));
|
|
749
|
-
}
|
|
750
|
-
get sseResponse() {
|
|
751
|
-
if (!this.stream) {
|
|
752
|
-
throw new Error("Stream not initialized");
|
|
753
|
-
}
|
|
754
|
-
return new Response(this.stream, {
|
|
755
|
-
headers: {
|
|
756
|
-
"Content-Type": "text/event-stream",
|
|
757
|
-
"Cache-Control": "no-cache",
|
|
758
|
-
Connection: "keep-alive"
|
|
759
|
-
}
|
|
760
|
-
});
|
|
761
|
-
}
|
|
762
|
-
/**
|
|
763
|
-
* Handles incoming Requests
|
|
764
|
-
*/
|
|
765
|
-
async handlePostMessage(req) {
|
|
766
|
-
if (this.closed || !this.controller) {
|
|
767
|
-
const message = "SSE connection not established";
|
|
768
|
-
return new Response(message, { status: 500 });
|
|
769
|
-
}
|
|
770
|
-
try {
|
|
771
|
-
const contentType = req.headers.get("content-type") || "";
|
|
772
|
-
if (!contentType.includes("application/json")) {
|
|
773
|
-
throw new Error(`Unsupported content-type: ${contentType}`);
|
|
774
|
-
}
|
|
775
|
-
const contentLength = Number.parseInt(
|
|
776
|
-
req.headers.get("content-length") || "0",
|
|
777
|
-
10
|
|
778
|
-
);
|
|
779
|
-
if (contentLength > MAXIMUM_MESSAGE_SIZE) {
|
|
780
|
-
throw new Error(`Request body too large: ${contentLength} bytes`);
|
|
781
|
-
}
|
|
782
|
-
const body = await req.json();
|
|
783
|
-
await this.handleMessage(body);
|
|
784
|
-
return new Response("Accepted", { status: 202 });
|
|
785
|
-
} catch (error) {
|
|
786
|
-
this.onerror?.(error);
|
|
787
|
-
return new Response(String(error), { status: 400 });
|
|
788
|
-
}
|
|
789
|
-
}
|
|
790
|
-
/**
|
|
791
|
-
* Handle a client message, regardless of how it arrived. This can be used to inform the server of messages that arrive via a means different than HTTP POST.
|
|
792
|
-
*/
|
|
793
|
-
async handleMessage(message) {
|
|
794
|
-
let parsedMessage;
|
|
795
|
-
try {
|
|
796
|
-
parsedMessage = JSONRPCMessageSchema.parse(message);
|
|
797
|
-
} catch (error) {
|
|
798
|
-
this.onerror?.(error);
|
|
799
|
-
throw error;
|
|
800
|
-
}
|
|
801
|
-
this.onmessage?.(parsedMessage);
|
|
802
|
-
}
|
|
803
|
-
async close() {
|
|
804
|
-
if (!this.closed && this.controller) {
|
|
805
|
-
this.controller.close();
|
|
806
|
-
this.stream.cancel();
|
|
807
|
-
this.closed = true;
|
|
808
|
-
this.onclose?.();
|
|
809
|
-
}
|
|
810
|
-
}
|
|
811
|
-
async send(message) {
|
|
812
|
-
if (this.closed || !this.controller) {
|
|
813
|
-
throw new Error("Not connected");
|
|
814
|
-
}
|
|
815
|
-
const messageText = `event: message
|
|
816
|
-
data: ${JSON.stringify(message)}
|
|
817
|
-
|
|
818
|
-
`;
|
|
819
|
-
this.controller.enqueue(new TextEncoder().encode(messageText));
|
|
820
|
-
}
|
|
821
|
-
};
|
|
822
|
-
|
|
823
|
-
// src/mcp.ts
|
|
824
|
-
function handleCORS(request, corsOptions) {
|
|
825
|
-
const origin = request.headers.get("Origin") || "*";
|
|
826
|
-
const corsHeaders = {
|
|
827
|
-
"Access-Control-Allow-Origin": corsOptions?.origin || origin,
|
|
828
|
-
"Access-Control-Allow-Methods": corsOptions?.methods || "GET, POST, OPTIONS",
|
|
829
|
-
"Access-Control-Allow-Headers": corsOptions?.headers || "Content-Type",
|
|
830
|
-
"Access-Control-Max-Age": (corsOptions?.maxAge || 86400).toString()
|
|
831
|
-
};
|
|
832
|
-
if (request.method === "OPTIONS") {
|
|
833
|
-
return new Response(null, { headers: corsHeaders });
|
|
834
|
-
}
|
|
835
|
-
return null;
|
|
836
|
-
}
|
|
837
|
-
var _agent;
|
|
838
|
-
var McpAgent = class extends DurableObject {
|
|
839
|
-
constructor(ctx, env) {
|
|
840
|
-
var _a;
|
|
841
|
-
super(ctx, env);
|
|
842
|
-
/**
|
|
843
|
-
* Since McpAgent's _aren't_ yet real "Agents" (they route differently, don't support
|
|
844
|
-
* websockets, don't support hibernation), let's only expose a couple of the methods
|
|
845
|
-
* to the outer class: initialState/state/setState/onStateUpdate/sql
|
|
846
|
-
*/
|
|
847
|
-
__privateAdd(this, _agent);
|
|
848
|
-
this.initRun = false;
|
|
849
|
-
const self = this;
|
|
850
|
-
__privateSet(this, _agent, new (_a = class extends Agent {
|
|
851
|
-
onStateUpdate(state, source) {
|
|
852
|
-
return self.onStateUpdate(state, source);
|
|
853
|
-
}
|
|
854
|
-
}, _a.options = {
|
|
855
|
-
hibernate: false
|
|
856
|
-
}, _a)(ctx, env));
|
|
857
|
-
}
|
|
858
|
-
get state() {
|
|
859
|
-
if (this.initialState) __privateGet(this, _agent).initialState = this.initialState;
|
|
860
|
-
return __privateGet(this, _agent).state;
|
|
861
|
-
}
|
|
862
|
-
sql(strings, ...values) {
|
|
863
|
-
return __privateGet(this, _agent).sql(strings, ...values);
|
|
864
|
-
}
|
|
865
|
-
setState(state) {
|
|
866
|
-
return __privateGet(this, _agent).setState(state);
|
|
867
|
-
}
|
|
868
|
-
onStateUpdate(state, source) {
|
|
869
|
-
}
|
|
870
|
-
async _init(props) {
|
|
871
|
-
this.props = props;
|
|
872
|
-
if (!this.initRun) {
|
|
873
|
-
this.initRun = true;
|
|
874
|
-
await this.init();
|
|
875
|
-
}
|
|
876
|
-
}
|
|
877
|
-
async onSSE(path) {
|
|
878
|
-
this.transport = new SSEEdgeTransport(
|
|
879
|
-
`${path}/message`,
|
|
880
|
-
this.ctx.id.toString()
|
|
881
|
-
);
|
|
882
|
-
await this.server.connect(this.transport);
|
|
883
|
-
return this.transport.sseResponse;
|
|
884
|
-
}
|
|
885
|
-
async onMCPMessage(request) {
|
|
886
|
-
return this.transport.handlePostMessage(request);
|
|
887
|
-
}
|
|
888
|
-
static mount(path, {
|
|
889
|
-
binding = "MCP_OBJECT",
|
|
890
|
-
corsOptions
|
|
891
|
-
} = {}) {
|
|
892
|
-
const basePattern = new URLPattern({ pathname: path });
|
|
893
|
-
const messagePattern = new URLPattern({ pathname: `${path}/message` });
|
|
894
|
-
return {
|
|
895
|
-
fetch: async (request, env, ctx) => {
|
|
896
|
-
const corsResponse = handleCORS(request, corsOptions);
|
|
897
|
-
if (corsResponse) return corsResponse;
|
|
898
|
-
const url = new URL(request.url);
|
|
899
|
-
const namespace = env[binding];
|
|
900
|
-
if (request.method === "GET" && basePattern.test(url)) {
|
|
901
|
-
const object = namespace.get(namespace.newUniqueId());
|
|
902
|
-
await object._init(ctx.props);
|
|
903
|
-
const response = await object.onSSE(path);
|
|
904
|
-
const headerObj = {};
|
|
905
|
-
response.headers.forEach((value, key) => {
|
|
906
|
-
headerObj[key] = value;
|
|
907
|
-
});
|
|
908
|
-
headerObj["Access-Control-Allow-Origin"] = corsOptions?.origin || "*";
|
|
909
|
-
return new Response(response.body, {
|
|
910
|
-
status: response.status,
|
|
911
|
-
statusText: response.statusText,
|
|
912
|
-
headers: headerObj
|
|
913
|
-
});
|
|
914
|
-
}
|
|
915
|
-
if (request.method === "POST" && messagePattern.test(url)) {
|
|
916
|
-
const sessionId = url.searchParams.get("sessionId");
|
|
917
|
-
if (!sessionId) {
|
|
918
|
-
return new Response(
|
|
919
|
-
`Missing sessionId. Expected POST to ${path} to initiate new one`,
|
|
920
|
-
{ status: 400 }
|
|
921
|
-
);
|
|
922
|
-
}
|
|
923
|
-
const object = namespace.get(namespace.idFromString(sessionId));
|
|
924
|
-
const response = await object.onMCPMessage(request);
|
|
925
|
-
const headerObj = {};
|
|
926
|
-
response.headers.forEach((value, key) => {
|
|
927
|
-
headerObj[key] = value;
|
|
928
|
-
});
|
|
929
|
-
headerObj["Access-Control-Allow-Origin"] = corsOptions?.origin || "*";
|
|
930
|
-
return new Response(response.body, {
|
|
931
|
-
status: response.status,
|
|
932
|
-
statusText: response.statusText,
|
|
933
|
-
headers: headerObj
|
|
934
|
-
});
|
|
935
|
-
}
|
|
936
|
-
return new Response("Not Found", { status: 404 });
|
|
937
|
-
}
|
|
938
|
-
};
|
|
939
|
-
}
|
|
940
|
-
};
|
|
941
|
-
_agent = new WeakMap();
|
|
942
|
-
export {
|
|
943
|
-
McpAgent
|
|
944
|
-
};
|
|
945
|
-
//# sourceMappingURL=mcp.js.map
|