Skip to content

oauth-hostBe the authorization server

OAuth 2.1 + OpenID Connect for an Express/Mongoose app you already have. Mount two routers and Claude, ChatGPT, or any other MCP client can connect to your API as a user.

js
import express from 'express';
import mongoose from 'mongoose';
import { createOAuthHost } from '@jeffjassky/oauth-host';

await mongoose.connect(process.env.MONGO_URL);

const oauth = createOAuthHost({
  connection: mongoose,
  issuer: 'https://api.example.com',
  resources: [{ id: 'https://api.example.com/mcp', label: 'MCP server' }],
  scopes: [
    { id: 'openid',        label: 'Sign you in' },
    { id: 'contacts.read', label: 'Read your contacts' },
  ],
  consentUrl: '/settings/authorize',
  loginUrl: '/login',
});

await oauth.syncIndexes();

const app = express();
app.use(express.json());              // yours, not ours

app.use(oauth.routes.discovery);      // origin root — /.well-known/* is fixed
app.use('/oauth', oauth.routes.oauth);
app.use('/mcp', oauth.protect('contacts.read'), mcpRouter);

Released under the MIT License.