Skip to content

Commit b66c921

Browse files
docs: add MCP connection state standardization and improvements
Synced from cloudflare/agents PR #672 - Document new MCPConnectionState enum with type-safe constants - Add new 'connected' state to connection state machine - Update state transition documentation - Document improved discovery error handling - Add changelog entry for breaking changes and new features Related PR: cloudflare/agents#672 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent ce0d3ab commit b66c921

File tree

2 files changed

+124
-2
lines changed

2 files changed

+124
-2
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
title: MCP connection state standardization and discovery improvements
3+
description: The Agents SDK now includes a formalized MCPConnectionState enum, a new 'connected' state for better connection lifecycle tracking, improved error handling for discovery failures, and the new discoverIfConnected() method for explicit capability discovery.
4+
products:
5+
- agents
6+
- workers
7+
date: 2025-11-25
8+
---
9+
10+
This release standardizes MCP client connection states and improves error handling during server capability discovery.
11+
12+
## MCPConnectionState enum
13+
14+
The new `MCPConnectionState` enum formalizes all possible connection states, replacing string literals with type-safe constants:
15+
16+
```ts
17+
import { MCPConnectionState } from "agents/mcp";
18+
19+
// Use enum constants instead of strings
20+
if (connection.state === MCPConnectionState.READY) {
21+
// Connection is ready
22+
}
23+
```
24+
25+
Available states:
26+
- `AUTHENTICATING` — Waiting for OAuth authorization
27+
- `CONNECTING` — Establishing transport connection
28+
- `CONNECTED` — Transport connected, capabilities not yet discovered
29+
- `DISCOVERING` — Fetching server capabilities
30+
- `READY` — Fully connected with tools available
31+
- `FAILED` — Connection failed
32+
33+
## New 'connected' state
34+
35+
A new `connected` state has been added to distinguish between:
36+
- Transport connection established (`connected`)
37+
- Capabilities discovered and tools available (`ready`)
38+
39+
This provides better visibility into the connection lifecycle, especially for debugging connection issues or building UI that shows connection progress.
40+
41+
**State transitions:**
42+
- Non-OAuth: `connecting``connected``discovering``ready`
43+
- OAuth: `authenticating` → (callback) → `connecting``connected``discovering``ready`
44+
45+
## Improved error handling
46+
47+
Discovery failures now throw errors immediately instead of silently continuing with empty tool arrays. This ensures connection problems are caught early and handled explicitly.
48+
49+
```ts
50+
try {
51+
await agent.addMcpServer("Server", "https://mcp.example.com/mcp");
52+
} catch (error) {
53+
// Discovery errors are now thrown immediately
54+
console.error("MCP connection failed:", error);
55+
}
56+
```
57+
58+
## discoverIfConnected() method
59+
60+
The new `discoverIfConnected()` method on `MCPClientManager` allows explicit capability discovery for servers in the `connected` or `ready` state. This is useful for:
61+
- Refreshing server capabilities
62+
- Manual discovery after connection establishment
63+
- Building refresh buttons in user interfaces
64+
65+
```ts
66+
// Discover capabilities for a connected server
67+
await mcpClientManager.discoverIfConnected(serverId);
68+
```
69+
70+
## Breaking change
71+
72+
`ClientConnection.init()` no longer automatically performs discovery. Discovery should now be initiated explicitly using `discoverIfConnected()` from the `MCPClientManager`. This change improves control over the connection lifecycle and makes the discovery step more explicit.
73+
74+
For most users using the `Agent` class methods like `addMcpServer()`, this change is handled automatically and requires no action.
75+
76+
## Resources
77+
78+
- [McpClient — API reference](/agents/model-context-protocol/mcp-client-api/)
79+
- [Connect to an MCP server](/agents/guides/connect-mcp-client/)

src/content/docs/agents/model-context-protocol/mcp-client-api.mdx

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,9 @@ An `MCPServersState` object containing:
193193
state:
194194
| "authenticating"
195195
| "connecting"
196-
| "ready"
196+
| "connected"
197197
| "discovering"
198+
| "ready"
198199
| "failed";
199200
capabilities: ServerCapabilities | null;
200201
instructions: string | null;
@@ -228,7 +229,21 @@ export class MyAgent extends Agent<Env, never> {
228229

229230
</TypeScriptExample>
230231

231-
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.
232+
The `state` field indicates the current connection status and follows a state machine:
233+
234+
- **`authenticating`** — Waiting for OAuth authorization to complete
235+
- **`connecting`** — Establishing transport connection to the MCP server
236+
- **`connected`** — Transport connection established, but capabilities not yet discovered
237+
- **`discovering`** — Fetching server capabilities (tools, resources, prompts)
238+
- **`ready`** — Fully connected with capabilities available
239+
- **`failed`** — Connection failed at some point
240+
241+
**State transitions:**
242+
- Non-OAuth servers: `connecting``connected``discovering``ready`
243+
- OAuth servers: `authenticating` → (callback) → `connecting``connected``discovering``ready`
244+
- Any state can transition to `failed` on error
245+
246+
Use this method to monitor connection status, list available tools, or build UI for connected servers.
232247

233248
## OAuth Configuration
234249

@@ -251,6 +266,28 @@ export class MyAgent extends Agent<Env, never> {
251266

252267
You can also provide a `customHandler` function for full control over the callback response. Refer to the [OAuth handling guide](/agents/guides/oauth-mcp-client) for details.
253268

269+
## Connection States
270+
271+
The `MCPConnectionState` enum defines all possible connection states:
272+
273+
<TypeScriptExample>
274+
275+
```ts title="src/index.ts"
276+
import { MCPConnectionState } from "agents/mcp";
277+
278+
// Access state constants
279+
MCPConnectionState.AUTHENTICATING // "authenticating"
280+
MCPConnectionState.CONNECTING // "connecting"
281+
MCPConnectionState.CONNECTED // "connected"
282+
MCPConnectionState.DISCOVERING // "discovering"
283+
MCPConnectionState.READY // "ready"
284+
MCPConnectionState.FAILED // "failed"
285+
```
286+
287+
</TypeScriptExample>
288+
289+
Use these constants when comparing connection states to ensure type safety and avoid string typos.
290+
254291
## Error Handling
255292

256293
Use error detection utilities to handle connection errors:
@@ -278,6 +315,12 @@ export class MyAgent extends Agent<Env, never> {
278315

279316
</TypeScriptExample>
280317

318+
:::note[Discovery error handling]
319+
320+
Starting with the latest release, MCP client discovery failures now throw errors immediately instead of silently continuing with empty tool arrays. This ensures connection issues are caught early and handled explicitly. Monitor the connection `state` field to detect `failed` states.
321+
322+
:::
323+
281324
## Next Steps
282325

283326
- [Connect your first MCP server](/agents/guides/connect-mcp-client) — Tutorial to get started

0 commit comments

Comments
 (0)