MCP: WWW-Authenticate с resource_metadata для OAuth-обнаружения
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -432,13 +432,19 @@ export function registerMcp(app: FastifyInstance, deps: McpDeps): ReviewTools {
|
|||||||
const tools = new ReviewTools(deps);
|
const tools = new ReviewTools(deps);
|
||||||
if (!deps.settings.mcp.enabled) return tools;
|
if (!deps.settings.mcp.enabled) return tools;
|
||||||
const authenticate = createMcpAuth(deps);
|
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) => {
|
app.post("/mcp", async (req, reply) => {
|
||||||
let personId: string;
|
let personId: string;
|
||||||
try {
|
try {
|
||||||
personId = (await authenticate(req.headers.authorization)).personId;
|
personId = (await authenticate(req.headers.authorization)).personId;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const status = e instanceof McpAuthError ? e.status : 401;
|
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" });
|
return reply.code(status).send({ error: e instanceof Error ? e.message : "unauthorized" });
|
||||||
}
|
}
|
||||||
const body = req.body as unknown;
|
const body = req.body as unknown;
|
||||||
|
|||||||
Reference in New Issue
Block a user