HonoHub Logo

HonoHub

Token Based

Using tokens to authenticate requests

This is the most simple way to authenticate requests to your MCP server. You can use a token to authenticate requests to your MCP server.

import { Hono } from "hono";
import { McpServer } from "@hono/mcp";

const app = new Hono();
const mcpServer = new McpServer({
  name: "my-mcp-server",
  version: "1.0.0",
});

// Middleware to verify the token
app.use(async (c, next) => {
  const token = c.req.header("Authorization")?.split(" ")[1];
  if (!token) {
    return c.json({ error: "Unauthorized" }, 401);
  }

  // ...
  // Verify the token
  // ...

  await next();
});

// Route to handle MCP requests
app.all("/mcp", async (c) => {
  const transport = new StreamableHTTPTransport();
  await mcpServer.connect(transport);
  return transport.handleRequest(c);
});

export default app;

Users can generate a token from your platform and pass it in the Authorization header of the request. The way they pass it depends on the MCP Client / platform they are using.

You can also use bearerAuth middleware from hono to verify the token. Hono Documentation

On this page

No Headings
Become a sponsor