Skip to content

Commit c0ca5d7

Browse files
docs: update MCP client API for enum-based connection states
Synchronizes documentation with cloudflare/agents PR #672 which introduces enum-based connection state management. Changes: - Document new MCPConnectionState enum export with usage examples - Update getMcpServers() return type to use MCPConnectionState enum - Add examples showing type-safe state checking with enum constants - Document fail-fast behavior during capability discovery Related PR: cloudflare/agents#672 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 6cff60b commit c0ca5d7

File tree

1 file changed

+51
-29
lines changed

1 file changed

+51
-29
lines changed

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

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,48 @@ sidebar:
99

1010
import { Render, TypeScriptExample } from "~/components";
1111

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:
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+
The Agents SDK exports an `MCPConnectionState` enum for type-safe connection state checking:
1317

1418
<TypeScriptExample>
1519

1620
```ts title="src/index.ts"
17-
import { Agent, type AgentNamespace } from "agents";
18-
19-
type Env = {
20-
MyAgent: AgentNamespace<MyAgent>;
21-
};
21+
import { Agent, MCPConnectionState } from "agents";
2222

2323
export class MyAgent extends Agent<Env, never> {
2424
async onRequest(request: Request): Promise<Response> {
25-
const url = new URL(request.url);
26-
27-
if (url.pathname === "/connect" && request.method === "POST") {
28-
const { id, authUrl } = await this.addMcpServer(
29-
"Weather API",
30-
"https://weather-mcp.example.com/mcp",
31-
);
25+
const mcpState = this.getMcpServers();
3226

33-
if (authUrl) {
34-
return new Response(JSON.stringify({ authUrl }), {
35-
headers: { "Content-Type": "application/json" },
36-
});
27+
for (const [serverId, server] of Object.entries(mcpState.servers)) {
28+
if (server.state === MCPConnectionState.READY) {
29+
console.log(`Server ${serverId} is ready`);
30+
} else if (server.state === MCPConnectionState.AUTHENTICATING) {
31+
console.log(`Server ${serverId} needs authentication`);
3732
}
38-
39-
return new Response(`Connected: ${id}`, { status: 200 });
4033
}
34+
35+
return new Response("OK");
4136
}
4237
}
4338
```
4439

4540
</TypeScriptExample>
4641

42+
The `MCPConnectionState` enum includes these values:
43+
44+
- **`MCPConnectionState.AUTHENTICATING`** (`"authenticating"`) — Waiting for OAuth authorization to complete
45+
- **`MCPConnectionState.CONNECTING`** (`"connecting"`) — Establishing transport connection to MCP server
46+
- **`MCPConnectionState.DISCOVERING`** (`"discovering"`) — Discovering server capabilities (tools, resources, prompts)
47+
- **`MCPConnectionState.READY`** (`"ready"`) — Fully connected and ready to use
48+
- **`MCPConnectionState.FAILED`** (`"failed"`) — Connection failed at some point
49+
50+
### Connection Behavior
51+
52+
When establishing connections, the MCP client follows a fail-fast approach during capability discovery. If any capability (tools, resources, or prompts) fails to load from the server, the entire connection will transition to the `FAILED` state immediately. This ensures that connections are only marked as `READY` when all advertised capabilities are successfully available.
53+
4754
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.
4855

4956
## Agent MCP Client Methods
@@ -183,19 +190,16 @@ None.
183190
An `MCPServersState` object containing:
184191

185192
```ts
193+
import type { MCPConnectionState } from "agents";
194+
186195
{
187196
servers: Record<
188197
string,
189198
{
190199
name: string;
191200
server_url: string;
192201
auth_url: string | null;
193-
state:
194-
| "authenticating"
195-
| "connecting"
196-
| "ready"
197-
| "discovering"
198-
| "failed";
202+
state: MCPConnectionState;
199203
capabilities: ServerCapabilities | null;
200204
instructions: string | null;
201205
}
@@ -211,24 +215,42 @@ An `MCPServersState` object containing:
211215
<TypeScriptExample>
212216

213217
```ts title="src/index.ts"
218+
import { Agent, MCPConnectionState } from "agents";
219+
214220
export class MyAgent extends Agent<Env, never> {
215221
async onRequest(request: Request): Promise<Response> {
216222
const url = new URL(request.url);
217223

218224
if (url.pathname === "/mcp-state") {
219225
const mcpState = this.getMcpServers();
220226

221-
return new Response(JSON.stringify(mcpState, null, 2), {
222-
headers: { "Content-Type": "application/json" },
223-
});
227+
// Check connection states using the enum
228+
const readyServers = Object.entries(mcpState.servers).filter(
229+
([_, server]) => server.state === MCPConnectionState.READY,
230+
);
231+
232+
return new Response(
233+
JSON.stringify(
234+
{
235+
total: Object.keys(mcpState.servers).length,
236+
ready: readyServers.length,
237+
servers: mcpState,
238+
},
239+
null,
240+
2,
241+
),
242+
{
243+
headers: { "Content-Type": "application/json" },
244+
},
245+
);
224246
}
225247
}
226248
}
227249
```
228250

229251
</TypeScriptExample>
230252

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.
253+
The `state` field is of type `MCPConnectionState` and can be compared using the enum constants (for example, `MCPConnectionState.READY`, `MCPConnectionState.AUTHENTICATING`). Use this method to monitor connection status, list available tools, or build UI for connected servers.
232254

233255
## OAuth Configuration
234256

0 commit comments

Comments
 (0)