mcp: 401 hides the server's diagnostic body and refresh-loops forever on a dead OAuth session
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 68/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- typescript
- Lĩnh vực
- authentication, cli
Hướng nghiên cứu
Start by tracing the remote MCP HTTP OAuth refresh path used by opencode mcp auth <name>, and reproduce the failed session while checking ~/.local/share/opencode/log/opencode.log. Locate the 401 handling, refresh-token invalidation, and opencode mcp list output paths, then add or update tests for a post-refresh 401 and an invalid_grant response. Done means the 401 body and WWW-Authenticate code are logged, one failed refresh invalidates the credential, and the CLI shows the re-authentication hint without repeating the refresh loop.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Summary
The remote MCP HTTP client discards the authorization server's 401 diagnostic (response body and WWW-Authenticate header) and surfaces only a generic Server returned 401 after re-authentication (HTTP 401). When a successful token refresh produces an access token the server still rejects, the client repeats refresh → 401 on every startup in every project directory instead of invalidating the credential once and surfacing a needs-authentication state. In my case this loop ran hourly for ~4 hours before the refresh token itself expired.
Environment
- opencode version: 2.4.2 (matches the newest release tag
v2.4.2) - Client: TUI (startup MCP connect, logs in
~/.local/share/opencode/log/opencode.log) - Model and provider: Not relevant
- OS: Darwin 27.2.0 (darwin arm64)
- Terminal: TERM_PROGRAM=tmux, TERM=tmux-256color, COLORTERM=truecolor
- Shell: /bin/zsh
- Install/channel: latest
- Active plugins:
./local-plugins/server/forge-session-title,./local-plugins/server/otel-status,@devtheops/[email protected],@mohak34/[email protected],@nm156/[email protected],@plannotator/[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected]- none touch MCP auth
Reproduction
- Authenticate a remote MCP server over OAuth with
opencode mcp auth <name>(mine washttps://opencode.ai/console/mcp). - Have the authorization server invalidate that OAuth session server-side while the stored credential remains. In my case the console's MCP session binding died while the account itself stayed healthy.
- Start opencode in any directory and watch
~/.local/share/opencode/log/opencode.log.
Reproducible consistently, on every startup and in every project directory, until the refresh token expired.
Expected Behavior
- The server's 401 diagnostic is visible in the log: the response body (e.g.
The session's workspace no longer exists. Sign in again.) and theWWW-Authenticateerror code, next to the generic message. - One refresh attempt that still ends in 401 stops the cycle: invalidate the credential, mark the server as needing authentication, and point at the fix (
opencode mcp auth <name>), instead of retrying on every startup. - A refresh that fails with
400 invalid_grantsays so in user-facing output (opencode mcp list) with the re-auth hint.
Actual Behavior
The client refreshed successfully (HTTP 200), stored the new access token, got 401 again, and repeated the whole cycle on every startup in every directory for ~4 hours (hourly, from 12:39Z to 16:16Z on 2026-11-28). The loop only ended when the refresh token itself finally returned 400 invalid_grant, at which point opencode deleted the credential and the server moved to needs-authentication.
Log excerpt (same pattern in every directory, ~/.local/share/opencode/log/opencode.log ~L145906-148398):
message="mcp http authentication rejected" status=401 origin=https://opencode.ai method=POST
message="mcp oauth request started" operation=refresh
message="mcp oauth request succeeded" status=200
message="credential updated" credentialID=cred_21f421473001zTnX8StAuvoEA0 integrationID=mcp_eec8489fdfd1e19a
message="mcp http authentication rejected" status=401
message="mcp connect failed" server=opencode_console status.error="Server returned 401 after re-authentication (HTTP 401)"
# ... cycle repeats on every startup ...
message="mcp oauth request rejected" status=400 error=invalid_grant message="The refresh token is invalid"
message="mcp oauth credential invalidated" credentialID=cred_21f421473001zTnX8StAuvoEA0 reason="The refresh token is invalid"
message="credential removed" credentialID=cred_21f421473001zTnX8StAuvoEA0
What the server was actually returning (bodies observed across attempts, never surfaced by the client):
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer error="invalid_token"
{"error":"unauthorized","message":"The session's workspace no longer exists. Sign in again."}
{"error":"unauthorized","message":"The console rejected the session token."}
{"error":"unauthorized","message":"Authenticate with the console (OAuth) and retry."}
The account and workspace were healthy throughout (GET /console/api/user, /console/api/user/workspaces, /console/api/integrations all returned 200), and an access token fetched directly and POSTed to /console/mcp returned 200 with a valid initialize result. Only the MCP OAuth session was dead.
Additional Context
Suggested fixes, in order of impact:
- Log the 401 response body and
WWW-Authenticateerror code alongside the genericmcp connect failedmessage, soThe session's workspace no longer exists. Sign in again.is visible instead of onlyServer returned 401 after re-authentication. - Detect a 401 that survives a successful refresh and stop the loop: the refresh token is bound to the same dead session, so retrying cannot succeed. Invalidate the credential and move the server to needs-authentication after one such attempt.
- When the refresh token returns
invalid_grant, surface arun opencode mcp auth <name>hint inopencode mcp listoutput.
Workaround: opencode mcp auth opencode_console re-registered a fresh dynamic OAuth client (new client_id, PKCE, loopback port) and re-ran the browser authorization, after which the server connected with all tools. opencode mcp logout was not needed and would not have helped, because the stored credential was already unusable and unrefreshable.
Related issues: #56195 (same generic error message, bot-protected hosts), #60612 (concurrent refreshes deleting credentials).
- Ngôn ngữ chính
- TypeScript
- Star
- 230k
- Fork
- 31.3k
- Merge trung bình
- 5 giờ 32 phút
- Pull request đã merge (30 ngày)
- 453
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của anomalyco/opencode
-
area/desktop platform/macos status/repro-plausible type/bug version/v2
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 88/100
anomalyco/opencode#67064 · 4 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 Nửa ngày Mức phù hợp với người mới 82/100
anomalyco/opencode#67009 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area/web type/feature version/v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
anomalyco/opencode#67001 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
skills: a Claude-compatible SKILL.md that fails to parse is dropped without any logCó thể đã có người làm @liwenjie200543 đã nhận 2 ngày trước. Đang mởarea/plugins platform/linux reproduced type/bug version/v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
anomalyco/opencode#66970 · 4 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
tui: Mermaid renderer displays <b>/<strong> tags literally and ignores \n line breaksCó thể đã có người làm @argszero đã nhận 2 ngày trước. Đang mởarea/tui platform/wsl status/repro-simulated type/bug version/v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
anomalyco/opencode#66773 · 4 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của anomalyco/opencode
Issue tương tự
-
needs-triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
EPPIcenter/sampledb3#162 ·
-
Particle-1 modelĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100
tashfeenahmed/freellmapi#2169 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
via-triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 79/100
Maintainer thường phản hồi trong vòng 1 ngày
-
🎙️ task - feat(mechanic): pave git.pull.close, a close of a superseded pull with its why on recordĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
ehmpathy/rhachet-roles-ehmpathy#1576 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
vercel-labs/ui#1563 ·
Maintainer thường phản hồi trong vòng 1 ngày