examples/mcp-server-adapters/README.md
This example demonstrates how to use MCP (Model Context Protocol) server endpoints with both Hono and Express server adapters.
@mastra/hono)@mastra/express)pnpm install
pnpm start:hono
pnpm start:express
With a server running, run the test script:
# Test Hono server (default port 3001)
pnpm test:mcp
# Test Express server (port 3002)
pnpm test:mcp -- --port 3002
Each server exposes the following MCP endpoints:
POST /api/mcp/{serverId}/mcp - Stateless HTTP transport for MCP messagesGET /api/mcp/{serverId}/sse - Establish SSE connectionPOST /api/mcp/{serverId}/messages - Send messages to SSE sessiongetWeather - Gets weather for a locationcalculate - Performs basic math operationsecho - Echoes back a messagecalculate - Performs basic math operationsYou can connect to these servers using @mastra/mcp's MCPClient:
import { MCPClient } from '@mastra/mcp';
const client = new MCPClient({
servers: {
main: {
// Use 'main-mcp' as the serverId (matches the key in Mastra's mcpServers config)
url: new URL('http://localhost:3001/api/mcp/main-mcp/mcp'),
},
},
});
// List available tools
const tools = await client.listTools();
// Execute a tool
const result = await tools['main_calculate'].execute({
operation: 'multiply',
a: 6,
b: 7,
});
The Express server demonstrates that MCP routes work correctly even when express.json() middleware is applied globally. The server adapter handles both pre-parsed request bodies (from middleware) and raw request streams.