(
response: {
readonly status: number;
readonly headers: Readonly<Record<string, string>>;
readonly text: Effect.Effect<string, unknown>;
},
method: "GET" | "POST",
)
| 295 | ); |
| 296 | |
| 297 | const classify = ( |
| 298 | response: { |
| 299 | readonly status: number; |
| 300 | readonly headers: Readonly<Record<string, string>>; |
| 301 | readonly text: Effect.Effect<string, unknown>; |
| 302 | }, |
| 303 | method: "GET" | "POST", |
| 304 | ): Effect.Effect<McpShapeProbeResult | null> => |
| 305 | Effect.gen(function* () { |
| 306 | const contentType = readHeader(response.headers, "content-type") ?? ""; |
| 307 | const isSse = /^\s*text\/event-stream\b/i.test(contentType); |
| 308 | |
| 309 | if (isAuthStatus(response.status)) { |
| 310 | const wwwAuth = readHeader(response.headers, "www-authenticate"); |
| 311 | if (!wwwAuth || !/^\s*bearer\b/i.test(wwwAuth)) { |
| 312 | // Spec-non-compliant challenge (no `Bearer`). Before giving |
| 313 | // up, check whether the server still publishes RFC 9728 |
| 314 | // protected-resource metadata for this path — some real MCP |
| 315 | // servers (Datadog) do exactly this. |
| 316 | if (yield* probeProtectedResourceMetadata(client, url, timeoutMs)) { |
| 317 | return { kind: "mcp", requiresAuth: true } as const; |
| 318 | } |
| 319 | return { |
| 320 | kind: "not-mcp", |
| 321 | category: "auth-required", |
| 322 | reason: `${response.status} without Bearer WWW-Authenticate — not an MCP auth challenge`, |
| 323 | } as const; |
| 324 | } |
| 325 | // Spec-compliant MCP signal: the auth spec mandates a |
| 326 | // `resource_metadata=` attribute pointing at the server's |
| 327 | // RFC 9728 document. Real OAuth-protected MCP servers |
| 328 | // (sentry.dev, etc.) include it. This attribute is rare on |
| 329 | // unrelated OAuth services and is the cleanest accept signal |
| 330 | // we have when the 401 body is RFC 6750 OAuth-shape rather |
| 331 | // than JSON-RPC. |
| 332 | if (/(?:^|[\s,])resource_metadata\s*=/i.test(wwwAuth)) { |
| 333 | return { kind: "mcp", requiresAuth: true } as const; |
| 334 | } |
| 335 | // Looser RFC 6750 §3.1 signal: the Bearer challenge carries |
| 336 | // `error=` / `error_description=` auth-params. Real MCP |
| 337 | // servers (Supabase, GitHub Copilot, Vercel, Neon, Tavily, |
| 338 | // Replicate, ...) include this even when they omit |
| 339 | // `resource_metadata=`. The body alone isn't enough for |
| 340 | // those — Supabase, e.g., returns `{"message":"Unauthorized"}` |
| 341 | // which is neither JSON-RPC nor RFC 6750. The `error=` |
| 342 | // auth-param is the tiebreaker. |
| 343 | if (/(?:^|[\s,])error\s*=/i.test(wwwAuth)) { |
| 344 | return { kind: "mcp", requiresAuth: true } as const; |
| 345 | } |
| 346 | // SSE responses can't carry a JSON-RPC error envelope; accept the |
| 347 | // Bearer challenge alone in that case (rare but spec-permissible). |
| 348 | if (isSse) return { kind: "mcp", requiresAuth: true } as const; |
| 349 | // Fallback for MCP servers whose 401 omits |
| 350 | // `resource_metadata=`. Two body shapes count: |
| 351 | // - JSON-RPC error (cubic.dev: API-key auth, JSON-RPC |
| 352 | // errors end-to-end). |
| 353 | // - RFC 6750 OAuth Bearer error envelope `{error: |
| 354 | // "invalid_token", ...}` without GraphQL `{errors:[...]}` |
no test coverage detected