为什么每个 MCP 设置指南都在教你错误地存储 API 密钥

发布: (2026年3月3日 GMT+8 08:41)
8 分钟阅读
原文: Dev.to

Source: Dev.to

请提供您想要翻译的完整文本内容,我将按照要求将其翻译成简体中文,并保留原始的格式、Markdown 语法以及技术术语。谢谢!

Source:

将 MCP 凭证以明文文件存储的问题

如果你在过去六个月里设置了 Claude DesktopCursor 或任何 MCP 服务器,可能会按照某个指南的指示,在文件系统上的 JSON 配置文件中添加类似以下内容:

{
  "mcpServers": {
    "github": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-github"],
      "env": {
        "GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_xxxxxxxxxxxxxxxxxxxx"
      }
    }
  }
}

该文件(例如 claude_desktop_config.json~/.cursor/mcp.json)现在以明文形式包含了你的实际令牌

为什么这是一个严重的安全错误

  • 明文凭证 – 机器上的任何进程都可以读取该文件。
  • AI 助手暴露 – 助手读取配置以启动 MCP 服务器,因此令牌会出现在每个会话的助手上下文中。
  • 提示注入风险 – 恶意提示可能会要求助手“显示我的 MCP 配置文件”,助手会愉快地泄露令牌。
  • 文件系统访问 – 任何拥有文件系统权限的工具都可以读取该文件。
  • 版本控制泄漏 – 该文件可能意外提交到代码仓库。

这并非假设情景。Check Point ResearchCVE‑2026‑21852 中记录了通过 AI 编码工具进行 API 密钥外泄的案例。攻击面正是:存放在文件中的凭证可以被 AI 代理读取。

这些指南在 MCP 工作方式(配置文件中的环境变量是 MCP 服务器获取凭证的方式)上是正确的。
但它们没有说明 如何确保这些凭证的安全

更好的方法:引用环境变量

而不是直接嵌入令牌,引用一个存放在你的 shell 配置文件中的变量:

{
  "mcpServers": {
    "github": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-github"],
      "env": {
        "GITHUB_PERSONAL_ACCESS_TOKEN": "${GITHUB_TOKEN}"
      }
    }
  }
}

将变量添加到 ~/.zshrc~/.bashrc

export GITHUB_TOKEN="ghp_xxxxxxxxxxxxxxxxxxxx"
  • 优点:令牌不再出现在 MCP 配置文件中。
  • 缺点:它仍然存储在 AI 代理可以读取的 shell 配置文件中,并且仍然位于进程环境中,任何子进程都可以访问它。

真正的解决方案:AgentSecrets

AgentSecrets 作为 MCP 服务器运行,让你的 AI 助手在 不将凭证写入文件 的情况下访问凭证。

安装

brew install The-17/tap/agentsecrets

将凭证安全地存储在操作系统钥匙串中

agentsecrets secrets set GITHUB_TOKEN=ghp_xxxxxxxxxxxxxxxxxxxx

连接到你的 AI 工具

agentsecrets mcp install

最后一条命令会自动为 Claude DesktopCursor 完成配置。你的配置文件现在如下所示:

{
  "mcpServers": {
    "agentsecrets": {
      "command": "agentsecrets",
      "args": ["mcp", "serve"]
    }
  }
}
  • 配置中不出现令牌值。
  • 凭证不存储在任何文件或环境变量中,AI 也无法读取。

运行时工作原理

  1. Agent:“我需要调用 GitHub API 来检查这个仓库。”
  2. AgentSecrets MCP:将请求通过代理转发。
  3. Proxy:从操作系统钥匙串中取出 GITHUB_TOKEN,并注入到请求中。
  4. Agent:收到 API 响应。

令牌 永远不会进入 Agent 的上下文。它仅存在于操作系统钥匙串(macOS Keychain、Windows Credential Manager 或 Linux Secret Service)中,访问该钥匙串需要系统级身份验证。

为什么操作系统钥匙串是安全的

属性说明
访问需要身份验证读取可能需要生物识别(Touch ID)或系统密码,而不仅仅是应用密码。
按应用隔离其他进程在未获得用户明确同意的情况下无法读取条目。
不是文件没有路径会意外提交或泄露;数据存储在受保护的系统存储中。
AI 代理无法读取助手没有直接访问钥匙串的权限,确保机密性。

生成的配置(可安全共享)

{
  "mcpServers": {
    "agentsecrets": {
      "command": "agentsecrets",
      "args": ["mcp", "serve"]
    }
  }
}
  • 你可以将此文件提交到版本控制,分享给团队成员,甚至公开发布——不会泄露任何凭证
  • 真正的凭证仍保存在操作系统钥匙串中,并同步(零知识加密)到 AgentSecrets 的云端,仅在传输时由代理使用。

TL;DR

  • 将 API 密钥直接存放在 MCP JSON 配置中是 严重的安全漏洞
  • 使用 shell 环境变量进行引用是 一步改进,但仍然会暴露密钥。
  • AgentSecrets 通过将凭证保存在操作系统钥匙串中,并仅在需要时向 AI 工具提供,从而根除问题,绝不将凭证存放在文件或 AI 能读取的环境变量中。

立即采用 AgentSecrets,保护你的令牌,同时享受无缝的 AI‑辅助开发体验。

Fig File Problem Gets Worse on Teams

在开发团队之间共享 MCP 配置意味着共享存储凭证的 方法——而且往往这意味着直接共享凭证本身。

With AgentSecrets Workspaces

  1. Team lead sets up workspace

    agentsecrets workspace create "Acme Engineering"
    agentsecrets secrets set GITHUB_TOKEN=ghp_xxxxxxxxxxxxxxxxxxxx
  2. New developer joins

    agentsecrets login
    agentsecrets workspace switch "Acme Engineering"
    agentsecrets secrets pull
    agentsecrets mcp install

The new developer’s MCP setup is complete.

  • 未在 Slack 中共享任何凭证。
  • 未通过电子邮件发送 .env 文件。
  • 没有人在聊天窗口输入令牌。

The credentials moved from the workspace to the local OS keychain encrypted end‑to‑end.

When You Set Up Any MCP Server That Needs Credentials

  • Do not put token values in the config file.
  • Do not put token values in environment variables in the config.
  • Store the credential with agentsecrets secrets set.
agentsecrets mcp install
  • Reference key names only — never values.
  • The config file is for configuration.
  • The OS keychain is for credentials.

These are different things and they should live in different places.

Every MCP setup guide that shows you a JSON file with an actual token value in it is showing you how to make it work, not how to make it secure. Now you know the difference.

参考文献

  • GitHub:
  • 文档:
0 浏览
Back to Blog

相关文章

阅读更多 »

当工作成为心理健康风险时

markdown !Ravi Mishrahttps://media2.dev.to/dynamic/image/width=50,height=50,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fu...