From a7946c9f0e49c46d9fa680d786d601c541289cad Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 5 Nov 2026 09:45:51 +0000 Subject: [PATCH] feat(mcp): personal API key for clients without OAuth login POST /mcp/oauth/api-key (site login, not admin or impersonated) returns a year-long access token to send as Authorization: Bearer. Disconnect-all revokes it like every other MCP token. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012GTicqnHNZPYxcmrvzQsZs --- mcp/__tests__/mcp.controller.spec.ts | 18 ++++++++++++++++++ mcp/oauth/mcp-oauth.controller.ts | 12 +++++++++++- mcp/oauth/mcp-oauth.provider.ts | 14 ++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/mcp/__tests__/mcp.controller.spec.ts b/mcp/__tests__/mcp.controller.spec.ts index cff1c0a..986e2b0 100644 --- a/mcp/__tests__/mcp.controller.spec.ts +++ b/mcp/__tests__/mcp.controller.spec.ts @@ -241,6 +241,24 @@ describe('MCP endpoint with OAuth login', () => { expect(login.headers['x-frame-options']).toBe('DENY'); }); + it('a personal key works as a bearer token until disconnect', async () => { + const provider = app.get(McpOAuthProvider); + expect(await provider.issueApiKey(9)).toBeNull(); + const apiKey = await provider.issueApiKey(42); + const whoami = () => + request(server()) + .post('/mcp') + .set('Authorization', `Bearer ${apiKey}`) + .set('Accept', ACCEPT) + .send({ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name: 'whoami', arguments: {} } }); + const call = await whoami(); + expect(call.status).toBe(200); + expect(JSON.parse(call.body.result.content[0].text).userId).toBe(42); + + await provider.disconnectAll(42); + expect((await whoami()).status).toBe(401); + }); + it('refuses GET', async () => { expect((await request(server()).get('/mcp')).status).toBe(405); }); diff --git a/mcp/oauth/mcp-oauth.controller.ts b/mcp/oauth/mcp-oauth.controller.ts index c295386..36a6e2b 100644 --- a/mcp/oauth/mcp-oauth.controller.ts +++ b/mcp/oauth/mcp-oauth.controller.ts @@ -1,4 +1,4 @@ -import { Body, Controller, Post, Request, Res, UseGuards } from '@nestjs/common'; +import { Body, Controller, ForbiddenException, Post, Request, Res, UseGuards } from '@nestjs/common'; import { Request as ExpressRequest, Response } from 'express'; import { JwtAuthGuard } from '@shared/auth/jwt-auth.guard'; import { AuthenticatedRequest } from '@shared/auth/auth.types'; @@ -60,6 +60,16 @@ export class McpOAuthController { res.redirect(302, this.provider.issueCodeRedirect(request, user.id)); } + /** "Personal key" in the app settings, for AI apps whose OAuth login doesn't work. Not for admins or impersonation. */ + @UseGuards(JwtAuthGuard) + @Post('api-key') + async apiKey(@Request() req: AuthenticatedRequest) { + const userId = req.user?.impersonated ? undefined : getUserIdFromUser(req.user, true); + const apiKey = userId ? await this.provider.issueApiKey(userId) : null; + if (!apiKey) throw new ForbiddenException(); + return { apiKey }; + } + /** "Disconnect" in the app settings: signs every connected AI client out. */ @UseGuards(JwtAuthGuard) @Post('disconnect') diff --git a/mcp/oauth/mcp-oauth.provider.ts b/mcp/oauth/mcp-oauth.provider.ts index 3da6d24..fcd8671 100644 --- a/mcp/oauth/mcp-oauth.provider.ts +++ b/mcp/oauth/mcp-oauth.provider.ts @@ -24,6 +24,7 @@ const ACCESS_TOKEN_SECONDS = 60 * 60; const REFRESH_TOKEN_SECONDS = 60 * 60 * 24 * 90; const CODE_SECONDS = 5 * 60; const LOGIN_REQUEST_SECONDS = 15 * 60; +const API_KEY_SECONDS = 60 * 60 * 24 * 365; /** A pending authorization, carried through the login form as a signed token. */ export interface LoginRequest { @@ -179,6 +180,19 @@ export class McpOAuthProvider implements OAuthServerProvider { if (payload) await this.disconnectAll(payload.userId); } + /** + * A personal key for clients that can't do the OAuth login (sent as `Authorization: Bearer `). + * It is an access token valid for a year; "disconnect all" revokes it with every other token. + */ + async issueApiKey(userId: number): Promise { + const version = await this.tokenVersion(userId); + if (version === null) return null; + return this.jwt.sign( + { typ: 'access', clientId: 'api-key', userId, scopes: [], v: version }, + { expiresIn: API_KEY_SECONDS }, + ); + } + /** Invalidates every MCP token the user holds, across all clients. */ async disconnectAll(userId: number): Promise { const user = await this.userRepository.findOneBy({ id: userId });