bug(api): MCP tool invoke drops OAuth Authorization when any custom header is set

未关闭 适合新手
#42,326 1 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

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

调研方向

从 api/core/tools/mcp_tool/tool.py 中的 MCPTool.invoke_remote_mcp_tool 开始,然后将其 header 处理与 api/services/tools/mcp_tools_manage_service.py 中的 _prepare_auth_headers 进行比较。使用自定义的 X-Tenant header 和已存储的 OAuth token 重现该单元测试用例,并验证调用会保留自定义 header、包含 Authorization,同时不会导致列表功能回归。

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

描述

Self Checks
  • I have read the Contributing Guide and Language Policy.
  • I have searched for existing issues and pull requests, including closed ones.
  • I confirm that I am using English to submit this report.
Dify version

main (current)

Cloud or Self Hosted

Both

Problem

MCP tool listing and MCP tool invoke prepare Authorization differently.

  • MCPToolManageService.list_provider_tools uses _prepare_auth_headers(), which always merges OAuth tokens into the request headers (custom headers + OAuth coexist).
  • MCPTool.invoke_remote_mcp_tool gated token injection behind if not headers:. Any non-empty custom header map (for example X-Tenant: abc) skipped retrieve_tokens() entirely.

Falsifiable consequence:

  1. Configure an MCP provider with at least one custom header and complete OAuth (tokens stored).
  2. Refresh / list tools → succeeds (Authorization present via _prepare_auth_headers).
  3. Invoke the same tool from Agent / Workflow → request goes out without Authorization, typically 401 from the MCP server, while listing still works.

This is an auth/header state asymmetry between list and invoke, not a UI issue.

Relevant code (before fix):

  • api/services/tools/mcp_tools_manage_service.py_prepare_auth_headers
  • api/core/tools/mcp_tool/tool.pyinvoke_remote_mcp_tool (if not headers: gate)
Steps to reproduce

Minimal unit repro (fails on main):

# decrypt_headers returns {"X-Tenant": "abc"}
# retrieve_tokens returns OAuthTokens(access_token="oauth-access-token", token_type="bearer", ...)
# invoke_remote_mcp_tool(...)
# MCPClientWithAuthRetry receives headers WITHOUT Authorization

Or manually:

  1. Add MCP server with custom header X-Tenant=abc and OAuth credentials.
  2. Click to fetch tools (list succeeds).
  3. Call the tool from a workflow/agent → auth failure / 401.
Expected Behavior

Invoke must apply the same header merge as listing: custom headers kept, OAuth Authorization still attached when tokens exist.

Actual Behavior

Invoke drops OAuth whenever any custom header is present.

I am preparing a focused fix with regression tests and will open a PR linking this issue.

主要语言
TypeScript
星标
157k
派生
24.7k
平均合并
22 小时 32 分钟
30 天内合并 PR
611

贡献指南

打开贡献指南

从这里开始

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

langgenius/dify 的其他 Issue

查看 langgenius/dify 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

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