@temporalio/strands-agents 1.22.0 → 1.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/failure-converter.js +1 -0
- package/lib/failure-converter.js.map +1 -1
- package/lib/plugin.d.ts +5 -4
- package/lib/plugin.js.map +1 -1
- package/lib/temporal-mcp-client.d.ts +5 -4
- package/lib/temporal-mcp-client.js +75 -37
- package/lib/temporal-mcp-client.js.map +1 -1
- package/lib/temporal-model.d.ts +0 -4
- package/lib/temporal-model.js +0 -8
- package/lib/temporal-model.js.map +1 -1
- package/package.json +14 -14
- package/src/failure-converter.ts +1 -0
- package/src/plugin.ts +5 -4
- package/src/temporal-mcp-client.ts +83 -38
- package/src/temporal-model.ts +1 -14
package/lib/failure-converter.js
CHANGED
|
@@ -56,5 +56,6 @@ class StrandsFailureConverter extends common_1.DefaultFailureConverter {
|
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
exports.StrandsFailureConverter = StrandsFailureConverter;
|
|
59
|
+
// ts-prune-ignore-next
|
|
59
60
|
exports.failureConverter = new StrandsFailureConverter();
|
|
60
61
|
//# sourceMappingURL=failure-converter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"failure-converter.js","sourceRoot":"","sources":["../src/failure-converter.ts"],"names":[],"mappings":";;;AAAA,6CAAwG;AACxG,+CAO4B;AAEf,QAAA,sBAAsB,GAAG,kBAAkB,CAAC;AAQzD,SAAS,gBAAgB,CAAC,GAAY;IACpC,IAAI,GAAG,KAAK,IAAI,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC1D,MAAM,SAAS,GAAG,GAA+B,CAAC;IAClD,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,SAAS,CAAC,UAAU,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IAC5F,MAAM,KAAK,GAAG,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AC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;QACtD,MAAM,KAAK,GAAG,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACnE,OAAO,2BAAkB,CAAC,MAAM,CAAC;YAC/B,OAAO,EAAE,aAAa,KAAK,EAAE;YAC7B,IAAI,EAAE,8BAAsB;YAC5B,YAAY,EAAE,IAAI;YAClB,OAAO;SACR,CAAC,CAAC;IACL,CAAC;IACD,IAAI,GAAG,YAAY,KAAK,EAAE,CAAC;QACzB,KAAK,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,oBAAoB,EAAE,CAAC;YAClD,IAAI,GAAG,YAAY,IAAI,EAAE,CAAC;gBACxB,OAAO,2BAAkB,CAAC,MAAM,CAAC;oBAC/B,OAAO,EAAE,GAAG,CAAC,OAAO;oBACpB,IAAI,EAAE,IAAI;oBACV,YAAY,EAAE,IAAI;iBACnB,CAAC,CAAC;YACL,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;;;;;GAOG;AACH,MAAa,uBAAwB,SAAQ,gCAAuB;IACzD,cAAc,CACrB,GAAY,EACZ,gBAAkC,EAClC,OAA8B;QAE9B,OAAO,KAAK,CAAC,cAAc,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE,gBAAgB,EAAE,OAAO,CAAC,CAAC;IAC5E,CAAC;CACF;AARD,0DAQC;AAED,uBAAuB;AACV,QAAA,gBAAgB,GAAqB,IAAI,uBAAuB,EAAE,CAAC"}
|
package/lib/plugin.d.ts
CHANGED
|
@@ -22,10 +22,11 @@ import { SimplePlugin } from '@temporalio/plugin';
|
|
|
22
22
|
* true` to list once at the start of the workflow instead.
|
|
23
23
|
*
|
|
24
24
|
* - `mcpConnectionIdleTimeout` — how long a worker-process MCP connection is
|
|
25
|
-
* kept open between `callTool` activities before it's disconnected. The
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
25
|
+
* kept open between `callTool` activities before it's disconnected. The window
|
|
26
|
+
* only starts once no activity is using the connection, so it never cuts off
|
|
27
|
+
* an in-flight call, however long it runs. Accepts a millisecond number or a
|
|
28
|
+
* duration string (e.g. `'5 minutes'`), like `startToCloseTimeout`. Defaults
|
|
29
|
+
* to {@link MCP_CONNECTION_IDLE_MS} (5 minutes).
|
|
29
30
|
*/
|
|
30
31
|
export interface StrandsPluginOptions {
|
|
31
32
|
models?: Record<string, () => Model>;
|
package/lib/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":";;;AACA,6CAA+D;AAE/D,sDAAiE;AAEjE,+CAAkD;AAClD,qDAAiD;AAEjD,+DAM+B;
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":";;;AACA,6CAA+D;AAE/D,sDAAiE;AAEjE,+CAAkD;AAClD,qDAAiD;AAEjD,+DAM+B;AAiC/B;;;;;;;;;;;;;;GAcG;AACH,MAAa,aAAc,SAAQ,qBAAY;IAC7C,YAAY,UAAgC,EAAE;QAC5C,IAAI,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC;QACpC,IAAI,WAA+B,CAAC;QACpC,IAAI,cAAc,KAAK,SAAS,EAAE,CAAC;YACjC,cAAc,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,IAAI,kBAAY,CAAC,EAAE,CAAC,EAAE,CAAC;YACzD,WAAW,GAAG,SAAS,CAAC;QAC1B,CAAC;aAAM,CAAC;YACN,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;YAC1C,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBACvB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B;IAChC,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAChD,MAAM,aAAa,GACjB,oJAAoJ,CAAC;IACvJ,OAAO;QACL,KAAK,CAAC,QAAQ;YACZ,MAAM,EAAE,6BAA6B,EAAE,GAAG,QAAQ,CAAC,OAAO,CAAC;YAC3D,IAAI,6BAA6B,CAAC,aAAa,EAAE,CAAC,QAAQ,EAAE,EAAE;gBAC5D,QAAQ,CAAC,OAAO,GAAG,KAAK,CAAC;YAC3B,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QACrB,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -90,10 +90,11 @@ export declare function buildListToolsActivity(server: string, factory: () => Mc
|
|
|
90
90
|
server: string;
|
|
91
91
|
}) => Promise<McpToolInfo[]>;
|
|
92
92
|
/**
|
|
93
|
-
* Default for how long an idle MCP connection is kept open before it's
|
|
94
|
-
* disconnected. The
|
|
95
|
-
* callTool}
|
|
96
|
-
*
|
|
93
|
+
* Default for how long an *idle* MCP connection is kept open before it's
|
|
94
|
+
* disconnected. The window only starts once no {@link callToolActivityName |
|
|
95
|
+
* callTool} or {@link listToolsActivityName | listTools} activity is using the
|
|
96
|
+
* connection, so a call that runs longer than this is never cut off. Override
|
|
97
|
+
* per worker via `StrandsPlugin({ mcpConnectionIdleTimeout })`.
|
|
97
98
|
*/
|
|
98
99
|
export declare const MCP_CONNECTION_IDLE_MS: number;
|
|
99
100
|
export declare function _evictConnection(server: string): Promise<void>;
|
|
@@ -127,8 +127,9 @@ function callToolActivityName(server) {
|
|
|
127
127
|
function buildListToolsActivity(server, factory, idleMs = exports.MCP_CONNECTION_IDLE_MS) {
|
|
128
128
|
const name = listToolsActivityName(server);
|
|
129
129
|
const fn = async (_input) => {
|
|
130
|
-
const
|
|
130
|
+
const record = acquireConnection(server, factory);
|
|
131
131
|
try {
|
|
132
|
+
const client = await record.client;
|
|
132
133
|
const tools = await client.listTools();
|
|
133
134
|
return tools.map((t) => ({
|
|
134
135
|
name: t.name,
|
|
@@ -142,66 +143,97 @@ function buildListToolsActivity(server, factory, idleMs = exports.MCP_CONNECTION
|
|
|
142
143
|
await _evictConnection(server);
|
|
143
144
|
throw err;
|
|
144
145
|
}
|
|
146
|
+
finally {
|
|
147
|
+
// No more in-flight call on this connection; let idle eviction resume
|
|
148
|
+
// (no-op if the connection was just evicted above).
|
|
149
|
+
releaseConnection(server, record, idleMs);
|
|
150
|
+
}
|
|
145
151
|
};
|
|
146
152
|
Object.defineProperty(fn, 'name', { value: name });
|
|
147
153
|
return fn;
|
|
148
154
|
}
|
|
149
155
|
/**
|
|
150
|
-
* Default for how long an idle MCP connection is kept open before it's
|
|
151
|
-
* disconnected. The
|
|
152
|
-
* callTool}
|
|
153
|
-
*
|
|
156
|
+
* Default for how long an *idle* MCP connection is kept open before it's
|
|
157
|
+
* disconnected. The window only starts once no {@link callToolActivityName |
|
|
158
|
+
* callTool} or {@link listToolsActivityName | listTools} activity is using the
|
|
159
|
+
* connection, so a call that runs longer than this is never cut off. Override
|
|
160
|
+
* per worker via `StrandsPlugin({ mcpConnectionIdleTimeout })`.
|
|
154
161
|
*/
|
|
155
162
|
exports.MCP_CONNECTION_IDLE_MS = 5 * 60 * 1000;
|
|
156
|
-
// Worker-process
|
|
163
|
+
// Worker-process cache, keyed by server name. Activities run in the worker's
|
|
157
164
|
// Node process, so this module state is shared across every activity invocation on the worker.
|
|
158
165
|
const CONNECTIONS = new Map();
|
|
159
|
-
const IDLE_TIMERS = new Map();
|
|
160
|
-
function resetIdleTimer(server, idleMs) {
|
|
161
|
-
const existing = IDLE_TIMERS.get(server);
|
|
162
|
-
if (existing !== undefined)
|
|
163
|
-
clearTimeout(existing);
|
|
164
|
-
const timer = setTimeout(() => void _evictConnection(server), idleMs);
|
|
165
|
-
// Don't let an idle MCP connection keep the worker process alive.
|
|
166
|
-
timer.unref?.();
|
|
167
|
-
IDLE_TIMERS.set(server, timer);
|
|
168
|
-
}
|
|
169
166
|
async function _evictConnection(server) {
|
|
170
|
-
const
|
|
171
|
-
if (
|
|
172
|
-
clearTimeout(timer);
|
|
173
|
-
IDLE_TIMERS.delete(server);
|
|
174
|
-
}
|
|
175
|
-
const entry = CONNECTIONS.get(server);
|
|
176
|
-
if (entry === undefined)
|
|
167
|
+
const record = CONNECTIONS.get(server);
|
|
168
|
+
if (record === undefined)
|
|
177
169
|
return;
|
|
170
|
+
// Delete before awaiting: a call arriving during `disconnect` must open a
|
|
171
|
+
// fresh connection rather than join the one being torn down.
|
|
178
172
|
CONNECTIONS.delete(server);
|
|
173
|
+
if (record.idleTimer !== undefined) {
|
|
174
|
+
clearTimeout(record.idleTimer);
|
|
175
|
+
record.idleTimer = undefined;
|
|
176
|
+
}
|
|
179
177
|
try {
|
|
180
|
-
const client = await
|
|
178
|
+
const client = await record.client;
|
|
181
179
|
await client.disconnect();
|
|
182
180
|
}
|
|
183
181
|
catch {
|
|
184
182
|
// Best-effort; the session may already be broken.
|
|
185
183
|
}
|
|
186
184
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
185
|
+
/**
|
|
186
|
+
* Return the cached connection for `server`, opening one lazily if needed, and
|
|
187
|
+
* mark a call in flight so idle eviction can't disconnect underneath it.
|
|
188
|
+
* Concurrent first-callers dedupe onto a single connect handshake by awaiting
|
|
189
|
+
* the same record. Every caller must {@link releaseConnection} once its call
|
|
190
|
+
* completes.
|
|
191
|
+
*/
|
|
192
|
+
function acquireConnection(server, factory) {
|
|
193
|
+
let record = CONNECTIONS.get(server);
|
|
194
|
+
if (record === undefined) {
|
|
195
|
+
const entry = {
|
|
196
|
+
inflight: 0,
|
|
197
|
+
client: (async () => {
|
|
198
|
+
const client = factory();
|
|
199
|
+
await client.connect();
|
|
200
|
+
return client;
|
|
201
|
+
})(),
|
|
202
|
+
};
|
|
196
203
|
// If the connect handshake fails, drop the rejected promise so the next
|
|
197
204
|
// call retries instead of caching the failure forever.
|
|
198
|
-
entry.catch(() => {
|
|
205
|
+
entry.client.catch(() => {
|
|
199
206
|
if (CONNECTIONS.get(server) === entry)
|
|
200
207
|
CONNECTIONS.delete(server);
|
|
201
208
|
});
|
|
209
|
+
CONNECTIONS.set(server, entry);
|
|
210
|
+
record = entry;
|
|
202
211
|
}
|
|
203
|
-
|
|
204
|
-
|
|
212
|
+
record.inflight += 1;
|
|
213
|
+
if (record.idleTimer !== undefined) {
|
|
214
|
+
clearTimeout(record.idleTimer);
|
|
215
|
+
record.idleTimer = undefined;
|
|
216
|
+
}
|
|
217
|
+
return record;
|
|
218
|
+
}
|
|
219
|
+
/** Mark a call done; arm idle eviction once no calls remain in flight. */
|
|
220
|
+
function releaseConnection(server, record, idleMs) {
|
|
221
|
+
record.inflight -= 1;
|
|
222
|
+
// Only the record still cached under this server arms a timer; a record
|
|
223
|
+
// already evicted must not schedule one, or it could later evict a
|
|
224
|
+
// different, healthy connection for the same server.
|
|
225
|
+
if (record.inflight > 0 || CONNECTIONS.get(server) !== record)
|
|
226
|
+
return;
|
|
227
|
+
const timer = setTimeout(() => {
|
|
228
|
+
// `acquireConnection` clears this timer synchronously, so a pending timer
|
|
229
|
+
// implies the connection is still idle; re-checked to keep that invariant
|
|
230
|
+
// local.
|
|
231
|
+
if (record.inflight === 0)
|
|
232
|
+
void _evictConnection(server);
|
|
233
|
+
}, idleMs);
|
|
234
|
+
// Don't let an idle MCP connection keep the worker process alive.
|
|
235
|
+
timer.unref?.();
|
|
236
|
+
record.idleTimer = timer;
|
|
205
237
|
}
|
|
206
238
|
/**
|
|
207
239
|
* Builds the per-server `{server}-callTool` activity. Reuses a worker-process
|
|
@@ -216,13 +248,14 @@ function buildCallToolActivity(server, factory, idleMs = exports.MCP_CONNECTION_
|
|
|
216
248
|
activity_1.Context.current().log.debug(`Calling MCP tool ${input.toolName} on server ${server}`, {
|
|
217
249
|
activityId: (0, activity_1.activityInfo)().activityId,
|
|
218
250
|
});
|
|
219
|
-
const
|
|
251
|
+
const record = acquireConnection(server, factory);
|
|
220
252
|
// Dispatch by name without re-listing tools on the connection: the agent
|
|
221
253
|
// already has the schema from `listTools`, and `McpClient.callTool` only
|
|
222
254
|
// reads `tool.name` to build the request. A minimal `{ name }` matches the
|
|
223
255
|
// by-name dispatch the Python SDK does via `session.call_tool`.
|
|
224
256
|
const tool = { name: input.toolName };
|
|
225
257
|
try {
|
|
258
|
+
const client = await record.client;
|
|
226
259
|
return await client.callTool(tool, input.args);
|
|
227
260
|
}
|
|
228
261
|
catch (err) {
|
|
@@ -230,6 +263,11 @@ function buildCallToolActivity(server, factory, idleMs = exports.MCP_CONNECTION_
|
|
|
230
263
|
await _evictConnection(server);
|
|
231
264
|
throw err;
|
|
232
265
|
}
|
|
266
|
+
finally {
|
|
267
|
+
// No more in-flight call on this connection; let idle eviction resume
|
|
268
|
+
// (no-op if the connection was just evicted above).
|
|
269
|
+
releaseConnection(server, record, idleMs);
|
|
270
|
+
}
|
|
233
271
|
};
|
|
234
272
|
Object.defineProperty(fn, 'name', { value: name });
|
|
235
273
|
return fn;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"temporal-mcp-client.js","sourceRoot":"","sources":["../src/temporal-mcp-client.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA8IA,sDAEC;AAED,oDAEC;AAQD,
|
|
1
|
+
{"version":3,"file":"temporal-mcp-client.js","sourceRoot":"","sources":["../src/temporal-mcp-client.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA8IA,sDAEC;AAED,oDAEC;AAQD,wDA6BC;AAyBD,4CAgBC;AA6DD,sDA+BC;AA9TD,6CAAgD;AAGhD,+DAAiD;AAEjD,mDAAgF;AAehF,MAAM,cAAc,GAAc;IAChC,KAAK,CAAC,KAAK;QACT,kEAAkE;IACpE,CAAC;IACD,KAAK,CAAC,IAAI;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,IAAI,CAAC,CAAC;QACjD,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,kEAAkE;YAClE,MAAM,gBAAgB,CAAC,MAAM,CAAC,CAAC;YAC/B,MAAM,GAAG,CAAC;QACZ,CAAC;gBAAS,CAAC;YACT,sEAAsE;YACtE,oDAAoD;YACpD,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;QAC5C,CAAC;IACH,CAAC,CAAC;IACF,MAAM,CAAC,cAAc,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IACnD,OAAO,EAAE,CAAC;AACZ,CAAC"}
|
package/lib/temporal-model.d.ts
CHANGED
|
@@ -2,7 +2,6 @@ import { Model } from '@strands-agents/sdk';
|
|
|
2
2
|
import type { BaseModelConfig, Message, ModelStreamEvent, StreamOptions } from '@strands-agents/sdk';
|
|
3
3
|
import type { ActivityOptions } from '@temporalio/workflow';
|
|
4
4
|
import type { Duration } from '@temporalio/common/lib/time';
|
|
5
|
-
import type { ModelActivity } from './model-activity';
|
|
6
5
|
/**
|
|
7
6
|
* Options for {@link TemporalModel}.
|
|
8
7
|
*
|
|
@@ -36,6 +35,3 @@ export declare class TemporalModel extends Model<BaseModelConfig> {
|
|
|
36
35
|
getConfig(): BaseModelConfig;
|
|
37
36
|
stream(messages: Message[], options?: StreamOptions): AsyncIterable<ModelStreamEvent>;
|
|
38
37
|
}
|
|
39
|
-
/** Type tag used only to forward the `ModelActivity` type from the activity side. */
|
|
40
|
-
export type _ModelActivityRef = ModelActivity;
|
|
41
|
-
export declare function rejectStructuredOutput(): never;
|
package/lib/temporal-model.js
CHANGED
|
@@ -24,13 +24,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.TemporalModel = void 0;
|
|
27
|
-
exports.rejectStructuredOutput = rejectStructuredOutput;
|
|
28
27
|
const sdk_1 = require("@strands-agents/sdk");
|
|
29
28
|
const workflow = __importStar(require("@temporalio/workflow"));
|
|
30
|
-
const common_1 = require("@temporalio/common");
|
|
31
|
-
const STRUCTURED_OUTPUT_DISABLED = 'TemporalModel.structuredOutput is not supported. Use ' +
|
|
32
|
-
'TemporalAgent({ structuredOutputSchema: ... }) which routes structured ' +
|
|
33
|
-
'output through stream() via the structured-output tool.';
|
|
34
29
|
/**
|
|
35
30
|
* A Strands {@link Model} whose {@link Model.stream} runs as a Temporal
|
|
36
31
|
* activity. Constructing a `TemporalModel` does no I/O, so instances are
|
|
@@ -82,7 +77,4 @@ class TemporalModel extends sdk_1.Model {
|
|
|
82
77
|
}
|
|
83
78
|
}
|
|
84
79
|
exports.TemporalModel = TemporalModel;
|
|
85
|
-
function rejectStructuredOutput() {
|
|
86
|
-
throw common_1.ApplicationFailure.nonRetryable(STRUCTURED_OUTPUT_DISABLED);
|
|
87
|
-
}
|
|
88
80
|
//# sourceMappingURL=temporal-model.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"temporal-model.js","sourceRoot":"","sources":["../src/temporal-model.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"temporal-model.js","sourceRoot":"","sources":["../src/temporal-model.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6CAA4C;AAE5C,+DAAiD;AAoBjD;;;;;;;;GAQG;AACH,MAAa,aAAc,SAAQ,WAAsB;IACtC,SAAS,CAAU;IACnB,eAAe,CAAkB;IACjC,cAAc,CAAU;IACxB,sBAAsB,CAAW;IAElD,YAAY,UAAgC,EAAE;QAC5C,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;QACnC,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC,eAAe,IAAI,EAAE,CAAC;QACrD,IAAI,CAAC,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC;QAC7C,IAAI,CAAC,sBAAsB,GAAG,OAAO,CAAC,sBAAsB,IAAI,kBAAkB,CAAC;IACrF,CAAC;IAEQ,YAAY,CAAC,OAAwB;QAC5C,8EAA8E;IAChF,CAAC;IAEQ,SAAS;QAChB,OAAO,EAAE,CAAC;IACZ,CAAC;IAEQ,KAAK,CAAC,CAAC,MAAM,CAAC,QAAmB,EAAE,OAAuB;QACjE,MAAM,iBAAiB,GAAG,QAAQ,CAAC,eAAe,CAG/C;YACD,mBAAmB,EAAE,YAAY;YACjC,GAAG,IAAI,CAAC,eAAe;SACxB,CAAC,CAAC;QAEH,MAAM,MAAM,GAAG,IAAI,CAAC,cAAc;YAChC,CAAC,CAAC,MAAM,iBAAiB,CAAC,oBAAoB,CAAC;gBAC3C,SAAS,EAAE,IAAI,CAAC,SAAS;gBACzB,QAAQ;gBACR,OAAO;gBACP,cAAc,EAAE,IAAI,CAAC,cAAc;gBACnC,sBAAsB,EAAE,IAAI,CAAC,sBAAsB;aACpD,CAAC;YACJ,CAAC,CAAC,MAAM,iBAAiB,CAAC,WAAW,CAAC;gBAClC,SAAS,EAAE,IAAI,CAAC,SAAS;gBACzB,QAAQ;gBACR,OAAO;aACR,CAAC,CAAC;QAEP,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC3B,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF;AAjDD,sCAiDC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@temporalio/strands-agents",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.24.0",
|
|
4
4
|
"description": "Temporal Strands Agents integration package",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -21,14 +21,14 @@
|
|
|
21
21
|
"workerThreads": false
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
+
"@temporalio/activity": "1.24.0",
|
|
25
|
+
"@temporalio/common": "1.24.0",
|
|
26
|
+
"@temporalio/plugin": "1.24.0",
|
|
27
|
+
"@temporalio/workflow": "1.24.0",
|
|
28
|
+
"@temporalio/workflow-streams": "1.24.0",
|
|
24
29
|
"@ungap/structured-clone": "^1.3.1",
|
|
25
30
|
"headers-polyfill": "^4.0.3",
|
|
26
|
-
"web-streams-polyfill": "^4.2.0"
|
|
27
|
-
"@temporalio/activity": "1.22.0",
|
|
28
|
-
"@temporalio/common": "1.22.0",
|
|
29
|
-
"@temporalio/workflow": "1.22.0",
|
|
30
|
-
"@temporalio/workflow-streams": "1.22.0",
|
|
31
|
-
"@temporalio/plugin": "1.22.0"
|
|
31
|
+
"web-streams-polyfill": "^4.2.0"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
34
|
"@strands-agents/sdk": "^1.3.0",
|
|
@@ -37,16 +37,16 @@
|
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@modelcontextprotocol/sdk": "^1.25.2",
|
|
39
39
|
"@strands-agents/sdk": "^1.3.0",
|
|
40
|
+
"@temporalio/client": "1.24.0",
|
|
41
|
+
"@temporalio/proto": "1.24.0",
|
|
42
|
+
"@temporalio/test-helpers": "1.24.0",
|
|
43
|
+
"@temporalio/testing": "1.24.0",
|
|
44
|
+
"@temporalio/worker": "1.24.0",
|
|
40
45
|
"ava": "^5.3.1",
|
|
41
|
-
"zod": "^4.1.12"
|
|
42
|
-
"@temporalio/test-helpers": "1.22.0",
|
|
43
|
-
"@temporalio/client": "1.22.0",
|
|
44
|
-
"@temporalio/proto": "1.22.0",
|
|
45
|
-
"@temporalio/testing": "1.22.0",
|
|
46
|
-
"@temporalio/worker": "1.22.0"
|
|
46
|
+
"zod": "^4.1.12"
|
|
47
47
|
},
|
|
48
48
|
"engines": {
|
|
49
|
-
"node": ">= 20.
|
|
49
|
+
"node": ">= 20.3.0"
|
|
50
50
|
},
|
|
51
51
|
"bugs": {
|
|
52
52
|
"url": "https://github.com/temporalio/sdk-typescript/issues"
|
package/src/failure-converter.ts
CHANGED
package/src/plugin.ts
CHANGED
|
@@ -33,10 +33,11 @@ import {
|
|
|
33
33
|
* true` to list once at the start of the workflow instead.
|
|
34
34
|
*
|
|
35
35
|
* - `mcpConnectionIdleTimeout` — how long a worker-process MCP connection is
|
|
36
|
-
* kept open between `callTool` activities before it's disconnected. The
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
36
|
+
* kept open between `callTool` activities before it's disconnected. The window
|
|
37
|
+
* only starts once no activity is using the connection, so it never cuts off
|
|
38
|
+
* an in-flight call, however long it runs. Accepts a millisecond number or a
|
|
39
|
+
* duration string (e.g. `'5 minutes'`), like `startToCloseTimeout`. Defaults
|
|
40
|
+
* to {@link MCP_CONNECTION_IDLE_MS} (5 minutes).
|
|
40
41
|
*/
|
|
41
42
|
export interface StrandsPluginOptions {
|
|
42
43
|
models?: Record<string, () => Model>;
|
|
@@ -161,8 +161,9 @@ export function buildListToolsActivity(
|
|
|
161
161
|
): (input: { server: string }) => Promise<McpToolInfo[]> {
|
|
162
162
|
const name = listToolsActivityName(server);
|
|
163
163
|
const fn = async (_input: { server: string }): Promise<McpToolInfo[]> => {
|
|
164
|
-
const
|
|
164
|
+
const record = acquireConnection(server, factory);
|
|
165
165
|
try {
|
|
166
|
+
const client = await record.client;
|
|
166
167
|
const tools = await client.listTools();
|
|
167
168
|
return tools.map((t) => ({
|
|
168
169
|
name: t.name,
|
|
@@ -174,6 +175,10 @@ export function buildListToolsActivity(
|
|
|
174
175
|
// the next call reconnects to the current deployment.
|
|
175
176
|
await _evictConnection(server);
|
|
176
177
|
throw err;
|
|
178
|
+
} finally {
|
|
179
|
+
// No more in-flight call on this connection; let idle eviction resume
|
|
180
|
+
// (no-op if the connection was just evicted above).
|
|
181
|
+
releaseConnection(server, record, idleMs);
|
|
177
182
|
}
|
|
178
183
|
};
|
|
179
184
|
Object.defineProperty(fn, 'name', { value: name });
|
|
@@ -181,61 +186,96 @@ export function buildListToolsActivity(
|
|
|
181
186
|
}
|
|
182
187
|
|
|
183
188
|
/**
|
|
184
|
-
* Default for how long an idle MCP connection is kept open before it's
|
|
185
|
-
* disconnected. The
|
|
186
|
-
* callTool}
|
|
187
|
-
*
|
|
189
|
+
* Default for how long an *idle* MCP connection is kept open before it's
|
|
190
|
+
* disconnected. The window only starts once no {@link callToolActivityName |
|
|
191
|
+
* callTool} or {@link listToolsActivityName | listTools} activity is using the
|
|
192
|
+
* connection, so a call that runs longer than this is never cut off. Override
|
|
193
|
+
* per worker via `StrandsPlugin({ mcpConnectionIdleTimeout })`.
|
|
188
194
|
*/
|
|
189
195
|
export const MCP_CONNECTION_IDLE_MS = 5 * 60 * 1000;
|
|
190
196
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
const timer = setTimeout(() => void _evictConnection(server), idleMs);
|
|
200
|
-
// Don't let an idle MCP connection keep the worker process alive.
|
|
201
|
-
timer.unref?.();
|
|
202
|
-
IDLE_TIMERS.set(server, timer);
|
|
197
|
+
/** A worker-process MCP connection plus the state governing its idle eviction. */
|
|
198
|
+
interface ConnectionRecord {
|
|
199
|
+
/** Resolves to the connected client, or rejects if the handshake failed. */
|
|
200
|
+
client: Promise<McpClient>;
|
|
201
|
+
/** Number of activities currently using this connection. */
|
|
202
|
+
inflight: number;
|
|
203
|
+
/** Armed only while `inflight` is 0. */
|
|
204
|
+
idleTimer?: NodeJS.Timeout;
|
|
203
205
|
}
|
|
204
206
|
|
|
207
|
+
// Worker-process cache, keyed by server name. Activities run in the worker's
|
|
208
|
+
// Node process, so this module state is shared across every activity invocation on the worker.
|
|
209
|
+
const CONNECTIONS: Map<string, ConnectionRecord> = new Map();
|
|
210
|
+
|
|
205
211
|
export async function _evictConnection(server: string): Promise<void> {
|
|
206
|
-
const
|
|
207
|
-
if (
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
}
|
|
211
|
-
const entry = CONNECTIONS.get(server);
|
|
212
|
-
if (entry === undefined) return;
|
|
212
|
+
const record = CONNECTIONS.get(server);
|
|
213
|
+
if (record === undefined) return;
|
|
214
|
+
// Delete before awaiting: a call arriving during `disconnect` must open a
|
|
215
|
+
// fresh connection rather than join the one being torn down.
|
|
213
216
|
CONNECTIONS.delete(server);
|
|
217
|
+
if (record.idleTimer !== undefined) {
|
|
218
|
+
clearTimeout(record.idleTimer);
|
|
219
|
+
record.idleTimer = undefined;
|
|
220
|
+
}
|
|
214
221
|
try {
|
|
215
|
-
const client = await
|
|
222
|
+
const client = await record.client;
|
|
216
223
|
await client.disconnect();
|
|
217
224
|
} catch {
|
|
218
225
|
// Best-effort; the session may already be broken.
|
|
219
226
|
}
|
|
220
227
|
}
|
|
221
228
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
229
|
+
/**
|
|
230
|
+
* Return the cached connection for `server`, opening one lazily if needed, and
|
|
231
|
+
* mark a call in flight so idle eviction can't disconnect underneath it.
|
|
232
|
+
* Concurrent first-callers dedupe onto a single connect handshake by awaiting
|
|
233
|
+
* the same record. Every caller must {@link releaseConnection} once its call
|
|
234
|
+
* completes.
|
|
235
|
+
*/
|
|
236
|
+
function acquireConnection(server: string, factory: () => McpClient): ConnectionRecord {
|
|
237
|
+
let record = CONNECTIONS.get(server);
|
|
238
|
+
if (record === undefined) {
|
|
239
|
+
const entry: ConnectionRecord = {
|
|
240
|
+
inflight: 0,
|
|
241
|
+
client: (async () => {
|
|
242
|
+
const client = factory();
|
|
243
|
+
await client.connect();
|
|
244
|
+
return client;
|
|
245
|
+
})(),
|
|
246
|
+
};
|
|
231
247
|
// If the connect handshake fails, drop the rejected promise so the next
|
|
232
248
|
// call retries instead of caching the failure forever.
|
|
233
|
-
entry.catch(() => {
|
|
249
|
+
entry.client.catch(() => {
|
|
234
250
|
if (CONNECTIONS.get(server) === entry) CONNECTIONS.delete(server);
|
|
235
251
|
});
|
|
252
|
+
CONNECTIONS.set(server, entry);
|
|
253
|
+
record = entry;
|
|
254
|
+
}
|
|
255
|
+
record.inflight += 1;
|
|
256
|
+
if (record.idleTimer !== undefined) {
|
|
257
|
+
clearTimeout(record.idleTimer);
|
|
258
|
+
record.idleTimer = undefined;
|
|
236
259
|
}
|
|
237
|
-
|
|
238
|
-
|
|
260
|
+
return record;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** Mark a call done; arm idle eviction once no calls remain in flight. */
|
|
264
|
+
function releaseConnection(server: string, record: ConnectionRecord, idleMs: number): void {
|
|
265
|
+
record.inflight -= 1;
|
|
266
|
+
// Only the record still cached under this server arms a timer; a record
|
|
267
|
+
// already evicted must not schedule one, or it could later evict a
|
|
268
|
+
// different, healthy connection for the same server.
|
|
269
|
+
if (record.inflight > 0 || CONNECTIONS.get(server) !== record) return;
|
|
270
|
+
const timer = setTimeout(() => {
|
|
271
|
+
// `acquireConnection` clears this timer synchronously, so a pending timer
|
|
272
|
+
// implies the connection is still idle; re-checked to keep that invariant
|
|
273
|
+
// local.
|
|
274
|
+
if (record.inflight === 0) void _evictConnection(server);
|
|
275
|
+
}, idleMs);
|
|
276
|
+
// Don't let an idle MCP connection keep the worker process alive.
|
|
277
|
+
timer.unref?.();
|
|
278
|
+
record.idleTimer = timer;
|
|
239
279
|
}
|
|
240
280
|
|
|
241
281
|
/**
|
|
@@ -255,18 +295,23 @@ export function buildCallToolActivity(
|
|
|
255
295
|
ActivityContext.current().log.debug(`Calling MCP tool ${input.toolName} on server ${server}`, {
|
|
256
296
|
activityId: activityInfo().activityId,
|
|
257
297
|
});
|
|
258
|
-
const
|
|
298
|
+
const record = acquireConnection(server, factory);
|
|
259
299
|
// Dispatch by name without re-listing tools on the connection: the agent
|
|
260
300
|
// already has the schema from `listTools`, and `McpClient.callTool` only
|
|
261
301
|
// reads `tool.name` to build the request. A minimal `{ name }` matches the
|
|
262
302
|
// by-name dispatch the Python SDK does via `session.call_tool`.
|
|
263
303
|
const tool = { name: input.toolName } as unknown as Parameters<McpClient['callTool']>[0];
|
|
264
304
|
try {
|
|
305
|
+
const client = await record.client;
|
|
265
306
|
return await client.callTool(tool, input.args);
|
|
266
307
|
} catch (err) {
|
|
267
308
|
// The session may be broken; drop it so the next call reconnects.
|
|
268
309
|
await _evictConnection(server);
|
|
269
310
|
throw err;
|
|
311
|
+
} finally {
|
|
312
|
+
// No more in-flight call on this connection; let idle eviction resume
|
|
313
|
+
// (no-op if the connection was just evicted above).
|
|
314
|
+
releaseConnection(server, record, idleMs);
|
|
270
315
|
}
|
|
271
316
|
};
|
|
272
317
|
Object.defineProperty(fn, 'name', { value: name });
|
package/src/temporal-model.ts
CHANGED
|
@@ -2,14 +2,8 @@ import { Model } from '@strands-agents/sdk';
|
|
|
2
2
|
import type { BaseModelConfig, Message, ModelStreamEvent, StreamOptions } from '@strands-agents/sdk';
|
|
3
3
|
import * as workflow from '@temporalio/workflow';
|
|
4
4
|
import type { ActivityOptions } from '@temporalio/workflow';
|
|
5
|
-
import { ApplicationFailure } from '@temporalio/common';
|
|
6
5
|
import type { Duration } from '@temporalio/common/lib/time';
|
|
7
|
-
import type { InvokeModelInput, InvokeModelStreamingInput
|
|
8
|
-
|
|
9
|
-
const STRUCTURED_OUTPUT_DISABLED =
|
|
10
|
-
'TemporalModel.structuredOutput is not supported. Use ' +
|
|
11
|
-
'TemporalAgent({ structuredOutputSchema: ... }) which routes structured ' +
|
|
12
|
-
'output through stream() via the structured-output tool.';
|
|
6
|
+
import type { InvokeModelInput, InvokeModelStreamingInput } from './model-activity';
|
|
13
7
|
|
|
14
8
|
/**
|
|
15
9
|
* Options for {@link TemporalModel}.
|
|
@@ -85,10 +79,3 @@ export class TemporalModel extends Model<BaseModelConfig> {
|
|
|
85
79
|
}
|
|
86
80
|
}
|
|
87
81
|
}
|
|
88
|
-
|
|
89
|
-
/** Type tag used only to forward the `ModelActivity` type from the activity side. */
|
|
90
|
-
export type _ModelActivityRef = ModelActivity;
|
|
91
|
-
|
|
92
|
-
export function rejectStructuredOutput(): never {
|
|
93
|
-
throw ApplicationFailure.nonRetryable(STRUCTURED_OUTPUT_DISABLED);
|
|
94
|
-
}
|