MCP auth retry leaves orphaned transport/session when 401 occurs during initialize
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 76/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- python
- 领域
- api, authentication, backend
调研方向
Start in api/core/mcp/auth_client.py, tracing _execute_with_retry, enter, _initialize, and connect_server, then inspect the repro assertion for the partial-session case. Verify that a 401 during initialization closes the existing ExitStack and clears session state before retrying, while the fully initialized path still retries correctly.
由索引模型根据 Issue 内容生成。
描述
Problem
MCPClientWithAuthRetry._execute_with_retry only tears down the MCP transport/ClientSession ExitStack when self._initialized is already True.
MCPClient.__enter__ sets _initialized = True after _initialize() returns. Inside _initialize → connect_server, the streamable-HTTP/SSE transport and ClientSession are already pushed onto _exit_stack before ClientSession.initialize() runs. A 401 from that handshake raises MCPAuthError while _initialized is still False.
On auth refresh + retry, the client therefore:
- Updates
Authorizationheaders - Calls
_initialize()again without closing the first ExitStack entries - Opens a second transport + session while the expired-token connection stays alive until the outer context exits
Why this matters
- Core MCP OAuth / session-binding path used by tool list + invoke
- Leaks concurrent MCP sessions (servers that bind one session per client can reject or confuse the second connect)
- Stale connection with the old token remains open alongside the refreshed one
Evidence
# Current gate in api/core/mcp/auth_client.py
if self._initialized:
self._exit_stack.close()
self._session = None
self._initialized = False
self._initialize()
self._initialized = True
Repro unit assertion (fails on main): after MCPAuthError with _initialized is False and a partial _session already attached, _exit_stack.close() is never called before the retry.
Proposed fix
Always close _exit_stack, clear _session, reset _initialized, and replace the ExitStack before retrying. Only call _initialize() again when the client was already fully initialized (list_tools / invoke path). The __enter__ retry path re-enters and initializes itself.
Related (not the same root cause)
- #41499 / #41482 — orphaned JSON-RPC responses crashing the receive loop (
base_session.py) - #42326 / #42327 — invoke skipping OAuth when custom headers are set (
mcp_tool/tool.py)
- 主要语言
- TypeScript
- 星标
- 157k
- 派生
- 24.7k
- 平均合并
- 22 小时 32 分钟
- 30 天内合并 PR
- 611
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
langgenius/dify 的其他 Issue
-
Annotation Reply: a stored score threshold of 0.0 is silently replaced with 1, disabling the feature 未关闭
难度 2/5 1-3 小时 新手友好度 88/100
langgenius/dify#42639 · 1 条评论 · 1 个 reaction ·
-
难度 2/5 1-3 小时 新手友好度 70/100
langgenius/dify#42468 · 1 条评论 · 1 个 reaction ·
-
🐞 bug
难度 2/5 1-3 小时 新手友好度 86/100
langgenius/dify#42446 · 1 个 reaction ·
-
难度 2/5 1-3 小时 新手友好度 88/100
langgenius/dify#42355 · 1 条评论 · 1 个 reaction ·
-
难度 2/5 1-3 小时 新手友好度 88/100
langgenius/dify#42350 · 1 条评论 · 1 个 reaction ·
相似的 Issue
-
calcite-components needs triage refactor
难度 2/5 1-3 小时 新手友好度 75/100
Esri/calcite-design-system#15203 ·
-
难度 2/5 1-3 小时 新手友好度 91/100
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
难度 1/5 1 小时以内 新手友好度 95/100
-
难度 2/5 1-3 小时 新手友好度 78/100
Automattic/studio#4908 ·
-
难度 2/5 1-3 小时 新手友好度 90/100