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: Improve MCP connection state documentation with enum usage
Enhanced documentation to properly reference MCPConnectionState enum:
- Import and use MCPConnectionState enum in all examples
- Update return type signatures to use typeof MCPConnectionState
- Add comprehensive state transition documentation
- Include breaking change callouts with migration guidance
- Document all connection states with detailed descriptions
This provides clearer guidance for developers using the updated API
from PR #672.
Your Agent can connect to external [Model Context Protocol (MCP)](https://modelcontextprotocol.io) servers to access tools, resources, and prompts. The `Agent` class provides three methods to manage MCP connections:
12
+
Your Agent can connect to external [Model Context Protocol (MCP)](https://modelcontextprotocol.io) servers to access tools, resources, and prompts. The `Agent` class provides three methods to manage MCP connections.
13
+
14
+
## Connection States
15
+
16
+
MCP connections progress through several states, represented by the `MCPConnectionState` enum:
17
+
18
+
```ts
19
+
import { MCPConnectionState } from"agents";
20
+
21
+
// Available states:
22
+
MCPConnectionState.AUTHENTICATING// Waiting for OAuth authorization
23
+
MCPConnectionState.CONNECTING// Establishing transport connection
24
+
MCPConnectionState.CONNECTED// Transport connected, capabilities not yet discovered
25
+
MCPConnectionState.DISCOVERING// Discovering server capabilities
26
+
MCPConnectionState.READY// Fully connected and ready to use
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.
52
77
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:
114
118
115
-
**When authentication is required:**
119
+
**When OAuth authentication is required:**
116
120
-**`id`** (string) — Unique identifier for this server connection
117
-
-**`state`**(`"authenticating"`) — Indicates OAuth authentication is needed
118
-
-**`authUrl`** (string) — OAuth authorization URL for user authentication
121
+
-**`state`**— `MCPConnectionState.AUTHENTICATING`
122
+
-**`authUrl`** (string) — OAuth authorization URL where the user must authenticate
119
123
120
-
**When connection is ready:**
124
+
**When connection is successful:**
121
125
-**`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
126
+
-**`state`**— `MCPConnectionState.READY`
127
+
-**`authUrl`**— `undefined`
124
128
125
129
#### Throws
126
130
127
-
Throws an error if the connection or discovery process fails. Always wrap `addMcpServer()` calls in try-catch blocks to handle connection failures gracefully.
131
+
- Throws an error if connection fails
132
+
- Throws an error if capability discovery fails
133
+
134
+
The server stays in storage after errors, allowing retry via `connectToServer(id)`
returnnewResponse(`Failed to connect: ${error.message}`, {
164
+
status: 500,
165
+
});
152
166
}
153
167
}
154
168
}
155
169
```
156
170
157
171
</TypeScriptExample>
158
172
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.
173
+
If the MCP server requires OAuth authentication, the result will include `authUrl` for user authentication. When connection and discovery succeed, the server is immediately in the `READY` state with all capabilities loaded.
160
174
161
-
:::note[Breaking Change: Error Handling Required]
175
+
:::caution[Breaking Change]
162
176
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.
177
+
As of this version, `addMcpServer()` now throws errors on connection or discovery failures instead of silently continuing. The method also returns a discriminated union with a `state` field instead of a simple object with `authUrl`. Wrap calls in try-catch blocks to handle errors:
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.
304
+
The `state` field follows the `MCPConnectionState` enum values:
305
+
-`authenticating` — Waiting for OAuth authorization
306
+
-`connecting` — Establishing transport connection
307
+
-`connected` — Transport connected, capabilities not yet discovered
308
+
-`discovering` — Discovering server capabilities (tools, resources, prompts)
309
+
-`ready` — Fully connected with all capabilities loaded
310
+
-`failed` — Connection failed
311
+
312
+
Use this method to monitor connection status, list available tools, or build UI for connected servers.
0 commit comments