import '@mcp-b/global';
import { McpClientProvider, useWebMCP, useMcpClient } from '@mcp-b/react-webmcp';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { TabClientTransport } from '@mcp-b/transports';
import { z } from 'zod';
const client = new Client({ name: 'MyApp', version: '1.0.0' });
const transport = new TabClientTransport('mcp');
function App() {
return (
<McpClientProvider client={client} transport={transport}>
<Counter />
</McpClientProvider>
);
}
function Counter() {
const [count, setCount] = useState(0);
const { client, tools, isConnected } = useMcpClient();
// Register tool
useWebMCP({
name: 'increment',
description: 'Increment counter',
inputSchema: { amount: z.number().default(1) },
handler: async ({ amount }) => {
setCount(prev => prev + amount);
return { newValue: count + amount };
}
});
// Call tool
const callTool = async () => {
const res = await client.callTool({
name: 'increment',
arguments: { amount: 5 }
});
};
return (
<div>
<p>Count: {count}</p>
<button onClick={callTool} disabled={!isConnected}>
Increment
</button>
</div>
);
}