Skip to content

Commit 12ee0ff

Browse files
docs: update MCP client connection states and API
Updates documentation for PR #672 (feat: enums connection state) ## Changes - Added new "connected" state to MCPConnectionState enum - Documented new discoverIfConnected() method for manual capability discovery - Updated connection state machine documentation with detailed descriptions - Added note about discovery failures now throwing errors immediately - Updated getMcpServers() return type to include "connected" state ## API Changes - New connection state "connected" for servers with transport connected but capabilities not yet discovered - New method discoverIfConnected(serverId) for refreshing server capabilities - Discovery failures now throw errors immediately instead of continuing with empty arrays Related PR: cloudflare/agents#672 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 5e83d1d commit 12ee0ff

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

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

Lines changed: 65 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,63 @@ 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 connection status:
233+
234+
- **`authenticating`** — Waiting for OAuth authorization to complete
235+
- **`connecting`** — Establishing transport connection to MCP server
236+
- **`connected`** — Transport connection established, but capabilities not yet discovered
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+
### `discoverIfConnected()`
244+
245+
Discover server capabilities if the connection is in the `connected` state. This method transitions the connection from `connected` to `discovering` to `ready`.
246+
247+
```ts
248+
async discoverIfConnected(serverId: string): Promise<void>
249+
```
250+
251+
#### Parameters
252+
253+
- **`serverId`** (string, required) — Server connection ID returned from `addMcpServer()`
254+
255+
#### Returns
256+
257+
A Promise that resolves when discovery is complete (or immediately if the connection is not in the `connected` state).
258+
259+
#### Example
260+
261+
<TypeScriptExample>
262+
263+
```ts title="src/index.ts"
264+
export class MyAgent extends Agent<Env, never> {
265+
async onRequest(request: Request): Promise<Response> {
266+
const url = new URL(request.url);
267+
268+
if (url.pathname === "/refresh" && request.method === "POST") {
269+
const { serverId } = await request.json();
270+
271+
// Force refresh of server capabilities
272+
await this.mcp.discoverIfConnected(serverId);
273+
274+
return new Response("Capabilities refreshed", { status: 200 });
275+
}
276+
}
277+
}
278+
```
279+
280+
</TypeScriptExample>
281+
282+
This method is useful for refreshing server capabilities (for example, from a UI refresh button) or when you need to manually trigger discovery after a connection has been established.
283+
284+
:::note[Automatic Discovery]
285+
286+
In most cases, you do not need to call this method directly. When you call `addMcpServer()`, the connection automatically discovers capabilities for non-OAuth connections. For OAuth connections, discovery happens automatically after the OAuth flow completes.
287+
288+
:::
232289

233290
## OAuth Configuration
234291

@@ -278,6 +335,12 @@ export class MyAgent extends Agent<Env, never> {
278335

279336
</TypeScriptExample>
280337

338+
:::note[Discovery Failures]
339+
340+
MCP client discovery failures now throw errors immediately instead of continuing with empty arrays. This means that if a server fails to respond during capability discovery (tools, resources, prompts), the connection will transition to the `failed` state and throw an error. This provides faster feedback and prevents silent failures.
341+
342+
:::
343+
281344
## Next Steps
282345

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

0 commit comments

Comments
 (0)