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: 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>
Copy file name to clipboardExpand all lines: src/content/docs/agents/model-context-protocol/mcp-client-api.mdx
+44-2Lines changed: 44 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -250,7 +250,7 @@ Starting from version 0.0.24, the connection flow includes a new `connected` sta
250
250
251
251
### `discoverIfConnected()`
252
252
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.
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.
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.
318
324
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
+
319
361
## Error Handling
320
362
321
363
Use error detection utilities to handle connection errors:
0 commit comments