Skip to content

Commit eb32b10

Browse files
docs: update MCP connection states to include CONNECTED state and enum constants
This updates the documentation to reflect PR #672 which: - Adds MCPConnectionState enum constants (AUTHENTICATING, CONNECTING, CONNECTED, DISCOVERING, READY, FAILED) - Introduces new CONNECTED state in the connection state machine - Improves type safety by providing enum-like constants instead of string literals Updated files: - Added new CONNECTED state to connection state type definitions - Documented the MCPConnectionState constants with usage examples - Updated OAuth guide with connection state flow including CONNECTED state - Added examples showing enum constant usage for better type safety 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 38e601b commit eb32b10

File tree

2 files changed

+106
-4
lines changed

2 files changed

+106
-4
lines changed

src/content/docs/agents/guides/oauth-mcp-client.mdx

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export class MyAgent extends Agent<Env, never> {
201201
([id, server]) => ({
202202
serverId: id,
203203
name: server.name,
204-
state: server.state, // "authenticating" | "connecting" | "ready" | "failed"
204+
state: server.state, // "authenticating" | "connecting" | "connected" | "discovering" | "ready" | "failed"
205205
isReady: server.state === "ready",
206206
needsAuth: server.state === "authenticating",
207207
authUrl: server.auth_url,
@@ -220,7 +220,7 @@ export class MyAgent extends Agent<Env, never> {
220220

221221
</TypeScriptExample>
222222

223-
Connection states: `authenticating` (needs OAuth) > `connecting` (completing setup) > `ready` (available for use)
223+
Connection states: `authenticating` (needs OAuth) > `connecting` (establishing transport) > `connected` (transport established) > `discovering` (loading capabilities) > `ready` (available for use)
224224

225225
## Handle authentication failures
226226

@@ -296,6 +296,59 @@ Common failure reasons:
296296

297297
Failed connections remain in state until you remove them with `removeMcpServer(serverId)`.
298298

299+
## Use connection state constants
300+
301+
For better type safety, use the `MCPConnectionState` enum constants instead of string literals when checking connection states:
302+
303+
<TypeScriptExample>
304+
305+
```tsx title="src/App.tsx"
306+
import { useAgent, MCPConnectionState } from "agents/react";
307+
import type { MCPServersState } from "agents";
308+
309+
function App() {
310+
const [mcpState, setMcpState] = useState<MCPServersState>({
311+
prompts: [],
312+
resources: [],
313+
servers: {},
314+
tools: [],
315+
});
316+
317+
const agent = useAgent({
318+
agent: "my-agent",
319+
name: "session-id",
320+
onMcpUpdate: (mcpServers: MCPServersState) => {
321+
setMcpState(mcpServers);
322+
},
323+
});
324+
325+
return (
326+
<div>
327+
{Object.entries(mcpState.servers).map(([id, server]) => (
328+
<div key={id}>
329+
<strong>{server.name}</strong>: {server.state}
330+
{server.state === MCPConnectionState.AUTHENTICATING && server.auth_url && (
331+
<button onClick={() => window.open(server.auth_url, "_blank")}>
332+
Authorize
333+
</button>
334+
)}
335+
{server.state === MCPConnectionState.FAILED && (
336+
<button onClick={() => handleRetry(id)}>Retry</button>
337+
)}
338+
{server.state === MCPConnectionState.READY && (
339+
<span>✓ Ready</span>
340+
)}
341+
</div>
342+
))}
343+
</div>
344+
);
345+
}
346+
```
347+
348+
</TypeScriptExample>
349+
350+
Using constants prevents typos and provides better IDE autocomplete. See the [Connection State Constants](/agents/model-context-protocol/mcp-client-api/#connection-state-constants) section for the full list.
351+
299352
## Complete example
300353

301354
This example demonstrates a complete Cloudflare Observability OAuth integration. Users connect to Cloudflare Observability, authorize in a popup window, and the connection becomes available. The Agent provides endpoints to connect, check status, and disconnect.

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

Lines changed: 51 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,55 @@ 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 state:
233+
234+
- `authenticating` — Waiting for OAuth authorization to complete
235+
- `connecting` — Establishing transport connection to MCP server
236+
- `connected` — Transport connection established successfully
237+
- `discovering` — Discovering server capabilities (tools, resources, prompts)
238+
- `ready` — Fully connected and ready to use
239+
- `failed` — Connection failed at some point
240+
241+
Use this method to monitor connection status, list available tools, or build UI for connected servers.
242+
243+
## Connection State Constants
244+
245+
The `MCPConnectionState` object provides enum-like constants for working with connection states:
246+
247+
<TypeScriptExample>
248+
249+
```ts title="src/index.ts"
250+
import { Agent, MCPConnectionState } from "agents";
251+
252+
export class MyAgent extends Agent<Env, never> {
253+
async onRequest(request: Request): Promise<Response> {
254+
const mcpState = this.getMcpServers();
255+
256+
for (const [serverId, server] of Object.entries(mcpState.servers)) {
257+
if (server.state === MCPConnectionState.READY) {
258+
console.log(`Server ${serverId} is ready`);
259+
} else if (server.state === MCPConnectionState.AUTHENTICATING) {
260+
console.log(`Server ${serverId} needs authentication`);
261+
}
262+
}
263+
264+
return new Response("OK");
265+
}
266+
}
267+
```
268+
269+
</TypeScriptExample>
270+
271+
Available constants:
272+
273+
- `MCPConnectionState.AUTHENTICATING``"authenticating"`
274+
- `MCPConnectionState.CONNECTING``"connecting"`
275+
- `MCPConnectionState.CONNECTED``"connected"`
276+
- `MCPConnectionState.DISCOVERING``"discovering"`
277+
- `MCPConnectionState.READY``"ready"`
278+
- `MCPConnectionState.FAILED``"failed"`
279+
280+
Using these constants instead of string literals provides better type safety and prevents typos.
232281

233282
## OAuth Configuration
234283

0 commit comments

Comments
 (0)