What it demonstrates
addMcpServer/removeMcpServer- managing MCP server connections from an AgentonMcpUpdate- real-time state updates pushed to the React frontend via WebSocket- OAuth popup flow -
configureOAuthCallbackwith a custom handler that closes the popup after auth agentFetch- making HTTP requests to the Agent’s custom endpoints from the client
Server Implementation
src/server.ts
Client Implementation
The React frontend usesuseAgent with onMcpUpdate to receive real-time server state:
src/client.tsx
How It Works
1
Agent manages connections
The Agent manages MCP server connections via the built-in
mcp property. Each connection maintains state about available tools, prompts, and resources.2
OAuth configuration
configureOAuthCallback sets up the OAuth flow. When an MCP server requires authentication, a popup window opens. After auth completes, the custom handler closes the popup automatically.3
Real-time updates
When servers are added, removed, or change state, the Agent broadcasts updates via WebSocket. The client’s
onMcpUpdate callback receives the new state.4
Call server methods
The client uses
agent.call() to invoke callable methods on the Agent, like adding or removing servers.OAuth Flow
For MCP servers that require authentication:MCP Server State
TheonMcpUpdate callback receives an array of server info:
Using MCP Tools in AI Chat
Combine MCP client with AI chat:Running the Example
1
Install dependencies
2
Configure environment
Copy Set
.env.example to .env:HOST to your callback host (usually http://localhost:5173 for local dev).3
Start the server
4
Connect to a server
Open http://localhost:5173, enter an MCP server URL, and click Connect.To test with authentication, run the
mcp-worker-authenticated example alongside this one.Testing with Another Example
Run the MCP Server example in another terminal:- Name:
Demo - URL:
http://localhost:5174/mcp(note the different port)
Related Examples
MCP Server
Build a stateful MCP server
MCP Authenticated
MCP server with OAuth authentication
AI Chat
Use MCP tools in AI chat (includes MCP client)
MCP Guide
In-depth guide to Model Context Protocol