< blog

MCP on Cloudflare Workers

2026-03-25 · ZERO

MCP (Model Context Protocol) is how AI agents discover and call external tools. If your Cloudflare Worker already has service functions, adding MCP support takes one file. I did it for two projects. Both came in under 70 lines.

CLOUDFLARE WORKER Hono API /api/stops, /api/... MCP Server /api/mcp Your existing services Browser REST AI Agent MCP

The key insight: MCP tools just call the same service functions your API routes already use. No new business logic. The MCP layer is pure protocol translation.

The pattern

Two packages: @modelcontextprotocol/sdk for the server and agents/mcp from Cloudflare's agents package for the HTTP transport handler. Create an McpServer, register tools, export the handler.

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { createMcpHandler } from "agents/mcp"; const server = new McpServer({ name: "my-api", version: "1.0.0" }); server.tool( "find_nearby_stops", "Find transit stops near a location.", { latitude: z.number(), longitude: z.number() }, async ({ latitude, longitude }) => { // same function your REST endpoint calls const stops = await getNearbyStops(latitude, longitude); return { content: [{ type: "text", text: JSON.stringify(stops) }] }; }, ); export const mcpHandler = createMcpHandler(server);

Each tool() call is a name, a description agents read to decide when to call it, a Zod schema for the inputs, and a handler that returns content blocks. The handler calls the same service functions your Hono routes call. Nothing new to write.

Wiring it up

In your Worker's fetch handler, intercept the MCP path before Hono. Three lines:

if (url.pathname === "/api/mcp") { return mcpHandler(req, env, ctx); }

That's it. Any MCP-compatible agent can now connect to https://your-worker.workers.dev/api/mcp and discover your tools. The rest of your API keeps working exactly as before.

Why bother?

REST endpoints serve browsers. MCP endpoints serve agents. Same data, different consumers. An agent connecting via MCP gets tool descriptions, typed schemas, and a standard protocol for discovery. It doesn't need to parse your API docs or guess at endpoints.

I added MCP to a Berlin transit app (3 tools, 68 lines) and a travel planning app (4 tools, 59 lines). Both took under an hour. Both reuse 100% of their existing service code.


transit MCP source (68 lines)
modelcontextprotocol.io