JSON Web Token

Secure access and protect your MCP server using JSON Web Tokens.

For the complete documentation index, see llms.txt. Markdown variants of every page are available by appending .md to the URL.

To enable JWT authentication, you can use the jwtAuthMiddleware middleware on your app.

src/middleware.ts
import { jwtAuthMiddleware, type Middleware } from "xmcp";

const middleware: Middleware = [
  jwtAuthMiddleware({
    secret: process.env.JWT_SECRET!,
    algorithms: ["HS256"],
  }),
  // ... other middlewares
];

export default middleware;

You can customize the middleware using the configuration object containing the JWT secret and verify options.

Code
const middleware = jwtAuthMiddleware({
  secret: process.env.JWT_SECRET!,
  algorithms: ["HS256"],
  issuer: "https://example.com",
  audience: "https://example.com",
  subject: "user-id",
  expiresIn: "1h",
  notBefore: "1h",
  clockTolerance: 30,
});

Check out the jsonwebtoken library for more details on the configuration options.