You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to set up an MCP OAuth 2.1 server with theAuth (Hono example)
#3
theAuth can act as the OAuth 2.1 authorization server for an MCP tool server. This thread shows the smallest working setup with Hono. The reference page is docs.theauth.dev/mcp, and it covers every option.
(This thread was first posted when the project was called kavachOS. The example is rewritten for the current API. The old names are listed in the rename map.)
RFC 8414, Authorization Server Metadata (/.well-known/oauth-authorization-server)
RFC 7591, Dynamic Client Registration
RFC 8707, Resource Indicators
Install
pnpm add @glinr/theauth @glinr/theauth-hono hono
The OAuth server is a separate module
createTheAuth gives you the main instance. The OAuth server comes from createMcpModule in @glinr/theauth/mcp. That module has no built-in database: you pass storage callbacks. The maps below are for illustration only, so back them with your own database in production.
// lib/theauth.tsimport{createTheAuth}from'@glinr/theauth';import{createMcpModule}from'@glinr/theauth/mcp';importtype{McpAccessToken,McpAuthorizationCode,McpClient}from'@glinr/theauth/mcp';constclients=newMap<string,McpClient>();constcodes=newMap<string,McpAuthorizationCode>();consttokens=newMap<string,McpAccessToken>();constbyRefreshToken=newMap<string,string>();exportconsttheauth=awaitcreateTheAuth({database: {provider: 'sqlite',url: 'theauth.db'},baseUrl: 'https://auth.yourapp.com',});exportconstmcp=createMcpModule({config: {enabled: true,issuer: 'https://auth.yourapp.com',// public origin plus the path where you mount the adapterbaseUrl: 'https://auth.yourapp.com/api/theauth',signingSecret: process.env.MCP_SIGNING_SECRET!,// at least 32 charactersscopes: ['mcp:read','mcp:execute'],loginPage: 'https://auth.yourapp.com/login',},storeClient: async(client)=>{clients.set(client.clientId,client);},findClient: async(clientId)=>clients.get(clientId)??null,storeAuthorizationCode: async(code)=>{codes.set(code.code,code);},consumeAuthorizationCode: async(code)=>{constfound=codes.get(code)??null;if(found)codes.delete(code);// a code must be single usereturnfound;},storeToken: async(token)=>{tokens.set(token.accessToken,token);if(token.refreshToken)byRefreshToken.set(token.refreshToken,token.accessToken);},findTokenByRefreshToken: async(refreshToken)=>{constaccessToken=byRefreshToken.get(refreshToken);returnaccessToken ? (tokens.get(accessToken)??null) : null;},revokeToken: async(accessToken)=>{tokens.delete(accessToken);},// Return the signed-in user's ID from your session, or null to send them to loginPageresolveUserId: async(request)=>{voidrequest;returnnull;},});
Mount it and protect your tools
// server.tsimport{Hono}from'hono';importtype{McpSession}from'@glinr/theauth/mcp';import{theAuthHono}from'@glinr/theauth-hono';import{theauth,mcp}from'./lib/theauth.js';constapp=newHono<{Variables: {mcpSession: McpSession}}>();// Serves /mcp/register, /mcp/authorize and /mcp/token under /api/theauthapp.route('/api/theauth',theAuthHono(theauth,{ mcp }));// MCP clients look for the discovery documents at the root of your originapp.get('/.well-known/oauth-authorization-server',(c)=>c.json(mcp.getMetadata()));app.get('/.well-known/oauth-protected-resource',(c)=>c.json(mcp.getProtectedResourceMetadata()));// Your tool endpoints: validate the bearer token and required scopeapp.use('/tools/*',async(c,next)=>{constcheck=awaitmcp.requireScopes(c.req.raw,['mcp:read']);if(!check.authorized)returncheck.response;// 401 or 403 with the right headersc.set('mcpSession',check.session);awaitnext();});app.get('/tools/search',(c)=>{constsession=c.get('mcpSession');// session.userId, session.clientId, session.scopesreturnc.json({ok: true,user: session.userId});});exportdefaultapp;
Things worth knowing
The adapter registers its routes relative to the mount path, so the well-known documents also need the root-level routes shown above when you mount under /api/theauth.
The authorization server metadata advertises a revocation_endpoint and a jwks_uri, but the TypeScript adapters do not serve those two routes today.
createTheAuth accepts an mcp: { enabled: true } key, but it only creates the tables behind the theauth.mcp.register registry. It does not build the OAuth server.
Access tokens are HS256 JWTs. validateToken does not compare the audience with your resource URL on its own, see the reference page if you need that check.
Other frameworks use the same pattern, see the adapters overview. Questions are welcome below.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
theAuth can act as the OAuth 2.1 authorization server for an MCP tool server. This thread shows the smallest working setup with Hono. The reference page is docs.theauth.dev/mcp, and it covers every option.
(This thread was first posted when the project was called kavachOS. The example is rewritten for the current API. The old names are listed in the rename map.)
What it implements
S256challenge method only/.well-known/oauth-protected-resource)/.well-known/oauth-authorization-server)Install
The OAuth server is a separate module
createTheAuthgives you the main instance. The OAuth server comes fromcreateMcpModulein@glinr/theauth/mcp. That module has no built-in database: you pass storage callbacks. The maps below are for illustration only, so back them with your own database in production.Mount it and protect your tools
Things worth knowing
/api/theauth.revocation_endpointand ajwks_uri, but the TypeScript adapters do not serve those two routes today.createTheAuthaccepts anmcp: { enabled: true }key, but it only creates the tables behind thetheauth.mcp.registerregistry. It does not build the OAuth server.validateTokendoes not compare the audience with your resource URL on its own, see the reference page if you need that check.Other frameworks use the same pattern, see the adapters overview. Questions are welcome below.
All reactions