agents 0.21.0 → 0.22.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/README.md +1 -0
- package/dist/{agent-tool-types-CzGGB-20.d.ts → agent-routing-CnkaHb-v.d.ts} +2921 -3057
- package/dist/agent-routing.d.ts +14 -0
- package/dist/agent-routing.js +187 -0
- package/dist/agent-routing.js.map +1 -0
- package/dist/agent-tool-types.d.ts +26 -26
- package/dist/{agent-tools-zR2d5uij.d.ts → agent-tools-C0XJqWzB.d.ts} +7 -7
- package/dist/agent-tools.d.ts +21 -21
- package/dist/agent-tools.js +2 -1
- package/dist/agent-tools.js.map +1 -1
- package/dist/browser/ai.js +2 -1
- package/dist/browser/ai.js.map +1 -1
- package/dist/capability-BjSKYpzg.js +42 -0
- package/dist/capability-BjSKYpzg.js.map +1 -0
- package/dist/capability-runner-CvHGZqUu.d.ts +150 -0
- package/dist/chat/index.d.ts +22 -15
- package/dist/chat/index.js +4 -4
- package/dist/chat/index.js.map +1 -1
- package/dist/chat/react.d.ts +14 -1
- package/dist/chat/react.js +82 -52
- package/dist/chat/react.js.map +1 -1
- package/dist/chat/transport.js +1 -1
- package/dist/chat-sdk/index.d.ts +4 -4
- package/dist/chat-sdk/index.js +1 -1
- package/dist/{client-zqKcsyFa.js → client-jagG8a9_.js} +129 -37
- package/dist/client-jagG8a9_.js.map +1 -0
- package/dist/client.d.ts +1 -1
- package/dist/client.js +1 -1
- package/dist/{cloudflare-BduZwmYK.js → cloudflare-Dzvc7V2N.js} +10 -3
- package/dist/{cloudflare-BduZwmYK.js.map → cloudflare-Dzvc7V2N.js.map} +1 -1
- package/dist/current-agent-CuMErtly.d.ts +283 -0
- package/dist/current-agent-DhoDkSnH.js +51 -0
- package/dist/current-agent-DhoDkSnH.js.map +1 -0
- package/dist/diagnostics-CaBjfz4J.js +41 -0
- package/dist/diagnostics-CaBjfz4J.js.map +1 -0
- package/dist/{do-oauth-client-provider-VTZj2VtM.d.ts → do-oauth-client-provider-Tmf1vgKz.d.ts} +2 -2
- package/dist/durable-object-lifecycle-D6nNQJJd.js +862 -0
- package/dist/durable-object-lifecycle-D6nNQJJd.js.map +1 -0
- package/dist/{email-CL27preh.d.ts → email-7TatiTnl.d.ts} +38 -9
- package/dist/email-send.d.ts +15 -0
- package/dist/email-send.js +32 -0
- package/dist/email-send.js.map +1 -0
- package/dist/email.d.ts +14 -10
- package/dist/email.js.map +1 -1
- package/dist/experimental/memory/session/index.d.ts +3 -2
- package/dist/experimental/memory/session/index.js +15 -10
- package/dist/experimental/memory/session/index.js.map +1 -1
- package/dist/{handler-stateless-C_bo-Ytq.d.ts → handler-stateless-DxYpJ_XF.d.ts} +3 -3
- package/dist/{handler-stateless-CIkKPETH.js → handler-stateless-VvrWSAVA.js} +5 -5
- package/dist/handler-stateless-VvrWSAVA.js.map +1 -0
- package/dist/{index-BRnybD6X.d.ts → index-DcSAZKsB.d.ts} +19 -31
- package/dist/index.d.ts +98 -95
- package/dist/index.js +10 -7234
- package/dist/internal_context-BlxFEWfn.d.ts +19 -0
- package/dist/internal_context.d.ts +10 -4
- package/dist/internal_context.js +1 -10
- package/dist/{client-invoker-BNSZxAkv.d.ts → invoker-CG0_p_Wq.d.ts} +2 -2
- package/dist/{client-invoker-VNZ7X0nn.js → invoker-CHMnoxIA.js} +2 -2
- package/dist/invoker-CHMnoxIA.js.map +1 -0
- package/dist/lifecycle/index.d.ts +48 -0
- package/dist/lifecycle/index.js +4 -0
- package/dist/mcp/{do-oauth-client-provider.d.ts → client/do-oauth-client-provider.d.ts} +1 -1
- package/dist/mcp/{do-oauth-client-provider.js → client/do-oauth-client-provider.js} +1 -1
- package/dist/mcp/client/do-oauth-client-provider.js.map +1 -0
- package/dist/mcp/client/index.d.ts +42 -0
- package/dist/mcp/{client.js → client/index.js} +1 -1
- package/dist/mcp/{x402.d.ts → client/x402.d.ts} +2 -2
- package/dist/mcp/{x402.js → client/x402.js} +2 -2
- package/dist/mcp/client/x402.js.map +1 -0
- package/dist/mcp/index.d.ts +36 -36
- package/dist/mcp/index.js +14 -16
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/{server.d.ts → server/index.d.ts} +1 -1
- package/dist/mcp/{server.js → server/index.js} +1 -1
- package/dist/observability/ai/index.js +50 -35
- package/dist/observability/ai/index.js.map +1 -1
- package/dist/observability/index.d.ts +4 -4
- package/dist/observability/index.js +3 -50
- package/dist/observability/index.js.map +1 -1
- package/dist/{protocol-Dqc2MQxo.js → protocol-B0nh6KNf.js} +19 -21
- package/dist/protocol-B0nh6KNf.js.map +1 -0
- package/dist/react.d.ts +4 -4
- package/dist/react.js +1 -1
- package/dist/schedule.d.ts +25 -94
- package/dist/schedule.js +1 -98
- package/dist/schedule.js.map +1 -1
- package/dist/scheduler-CR9RHGos.js +857 -0
- package/dist/scheduler-CR9RHGos.js.map +1 -0
- package/dist/scheduler-DQoTGoAW.d.ts +224 -0
- package/dist/schedules/index.d.ts +22 -0
- package/dist/schedules/index.js +2 -0
- package/dist/schedules/parser.d.ts +79 -0
- package/dist/schedules/parser.js +103 -0
- package/dist/schedules/parser.js.map +1 -0
- package/dist/serializable.d.ts +1 -1
- package/dist/sql-error-CPY-GXyI.d.ts +12 -0
- package/dist/sql-error.d.ts +2 -0
- package/dist/sql-error.js +16 -0
- package/dist/sql-error.js.map +1 -0
- package/dist/src-5W6JNKVb.js +6613 -0
- package/dist/src-5W6JNKVb.js.map +1 -0
- package/dist/sub-routing.d.ts +12 -12
- package/dist/workflow-types.d.ts +25 -25
- package/dist/workflows.d.ts +22 -22
- package/dist/workflows.js +2 -1
- package/dist/workflows.js.map +1 -1
- package/dist/{ws-chat-transport-CIoOBbO7.js → ws-chat-transport-rWwta645.js} +152 -15
- package/dist/ws-chat-transport-rWwta645.js.map +1 -0
- package/docs/agent-class.md +28 -86
- package/docs/chat-agents.md +13 -12
- package/docs/durable-execution.md +1 -1
- package/docs/http-websockets.md +1 -11
- package/docs/human-in-the-loop.md +1 -1
- package/docs/index.md +1 -0
- package/docs/lifecycle.md +323 -0
- package/docs/long-running-agents.md +2 -4
- package/docs/mcp-client.md +55 -0
- package/docs/observability.md +11 -11
- package/docs/resumable-streaming.md +1 -1
- package/docs/scheduling.md +175 -15
- package/docs/server-driven-messages.md +1 -1
- package/docs/sub-agents.md +1 -1
- package/package.json +30 -21
- package/dist/cli/index.d.ts +0 -1
- package/dist/cli/index.js +0 -26
- package/dist/cli/index.js.map +0 -1
- package/dist/client-invoker-VNZ7X0nn.js.map +0 -1
- package/dist/client-zqKcsyFa.js.map +0 -1
- package/dist/handler-stateless-CIkKPETH.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/internal_context-Dg4Cgjcu.d.ts +0 -37
- package/dist/internal_context.js.map +0 -1
- package/dist/mcp/client.d.ts +0 -42
- package/dist/mcp/do-oauth-client-provider.js.map +0 -1
- package/dist/mcp/x402.js.map +0 -1
- package/dist/protocol-Dqc2MQxo.js.map +0 -1
- package/dist/ws-chat-transport-CIoOBbO7.js.map +0 -1
package/docs/mcp-client.md
CHANGED
|
@@ -13,6 +13,61 @@ The MCP client capability lets your agent:
|
|
|
13
13
|
|
|
14
14
|
> **Note:** This page covers connecting to MCP servers as a client. To create your own MCP server, see [Creating MCP Servers](./mcp-servers.md).
|
|
15
15
|
|
|
16
|
+
## Compose the MCP client with a Durable Object
|
|
17
|
+
|
|
18
|
+
`MCPClientManager` is a lifecycle capability. A class can extend the platform
|
|
19
|
+
`DurableObject` directly and install the manager without extending `Agent`:
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { DurableObject } from "cloudflare:workers";
|
|
23
|
+
import { Lifecycle } from "agents/lifecycle";
|
|
24
|
+
import { MCPClientManager } from "agents/mcp/client";
|
|
25
|
+
|
|
26
|
+
export class MyObject extends DurableObject<Env> {
|
|
27
|
+
readonly mcp = new MCPClientManager("my-object", "1.0.0");
|
|
28
|
+
|
|
29
|
+
readonly lifecycle = Lifecycle.install(this).use(this.mcp);
|
|
30
|
+
|
|
31
|
+
onRequest() {
|
|
32
|
+
return Response.json({ tools: this.mcp.listTools() });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The lifecycle calls the manager automatically:
|
|
38
|
+
|
|
39
|
+
- `onStart()` initializes its schema and restores persisted connections before
|
|
40
|
+
the host handles work.
|
|
41
|
+
- `onRequest()` intercepts registered OAuth callback URLs before the host's
|
|
42
|
+
request handler.
|
|
43
|
+
|
|
44
|
+
Do not call these hooks manually. For a native Durable Object RPC method,
|
|
45
|
+
which bypasses `fetch`, call `await this.lifecycle.start()` before using the
|
|
46
|
+
manager. Explicit cleanup remains available through `mcp.dispose()`; Durable
|
|
47
|
+
Objects do not provide an eviction callback.
|
|
48
|
+
|
|
49
|
+
The manager does not require a particular OAuth callback route. Pass the exact
|
|
50
|
+
callback URL to `registerServer()` and route that request to the same named
|
|
51
|
+
Durable Object. The manager persists the URL and only intercepts a callback
|
|
52
|
+
whose origin and pathname match it.
|
|
53
|
+
|
|
54
|
+
An HTTP-only manager does not need `env`. Pass the Durable Object environment
|
|
55
|
+
when the catalog can contain RPC servers so persisted binding names can be
|
|
56
|
+
resolved after a wake:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
readonly mcp = new MCPClientManager("my-object", "1.0.0", {
|
|
60
|
+
env: this.env
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
If an RPC row exists without `env`, startup logs a warning identifying the
|
|
65
|
+
server and does not recreate that connection.
|
|
66
|
+
|
|
67
|
+
`Agent` installs this same capability object directly. Existing `this.mcp`,
|
|
68
|
+
`addMcpServer()`, `removeMcpServer()`, and `getMcpServers()` APIs remain
|
|
69
|
+
available.
|
|
70
|
+
|
|
16
71
|
## Quick Start
|
|
17
72
|
|
|
18
73
|
Install the exact MCP client peer used by this Agents release:
|
package/docs/observability.md
CHANGED
|
@@ -158,17 +158,17 @@ These events are emitted by `AIChatAgent` from `@cloudflare/ai-chat`. They track
|
|
|
158
158
|
|
|
159
159
|
### Chat recovery events
|
|
160
160
|
|
|
161
|
-
| Type | Payload | When
|
|
162
|
-
| ------------------------- | ------------------------------------------------------------------------ |
|
|
163
|
-
| `chat:request:failed` | `{ requestId?, stage, messagesPersisted?, error }` | A Think chat request fails while parsing, persisting, running, or streaming
|
|
164
|
-
| `chat:recovery:detected` | `{ incidentId, requestId, attempt, maxAttempts, recoveryKind }` | An interrupted chat fiber is first observed
|
|
165
|
-
| `chat:recovery:attempt` | `{ incidentId, requestId, attempt, maxAttempts, recoveryKind }` | The framework begins a recovery attempt
|
|
166
|
-
| `chat:recovery:scheduled` | `{ incidentId, requestId, attempt, maxAttempts, recoveryKind }` | A retry or continuation callback is scheduled
|
|
167
|
-
| `chat:recovery:completed` | `{ incidentId, requestId, attempt, maxAttempts, recoveryKind }` | Recovery completed successfully
|
|
168
|
-
| `chat:recovery:skipped` | `{ incidentId, requestId, attempt, maxAttempts, recoveryKind, reason? }` | Recovery was skipped because the conversation changed or was no longer recoverable
|
|
169
|
-
| `chat:recovery:failed` | `{ incidentId, requestId, attempt, maxAttempts, recoveryKind, reason? }` | Recovery ran but failed
|
|
170
|
-
| `chat:recovery:exhausted` | `{ incidentId, requestId, attempt, maxAttempts, recoveryKind, reason }` | Recovery exceeded its configured attempt budget
|
|
171
|
-
| `chat:stream:stalled` | `{ requestId, timeoutMs }` | The inactivity watchdog fired — no stream chunk arrived within `chatStreamStallTimeoutMs`.
|
|
161
|
+
| Type | Payload | When |
|
|
162
|
+
| ------------------------- | ------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
163
|
+
| `chat:request:failed` | `{ requestId?, stage, messagesPersisted?, error }` | A Think chat request fails while parsing, persisting, running, or streaming |
|
|
164
|
+
| `chat:recovery:detected` | `{ incidentId, requestId, attempt, maxAttempts, recoveryKind }` | An interrupted chat fiber is first observed |
|
|
165
|
+
| `chat:recovery:attempt` | `{ incidentId, requestId, attempt, maxAttempts, recoveryKind }` | The framework begins a recovery attempt |
|
|
166
|
+
| `chat:recovery:scheduled` | `{ incidentId, requestId, attempt, maxAttempts, recoveryKind }` | A retry or continuation callback is scheduled |
|
|
167
|
+
| `chat:recovery:completed` | `{ incidentId, requestId, attempt, maxAttempts, recoveryKind }` | Recovery completed successfully |
|
|
168
|
+
| `chat:recovery:skipped` | `{ incidentId, requestId, attempt, maxAttempts, recoveryKind, reason? }` | Recovery was skipped because the conversation changed or was no longer recoverable |
|
|
169
|
+
| `chat:recovery:failed` | `{ incidentId, requestId, attempt, maxAttempts, recoveryKind, reason? }` | Recovery ran but failed |
|
|
170
|
+
| `chat:recovery:exhausted` | `{ incidentId, requestId, attempt, maxAttempts, recoveryKind, reason }` | Recovery exceeded its configured attempt budget |
|
|
171
|
+
| `chat:stream:stalled` | `{ requestId, timeoutMs }` | The inactivity watchdog fired — no stream chunk arrived within `chatStreamStallTimeoutMs`. The turn then routes into bounded recovery; look for `chat:recovery:*`. See [Think configuration](https://github.com/cloudflare/agents/blob/main/docs/think/index.md) |
|
|
172
172
|
|
|
173
173
|
`recoveryKind` is `"retry"` when recovery replays an unanswered user turn and `"continue"` when it continues a partial assistant turn.
|
|
174
174
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
The `AIChatAgent` class provides **automatic resumable streaming** out of the box. When a client disconnects and reconnects during an active stream, the response automatically resumes from where it left off.
|
|
4
4
|
|
|
5
|
-
This is client reconnect recovery, not Durable Object eviction recovery.
|
|
5
|
+
This is client reconnect recovery, not Durable Object eviction recovery. Durable Object eviction recovery is also always enabled for `AIChatAgent` and `Think`: every turn runs inside a recoverable fiber. Use `chatRecovery` only to tune its budgets and terminal behavior.
|
|
6
6
|
|
|
7
7
|
## How It Works
|
|
8
8
|
|
package/docs/scheduling.md
CHANGED
|
@@ -15,6 +15,91 @@ The scheduling system supports four modes:
|
|
|
15
15
|
|
|
16
16
|
Under the hood, scheduling uses [Durable Object alarms](https://developers.cloudflare.com/durable-objects/api/alarms/) to wake the agent at the right time. Tasks are stored in a SQLite table and executed in order.
|
|
17
17
|
|
|
18
|
+
## Scheduler Lifecycle primitive
|
|
19
|
+
|
|
20
|
+
> **Experimental.** The `Scheduler` primitive and the `agents/lifecycle`
|
|
21
|
+
> surface it builds on may change between releases. Agent's established
|
|
22
|
+
> scheduling methods (`this.schedule()` and friends) are stable.
|
|
23
|
+
|
|
24
|
+
`Scheduler` is a reusable Lifecycle capability. A plain Lifecycle Object can
|
|
25
|
+
install it without extending `Agent`:
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { DurableObject } from "cloudflare:workers";
|
|
29
|
+
import { Lifecycle } from "agents/lifecycle";
|
|
30
|
+
import { Scheduler, type Schedule } from "agents/schedules";
|
|
31
|
+
|
|
32
|
+
export class ReminderObject extends DurableObject<Env> {
|
|
33
|
+
readonly scheduler = new Scheduler({
|
|
34
|
+
callbacks: {
|
|
35
|
+
sendReminder: (
|
|
36
|
+
payload: { message: string },
|
|
37
|
+
schedule: Schedule<{ message: string }>
|
|
38
|
+
) => {
|
|
39
|
+
console.log(schedule.id, payload.message);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
readonly lifecycle = Lifecycle.install(this).use(this.scheduler);
|
|
45
|
+
|
|
46
|
+
async createReminder(message: string): Promise<string> {
|
|
47
|
+
const schedule = await this.scheduler.set(300, "sendReminder", {
|
|
48
|
+
message
|
|
49
|
+
});
|
|
50
|
+
return schedule.id;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Lifecycle owns the physical Durable Object alarm. Scheduler contributes its
|
|
56
|
+
earliest pending task or hung-interval recheck. Lifecycle selects the earliest
|
|
57
|
+
contribution from Scheduler, other capabilities, and the host, then rearms after
|
|
58
|
+
every alarm phase. A future Fiber or MCP capability can contribute its own wake
|
|
59
|
+
time without storing work in Scheduler or depending on it.
|
|
60
|
+
|
|
61
|
+
Scheduler's primary API is small: callbacks are registered by name in the
|
|
62
|
+
constructor, `set()` and `every()` create schedules typed against that
|
|
63
|
+
registration, and `get()`, `list()`, and `cancel()` manage them. All of these
|
|
64
|
+
are asynchronous and work inside routed sub-agents.
|
|
65
|
+
|
|
66
|
+
Scheduler Lifecycle hooks run without ambient host context. Registered
|
|
67
|
+
callbacks are user code, so they run inside the host invocation context with
|
|
68
|
+
the Lifecycle Object available through `getCurrentAgent()`.
|
|
69
|
+
|
|
70
|
+
## Using Scheduler through Agent
|
|
71
|
+
|
|
72
|
+
Every `Agent` constructs and installs the same primitive at `this.scheduler`.
|
|
73
|
+
Existing Agent applications continue to use the established methods:
|
|
74
|
+
|
|
75
|
+
- `this.schedule()` and `this.scheduleEvery()` create schedules.
|
|
76
|
+
- `this.getScheduleById()` and `this.listSchedules()` read schedules.
|
|
77
|
+
- `this.cancelSchedule()` removes a schedule.
|
|
78
|
+
|
|
79
|
+
These methods delegate to `this.scheduler`; no setup or migration is required.
|
|
80
|
+
Agent registers no callbacks map — a composition-root resolver keeps
|
|
81
|
+
`this.schedule(60, "methodName")` dispatching to Agent methods. Agent passes
|
|
82
|
+
only policy options (retry defaults, hung-interval timeout, error routing) and
|
|
83
|
+
adapts Lifecycle's event sink, facet transport, and host invocation boundary
|
|
84
|
+
at its composition root — there is no Agent-specific Scheduler adapter. Scheduler contributes its next wake time to the same Lifecycle alarm
|
|
85
|
+
selection as Agent keep-alive, fibers, sub-agent work, and deferred
|
|
86
|
+
destruction.
|
|
87
|
+
|
|
88
|
+
Import `Scheduler` and runtime schedule types from the dependency-light entry
|
|
89
|
+
point:
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import {
|
|
93
|
+
Scheduler,
|
|
94
|
+
type Schedule,
|
|
95
|
+
type ScheduleCriteria
|
|
96
|
+
} from "agents/schedules";
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Natural-language parsing helpers use Zod and live under
|
|
100
|
+
`agents/schedules/parser`. The previous `agents/schedule` path remains as a
|
|
101
|
+
deprecated compatibility alias.
|
|
102
|
+
|
|
18
103
|
## Quick Start
|
|
19
104
|
|
|
20
105
|
```typescript
|
|
@@ -299,9 +384,9 @@ This is the recommended approach since you cannot forget to dispose the heartbea
|
|
|
299
384
|
|
|
300
385
|
### How it works
|
|
301
386
|
|
|
302
|
-
`keepAlive()` uses an in-memory reference count
|
|
387
|
+
`keepAlive()` uses an in-memory reference count. Each call increments the count; the disposer decrements it. While the count is above zero, Agent contributes a wake time every 30 seconds to Lifecycle. No schedule rows are created and no observability events are emitted, so the heartbeat is invisible to `listSchedules()` and the scheduling diagnostics channel.
|
|
303
388
|
|
|
304
|
-
The heartbeat does not conflict with
|
|
389
|
+
The heartbeat does not conflict with scheduled work. Lifecycle selects one physical alarm from both contributions.
|
|
305
390
|
|
|
306
391
|
Inside sub-agents, `keepAlive()` delegates that heartbeat ref to the top-level parent because facets do not have independent alarm slots. `keepAliveWhile()` works the same way because it calls `keepAlive()` and automatically disposes the delegated ref when the scoped work completes.
|
|
307
392
|
|
|
@@ -642,14 +727,14 @@ class TimezoneAgent extends Agent {
|
|
|
642
727
|
|
|
643
728
|
## AI-Assisted Scheduling
|
|
644
729
|
|
|
645
|
-
The SDK includes utilities for parsing natural language scheduling requests with AI.
|
|
730
|
+
The SDK includes utilities for parsing natural language scheduling requests with AI. Import them from `agents/schedules/parser`.
|
|
646
731
|
|
|
647
732
|
### getSchedulePrompt()
|
|
648
733
|
|
|
649
734
|
Returns a system prompt for parsing natural language into scheduling parameters:
|
|
650
735
|
|
|
651
736
|
```typescript
|
|
652
|
-
import { getSchedulePrompt, scheduleSchema } from "agents";
|
|
737
|
+
import { getSchedulePrompt, scheduleSchema } from "agents/schedules/parser";
|
|
653
738
|
import { generateObject } from "ai";
|
|
654
739
|
import { openai } from "@ai-sdk/openai";
|
|
655
740
|
|
|
@@ -703,7 +788,7 @@ class SmartScheduler extends Agent {
|
|
|
703
788
|
A Zod schema for validating parsed scheduling data:
|
|
704
789
|
|
|
705
790
|
```typescript
|
|
706
|
-
import { scheduleSchema } from "agents";
|
|
791
|
+
import { scheduleSchema } from "agents/schedules/parser";
|
|
707
792
|
|
|
708
793
|
// The schema uses a discriminated union on `when.type`:
|
|
709
794
|
// {
|
|
@@ -751,7 +836,81 @@ When using this schema with OpenAI models via the AI SDK, you must pass `provide
|
|
|
751
836
|
|
|
752
837
|
## API Reference
|
|
753
838
|
|
|
754
|
-
|
|
839
|
+
Two surfaces share these semantics: the experimental `Scheduler` primitive
|
|
840
|
+
(`agents/schedules`, methods on the scheduler instance) and the stable `Agent`
|
|
841
|
+
methods (on the Agent class, delegating to `this.scheduler`).
|
|
842
|
+
|
|
843
|
+
### Scheduler primitive
|
|
844
|
+
|
|
845
|
+
#### `new Scheduler(options?)`
|
|
846
|
+
|
|
847
|
+
```typescript
|
|
848
|
+
new Scheduler({
|
|
849
|
+
callbacks?,
|
|
850
|
+
retry?,
|
|
851
|
+
hungScheduleTimeoutSeconds?,
|
|
852
|
+
onError?
|
|
853
|
+
});
|
|
854
|
+
```
|
|
855
|
+
|
|
856
|
+
- `callbacks` registers scheduled callbacks by name. `set()` and `every()`
|
|
857
|
+
type both the name and the payload against this map, and dispatch runs the
|
|
858
|
+
registered function — the typed scheduling surface and the runtime dispatch
|
|
859
|
+
target are the same object.
|
|
860
|
+
- Lifecycle supplies storage, readiness, startup state, alarm coordination,
|
|
861
|
+
the host invocation boundary, events, and routing.
|
|
862
|
+
- `retry` supplies callback retry defaults. The defaults are three attempts,
|
|
863
|
+
100 ms base delay, and 3,000 ms maximum delay.
|
|
864
|
+
- `hungScheduleTimeoutSeconds` defaults to 30 seconds.
|
|
865
|
+
- `onError` observes terminal callback failures outside ambient host context.
|
|
866
|
+
|
|
867
|
+
Install the constructed object with `Lifecycle.use()` before use. Scheduler
|
|
868
|
+
starts Lifecycle automatically when its asynchronous API is entered. It
|
|
869
|
+
publishes `schedule:*` events through Lifecycle's best-effort event bus. A plain
|
|
870
|
+
Lifecycle Object writes them to the existing `agents:schedule` diagnostics
|
|
871
|
+
channel; `Agent` sends them through its existing observability implementation.
|
|
872
|
+
|
|
873
|
+
#### set()
|
|
874
|
+
|
|
875
|
+
```typescript
|
|
876
|
+
async set(when, callback, payload?, options?): Promise<Schedule<Payload>>
|
|
877
|
+
```
|
|
878
|
+
|
|
879
|
+
Create a one-shot or cron schedule. `when`, `options`, idempotency, and return
|
|
880
|
+
value follow [`schedule()`](#schedule) below. `callback` must be a name
|
|
881
|
+
registered in the constructor's `callbacks` map, and `payload` is typed
|
|
882
|
+
against that callback's first parameter.
|
|
883
|
+
|
|
884
|
+
#### every()
|
|
885
|
+
|
|
886
|
+
```typescript
|
|
887
|
+
async every(intervalSeconds, callback, payload?, options?): Promise<Schedule<Payload>>
|
|
888
|
+
```
|
|
889
|
+
|
|
890
|
+
Create a fixed-interval schedule. Semantics follow
|
|
891
|
+
[`scheduleEvery()`](#scheduleevery) below, and `every()` also accepts
|
|
892
|
+
`options.idempotent` to opt out of interval dedup.
|
|
893
|
+
|
|
894
|
+
#### get() / list() / cancel()
|
|
895
|
+
|
|
896
|
+
```typescript
|
|
897
|
+
async get(id): Promise<Schedule<unknown> | undefined>
|
|
898
|
+
async list(criteria?): Promise<Schedule<unknown>[]>
|
|
899
|
+
async cancel(id): Promise<boolean>
|
|
900
|
+
```
|
|
901
|
+
|
|
902
|
+
Read and cancel schedules — the primitive's equivalents of
|
|
903
|
+
[`getScheduleById()`](#getschedulebyid), [`listSchedules()`](#listschedules),
|
|
904
|
+
and [`cancelSchedule()`](#cancelschedule) below. (Internal synchronous
|
|
905
|
+
variants on the Scheduler back Agent's deprecated `getSchedule()` and
|
|
906
|
+
`getSchedules()`; they are not part of the primitive's contract.)
|
|
907
|
+
|
|
908
|
+
### Agent methods
|
|
909
|
+
|
|
910
|
+
Methods on the `Agent` class, delegating to `this.scheduler`. Callback names
|
|
911
|
+
resolve to methods on the Agent.
|
|
912
|
+
|
|
913
|
+
#### schedule()
|
|
755
914
|
|
|
756
915
|
```typescript
|
|
757
916
|
async schedule<T = string>(
|
|
@@ -789,7 +948,7 @@ class MyAgent extends Agent {
|
|
|
789
948
|
}
|
|
790
949
|
```
|
|
791
950
|
|
|
792
|
-
|
|
951
|
+
#### scheduleEvery()
|
|
793
952
|
|
|
794
953
|
```typescript
|
|
795
954
|
async scheduleEvery<T = string>(
|
|
@@ -800,7 +959,8 @@ async scheduleEvery<T = string>(
|
|
|
800
959
|
): Promise<Schedule<T>>
|
|
801
960
|
```
|
|
802
961
|
|
|
803
|
-
Schedule a task to run repeatedly at a fixed interval.
|
|
962
|
+
Schedule a task to run repeatedly at a fixed interval. Idempotent by design
|
|
963
|
+
(the primitive's `every()` accepts `options.idempotent` to opt out).
|
|
804
964
|
|
|
805
965
|
**Parameters:**
|
|
806
966
|
|
|
@@ -819,7 +979,7 @@ Schedule a task to run repeatedly at a fixed interval.
|
|
|
819
979
|
- If callback throws an error, the interval continues
|
|
820
980
|
- Cancel with `cancelSchedule(id)` to stop the entire interval
|
|
821
981
|
|
|
822
|
-
|
|
982
|
+
#### getScheduleById()
|
|
823
983
|
|
|
824
984
|
```typescript
|
|
825
985
|
async getScheduleById(id: string): Promise<Schedule<unknown> | undefined>
|
|
@@ -827,7 +987,7 @@ async getScheduleById(id: string): Promise<Schedule<unknown> | undefined>
|
|
|
827
987
|
|
|
828
988
|
Get a scheduled task by ID. This method works in both top-level agents and sub-agents.
|
|
829
989
|
|
|
830
|
-
|
|
990
|
+
#### listSchedules()
|
|
831
991
|
|
|
832
992
|
```typescript
|
|
833
993
|
async listSchedules(criteria?: {
|
|
@@ -839,7 +999,7 @@ async listSchedules(criteria?: {
|
|
|
839
999
|
|
|
840
1000
|
Get scheduled tasks matching the criteria. This method works in both top-level agents and sub-agents.
|
|
841
1001
|
|
|
842
|
-
|
|
1002
|
+
#### getSchedule()
|
|
843
1003
|
|
|
844
1004
|
```typescript
|
|
845
1005
|
getSchedule<T = string>(id: string): Schedule<T> | undefined
|
|
@@ -847,7 +1007,7 @@ getSchedule<T = string>(id: string): Schedule<T> | undefined
|
|
|
847
1007
|
|
|
848
1008
|
Deprecated. Get a scheduled task by ID synchronously. This method only works in top-level agents; use `await this.getScheduleById(id)` instead.
|
|
849
1009
|
|
|
850
|
-
|
|
1010
|
+
#### getSchedules()
|
|
851
1011
|
|
|
852
1012
|
```typescript
|
|
853
1013
|
getSchedules<T = string>(criteria?: {
|
|
@@ -859,7 +1019,7 @@ getSchedules<T = string>(criteria?: {
|
|
|
859
1019
|
|
|
860
1020
|
Deprecated. Get scheduled tasks matching the criteria synchronously. This method only works in top-level agents; use `await this.listSchedules(criteria)` instead.
|
|
861
1021
|
|
|
862
|
-
|
|
1022
|
+
#### cancelSchedule()
|
|
863
1023
|
|
|
864
1024
|
```typescript
|
|
865
1025
|
async cancelSchedule(id: string): Promise<boolean>
|
|
@@ -867,7 +1027,7 @@ async cancelSchedule(id: string): Promise<boolean>
|
|
|
867
1027
|
|
|
868
1028
|
Cancel a scheduled task. Returns `true` if cancelled, `false` if not found.
|
|
869
1029
|
|
|
870
|
-
|
|
1030
|
+
#### keepAlive()
|
|
871
1031
|
|
|
872
1032
|
```typescript
|
|
873
1033
|
async keepAlive(): Promise<() => void>
|
|
@@ -877,7 +1037,7 @@ Create an alarm-backed heartbeat that prevents the Durable Object from being evi
|
|
|
877
1037
|
|
|
878
1038
|
See [Keeping the Agent Alive](#keeping-the-agent-alive) for usage details.
|
|
879
1039
|
|
|
880
|
-
|
|
1040
|
+
#### keepAliveWhile()
|
|
881
1041
|
|
|
882
1042
|
```typescript
|
|
883
1043
|
async keepAliveWhile<T>(fn: () => Promise<T>): Promise<T>
|
|
@@ -457,7 +457,7 @@ Pre-aborted signals short-circuit before any model work runs.
|
|
|
457
457
|
### Limitations
|
|
458
458
|
|
|
459
459
|
- **Signals cannot cross Durable Object boundaries.** `AbortSignal` is not an RPC-serializable type. Construct the controller inside the DO that calls `saveMessages`. For Think child-agent orchestration, use [Agent Tools](./agent-tools.md); `runAgentTool()` bridges parent aborts into the child run. For lower-level custom RPC, return a `ReadableStream` from the child and let the parent cancel it — workerd propagates the cancel back to the source's `cancel` callback.
|
|
460
|
-
- **Hibernation drops the listener.** The signal lives in memory.
|
|
460
|
+
- **Hibernation drops the listener.** The signal lives in memory. After the DO restarts, durable recovery usually calls `continueLastTurn()` without the original signal, so an abort fired after restart has no effect. For pre-stream interruptions, recovery can instead retry the latest unanswered user message automatically. This is true for top-level agents and sub-agents. If cancellation must survive restart, persist the cancellation intent in agent state or SQL, check it in `onChatRecovery()`, and return `{ continue: false }`. Durable recovery cannot be disabled.
|
|
461
461
|
|
|
462
462
|
This is the integration point for agent-tool orchestration where the parent's AI SDK abort signal needs to propagate into a child DO's `saveMessages` call. See [`cloudflare/agents#1406`](https://github.com/cloudflare/agents/issues/1406) for the original use case.
|
|
463
463
|
|
package/docs/sub-agents.md
CHANGED
|
@@ -316,7 +316,7 @@ The Worker must pass the incoming request to `routeAgentRequest()`. Each ancesto
|
|
|
316
316
|
|
|
317
317
|
`buildAgentUrl()` accepts an HTTP(S) or WS(S) origin without a pathname, query, fragment, or credentials. Set callback query parameters through the returned URL's `searchParams`. If you use a custom routing prefix, pass the same value to both `buildAgentPath()` and `routeAgentRequest()`.
|
|
318
318
|
|
|
319
|
-
Root Agent names follow
|
|
319
|
+
Root Agent names follow `routeAgentRequest`'s raw pathname-segment behavior and must already be externally routable. The `sub` segment is reserved in routing prefixes, class and binding names, and root Agent names. Descendant names are URL-encoded by the helper, so names containing spaces, Unicode, `/`, or URL-reserved characters round-trip safely.
|
|
320
320
|
|
|
321
321
|
### Custom routing
|
|
322
322
|
|
package/package.json
CHANGED
|
@@ -9,16 +9,13 @@
|
|
|
9
9
|
"durable objects"
|
|
10
10
|
],
|
|
11
11
|
"type": "module",
|
|
12
|
-
"version": "0.
|
|
12
|
+
"version": "0.22.0",
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"repository": {
|
|
15
15
|
"directory": "packages/agents",
|
|
16
16
|
"type": "git",
|
|
17
17
|
"url": "git+https://github.com/cloudflare/agents.git"
|
|
18
18
|
},
|
|
19
|
-
"bin": {
|
|
20
|
-
"agents": "dist/cli/index.js"
|
|
21
|
-
},
|
|
22
19
|
"author": "Cloudflare Inc.",
|
|
23
20
|
"bugs": {
|
|
24
21
|
"url": "https://github.com/cloudflare/agents/issues"
|
|
@@ -31,10 +28,8 @@
|
|
|
31
28
|
"esbuild": "^0.28.1",
|
|
32
29
|
"mimetext": "^3.0.28",
|
|
33
30
|
"nanoid": "^5.1.16",
|
|
34
|
-
"partyserver": "^0.5.9",
|
|
35
31
|
"partysocket": "1.3.0",
|
|
36
|
-
"yaml": "^2.9.0"
|
|
37
|
-
"yargs": "^18.0.0"
|
|
32
|
+
"yaml": "^2.9.0"
|
|
38
33
|
},
|
|
39
34
|
"devDependencies": {
|
|
40
35
|
"@ai-sdk/react": "^4.0.0",
|
|
@@ -46,7 +41,6 @@
|
|
|
46
41
|
"@modelcontextprotocol/server": "2.0.0",
|
|
47
42
|
"@tanstack/ai": "0.38.0",
|
|
48
43
|
"@types/react": "^19.2.17",
|
|
49
|
-
"@types/yargs": "^17.0.35",
|
|
50
44
|
"@valibot/to-json-schema": "1.3.0",
|
|
51
45
|
"@x402/core": "^2.17.0",
|
|
52
46
|
"@x402/evm": "^2.17.0",
|
|
@@ -123,6 +117,11 @@
|
|
|
123
117
|
"import": "./dist/client.js",
|
|
124
118
|
"require": "./dist/client.js"
|
|
125
119
|
},
|
|
120
|
+
"./lifecycle": {
|
|
121
|
+
"types": "./dist/lifecycle/index.d.ts",
|
|
122
|
+
"import": "./dist/lifecycle/index.js",
|
|
123
|
+
"require": "./dist/lifecycle/index.js"
|
|
124
|
+
},
|
|
126
125
|
"./agent-tools": {
|
|
127
126
|
"types": "./dist/agent-tools.d.ts",
|
|
128
127
|
"import": "./dist/agent-tools.js",
|
|
@@ -139,19 +138,19 @@
|
|
|
139
138
|
"require": "./dist/mcp/index.js"
|
|
140
139
|
},
|
|
141
140
|
"./mcp/client": {
|
|
142
|
-
"types": "./dist/mcp/client.d.ts",
|
|
143
|
-
"import": "./dist/mcp/client.js",
|
|
144
|
-
"require": "./dist/mcp/client.js"
|
|
141
|
+
"types": "./dist/mcp/client/index.d.ts",
|
|
142
|
+
"import": "./dist/mcp/client/index.js",
|
|
143
|
+
"require": "./dist/mcp/client/index.js"
|
|
145
144
|
},
|
|
146
145
|
"./mcp/server": {
|
|
147
|
-
"types": "./dist/mcp/server.d.ts",
|
|
148
|
-
"import": "./dist/mcp/server.js",
|
|
149
|
-
"require": "./dist/mcp/server.js"
|
|
146
|
+
"types": "./dist/mcp/server/index.d.ts",
|
|
147
|
+
"import": "./dist/mcp/server/index.js",
|
|
148
|
+
"require": "./dist/mcp/server/index.js"
|
|
150
149
|
},
|
|
151
150
|
"./mcp/do-oauth-client-provider": {
|
|
152
|
-
"types": "./dist/mcp/do-oauth-client-provider.d.ts",
|
|
153
|
-
"import": "./dist/mcp/do-oauth-client-provider.js",
|
|
154
|
-
"require": "./dist/mcp/do-oauth-client-provider.js"
|
|
151
|
+
"types": "./dist/mcp/client/do-oauth-client-provider.d.ts",
|
|
152
|
+
"import": "./dist/mcp/client/do-oauth-client-provider.js",
|
|
153
|
+
"require": "./dist/mcp/client/do-oauth-client-provider.js"
|
|
155
154
|
},
|
|
156
155
|
"./observability": {
|
|
157
156
|
"types": "./dist/observability/index.d.ts",
|
|
@@ -173,6 +172,16 @@
|
|
|
173
172
|
"import": "./dist/schedule.js",
|
|
174
173
|
"require": "./dist/schedule.js"
|
|
175
174
|
},
|
|
175
|
+
"./schedules": {
|
|
176
|
+
"types": "./dist/schedules/index.d.ts",
|
|
177
|
+
"import": "./dist/schedules/index.js",
|
|
178
|
+
"require": "./dist/schedules/index.js"
|
|
179
|
+
},
|
|
180
|
+
"./schedules/parser": {
|
|
181
|
+
"types": "./dist/schedules/parser.d.ts",
|
|
182
|
+
"import": "./dist/schedules/parser.js",
|
|
183
|
+
"require": "./dist/schedules/parser.js"
|
|
184
|
+
},
|
|
176
185
|
"./workflows": {
|
|
177
186
|
"types": "./dist/workflows.d.ts",
|
|
178
187
|
"import": "./dist/workflows.js",
|
|
@@ -194,9 +203,9 @@
|
|
|
194
203
|
"require": "./dist/experimental/webmcp.js"
|
|
195
204
|
},
|
|
196
205
|
"./x402": {
|
|
197
|
-
"types": "./dist/mcp/x402.d.ts",
|
|
198
|
-
"import": "./dist/mcp/x402.js",
|
|
199
|
-
"require": "./dist/mcp/x402.js"
|
|
206
|
+
"types": "./dist/mcp/client/x402.d.ts",
|
|
207
|
+
"import": "./dist/mcp/client/x402.js",
|
|
208
|
+
"require": "./dist/mcp/client/x402.js"
|
|
200
209
|
},
|
|
201
210
|
"./chat": {
|
|
202
211
|
"types": "./dist/chat/index.d.ts",
|
|
@@ -308,7 +317,7 @@
|
|
|
308
317
|
"build": "tsx ./scripts/build.ts",
|
|
309
318
|
"evals": "(cd evals; evalite)",
|
|
310
319
|
"test": "vitest --run",
|
|
311
|
-
"test:
|
|
320
|
+
"test:node": "vitest --project node",
|
|
312
321
|
"test:react": "vitest --project react",
|
|
313
322
|
"test:browser": "vitest run --config src/browser-tests/vitest.config.ts",
|
|
314
323
|
"test:workers": "vitest --project workers",
|
package/dist/cli/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/cli/index.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import yargs from "yargs";
|
|
3
|
-
import { hideBin } from "yargs/helpers";
|
|
4
|
-
//#region src/cli/create.ts
|
|
5
|
-
function createCli(argv = process.argv) {
|
|
6
|
-
return yargs(hideBin(argv)).scriptName("agents").usage("$0 <command> [options]").command(["init", "create"], "Initialize an agents project", (cmd) => cmd, async () => {
|
|
7
|
-
console.log("agents init: not implemented yet");
|
|
8
|
-
process.exit(0);
|
|
9
|
-
}).command("dev", "Start development server", (cmd) => cmd, async () => {
|
|
10
|
-
console.log("agents dev: not implemented yet");
|
|
11
|
-
process.exit(0);
|
|
12
|
-
}).command("deploy", "Deploy agents to Cloudflare", (cmd) => cmd, async () => {
|
|
13
|
-
console.log("agents deploy: not implemented yet");
|
|
14
|
-
process.exit(0);
|
|
15
|
-
}).command("mcp", "The agents mcp server", (cmd) => cmd, async () => {
|
|
16
|
-
console.log("agents mcp: not implemented yet");
|
|
17
|
-
process.exit(0);
|
|
18
|
-
}).demandCommand(1, "Please provide a command").strict().help();
|
|
19
|
-
}
|
|
20
|
-
//#endregion
|
|
21
|
-
//#region src/cli/index.ts
|
|
22
|
-
createCli().parse();
|
|
23
|
-
//#endregion
|
|
24
|
-
export {};
|
|
25
|
-
|
|
26
|
-
//# sourceMappingURL=index.js.map
|
package/dist/cli/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/cli/create.ts","../../src/cli/index.ts"],"sourcesContent":["import yargs from \"yargs\";\nimport { hideBin } from \"yargs/helpers\";\n\nexport function createCli(argv = process.argv) {\n return yargs(hideBin(argv))\n .scriptName(\"agents\")\n .usage(\"$0 <command> [options]\")\n .command(\n [\"init\", \"create\"],\n \"Initialize an agents project\",\n (cmd) => cmd,\n async () => {\n console.log(\"agents init: not implemented yet\");\n process.exit(0);\n }\n )\n .command(\n \"dev\",\n \"Start development server\",\n (cmd) => cmd,\n async () => {\n console.log(\"agents dev: not implemented yet\");\n process.exit(0);\n }\n )\n .command(\n \"deploy\",\n \"Deploy agents to Cloudflare\",\n (cmd) => cmd,\n async () => {\n console.log(\"agents deploy: not implemented yet\");\n process.exit(0);\n }\n )\n .command(\n \"mcp\",\n \"The agents mcp server\",\n (cmd) => cmd,\n async () => {\n console.log(\"agents mcp: not implemented yet\");\n process.exit(0);\n }\n )\n .demandCommand(1, \"Please provide a command\")\n .strict()\n .help();\n}\n","#!/usr/bin/env node\n\nimport { createCli } from \"./create\";\n\nvoid createCli().parse();\n"],"mappings":";;;;AAGA,SAAgB,UAAU,OAAO,QAAQ,MAAM;CAC7C,OAAO,MAAM,QAAQ,IAAI,CAAC,CAAC,CACxB,WAAW,QAAQ,CAAC,CACpB,MAAM,wBAAwB,CAAC,CAC/B,QACC,CAAC,QAAQ,QAAQ,GACjB,iCACC,QAAQ,KACT,YAAY;EACV,QAAQ,IAAI,kCAAkC;EAC9C,QAAQ,KAAK,CAAC;CAChB,CACF,CAAC,CACA,QACC,OACA,6BACC,QAAQ,KACT,YAAY;EACV,QAAQ,IAAI,iCAAiC;EAC7C,QAAQ,KAAK,CAAC;CAChB,CACF,CAAC,CACA,QACC,UACA,gCACC,QAAQ,KACT,YAAY;EACV,QAAQ,IAAI,oCAAoC;EAChD,QAAQ,KAAK,CAAC;CAChB,CACF,CAAC,CACA,QACC,OACA,0BACC,QAAQ,KACT,YAAY;EACV,QAAQ,IAAI,iCAAiC;EAC7C,QAAQ,KAAK,CAAC;CAChB,CACF,CAAC,CACA,cAAc,GAAG,0BAA0B,CAAC,CAC5C,OAAO,CAAC,CACR,KAAK;AACV;;;AC1CK,UAAU,CAAC,CAAC,MAAM"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"client-invoker-VNZ7X0nn.js","names":[],"sources":["../src/mcp/client-invoker.ts"],"sourcesContent":["import type {\n CallToolRequest,\n CallToolRequestOptions,\n CallToolResult,\n Client,\n ListToolsResult\n} from \"@modelcontextprotocol/client\";\nimport type { Client as LegacyClient } from \"@modelcontextprotocol/sdk/client/index.js\";\nimport type {\n CallToolResult as LegacyCallToolResult,\n CallToolResultSchema,\n CompatibilityCallToolResultSchema,\n ListToolsRequest\n} from \"@modelcontextprotocol/sdk/types.js\";\nimport type { RequestOptions as LegacyRequestOptions } from \"@modelcontextprotocol/sdk/shared/protocol.js\";\n\nexport type LegacyCallToolResultSchema =\n | typeof CallToolResultSchema\n | typeof CompatibilityCallToolResultSchema;\n\nexport type CallToolSchemaOrOptions =\n | LegacyCallToolResultSchema\n | CallToolRequestOptions;\n\nexport type CompatibleMcpClient = Client | LegacyClient;\n\nexport interface BoundMcpClientInvoker {\n listTools(\n params?: ListToolsRequest[\"params\"],\n options?: CallToolRequestOptions\n ): Promise<ListToolsResult>;\n callTool(\n params: CallToolRequest[\"params\"],\n schemaOrOptions?: CallToolSchemaOrOptions,\n options?: CallToolRequestOptions\n ): Promise<LegacyCallToolResult>;\n}\n\nfunction isLegacyResultSchema(\n value: CallToolSchemaOrOptions | undefined\n): value is LegacyCallToolResultSchema {\n return (\n typeof (value as { parse?: unknown } | undefined)?.parse === \"function\"\n );\n}\n\nfunction normalizeCallToolArguments(\n schemaOrOptions?: CallToolSchemaOrOptions,\n options?: CallToolRequestOptions\n): {\n schema?: LegacyCallToolResultSchema;\n options?: CallToolRequestOptions;\n} {\n return isLegacyResultSchema(schemaOrOptions)\n ? { schema: schemaOrOptions, options }\n : { options: schemaOrOptions ?? options };\n}\n\n/**\n * Invoke SDK v2 while preserving the deprecated v1 result-schema overload.\n * The v2 request funnel still owns decoding and automatic MRTR.\n */\nexport function callV2Tool(\n client: Client,\n params: CallToolRequest[\"params\"],\n schemaOrOptions?: CallToolSchemaOrOptions,\n options?: CallToolRequestOptions\n): Promise<CallToolResult> {\n const normalized = normalizeCallToolArguments(schemaOrOptions, options);\n if (normalized.schema) {\n return client.request(\n { method: \"tools/call\", params },\n normalized.schema as never,\n normalized.options\n ) as Promise<CallToolResult>;\n }\n return client.callTool(params, normalized.options);\n}\n\n/**\n * Bind the original methods before decorators such as x402 replace them. All\n * generation-specific calling conventions stay behind this small interface.\n */\nexport function bindMcpClient(\n client: CompatibleMcpClient\n): BoundMcpClientInvoker {\n const isV2 =\n \"getProtocolEra\" in client &&\n typeof (client as { getProtocolEra?: unknown }).getProtocolEra ===\n \"function\";\n\n if (isV2) {\n const v2Client = client as Client;\n const listTools = v2Client.listTools.bind(v2Client);\n const callTool = v2Client.callTool.bind(v2Client);\n return {\n listTools: (params, options) => listTools(params, options),\n callTool: (params, schemaOrOptions, options) => {\n const normalized = normalizeCallToolArguments(schemaOrOptions, options);\n const pending = normalized.schema\n ? v2Client.request(\n { method: \"tools/call\", params },\n normalized.schema as never,\n normalized.options\n )\n : callTool(params, normalized.options);\n return pending as Promise<LegacyCallToolResult>;\n }\n };\n }\n\n const legacy = client as LegacyClient;\n const listTools = legacy.listTools.bind(legacy);\n const callTool = legacy.callTool.bind(legacy);\n return {\n listTools: (params, options) =>\n listTools(\n params,\n options as LegacyRequestOptions\n ) as Promise<ListToolsResult>,\n callTool: (params, schemaOrOptions, options) => {\n const normalized = normalizeCallToolArguments(schemaOrOptions, options);\n return callTool(\n params as Parameters<LegacyClient[\"callTool\"]>[0],\n normalized.schema,\n normalized.options as LegacyRequestOptions\n ) as Promise<LegacyCallToolResult>;\n }\n };\n}\n"],"mappings":";AAsCA,SAAS,qBACP,OACqC;CACrC,OACE,OAAQ,OAA2C,UAAU;AAEjE;AAEA,SAAS,2BACP,iBACA,SAIA;CACA,OAAO,qBAAqB,eAAe,IACvC;EAAE,QAAQ;EAAiB;CAAQ,IACnC,EAAE,SAAS,mBAAmB,QAAQ;AAC5C;;;;;AAMA,SAAgB,WACd,QACA,QACA,iBACA,SACyB;CACzB,MAAM,aAAa,2BAA2B,iBAAiB,OAAO;CACtE,IAAI,WAAW,QACb,OAAO,OAAO,QACZ;EAAE,QAAQ;EAAc;CAAO,GAC/B,WAAW,QACX,WAAW,OACb;CAEF,OAAO,OAAO,SAAS,QAAQ,WAAW,OAAO;AACnD;;;;;AAMA,SAAgB,cACd,QACuB;CAMvB,IAJE,oBAAoB,UACpB,OAAQ,OAAwC,mBAC9C,YAEM;EACR,MAAM,WAAW;EACjB,MAAM,YAAY,SAAS,UAAU,KAAK,QAAQ;EAClD,MAAM,WAAW,SAAS,SAAS,KAAK,QAAQ;EAChD,OAAO;GACL,YAAY,QAAQ,YAAY,UAAU,QAAQ,OAAO;GACzD,WAAW,QAAQ,iBAAiB,YAAY;IAC9C,MAAM,aAAa,2BAA2B,iBAAiB,OAAO;IAQtE,OAPgB,WAAW,SACvB,SAAS,QACP;KAAE,QAAQ;KAAc;IAAO,GAC/B,WAAW,QACX,WAAW,OACb,IACA,SAAS,QAAQ,WAAW,OAAO;GAEzC;EACF;CACF;CAEA,MAAM,SAAS;CACf,MAAM,YAAY,OAAO,UAAU,KAAK,MAAM;CAC9C,MAAM,WAAW,OAAO,SAAS,KAAK,MAAM;CAC5C,OAAO;EACL,YAAY,QAAQ,YAClB,UACE,QACA,OACF;EACF,WAAW,QAAQ,iBAAiB,YAAY;GAC9C,MAAM,aAAa,2BAA2B,iBAAiB,OAAO;GACtE,OAAO,SACL,QACA,WAAW,QACX,WAAW,OACb;EACF;CACF;AACF"}
|