Skip to content

Commit a99dca9

Browse files
hakonharnesHåkon Harnes
andauthored
fix(mcp): improve 401 detection for OAuth flow trigger (#362)
The `StreamableHTTPError` from `@modelcontextprotocol/sdk` stores the HTTP status in `error.code` (not `error.status`), and some servers return 'Authentication required' instead of 'Unauthorized' in their error messages. This caused the better-chatbot to incorrectly fall back to SSE transport instead of retrying with OAuth provider when connecting to MCP servers that require authentication. For example, I found that MCP servers that were built using the [FastMCP](https://github.com/punkpeye/fastmcp) Typescript framework did not work with better-chatbot. This PR addresses that. Co-authored-by: Håkon Harnes <hakonh@basis.no>
1 parent 5dd756b commit a99dca9

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

src/lib/ai/mcp/create-mcp-client.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,12 @@ function isUnauthorized(error: any): boolean {
434434
return (
435435
error instanceof UnauthorizedError ||
436436
error?.status === 401 ||
437+
error?.code === 401 ||
437438
error?.message?.includes("401") ||
438439
error?.message?.includes("Unauthorized") ||
439440
error?.message?.includes("invalid_token") ||
440-
error?.message?.includes("HTTP 401")
441+
error?.message?.includes("HTTP 401") ||
442+
error?.message?.includes("Authentication required")
441443
);
442444
}
443445

0 commit comments

Comments
 (0)