From 198eafa86f5de68d334c7b98f51964feb6dd78b9 Mon Sep 17 00:00:00 2001 From: Yuri Yaryshev Date: Wed, 18 Nov 2026 08:36:18 +0300 Subject: [PATCH] =?UTF-8?q?MCP:=20WWW-Authenticate=20=D1=81=20resource=5Fm?= =?UTF-8?q?etadata=20=D0=B4=D0=BB=D1=8F=20OAuth-=D0=BE=D0=B1=D0=BD=D0=B0?= =?UTF-8?q?=D1=80=D1=83=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 --- packages/accounting_automation/src/mcp.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/accounting_automation/src/mcp.ts b/packages/accounting_automation/src/mcp.ts index 1472fbb..20c5707 100644 --- a/packages/accounting_automation/src/mcp.ts +++ b/packages/accounting_automation/src/mcp.ts @@ -432,13 +432,19 @@ export function registerMcp(app: FastifyInstance, deps: McpDeps): ReviewTools { const tools = new ReviewTools(deps); if (!deps.settings.mcp.enabled) return tools; const authenticate = createMcpAuth(deps); + // подсказка клиенту (Claude), где искать описание защищённого ресурса (RFC 9728) + let challenge = "Bearer"; + if (deps.settings.mcp.publicUrl) { + const u = new URL(deps.settings.mcp.publicUrl); + challenge = `Bearer resource_metadata="${u.origin}/.well-known/oauth-protected-resource${u.pathname.replace(/\/$/, "")}/mcp"`; + } app.post("/mcp", async (req, reply) => { let personId: string; try { personId = (await authenticate(req.headers.authorization)).personId; } catch (e) { const status = e instanceof McpAuthError ? e.status : 401; - if (status === 401) void reply.header("WWW-Authenticate", "Bearer"); + if (status === 401) void reply.header("WWW-Authenticate", challenge); return reply.code(status).send({ error: e instanceof Error ? e.message : "unauthorized" }); } const body = req.body as unknown;