MCP: WWW-Authenticate с resource_metadata для OAuth-обнаружения
All checks were successful
ci / test (push) Successful in 38s
ci / deploy (push) Successful in 2m37s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 08:36:18 +03:00
parent 22d87d8d01
commit 198eafa86f

View File

@@ -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;