agents 0.3.4 → 0.3.5
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/{client-C7SOlYfK.js → client-0lfEZpSQ.js} +7 -1
- package/dist/client-0lfEZpSQ.js.map +1 -0
- package/dist/{client-CbcnyqCo.d.ts → client-Cxno-5sH.d.ts} +141 -6
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/mcp/client.d.ts +1 -1
- package/dist/mcp/client.js +1 -1
- package/dist/mcp/index.d.ts +34 -2
- package/dist/mcp/index.js +64 -15
- package/dist/mcp/index.js.map +1 -1
- package/dist/observability/index.js +2 -2
- package/dist/react.d.ts +1 -1
- package/dist/{src-BRXU0m-N.js → src-C_iKczoR.js} +2 -2
- package/dist/{src-BRXU0m-N.js.map → src-C_iKczoR.js.map} +1 -1
- package/package.json +2 -2
- package/dist/client-C7SOlYfK.js.map +0 -1
package/dist/mcp/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { t as MessageType } from "../types-4b5tlB0u.js";
|
|
2
2
|
import "../client-CEO0P7vN.js";
|
|
3
3
|
import "../internal_context-oN047Id3.js";
|
|
4
|
-
import "../client-
|
|
4
|
+
import "../client-0lfEZpSQ.js";
|
|
5
5
|
import "../do-oauth-client-provider-BfPFgQU0.js";
|
|
6
|
-
import { c as getAgentByName, l as getCurrentAgent, t as Agent } from "../src-
|
|
6
|
+
import { c as getAgentByName, l as getCurrentAgent, t as Agent } from "../src-C_iKczoR.js";
|
|
7
7
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
8
8
|
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
9
9
|
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
10
|
-
import { ElicitRequestSchema, InitializeRequestSchema, JSONRPCMessageSchema, SUPPORTED_PROTOCOL_VERSIONS, isInitializeRequest,
|
|
10
|
+
import { ElicitRequestSchema, InitializeRequestSchema, JSONRPCMessageSchema, SUPPORTED_PROTOCOL_VERSIONS, isInitializeRequest, isJSONRPCErrorResponse, isJSONRPCNotification, isJSONRPCRequest, isJSONRPCResultResponse } from "@modelcontextprotocol/sdk/types.js";
|
|
11
11
|
|
|
12
12
|
//#region src/mcp/utils.ts
|
|
13
13
|
/**
|
|
@@ -208,7 +208,7 @@ const createStreamingHttpHandler = (basePath, namespace, options = {}) => {
|
|
|
208
208
|
}
|
|
209
209
|
onClose().catch(console.error);
|
|
210
210
|
});
|
|
211
|
-
if (messages.every((msg) => isJSONRPCNotification(msg) ||
|
|
211
|
+
if (messages.every((msg) => isJSONRPCNotification(msg) || isJSONRPCResultResponse(msg))) {
|
|
212
212
|
ws.close();
|
|
213
213
|
return new Response(null, {
|
|
214
214
|
headers: corsHeaders(request, options.corsOptions),
|
|
@@ -634,9 +634,9 @@ var StreamableHTTPServerTransport = class {
|
|
|
634
634
|
const { agent } = getCurrentAgent();
|
|
635
635
|
if (!agent) throw new Error("Agent was not found in send");
|
|
636
636
|
let requestId = options?.relatedRequestId;
|
|
637
|
-
if (
|
|
637
|
+
if (isJSONRPCResultResponse(message) || isJSONRPCErrorResponse(message)) requestId = message.id;
|
|
638
638
|
if (requestId === void 0) {
|
|
639
|
-
if (
|
|
639
|
+
if (isJSONRPCResultResponse(message) || isJSONRPCErrorResponse(message)) throw new Error("Cannot send a response on a standalone SSE stream unless resuming a previous client request");
|
|
640
640
|
let standaloneConnection;
|
|
641
641
|
for (const conn of agent.getConnections()) if (conn.state?._standaloneSse) standaloneConnection = conn;
|
|
642
642
|
if (standaloneConnection === void 0) return;
|
|
@@ -650,7 +650,7 @@ var StreamableHTTPServerTransport = class {
|
|
|
650
650
|
let eventId;
|
|
651
651
|
if (this._eventStore) eventId = await this._eventStore.storeEvent(connection.id, message);
|
|
652
652
|
let shouldClose = false;
|
|
653
|
-
if (
|
|
653
|
+
if (isJSONRPCResultResponse(message) || isJSONRPCErrorResponse(message)) {
|
|
654
654
|
this._requestResponseMap.set(requestId, message);
|
|
655
655
|
const relatedIds = connection.state?.requestIds ?? [];
|
|
656
656
|
shouldClose = relatedIds.every((id) => this._requestResponseMap.has(id));
|
|
@@ -708,8 +708,11 @@ var WorkerTransport = class {
|
|
|
708
708
|
this.sessionIdGenerator = options?.sessionIdGenerator;
|
|
709
709
|
this.enableJsonResponse = options?.enableJsonResponse ?? false;
|
|
710
710
|
this.onsessioninitialized = options?.onsessioninitialized;
|
|
711
|
+
this.onsessionclosed = options?.onsessionclosed;
|
|
711
712
|
this.corsOptions = options?.corsOptions;
|
|
712
713
|
this.storage = options?.storage;
|
|
714
|
+
this.eventStore = options?.eventStore;
|
|
715
|
+
this.retryInterval = options?.retryInterval;
|
|
713
716
|
}
|
|
714
717
|
/**
|
|
715
718
|
* Restore transport state from persistent storage.
|
|
@@ -818,7 +821,12 @@ var WorkerTransport = class {
|
|
|
818
821
|
if (sessionError) return sessionError;
|
|
819
822
|
const versionError = this.validateProtocolVersion(request);
|
|
820
823
|
if (versionError) return versionError;
|
|
821
|
-
|
|
824
|
+
let streamId = this.standaloneSseStreamId;
|
|
825
|
+
const lastEventId = request.headers.get("Last-Event-ID");
|
|
826
|
+
if (lastEventId && this.eventStore) {
|
|
827
|
+
const eventStreamId = await this.eventStore.getStreamIdForEventId?.(lastEventId);
|
|
828
|
+
if (eventStreamId) streamId = eventStreamId;
|
|
829
|
+
}
|
|
822
830
|
if (this.streamMapping.get(streamId) !== void 0) return new Response(JSON.stringify({
|
|
823
831
|
jsonrpc: "2.0",
|
|
824
832
|
error: {
|
|
@@ -859,6 +867,26 @@ var WorkerTransport = class {
|
|
|
859
867
|
writer.close().catch(() => {});
|
|
860
868
|
}
|
|
861
869
|
});
|
|
870
|
+
if (this.retryInterval !== void 0) await writer.write(encoder.encode(`retry: ${this.retryInterval}\n\n`));
|
|
871
|
+
if (lastEventId && this.eventStore) {
|
|
872
|
+
const replayedStreamId = await this.eventStore.replayEventsAfter(lastEventId, { send: async (eventId, message) => {
|
|
873
|
+
const data = `id: ${eventId}\nevent: message\ndata: ${JSON.stringify(message)}\n\n`;
|
|
874
|
+
await writer.write(encoder.encode(data));
|
|
875
|
+
} });
|
|
876
|
+
if (replayedStreamId !== streamId) {
|
|
877
|
+
this.streamMapping.delete(streamId);
|
|
878
|
+
streamId = replayedStreamId;
|
|
879
|
+
this.streamMapping.set(streamId, {
|
|
880
|
+
writer,
|
|
881
|
+
encoder,
|
|
882
|
+
cleanup: () => {
|
|
883
|
+
clearInterval(keepAlive);
|
|
884
|
+
this.streamMapping.delete(streamId);
|
|
885
|
+
writer.close().catch(() => {});
|
|
886
|
+
}
|
|
887
|
+
});
|
|
888
|
+
}
|
|
889
|
+
}
|
|
862
890
|
return new Response(readable, { headers });
|
|
863
891
|
}
|
|
864
892
|
async handlePostRequest(request, parsedBody) {
|
|
@@ -1017,7 +1045,9 @@ var WorkerTransport = class {
|
|
|
1017
1045
|
if (sessionError) return sessionError;
|
|
1018
1046
|
const versionError = this.validateProtocolVersion(request);
|
|
1019
1047
|
if (versionError) return versionError;
|
|
1048
|
+
const closedSessionId = this.sessionId;
|
|
1020
1049
|
await this.close();
|
|
1050
|
+
if (closedSessionId && this.onsessionclosed) this.onsessionclosed(closedSessionId);
|
|
1021
1051
|
return new Response(null, {
|
|
1022
1052
|
status: 200,
|
|
1023
1053
|
headers: { ...this.getHeaders() }
|
|
@@ -1097,15 +1127,32 @@ var WorkerTransport = class {
|
|
|
1097
1127
|
this.requestResponseMap.clear();
|
|
1098
1128
|
this.onclose?.();
|
|
1099
1129
|
}
|
|
1130
|
+
/**
|
|
1131
|
+
* Close an SSE stream for a specific request, triggering client reconnection.
|
|
1132
|
+
* Use this to implement polling behavior during long-running operations -
|
|
1133
|
+
* client will reconnect after the retry interval specified in the priming event.
|
|
1134
|
+
*/
|
|
1135
|
+
closeSSEStream(requestId) {
|
|
1136
|
+
const streamId = this.requestToStreamMapping.get(requestId);
|
|
1137
|
+
if (!streamId) return;
|
|
1138
|
+
const stream = this.streamMapping.get(streamId);
|
|
1139
|
+
if (stream) stream.cleanup();
|
|
1140
|
+
for (const [reqId, sid] of this.requestToStreamMapping.entries()) if (sid === streamId) {
|
|
1141
|
+
this.requestToStreamMapping.delete(reqId);
|
|
1142
|
+
this.requestResponseMap.delete(reqId);
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1100
1145
|
async send(message, options) {
|
|
1101
1146
|
let requestId = options?.relatedRequestId;
|
|
1102
|
-
if (
|
|
1147
|
+
if (isJSONRPCResultResponse(message) || isJSONRPCErrorResponse(message)) requestId = message.id;
|
|
1103
1148
|
if (requestId === void 0) {
|
|
1104
|
-
if (
|
|
1149
|
+
if (isJSONRPCResultResponse(message) || isJSONRPCErrorResponse(message)) throw new Error("Cannot send a response on a standalone SSE stream unless resuming a previous client request");
|
|
1105
1150
|
const standaloneSse = this.streamMapping.get(this.standaloneSseStreamId);
|
|
1106
1151
|
if (standaloneSse === void 0) return;
|
|
1107
1152
|
if (standaloneSse.writer && standaloneSse.encoder) {
|
|
1108
|
-
|
|
1153
|
+
let eventId;
|
|
1154
|
+
if (this.eventStore) eventId = await this.eventStore.storeEvent(this.standaloneSseStreamId, message);
|
|
1155
|
+
const data = `${eventId ? `id: ${eventId}\n` : ""}event: message\ndata: ${JSON.stringify(message)}\n\n`;
|
|
1109
1156
|
await standaloneSse.writer.write(standaloneSse.encoder.encode(data));
|
|
1110
1157
|
}
|
|
1111
1158
|
return;
|
|
@@ -1116,11 +1163,13 @@ var WorkerTransport = class {
|
|
|
1116
1163
|
if (!response) throw new Error(`No connection established for request ID: ${String(requestId)}`);
|
|
1117
1164
|
if (!this.enableJsonResponse) {
|
|
1118
1165
|
if (response.writer && response.encoder) {
|
|
1119
|
-
|
|
1166
|
+
let eventId;
|
|
1167
|
+
if (this.eventStore) eventId = await this.eventStore.storeEvent(streamId, message);
|
|
1168
|
+
const data = `${eventId ? `id: ${eventId}\n` : ""}event: message\ndata: ${JSON.stringify(message)}\n\n`;
|
|
1120
1169
|
await response.writer.write(response.encoder.encode(data));
|
|
1121
1170
|
}
|
|
1122
1171
|
}
|
|
1123
|
-
if (
|
|
1172
|
+
if (isJSONRPCResultResponse(message) || isJSONRPCErrorResponse(message)) {
|
|
1124
1173
|
this.requestResponseMap.set(requestId, message);
|
|
1125
1174
|
const relatedIds = Array.from(this.requestToStreamMapping.entries()).filter(([, sid]) => sid === streamId).map(([id]) => id);
|
|
1126
1175
|
if (relatedIds.every((id) => this.requestResponseMap.has(id))) {
|
|
@@ -1379,14 +1428,14 @@ var McpAgent = class McpAgent extends Agent {
|
|
|
1379
1428
|
}
|
|
1380
1429
|
/** Handle elicitation responses */
|
|
1381
1430
|
async _handleElicitationResponse(message) {
|
|
1382
|
-
if (
|
|
1431
|
+
if (isJSONRPCResultResponse(message) && message.result) {
|
|
1383
1432
|
const requestId = message.id?.toString();
|
|
1384
1433
|
if (!requestId || !requestId.startsWith("elicit_")) return false;
|
|
1385
1434
|
if (!await this.ctx.storage.get(`elicitation:${requestId}`)) return false;
|
|
1386
1435
|
await this.ctx.storage.put(`elicitation:response:${requestId}`, message.result);
|
|
1387
1436
|
return true;
|
|
1388
1437
|
}
|
|
1389
|
-
if (
|
|
1438
|
+
if (isJSONRPCErrorResponse(message)) {
|
|
1390
1439
|
const requestId = message.id?.toString();
|
|
1391
1440
|
if (!requestId || !requestId.startsWith("elicit_")) return false;
|
|
1392
1441
|
if (!await this.ctx.storage.get(`elicitation:${requestId}`)) return false;
|