Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(serve-http): add resource_metadata to WWW-Authenticate per MCP spec + RFC 9728 (#1410)
Ryan Ayersd43fb631bc2a
diff --git a/src/commands/serve-http.ts b/src/commands/serve-http.tsindex 2cbae64bce..9b01a183f3 100644--- a/src/commands/serve-http.ts+++ b/src/commands/serve-http.ts@@ -843,6 +843,21 @@ export async function runServeHttp(engine: BrainEngine, options: ServeHttpOption// reverse proxies / tunnels; default to localhost for dev.const issuerUrl = new URL(publicUrl || `http://localhost:${port}`);+ // MCP authorization spec (2025-06-18 draft §5.1) and RFC 9728 require the+ // protected resource server to return its discovery metadata URL in the+ // WWW-Authenticate header on 401 responses:+ //+ // WWW-Authenticate: Bearer resource_metadata="<URL>"+ //+ // Clients (claude.ai, Cursor, every other MCP-aware OAuth client) use that+ // URL to find the authorization-server discovery doc + token endpoint+ // without the user having to paste those URLs manually. Pre-fix the header+ // shipped `Bearer error="invalid_token", ...` with no resource_metadata+ // parameter, so MCP clients couldn't begin the OAuth flow from a fresh+ // 401 — they would silently fail to connect with a generic "couldn't+ // reach the MCP server" error.+ const resourceMetadataUrl = `${issuerUrl.toString().replace(/\/$/, '')}/.well-known/oauth-protected-resource`;+// F9: cookie `secure` flag honors both the request's TLS state (req.secure// is set when express trust-proxy lands an X-Forwarded-Proto: https) AND// the operator's declared issuer protocol (so a Cloudflare-tunnel deploy@@ -1601,7 +1616,7 @@ export async function runServeHttp(engine: BrainEngine, options: ServeHttpOptionres.status(405).json({ jsonrpc: '2.0', error: { code: -32000, message: 'Method not allowed' }, id: null });});- app.post('/mcp', requireBearerAuth({ verifier: oauthProvider }), async (req: Request, res: Response) => {+ app.post('/mcp', requireBearerAuth({ verifier: oauthProvider, resourceMetadataUrl }), async (req: Request, res: Response) => {const startTime = Date.now();const authInfo = (req as any).auth as AuthInfo;@@ -1944,7 +1959,7 @@ export async function runServeHttp(engine: BrainEngine, options: ServeHttpOptionapp.post('/ingest',ingestRateLimiter,- requireBearerAuth({ verifier: oauthProvider, requiredScopes: ['write'] }),+ requireBearerAuth({ verifier: oauthProvider, requiredScopes: ['write'], resourceMetadataUrl }),express.raw({ type: '*/*', limit: ingestMaxBytes }),async (req: Request, res: Response) => {const startTime = Date.now();