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: Update MCP client API for connection state improvements
Sync documentation for PR #672 which introduces:
- MCPConnectionState enum with standardized connection states
- Breaking change to addMcpServer() return type (now discriminated union)
- New error throwing behavior for connection/discovery failures
- New "connected" state between connection and discovery
- New discoverIfConnected() function for capability discovery
Updated mcp-client-api.mdx with:
- Connection state lifecycle documentation
- Updated addMcpServer() signature and examples with try-catch
- Error handling requirements and examples
- Updated getMcpServers() return type with new "connected" state
- Breaking change callouts for error handling requirements
Added comprehensive changelog entry documenting breaking changes and new features.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
title: MCP connection state improvements and standardized discovery
3
+
description: Introduces MCPConnectionState enum, improved error handling, and separates connection from discovery phases
4
+
products:
5
+
- agents
6
+
- workers
7
+
date: 2025-11-26
8
+
---
9
+
10
+
The latest update to [@cloudflare/agents](https://github.com/cloudflare/agents) brings improvements to MCP client connection lifecycle management and error handling. This release standardizes connection states and makes discovery failures more visible.
11
+
12
+
## Breaking changes
13
+
14
+
### `addMcpServer()` return type changed
15
+
16
+
The `addMcpServer()` method now returns a discriminated union that includes the connection state:
const result =awaitthis.addMcpServer("Server", "https://...");
27
+
if (result.state==="authenticating") {
28
+
// Handle OAuth with result.authUrl
29
+
}
30
+
```
31
+
32
+
### Error handling now required
33
+
34
+
`addMcpServer()` now throws errors when connection or discovery fails. Previously, servers would silently enter the `failed` state. Update your code to handle errors:
35
+
36
+
```ts
37
+
try {
38
+
const result =awaitthis.addMcpServer("Server", "https://...");
39
+
// Connection succeeded
40
+
} catch (error) {
41
+
// Handle connection or discovery failure
42
+
console.error(`Failed to connect: ${error.message}`);
43
+
}
44
+
```
45
+
46
+
### `ClientConnection.init()` no longer discovers automatically
47
+
48
+
Tool discovery must now be explicitly triggered after initialization. The `MCPClientManager` handles this automatically, but if you are using `ClientConnection` directly, you need to call `discover()` after successful initialization.
49
+
50
+
## New features
51
+
52
+
### MCPConnectionState enum
53
+
54
+
A new `MCPConnectionState` enum standardizes connection states:
55
+
56
+
-`authenticating` — Waiting for OAuth authorization
57
+
-`connecting` — Establishing transport connection
58
+
-`connected` — Transport connected but tools not yet discovered
59
+
-`discovering` — Discovering server capabilities
60
+
-`ready` — Fully connected with all capabilities available
61
+
-`failed` — Connection failed
62
+
63
+
The new `connected` state separates transport connection from capability discovery, providing better visibility into the connection lifecycle.
64
+
65
+
### `discoverIfConnected()` function
66
+
67
+
A new convenience function on `MCPClientManager` simplifies capability discovery:
68
+
69
+
```ts
70
+
const result =awaitthis.mcp.discoverIfConnected(serverId);
Discovery failures now throw errors immediately using `Promise.all()` instead of continuing with empty arrays. This makes issues visible immediately rather than silently degrading functionality.
81
+
82
+
### Better connection state visibility
83
+
84
+
The connection state machine is now formalized with explicit enum values, making it easier to understand and monitor connection lifecycle in `getMcpServers()`.
85
+
86
+
Learn more in the [MCP Client API documentation](/agents/model-context-protocol/mcp-client-api/).
Connections persist in the Agent's [SQL storage](/agents/api-reference/store-and-sync-state), and when an Agent connects to an MCP server, all tools from that server become available automatically.
48
52
53
+
## Connection States
54
+
55
+
When connecting to an MCP server, the connection progresses through different states represented by the `MCPConnectionState` enum:
56
+
57
+
-**`authenticating`** — Server requires OAuth; waiting for user authentication
58
+
-**`connecting`** — Establishing connection to the server
59
+
-**`connected`** — Transport connection established, awaiting initialization
60
+
-**`discovering`** — Retrieving available tools, prompts, and resources
61
+
-**`ready`** — Fully connected and all capabilities available
A Promise that resolves to a discriminated union based on the connection state:
86
114
115
+
**When authentication is required:**
87
116
-**`id`** (string) — Unique identifier for this server connection
88
-
-**`authUrl`** (string | undefined) — OAuth authorization URL if authentication is required, otherwise `undefined`
117
+
-**`state`** (`"authenticating"`) — Indicates OAuth authentication is needed
118
+
-**`authUrl`** (string) — OAuth authorization URL for user authentication
119
+
120
+
**When connection is ready:**
121
+
-**`id`** (string) — Unique identifier for this server connection
122
+
-**`state`** (`"ready"`) — Indicates the server is fully connected and ready
123
+
-**`authUrl`** (undefined) — No authentication URL when ready
124
+
125
+
#### Throws
126
+
127
+
Throws an error if the connection or discovery process fails. Always wrap `addMcpServer()` calls in try-catch blocks to handle connection failures gracefully.
89
128
90
129
#### Example
91
130
@@ -94,26 +133,36 @@ A Promise that resolves to an object containing:
If the MCP server requires OAuth authentication, `authUrl` will be returned for user authentication. Connections persist across requests and the Agent will automatically reconnect if the connection is lost.
159
+
If the MCP server requires OAuth authentication, the response will have `state: "authenticating"` with an `authUrl` for user authentication. Connections persist across requests and the Agent will automatically reconnect if the connection is lost.
160
+
161
+
:::note[Breaking Change: Error Handling Required]
162
+
163
+
As of the latest version, `addMcpServer()` now throws errors on connection or discovery failures instead of returning a failed state. Always wrap calls in try-catch blocks to handle errors gracefully. The return type is now a discriminated union based on the connection state.
164
+
165
+
:::
117
166
118
167
:::note[Default JSON Schema Validation]
119
168
@@ -193,6 +242,7 @@ An `MCPServersState` object containing:
The `state` field can be: `authenticating`, `connecting`, `ready`, `discovering`, or `failed`. Use this method to monitor connection status, list available tools, or build UI for connected servers.
281
+
The `state` field can be: `authenticating`, `connecting`, `connected`, `ready`, `discovering`, or `failed`. Use this method to monitor connection status, list available tools, or build UI for connected servers.
0 commit comments