Elysia
An MCP endpoint inside an Elysia app.
The handler defineMcpHandler returns has a fetch(request) method. Elysia hands a route its web Request, so the MCP endpoint is one line in an Elysia app. You do not need an H3 app.
#The Route
npm i okmcp h3 elysia zodimport { Elysia } from "elysia";
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 = defineMcpHandle;
new Elysia()
.get("/", () => "Hello Elysia!")
.all("/mcp", ({ request }) => mcp.fetch(request), { parse: "none" })
.listen(3000);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. - Keep
parse: "none".okmcpreads the request body itself. Elysia only parses a body when a handler uses it, so the route works without the option. Withparse: "none", a later change, such as addingbodyto the handler or a hook that reads it, cannot consume the body beforeokmcpreads it.
mcp.fetch does not route, so the path is entirely Elysia's. You can put the route in a group with a prefix and nothing else changes. Origin checks, auth and limits are okmcp options. They run inside mcp.fetch, so you do not need Elysia plugins 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", ({ request }) => metadata.fetch(request));#Running It
bun run src/index.ts.listen() uses Bun's server. For Node, Workers and other runtimes, Elysia uses an adapter. The forwarding route stays the same; see Elysia's integration guides for each target.