diff --git a/server/middleware/auth.ts b/server/middleware/auth.ts index d9c5ce5..08de7c4 100644 --- a/server/middleware/auth.ts +++ b/server/middleware/auth.ts @@ -14,8 +14,9 @@ export default defineEventHandler((event) => { path.startsWith('/_nuxt') || path.startsWith('/favicon') || path === '/login' || - path === '/.well-known/oauth-protected-resource' || - path.startsWith('/.well-known/oauth-protected-resource/') + // Claude MCP discovery — never 302 to /login (JSON 404 handlers serve these). + path === '/.well-known/oauth-authorization-server' || + path === '/.well-known/openid-configuration' ) { return } diff --git a/server/routes/.well-known/oauth-protected-resource.get.ts b/server/routes/.well-known/oauth-authorization-server.get.ts similarity index 55% rename from server/routes/.well-known/oauth-protected-resource.get.ts rename to server/routes/.well-known/oauth-authorization-server.get.ts index 31ee891..e7b45dd 100644 --- a/server/routes/.well-known/oauth-protected-resource.get.ts +++ b/server/routes/.well-known/oauth-authorization-server.get.ts @@ -1,7 +1,7 @@ -import { mcpOauthProtectedResource } from '~/server/utils/aigenMcp' - +/** Claude discovery probe — no OAuth; never redirect to /login. */ export default defineEventHandler((event) => { + setResponseStatus(event, 404) setResponseHeader(event, 'Content-Type', 'application/json') setResponseHeader(event, 'Cache-Control', 'no-store') - return mcpOauthProtectedResource() + return { error: 'not found' } }) diff --git a/server/routes/.well-known/oauth-protected-resource/mcp.get.ts b/server/routes/.well-known/openid-configuration.get.ts similarity index 55% rename from server/routes/.well-known/oauth-protected-resource/mcp.get.ts rename to server/routes/.well-known/openid-configuration.get.ts index 31ee891..e7b45dd 100644 --- a/server/routes/.well-known/oauth-protected-resource/mcp.get.ts +++ b/server/routes/.well-known/openid-configuration.get.ts @@ -1,7 +1,7 @@ -import { mcpOauthProtectedResource } from '~/server/utils/aigenMcp' - +/** Claude discovery probe — no OAuth; never redirect to /login. */ export default defineEventHandler((event) => { + setResponseStatus(event, 404) setResponseHeader(event, 'Content-Type', 'application/json') setResponseHeader(event, 'Cache-Control', 'no-store') - return mcpOauthProtectedResource() + return { error: 'not found' } }) diff --git a/server/utils/aigenMcp.ts b/server/utils/aigenMcp.ts index 6da7619..dd0f7ee 100644 --- a/server/utils/aigenMcp.ts +++ b/server/utils/aigenMcp.ts @@ -8,7 +8,6 @@ import { readAigenJob } from '~/server/utils/aigenJob' export const MCP_PROTOCOL_VERSION = '2025-03-26' export const MCP_SERVER_INFO = { name: 'aigen', version: '1.0.0' } export const MCP_SUPPORTED_PROTOCOL_VERSIONS = ['2025-03-26', '2025-06-18', '2025-11-25'] as const -export const MCP_RESOURCE = 'https://aigen.carrgarage.com/mcp' const ENGINE_ENUM = [...STUDIO2_IMAGE_ENGINES] @@ -68,13 +67,6 @@ export function applyMcpCors(event: H3Event) { if (origin) setResponseHeader(event, 'Vary', 'Origin') } -export function mcpOauthProtectedResource() { - return { - resource: MCP_RESOURCE, - authorization_servers: [] as string[] - } -} - export function resolveMcpProtocolVersion(requested: unknown) { const version = String(requested || '').trim() if ((MCP_SUPPORTED_PROTOCOL_VERSIONS as readonly string[]).includes(version)) return version @@ -97,11 +89,12 @@ export function jsonRpcResult(id: unknown, result: unknown) { } } -/** 401 with JSON-RPC body — never a Nuxt error page. */ +/** 401 with JSON-RPC body — never a Nuxt error page, never WWW-Authenticate (no OAuth). */ export function mcpUnauthorized(event: H3Event, id: unknown = null) { applyMcpCors(event) setResponseStatus(event, 401) setResponseHeader(event, 'Content-Type', 'application/json') + removeResponseHeader(event, 'WWW-Authenticate') return jsonRpcError(id, -32001, 'Unauthorized') } diff --git a/tests/aigen-api.test.mjs b/tests/aigen-api.test.mjs index dc6ac0a..7546491 100644 --- a/tests/aigen-api.test.mjs +++ b/tests/aigen-api.test.mjs @@ -1,6 +1,6 @@ import test from 'node:test' import assert from 'node:assert/strict' -import { readFileSync } from 'node:fs' +import { existsSync, readFileSync } from 'node:fs' import { fileURLToPath } from 'node:url' import { dirname, join } from 'node:path' import { @@ -75,7 +75,9 @@ test('aigen routes exist, stay on the studio-2 enqueue path, and 404 on xAIGen', assert.match(job, /stillAbsoluteUrl/) assert.match(middleware, /aigenBearerAuthorized/) assert.match(middleware, /aigenApiPathAllowed/) - assert.match(middleware, /\/\.well-known\/oauth-protected-resource/) + assert.match(middleware, /\/\.well-known\/oauth-authorization-server/) + assert.match(middleware, /\/\.well-known\/openid-configuration/) + assert.doesNotMatch(middleware, /oauth-protected-resource/) assert.match(authPaths, /path === '\/mcp'/) assert.match(mcp, /mcpBearerOk/) assert.match(mcp, /text\/event-stream/) @@ -88,15 +90,18 @@ test('aigen routes exist, stay on the studio-2 enqueue path, and 404 on xAIGen', assert.match(mcpUtil, /aigen_job/) assert.match(mcpUtil, /queueAigenGenerate/) assert.match(mcpUtil, /readAigenJob/) - assert.match(mcpUtil, /authorization_servers/) + assert.match(mcpUtil, /removeResponseHeader\(event,\s*'WWW-Authenticate'\)/) + assert.doesNotMatch(mcpUtil, /authorization_servers/) assert.match(mcpUtil, /2025-06-18/) assert.match(nuxt, /aigenApiKey:\s*process\.env\.AIGEN_API_KEY/) assert.match(bench, /studio2EngineDefaults/) - const oauthRoot = readFileSync(join(root, 'server/routes/.well-known/oauth-protected-resource.get.ts'), 'utf8') - const oauthMcp = readFileSync(join(root, 'server/routes/.well-known/oauth-protected-resource/mcp.get.ts'), 'utf8') - assert.match(oauthRoot, /mcpOauthProtectedResource/) - assert.match(oauthMcp, /mcpOauthProtectedResource/) + const as = readFileSync(join(root, 'server/routes/.well-known/oauth-authorization-server.get.ts'), 'utf8') + const oidc = readFileSync(join(root, 'server/routes/.well-known/openid-configuration.get.ts'), 'utf8') + assert.match(as, /not found/) + assert.match(oidc, /not found/) + assert.equal(existsSync(join(root, 'server/routes/.well-known/oauth-protected-resource.get.ts')), false) + assert.equal(existsSync(join(root, 'server/routes/.well-known/oauth-protected-resource/mcp.get.ts')), false) }) test('defaultLibraryFolderId prefers Inbox then the first folder', () => {