You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs(agents): updates for agents 0.7.0 release (#28695)
* docs(agents): observability, MCP & scheduling updates
Revise and expand agent docs: introduce structured observability (diagnostics channels, subscribe helper, Tail Workers) and provide an event reference; update MCP client docs with URL security rules, idempotency/URL normalization details, and this.mcp.waitForConnections(); add AI chat doc improvements (requestId in onChatMessage, waitForMcpConnections option, and addToolOutput for custom denial messages); add keepAlive() and keepAliveWhile() scheduling APIs and usage guidance; clarify transport deduplication rules and adjust a few state handler names in examples. Various table/link updates and examples included for clarity.
* Fix broken Tail Workers links in observability docs
Co-authored-by: threepointone <[email protected]>
---------
Co-authored-by: ask-bonk[bot] <ask-bonk[bot]@users.noreply.github.com>
Co-authored-by: threepointone <[email protected]>
Copy file name to clipboardExpand all lines: src/content/docs/agents/api-reference/chat-agents.mdx
+58-1Lines changed: 58 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -213,13 +213,17 @@ export class ChatAgent extends AIChatAgent {
213
213
}
214
214
```
215
215
216
-
**Accessing custom body data**:
216
+
**Accessing custom body data and request ID**:
217
217
218
218
```ts
219
219
exportclassChatAgentextendsAIChatAgent {
220
220
async onChatMessage(_onFinish, options) {
221
221
const { timezone, userId } =options?.body?? {};
222
222
// Use these values in your LLM call or business logic
223
+
224
+
// options.requestId — unique identifier for this chat request,
225
+
// useful for logging and correlating events
226
+
console.log("Request ID:", options?.requestId);
223
227
}
224
228
}
225
229
```
@@ -269,6 +273,36 @@ export class ChatAgent extends AIChatAgent {
269
273
270
274
</TypeScriptExample>
271
275
276
+
### `waitForMcpConnections`
277
+
278
+
Controls whether `AIChatAgent` waits for MCP server connections to settle before calling `onChatMessage`. This ensures `this.mcp.getAITools()` returns the full set of tools, especially after Durable Object hibernation when connections are being restored in the background.
When a user rejects a tool, `addToolApprovalResponse({ id, approved: false })` sets the tool state to `output-denied` with a generic message. To give the LLM a more specific reason for the denial, use `addToolOutput` with `state:"output-error"` instead:
590
+
591
+
<TypeScriptExample>
592
+
593
+
```ts
594
+
const { addToolOutput } =useAgentChat({ agent });
595
+
596
+
// Reject with a custom error message
597
+
addToolOutput({
598
+
toolCallId: part.toolCallId,
599
+
state: "output-error",
600
+
errorText: "User declined: insufficient budget for this quarter",
601
+
});
602
+
```
603
+
604
+
</TypeScriptExample>
605
+
606
+
This sends a `tool_result` to the LLM with your custom error text, so it can respond appropriately (for example, suggest an alternative or ask clarifying questions).
607
+
608
+
`addToolApprovalResponse` (with `approved:false`) auto-continues the conversation when `autoContinueAfterToolResult` is enabled (the default). `addToolOutput` with `state:"output-error"` does **not** auto-continue — call `sendMessage()` afterward if you want the LLM to respond to the error.
609
+
553
610
For more patterns, refer to [Human-in-the-loop](/agents/concepts/human-in-the-loop/).
MCP server URLs are validated before connection to prevent Server-Side Request Forgery (SSRF). The following URL targets are blocked:
125
+
126
+
- Private/internal IP ranges (RFC 1918: `10.x`, `172.16-31.x`, `192.168.x`)
127
+
- Loopback addresses (`127.x`, `::1`)
128
+
- Link-local addresses (`169.254.x`, `fe80::`)
129
+
- Cloud metadata endpoints (`169.254.169.254`)
130
+
131
+
If you need to connect to an internal MCP server, use the [RPC transport](/agents/model-context-protocol/transport/) with a Durable Object binding instead of HTTP.
132
+
122
133
### Return value
123
134
124
135
`addMcpServer()` returns the connection state:
@@ -445,7 +456,13 @@ function Dashboard() {
445
456
446
457
### `addMcpServer()`
447
458
448
-
Add a connection to an MCP server and make its tools available to your agent. If `addMcpServer` is called with a `serverName` that already has an active connection, the existing connection is returned instead of creating a duplicate. This makes it safe to call in `onStart()` without worrying about duplicate connections on restart.
459
+
Add a connection to an MCP server and make its tools available to your agent.
460
+
461
+
Calling `addMcpServer` is idempotent when both the server name **and** URL match an existing active connection — the existing connection is returned without creating a duplicate. This makes it safe to call in `onStart()` without worrying about duplicate connections on restart.
462
+
463
+
If you call `addMcpServer` with the same name but a **different** URL, a new connection is created. Both connections remain active and their tools are merged in `getAITools()`. To replace a server, call `removeMcpServer(oldId)` first.
464
+
465
+
URLs are normalized before comparison (trailing slashes, default ports, and hostname case are handled), so `https://MCP.Example.com` and `https://mcp.example.com/` are treated as the same URL.
449
466
450
467
```ts
451
468
// HTTP transport (Streamable HTTP, SSE)
@@ -812,6 +829,22 @@ type MCPDiscoverResult = {
812
829
}
813
830
```
814
831
832
+
#### `this.mcp.waitForConnections()`
833
+
834
+
Wait for all in-flight MCP connection and discovery operations to settle. This is useful when you need `this.mcp.getAITools()` to return the full set of tools immediately after the agent wakes from hibernation.
`AIChatAgent` calls this automatically via its [`waitForMcpConnections`](/agents/api-reference/chat-agents/#waitformcpconnections) property (defaults to `{ timeout: 10_000 }`). You only need `waitForConnections()` directly when using `Agent` with MCP, or when you want finer control inside `onChatMessage`.
846
+
:::
847
+
815
848
#### `this.mcp.closeConnection()`
816
849
817
850
Close the connection to a specific server while keeping it registered.
0 commit comments