
# Elysia

> An MCP endpoint inside an [Elysia](https://elysiajs.com) 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

:pm-install{name="okmcp h3 elysia zod"}

```ts [src/index.ts]
import { 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`, not `post`.** JSON-RPC uses `POST`, but [legacy sessions](/protocols/legacy/sessions) also use `GET` and `DELETE`. With only `post`, the legacy era breaks.
- **Keep `parse: "none"`.** `okmcp` reads the request body itself. Elysia only parses a body when a handler uses it, so the route works without the option. With `parse: "none"`, a later change, such as adding `body` to the handler or a hook that reads it, cannot consume the body before `okmcp` reads 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](/security/auth) and [limits](/security/limits) are `okmcp` options. They run inside `mcp.fetch`, so you do not need Elysia plugins for them.

## OAuth Metadata

[`protectedResourceMetadata()`](/security/auth#protected-resource-metadata) returns the same kind of handler, so the RFC 9728 document mounts the same way:

```ts
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

```bash
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](https://elysiajs.com/integrations/node) for each target.

:read-more{to="/examples/deploy" title="Deploy Anywhere"}
