Skip to content

Latest commit

 

History

History
53 lines (41 loc) · 1.67 KB

File metadata and controls

53 lines (41 loc) · 1.67 KB

MCP Server (createMcpHandler)

The simplest way to run a stateless MCP server on Cloudflare Workers. Uses createMcpHandler from the Agents SDK to handle all MCP protocol details in one line.

What it demonstrates

  • createMcpHandler — the Agents SDK helper that turns an McpServer factory into a Worker-compatible fetch handler
  • Minimal setup — define tools in a factory, pass the factory to createMcpHandler, done
  • Stateless — no Durable Objects, no persistent state, each request is independent

Running

pnpm install
pnpm start

Open the browser to see the built-in tool tester, or connect with the MCP Inspector at http://localhost:5173/mcp.

How it works

import { McpServer } from "@modelcontextprotocol/server";
import { createMcpHandler } from "agents/mcp/server";
import { z } from "zod";

function createServer() {
  const server = new McpServer({ name: "Hello MCP Server", version: "1.0.0" });
  server.registerTool(
    "hello",
    {
      description: "Returns a greeting",
      inputSchema: { name: z.string().optional() }
    },
    async ({ name }) => ({
      content: [{ type: "text", text: `Hello, ${name ?? "World"}!` }]
    })
  );
  return server;
}

export default {
  fetch(request, env, ctx) {
    return createMcpHandler(createServer)(request, env, ctx);
  }
} satisfies ExportedHandler;

Related examples