mcp: 401 hides the server's diagnostic body and refresh-loops forever on a dead OAuth session

未关闭
#67,044 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
68/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
typescript

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

area/mcp platform/macos status/repro-simulated type/bug version/v2

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

Reproduction

  1. Authenticate a remote MCP server over OAuth with opencode mcp auth <name> (mine was https://opencode.ai/console/mcp).
  2. 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.
  3. 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

  1. 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 the WWW-Authenticate error code, next to the generic message.
  2. 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.
  3. A refresh that fails with 400 invalid_grant says 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:

  1. Log the 401 response body and WWW-Authenticate error code alongside the generic mcp connect failed message, so The session's workspace no longer exists. Sign in again. is visible instead of only Server returned 401 after re-authentication.
  2. 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.
  3. When the refresh token returns invalid_grant, surface a run opencode mcp auth <name> hint in opencode mcp list output.

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).

主要语言
TypeScript
星标
230k
派生
31.3k
平均合并
5 小时 32 分钟
30 天内合并 PR
453

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

anomalyco/opencode 的其他 Issue

查看 anomalyco/opencode 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。