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;