import { useAgent } from "agents/react"; import { useCallback, useRef, useState } from "react"; import { createRoot } from "react-dom/client"; import { ThemeProvider } from "@cloudflare/agents-ui/hooks"; import { ConnectionIndicator, ModeToggle, PoweredByAgents, type ConnectionStatus } from "@cloudflare/agents-ui"; import { Button, Badge, Surface, Text, Empty } from "@cloudflare/kumo"; import { PlusIcon, PlugIcon, PlugsConnectedIcon, WrenchIcon, ChatTextIcon, DatabaseIcon, TrashIcon, SignInIcon, InfoIcon } from "@phosphor-icons/react"; import type { MCPServersState } from "agents"; import { nanoid } from "nanoid"; import "./styles.css"; let sessionId = localStorage.getItem("sessionId"); if (!sessionId) { sessionId = nanoid(8); localStorage.setItem("sessionId", sessionId); } function App() { const [connectionStatus, setConnectionStatus] = useState("connecting"); const mcpUrlInputRef = useRef(null); const mcpNameInputRef = useRef(null); const [mcpState, setMcpState] = useState({ prompts: [], resources: [], servers: {}, tools: [] }); const agent = useAgent({ agent: "my-agent", name: sessionId!, onClose: useCallback(() => setConnectionStatus("disconnected"), []), onMcpUpdate: useCallback((mcpServers: MCPServersState) => { setMcpState(mcpServers); }, []), onOpen: useCallback(() => setConnectionStatus("connected"), []) }); function openPopup(authUrl: string) { window.open( authUrl, "popupWindow", "width=600,height=800,resizable=yes,scrollbars=yes" ); } const handleMcpSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!mcpUrlInputRef.current || !mcpUrlInputRef.current.value.trim()) return; if (!mcpNameInputRef.current || !mcpNameInputRef.current.value.trim()) return; const serverName = mcpNameInputRef.current.value; const serverUrl = mcpUrlInputRef.current.value; agent.call("addServer", [serverName, serverUrl]); mcpUrlInputRef.current.value = ""; mcpNameInputRef.current.value = ""; }; const handleDisconnect = async (serverId: string) => { await agent.call("disconnectServer", [serverId]); }; const serverEntries = Object.entries(mcpState.servers); return (

MCP Client

MCP Client This Agent acts as an MCP client — dynamically connecting to remote MCP servers, handling OAuth authentication automatically, and aggregating tools, prompts, and resources from all connected servers. Add a server URL below to get started.
{/* Add Server Form */}
Connect to an MCP Server
{/* Connected Servers */}
Servers {serverEntries.length}
{serverEntries.length === 0 ? ( } title="No servers connected" description="Add an MCP server URL above to get started." /> ) : (
{serverEntries.map(([id, server]) => (
{server.name} {server.state}
{server.server_url} {server.state === "failed" && server.error && ( {server.error} )}
{server.state === "authenticating" && server.auth_url && ( )}
))}
)}
{/* Aggregated Data */} {mcpState.tools.length > 0 && (
Tools {mcpState.tools.length}
{mcpState.tools.map((tool) => (
{tool.name} {tool.serverId}
                      {JSON.stringify(tool, null, 2)}
                    
))}
)} {mcpState.prompts.length > 0 && (
Prompts {mcpState.prompts.length}
{mcpState.prompts.map((prompt) => ( {prompt.name}
                      {JSON.stringify(prompt, null, 2)}
                    
))}
)} {mcpState.resources.length > 0 && (
Resources {mcpState.resources.length}
{mcpState.resources.map((resource) => ( {resource.name}
                      {JSON.stringify(resource, null, 2)}
                    
))}
)}
); } createRoot(document.getElementById("root")!).render( );