Skip to main content
An Agent that acts as an MCP client - dynamically connects to remote MCP servers, handles OAuth authentication, and aggregates tools, prompts, and resources from all connected servers.

What it demonstrates

  • addMcpServer / removeMcpServer - managing MCP server connections from an Agent
  • onMcpUpdate - real-time state updates pushed to the React frontend via WebSocket
  • OAuth popup flow - configureOAuthCallback with 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 uses useAgent 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

The onMcpUpdate 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 .env.example to .env:
Set 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:
Then in this example, add the server:
  • Name: Demo
  • URL: http://localhost:5174/mcp (note the different port)
You’ll see the server’s tools and resources appear in the UI.

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