Skip to content

Commit e9c93e1

Browse files
docs: enhance MCP connection documentation with state machine and breaking changes
Improves the MCP client API documentation with: - Enhanced discoverIfConnected() description to clarify it works with both connected and ready states - Added comprehensive Connection State Machine section showing OAuth and non-OAuth flows - Added detailed breaking changes notice explaining the separation of connection and discovery - Improved use case documentation for discoverIfConnected() - Added note about when the method skips discovery based on connection state These additions provide clearer guidance for developers migrating to the new connection flow and understanding the connection lifecycle. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 2e3bca2 commit e9c93e1

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

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

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ Starting from version 0.0.24, the connection flow includes a new `connected` sta
250250

251251
### `discoverIfConnected()`
252252

253-
Discover server capabilities if the connection is in the `connected` state. This method transitions the connection from `connected` to `discovering` to `ready`.
253+
Discover or refresh server capabilities for a connected MCP server. This method transitions the connection from `connected` or `ready` state to `discovering` state, then fetches available tools, resources, and prompts from the server.
254254

255255
```ts
256256
async discoverIfConnected(serverId: string): Promise<void>
@@ -287,7 +287,13 @@ export class MyAgent extends Agent<Env, never> {
287287

288288
</TypeScriptExample>
289289

290-
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.
290+
This method is useful for:
291+
292+
- Refreshing server capabilities after a server update
293+
- Implementing a manual refresh button in your UI
294+
- Re-discovering capabilities if they were not loaded during initial connection
295+
296+
The method only performs discovery if the connection is in `connected` or `ready` state. If the connection is in any other state (such as `authenticating`, `connecting`, or `failed`), the method will skip discovery and emit an observability event.
291297

292298
:::note[Automatic Discovery]
293299

@@ -316,6 +322,42 @@ export class MyAgent extends Agent<Env, never> {
316322

317323
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.
318324

325+
## Connection State Machine
326+
327+
MCP connections follow a well-defined state machine to manage the connection lifecycle:
328+
329+
**Non-OAuth flow:**
330+
1. **`connecting`** — Transport connection establishing
331+
2. **`connected`** — Transport established, awaiting capability discovery
332+
3. **`discovering`** — Fetching server capabilities (tools, resources, prompts)
333+
4. **`ready`** — Fully operational with all capabilities available
334+
335+
**OAuth flow:**
336+
1. **`authenticating`** — User must complete OAuth authorization
337+
2. **`connecting`** — Transport connection establishing (after OAuth callback)
338+
3. **`connected`** — Transport established, awaiting capability discovery
339+
4. **`discovering`** — Fetching server capabilities (tools, resources, prompts)
340+
5. **`ready`** — Fully operational with all capabilities available
341+
342+
Any state can transition to **`failed`** if an error occurs during the connection process.
343+
344+
:::note[Breaking Change in Connection Flow]
345+
346+
Starting from version 0.0.24, the connection establishment and capability discovery are separated into distinct steps:
347+
348+
**What changed:**
349+
350+
- `ClientConnection.init()` no longer automatically discovers capabilities
351+
- A new `connected` state indicates when transport is established but capabilities have not been discovered yet
352+
- Discovery now happens via `discoverIfConnected()` which is called automatically by `addMcpServer()` for non-OAuth connections
353+
- For OAuth connections, `establishConnection()` handles both connection and discovery after authorization completes
354+
355+
**Migration:**
356+
357+
Most users will not need to change their code, as the `Agent` class handles discovery automatically. However, if you are using the lower-level `MCPClientManager` API directly, you must call `discoverIfConnected()` after establishing a connection.
358+
359+
:::
360+
319361
## Error Handling
320362

321363
Use error detection utilities to handle connection errors:

0 commit comments

Comments
 (0)