Fix Unauthorized mcp issue - #3188
Conversation
|
Warning Review limit reached
Next review available in: 46 minutes You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThe API now distinguishes upstream MCP credential rejection from caller authentication expiry. The workspace portal parses error codes before handling 401 responses and logs out only for caller authentication failures. ChangesMCP authorization error handling
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant MCPServer
participant FetchMCPServerInfo
participant APIError
participant Portal
participant LogoutHandler
MCPServer-->>FetchMCPServerInfo: HTTP 401
FetchMCPServerInfo->>APIError: MCPProxyUpstreamUnauthorized
APIError-->>Portal: Error code
Portal->>LogoutHandler: Response and error code
LogoutHandler-->>Portal: Preserve session for coded upstream error
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@platform-api/internal/utils/mcp_test.go`:
- Around line 127-130: Update the assertion in the MCP error test to require
appErr.HTTPStatus to equal http.StatusBadRequest exactly, rather than only
rejecting http.StatusUnauthorized. Preserve the existing error message context
while ensuring HTTP 403, 500, and other statuses fail the test.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 53e7b004-3e83-48be-b06c-f33d2422ee9f
📒 Files selected for processing (8)
platform-api/internal/apperror/catalog.goplatform-api/internal/apperror/codes.goplatform-api/internal/utils/mcp.goplatform-api/internal/utils/mcp_test.goportals/ai-workspace/src/apis/platformApis.tsportals/ai-workspace/src/auth/logout.tsportals/ai-workspace/src/clients/choreoApiClient.tsportals/ai-workspace/src/contexts/ChoreoUserContext.tsx
This pull request addresses a subtle but important distinction in error handling for authentication failures when proxying to upstream MCP servers. It ensures that a 401 Unauthorized response from an upstream MCP server is not mistaken for the user's own session expiring, which previously could have caused clients to log users out unnecessarily. The change introduces a new error code and status for this scenario, updates backend and frontend logic to handle it, and adds tests to pin the correct behavior.
Backend changes:
MCP_PROXY_UPSTREAM_UNAUTHORIZEDand corresponding error entry, ensuring that an upstream MCP server's 401 is surfaced as a 400 with a distinct code, not as a standard Unauthorized error. This prevents clients from logging out users when the error is not about their own session. [1] [2] [3]TestFetchMCPServerInfoUpstream401IsNotOurUnauthorized) to verify that an upstream 401 is mapped to the new error code and does not result in a 401 to the client.Frontend changes:
handleUnauthorizedResponsefunction and all its call sites to only trigger a logout if the error code is exactlyUNAUTHORIZED. 401 responses with other codes (such asMCP_PROXY_UPSTREAM_UNAUTHORIZED) no longer cause the session to be torn down. [1] [2] [3] [4] [5]These changes together ensure that only genuine user session expiry logs the user out, while upstream authentication issues are surfaced as errors without disrupting the user's session.