Hono
An MCP endpoint inside a Hono app.
The handler defineMcpHandler returns has a fetch(request) method. Hono hands a route its raw web Request, so the MCP endpoint is one line in a Hono app. You do not need an H3 app.
#The Route
npm i okmcp h3 hono zodimport { Hono } from "hono";
import { defineMcpHandler, defineTool } from "okmcp";
import { z } from "zod";
const greet = defineTool({
name: "greet",
description: "Greets someone by name",
inputSchema: z.object({ name: z.string() }),
handler: async ({ name }) => ({ content: [{ type: "text", text: `Hello, ${name}!` }] }),
});
const mcp = defineMcpHandler({ name: "my-server", version: "1.0.0", tools: [greet] });
const app = new Hono()
.get("/", (c) => c.text("Hello Hono!"))
.all("/mcp", (c) => mcp.fetch(c.req.raw));
export default app;h3 is still a peer dependency, because okmcp is built on it. It is not imported in your code. The wiring has two rules:
- Use
all, notpost. JSON-RPC usesPOST, but legacy sessions also useGETandDELETE. With onlypost, the legacy era breaks. - Do not read the body first.
okmcpreads the request body itself. Middleware that consumes the body on this route, such as a validator, leaves nothing forokmcpto read. Middleware that only uses headers is fine, such ascors()andlogger().
mcp.fetch does not route, so the path is entirely Hono's. You can put the route under app.route("/api", …) or a basePath and nothing else changes. Origin checks, auth and limits are okmcp options. They run inside mcp.fetch, so you do not need Hono middleware for them.
#OAuth Metadata
protectedResourceMetadata() returns the same kind of handler, so the RFC 9728 document mounts the same way:
import { protectedResourceMetadata } from "okmcp/oauth";
const metadata = protectedResourceMetadata({
resource: "https://api.example.com/mcp",
authorizationServers: ["https://auth.example.com"],
});
app.get("/.well-known/oauth-protected-resource", (c) => metadata.fetch(c.req.raw));#Running It
export default app is the fetch-handler shape that Bun, Deno and Cloudflare Workers serve directly:
bun run src/index.tsOn Node, use @hono/node-server, or H3's own serve, which accepts any fetch handler:
import { serve } from "h3";
import app from "./src/index.ts";
serve({ fetch: app.fetch, port: 3000 });