MCP (Model Context Protocol) Security Cheat Sheet / MCP(模型上下文协议)安全速查表¶
Introduction / 介绍¶
The Model Context Protocol (MCP), introduced by Anthropic in November 2024, standardizes how AI applications (LLM clients) connect to external tools, data sources, and services. Think of it as a universal interface layer, a "USB-C port for AI", replacing fragmented, custom integrations with a single protocol.
由Anthropic于2024年11月推出的模型上下文协议(MCP),标准化了AI应用(LLM客户端)如何连接外部工具、数据源和服务。将其视为一个通用接口层,'AI的USB-C端口',用单一协议取代了碎片化、定制的集成。
However, MCP introduces a fundamentally new attack surface: AI agents dynamically executing tools based on natural language, with access to sensitive systems. Unlike traditional APIs where developers control every call, MCP lets LLMs decide which tools to invoke, when, and with what parameters. This is creating unique security risks that combine prompt injection, supply chain attacks, and confused deputy problems.
然而,MCP引入了一个全新的攻击面:AI代理根据自然语言动态执行工具,并有权访问敏感系统。与开发者控制每个调用的传统API不同,MCP让LLM决定调用哪些工具、何时调用以及使用什么参数。这正在创造独特的安全风险,这些风险结合了提示注入、供应链攻击和困惑代理问题。
This cheat sheet provides best practices to secure MCP deployments and minimize attack surfaces across clients, servers, and the connections between them.
本速查表提供了确保MCP部署安全并最小化客户端、服务器及其之间连接的攻击面的最佳实践。
Architecture Overview / 架构概述¶
User ↔ MCP Host (AI App) ↔ MCP Client ↔ MCP Server(s) ↔ Tools / Data / APIs
- MCP Host: The AI application (e.g., Claude Desktop, Cursor, IDE plugins).
中文:MCP主机:AI应用(例如,Claude桌面、Cursor、IDE插件)。 - MCP Client: Connects to one or more MCP servers, passes tool definitions to the LLM.
中文:MCP客户端:连接到一个或多个MCP服务器,将工具定义传递给LLM。 - MCP Server: Lightweight program exposing tools, resources, and prompts via the protocol.
中文:MCP服务器:通过协议暴露工具、资源和提示的轻量级程序。 - Transports:
stdio(local) or Streamable HTTP (remote). The older HTTP+SSE transport is deprecated. See the Streamable HTTP specification.
中文:传输方式:stdio(本地) 或可流式传输的 HTTP (远程)。较旧的 HTTP+SSE 传输方式已弃用。请参阅 可流式传输的 HTTP 规范。
The LLM sees all tool descriptions from all connected servers in its context — this is critical to understanding cross-server attacks.
LLM可以看到所有连接服务器的工具描述——这对于理解跨服务器攻击至关重要。
Key Risks / 主要风险¶
- Tool Poisoning: Malicious instructions hidden in tool descriptions, parameter schemas, or return values that manipulate the LLM's behavior.
中文:工具投毒攻击:隐藏在工具描述、参数模式或返回值中的恶意指令,用于操纵LLM的行为。 - Rug Pull Attacks: A server changes its tool definitions after initial user approval, turning a trusted tool malicious.
中文: rug pull 攻击:服务器在初始用户批准后更改其工具定义,将受信任的工具变为恶意。 - Tool Shadowing / Cross-Origin Escalation: A malicious server's tool description manipulates how the agent behaves with tools from other trusted servers.
中文:工具阴影/跨源提升: 恶意服务器的工具描述会操纵代理与来自 其他 受信任服务器的工具之间的行为。 - Confused Deputy Problem: The MCP server executes actions with its own (often broad) privileges, not the requesting user's permissions.
中文:困惑的副手问题:MCP服务器使用自己的(通常是广泛的)权限执行操作,而不是请求用户的权限。 - Data Exfiltration via Legitimate Channels: Attackers use prompt injection to encode sensitive data into seemingly normal tool calls (e.g., search queries, email subjects).
中文:通过合法渠道的数据泄露:攻击者使用提示注入将敏感数据编码到看似正常的工具调用中(例如,搜索查询、电子邮件主题)。 - Excessive Permissions / Over-Scoped Tokens: MCP servers request broad OAuth scopes (full Gmail access vs. read-only), creating aggregation risk.
中文:过多权限/过范围令牌: MCP 服务器请求广泛的 OAuth 范围(完整 Gmail 访问与只读访问),从而产生聚合风险。 - Supply Chain Attacks: Untrusted or compromised MCP server packages installed from public registries without review.
中文:供应链攻击:未经审查或已泄露的MCP服务器包从公共注册表中安装。 - Message Tampering and Replay: JSON-RPC payloads modified after TLS termination by compromised proxies or middleware, or captured and re-sent to duplicate actions.
中文:消息篡改和重放:JSON-RPC有效载荷在TLS终止后被泄露的代理或中间件修改,或捕获后重新发送以重复操作。 - Sandbox Escapes: Local MCP servers running with full host access, enabling file system traversal, credential theft, or arbitrary code execution.
中文:沙盒逃逸:以完整主机访问权限运行的本地MCP服务器,允许文件系统遍历、凭证盗窃或任意代码执行。
Best Practices / 最佳实践¶
1. Principle of Least Privilege / 1. 最小权限原则¶
- Grant each MCP server the minimum permissions needed for its function.
中文:为每个MCP服务器授予其功能所需的最低权限。 - Use scoped, per-server credentials — never share tokens across servers.
中文:使用按服务器分域的凭证 — 永远不要在服务器之间共享令牌。 - Request narrow OAuth scopes (e.g.,
mail.readonlyinstead ofmail.modifyormail.full_access).
中文:请求狭窄的OAuth作用域(例如,使用mail.readonly而不是mail.modify或mail.full_access)。 - Prefer ephemeral, short-lived tokens over long-lived PATs.
中文:优先使用短暂、短期令牌而非长期PATs。
2. Tool Description & Schema Integrity / 2. 工具描述与模式完整性¶
- Inspect all tool descriptions, parameter names, types, and return schemas before approval.
中文:在批准前检查所有工具描述、参数名称、类型和返回模式。 - Treat the entire tool schema as a potential injection surface — not just the
descriptionfield.
中文:将整个工具模式视为潜在的注入面 — 而不仅仅是description字段。 - Pin reviewed tool definitions using cryptographic hashes and require review when they change. Hash the complete tool definition (including
inputSchema,outputSchema, andannotations), not only the description; a flippedreadOnlyHintordestructiveHintcan change client approval behavior without any visible description change. This detects metadata changes, not changes to server code or behavior behind an unchanged definition; tool annotations are hints, not enforcement.
中文:使用加密散列固定已审查的工具定义,并在它们更改时要求进行审查。散列完整的 工具定义(包括inputSchema、outputSchema和annotations),而不仅仅是描述;readOnlyHint或destructiveHint的翻转可能会改变客户端的批准行为,而无需任何可见的描述更改。这可以检测元数据更改,而不是更改服务器代码或定义未变的行为;工具注释是提示,而不是强制。 - Use tools like
mcp-scanto automatically detect poisoned descriptions and cross-server shadowing.
中文:使用mcp-scan等工具自动检测中毒描述和跨服务器影子攻击。 - Use strict JSON Schema for tool parameters: set
additionalProperties: falseand usepattern(or similar) on string fields so only declared parameters and valid formats are accepted.
中文:为工具参数使用严格的 JSON Schema:设置additionalProperties: false并在字符串字段上使用pattern(或类似)以确保只接受声明的参数和有效格式。
3. Sandbox and Isolate MCP Servers / 3. 沙盒化和隔离 MCP 服务器¶
- Run local MCP servers in a sandbox that enforces minimal privileges and access to host resources, following the MCP local-server guidance. A bare
chrootis not a process sandbox.
中文:在遵循 MCP 本地服务器指南 的基础上,在强制最小权限和主机资源访问的沙盒中运行本地 MCP 服务器。裸露的chroot并不是一个进程沙盒。 - Restrict file system access to only required directories.
中文:仅限制对所需目录的文件系统访问。 - Disable network access unless explicitly needed.
中文:除非明确需要,否则禁用网络访问。 - Use
stdiofor local MCP communication through the launched process's standard streams. This avoids a listening MCP endpoint; it does not restrict the server process's file system, network, or credential access.
中文:使用stdio通过启动进程的标准流进行本地MCP通信。这避免了监听MCP端点;它不会限制服务器进程的文件系统、网络或凭证访问。 - Separate sensitive servers (payment, auth, PII) from general-purpose ones.
中文:将敏感服务器(支付、认证、PII)与通用服务器分离。
4. Human-in-the-Loop for Sensitive Actions / 4. 对于敏感操作采用人工干预¶
- Require explicit user confirmation for destructive, financial, or data-sharing operations.
中文:要求对破坏性、财务或数据共享操作进行显式用户确认。 - Display full tool call parameters to the user — not just a summary name.
中文:向用户显示完整的工具调用参数,而不仅仅是摘要名称。 - Never auto-approve tool calls, especially in multi-server setups.
中文:永远不要自动批准工具调用,尤其是在多服务器设置中。 - Ensure the confirmation UI cannot be bypassed by LLM-crafted responses.
中文:确保确认用户界面不能被LLM生成的响应绕过。 - Do not assume that an available MCP tool has the user's consent or approval for a particular invocation. The MCP specification states that hosts must obtain explicit user consent before invoking any tool and that a human should be able to deny tool invocations.
中文:不要假设可用的MCP工具已经获得了用户对特定调用的同意或批准。MCP规范指出,主机必须在调用任何工具之前获得用户的明确同意,并且人类应该能够拒绝工具调用。 - Assess whether tool operations require additional authorization based on their possible effects, including sensitive reads, writes, data disclosure, and irreversible actions; a tool's read/write label alone may not establish the risk, and clients must treat tool annotations as untrusted unless they come from trusted servers.
中文:评估工具操作是否需要额外的授权,基于它们可能产生的影响,包括敏感读取、写入、数据披露和不可逆操作;一个工具的读写标签本身可能不足以确定风险,并且客户端必须将工具注释视为不受信任,除非它们来自受信任的服务器。 - Choose controls according to that risk, such as explicit user confirmation, narrowly scoped authorization, or policy checks enforced at execution time. For binding approvals to specific actions and arguments and preventing their reuse, follow the AI Agent Security Cheat Sheet.
中文:根据该风险选择控制措施,例如明确用户确认、狭窄的授权范围或在执行时强制执行的策略检查。为了将批准绑定到特定动作和参数并防止其重复使用,请遵循AI代理安全备忘录。
5. Input and Output Validation / 5. 输入和输出验证¶
- Validate all inputs to MCP server tools — treat them as untrusted (they originate from LLM output influenced by potentially malicious context).
中文:验证所有输入到MCP服务器工具——将它们视为不可信(它们来自受潜在恶意上下文影响的LLM输出)。 - Sanitize inputs against injection attacks (SQL, OS command, path traversal).
中文:对输入进行清理以防止注入攻击(SQL、操作系统命令、路径遍历)。 - Validate and sanitize tool outputs before returning them to the LLM context — output is often used as input by other tools and can cause downstream SSRF or command injection if unsanitized.
中文:在将工具输出返回到LLM上下文之前进行验证和清理——输出通常被其他工具用作输入,如果未经清理,可能会导致下游的SSRF或命令注入。 - Never pass raw shell commands or unsanitized file paths.
中文:永远不要传递原始的shell命令或未清理的文件路径。 - Protect against SSRF: MCP tools that fetch URLs based on LLM-generated parameters can be manipulated via prompt injection to access internal services (e.g., cloud metadata endpoints). Never fetch arbitrary URLs provided by the LLM without strict allowlist validation.
中文:防止SSRF攻击:基于LLM生成的参数获取URL的MCP工具可以通过提示注入被操纵以访问内部服务(例如,云元数据端点)。在未经严格的允许列表验证的情况下,永远不要获取LLM提供的任意URL。
6. Authentication, Authorization & Transport Security / 6. 认证、授权和传输安全¶
- Require authentication when remote endpoints expose non-public tools or data. Authorization is optional in the protocol; see the MCP authorization specification.
中文:当远程端点公开非公开工具或数据时,需要身份验证。在协议中,授权是可选的;参见MCP授权规范。 - When using OAuth over HTTP, follow the MCP OAuth 2.1 profile. Clients must identify the intended MCP server with the
resourceparameter. See resource indicators.
中文:在使用HTTP上的OAuth时,遵循MCP OAuth 2.1配置文件。客户端必须使用resource参数识别预期的MCP服务器。参见资源指示器。 - Validate that each access token was issued for this MCP server as its intended audience. Reject invalid tokens and never pass an MCP access token to an upstream API. See token handling.
中文:验证每个访问令牌是否是为此MCP服务器作为其预期受众而签发的。拒绝无效的令牌,并且永远不要将MCP访问令牌传递给上游API。参见令牌处理。 - Validate authorization on every protected request. The current Streamable HTTP transport has no protocol-level sessions. If supporting older session-based revisions, use random session IDs, bind them to the authenticated user, and never treat an ID as authentication; see the legacy session guidance.
中文:在每个受保护请求上验证授权。当前的可流式HTTP传输没有协议级别的会话。如果支持较旧的基于会话的修订版,请使用随机会话ID,将其绑定到已认证的用户,并且永远不要将ID视为身份验证;参见遗留会话指南。 - Use TLS for remote Streamable HTTP connections.
中文:使用TLS进行远程可流式HTTP连接。 - Verify server identity via certificate pinning or cryptographic server verification for remote servers.
中文:通过证书固定或加密服务器验证来验证远程服务器的身份。 - Apply resource controls (rate limits, quotas, timeouts) per session or tenant to resist DoS and limit impact of abuse; combine with sandboxing to contain local escape impact.
中文:按会话或租户应用资源控制(速率限制、配额、超时)以抵抗DoS攻击并限制滥用的影响;与沙箱结合使用以限制本地逃逸的影响。 - Use OS-native secure credential storage (macOS Keychain, Windows Credential Manager, Linux Secret Service) for OAuth access and refresh tokens.
中文:使用操作系统本地的安全凭证存储(macOS Keychain、Windows凭证管理器、Linux秘密服务)来存储OAuth访问和刷新令牌。 - Never store OAuth tokens in plaintext in MCP config files or application settings.
中文:永远不要在MCP配置文件或应用程序设置中以明文形式存储OAuth令牌。 - Bind local Streamable HTTP servers to localhost unless network access is explicitly needed. See the transport security rules.
中文:除非需要显式网络访问,否则将本地Streamable HTTP服务器绑定到localhost。参见传输安全规则。 - Validate the
Originheader on incoming Streamable HTTP requests. Reject a present but invalid Origin with HTTP 403. Do not reject non-browser clients solely because they send no Origin header. See the transport security rules.
中文:验证传入的流式 HTTP 请求的Origin标头。如果存在但无效的 Origin,则使用 HTTP 403 拒绝。不要仅因为非浏览器客户端未发送 Origin 标头而拒绝它们。参见 传输安全规则。 - Validate the Host header on incoming requests; reject unexpected hostnames.
中文:验证传入请求的Host头;拒绝意外的主机名。
7. Optional Message-Level Integrity / 7. 可选的消息级完整性¶
TLS protects messages in transit, but a component that changes data after TLS termination is a separate threat. The core MCP transport specification does not require every JSON-RPC message to be signed. The MCPS Internet-Draft proposes a separate signing and replay-protection layer; it is an individual work in progress, not an adopted MCP standard.
TLS保护传输中的消息,但TLS终止后更改数据的组件是单独的威胁。核心MCP传输规范不要求每个JSON-RPC消息都必须签名。MCPS互联网草案提出一个单独的签名和重放保护层;这是一项正在进行中的个人工作,不是一个采用的标准MCP。
If the threat model calls for integrity after TLS termination, choose a reviewed mechanism supported by both endpoints. Define how keys are trusted, what is signed, how replay is rejected, and what happens when verification fails. Do not present optional signing as a requirement for ordinary MCP deployments.
如果威胁模型要求TLS终止后的完整性,请选择两端都支持的已审查机制。定义如何信任密钥、签什么、如何拒绝重放以及验证失败时会发生什么。不要将可选的签名作为普通MCP部署的要求。
8. Multi-Server Isolation & Cross-Origin Protection / 8. 多服务器隔离与跨源保护¶
- Treat each MCP server as an untrusted, independent security domain.
中文:将每个MCP服务器视为一个不受信任的独立安全域。 - Prevent tool descriptions from one server from referencing or modifying the behavior of tools from another server.
中文:防止一个服务器上的工具描述引用或修改另一个服务器上工具的行为。 - Monitor for cross-server data flows (e.g., credentials from server A appearing in calls to server B).
中文:监控跨服务器数据流(例如,服务器A的凭据出现在对服务器B的调用中)。 - Use an MCP proxy or gateway to enforce isolation policies between servers.
中文:使用MCP代理或网关在服务器之间强制执行隔离策略。
9. Supply Chain Security / 9. 供应链安全¶
- Only install MCP servers from trusted, verified sources.
中文:仅从受信任、经过验证的来源安装MCP服务器。 - Review server source code and tool definitions before installation.
中文:在安装前审查服务器源代码和工具定义。 - Verify package integrity with checksums or code signing.
中文:使用校验和或代码签名验证软件包完整性。 - Scan MCP server dependencies for known vulnerabilities.
中文:扫描MCP服务器依赖项以查找已知漏洞。 - Monitor for changes to tool descriptions post-installation (rug pull detection).
中文:监控安装后工具描述的变化(拉毛攻击检测)。 - Carefully verify package names before installation — typosquatting (e.g., mcp-server-filesystem vs mcp-server-filesytem) is a common attack vector.
中文:在安装前仔细验证软件包名称——打字欺骗(例如,mcp-server-filesystem与mcp-server-filesytem)是一种常见的攻击向量。 - Use tools like
mcp-scanto automatically analyze and monitor installed servers for malicious behavior or changes.
中文:使用mcp-scan等工具自动分析和监控已安装服务器是否存在恶意行为或更改。
10. Monitoring, Logging & Auditing / 10. 监控、日志记录与审计¶
- Log all MCP tool invocations with full parameters, user context, and timestamps.
中文:记录所有 MCP 工具调用,包括完整参数、用户上下文和时间戳。 - Feed MCP logs into SIEM for anomaly detection.
中文:将 MCP 日志输入 SIEM 进行异常检测。 - Alert on unusual patterns: new tools being called, admin-level queries, abnormal call frequency.
中文:对异常模式发出警报:调用新工具、管理员级别查询、异常调用频率。 - Redact secrets and PII from logs.
中文:从日志中删除机密信息和 PII。 - Conduct regular security audits and simulated attacks against MCP setups.
中文:定期对 MCP 设置进行安全审计和模拟攻击。 - When the logs come from a component you did not build, see the Verifying Third-Party Agent Execution Evidence Cheat Sheet for how to judge what such a record establishes.
中文:当日志来自非您构建的组件时,请参阅验证第三方代理执行证据速查表以了解如何判断此类记录所建立的内容。
11. Consent & Installation Security / 11. 同意与安装安全¶
- Display a clear consent dialog before connecting any new MCP server.
中文:在连接任何新的MCP服务器之前,显示一个清晰的同意对话框。 - Show the exact command that will be executed (for local servers), without truncation.
中文:显示将要执行的精确命令(对于本地服务器),不允许截断。 - Clearly identify the source and publisher of the MCP server.
中文:清楚地标识MCP服务器的来源和发布者。 - Re-prompt for consent when tool definitions change.
中文:当工具定义更改时,重新提示同意。 - Never allow web content or untrusted data to trigger MCP server installation.
中文:绝不允许网络内容或不受信任的数据触发MCP服务器安装。
12. Prompt Injection via Tool Return Values / 12. 通过工具返回值进行提示注入¶
- Treat every tool response as untrusted data, including responses from approved servers.
中文:将每个工具响应视为不受信任的数据,包括来自批准服务器的响应。 - Separate and clearly label tool data in the model context, and instruct the model to treat it as data rather than instructions.
中文:在模型上下文中分离并清楚地标记工具数据,并指导模型将其视为数据而不是指令。 - Use tag stripping and instruction-pattern detection only as additional filtering or alerting measures. They cannot establish that the remaining text is safe; OWASP describes encoded and multilingual prompt injections that evade filters.
中文:仅将标签剥离和指令模式检测作为额外的过滤或警报措施。它们不能确保剩余文本的安全性;OWASP 描述了绕过滤器的编码和多语言提示注入。 - Enforce authorization and validate subsequent tool calls in trusted application code, independently of the model's interpretation. Apply least privilege and human approval for sensitive actions.
中文:在受信任的应用代码中强制执行授权并验证后续工具调用,独立于模型的解释。应用最小权限原则和敏感操作的在线人工审核。 - For web-scraping and retrieval tools, extract the required structured data instead of passing raw HTML. Text inside structured fields remains untrusted.
中文:对于网络爬虫和检索工具,提取所需的格式化数据而不是传递原始HTML。结构化字段内的文本保持不受信任。
Do's and Don'ts / 该做什么和不该做什么¶
Do:
执行:
- Enforce least privilege per MCP server and per tool.
中文:按MCP服务器和工具强制执行最小权限原则。 - Inspect and pin all tool descriptions and schemas.
中文:检查并固定所有工具描述和模式。 - Sandbox local MCP servers in containers or restricted environments.
中文:在容器或受限环境中沙盒化本地MCP服务器。 - Require human approval for sensitive or destructive tool calls.
中文:对敏感或破坏性的工具调用要求人工审核。 - Apply risk-appropriate authorization to sensitive tool calls and enforce its scope at execution time.
中文:对敏感工具调用应用风险适当的授权,并在执行时强制执行其范围。 - Validate all inputs and outputs at the MCP server layer.
中文:在MCP服务器层验证所有输入和输出。 - Use
mcp-scanor equivalent tooling to detect poisoned tools.
中文:使用mcp-scan或等效工具检测受污染的工具。 - Log and monitor all tool invocations centrally.
中文:集中记录和监控所有工具调用。 - Verify MCP server sources and scan dependencies.
中文:验证MCP服务器源和扫描依赖项。 - Validate the Origin of Streamable HTTP requests and the audience of OAuth access tokens.
中文:验证可流式传输的HTTP请求的来源和OAuth访问令牌的受众。 - Pin tool definitions with cryptographic hashes and verify before each execution.
中文:使用加密散列固定工具定义,并在每次执行前进行验证。
Don't:
不要:
- Auto-approve tool calls without showing full parameters to the user.
中文:在没有向用户显示完整参数的情况下自动批准工具调用。 - Trust tool descriptions blindly — they are a prompt injection vector.
中文:盲目信任工具描述——它们是提示注入的向量。 - Share OAuth tokens or credentials across MCP servers.
中文:在MCP服务器之间共享OAuth令牌或凭据。 - Run MCP servers with full host access or
*permissions.
中文:以完全主机访问或*权限运行MCP服务器。 - Install MCP servers from unverified public registries without review.
中文:未经审查,从未经验证的公共注册表中安装MCP服务器。 - Assume a tool approved yesterday is the same tool today (rug pulls).
中文:假设昨天批准的工具今天仍然是相同的工具( rug pulls)。 - Ignore cross-server interactions — shadowing attacks are real.
中文:忽略跨服务器交互——影子攻击是真实的。 - Store secrets in source code, plaintext config files, or logs.
中文:在源代码、明文配置文件或日志中存储机密。 - Treat optional message signing as a core MCP requirement.
中文:将可选的消息签名视为MCP的核心要求。 - Accept server public keys from unverified first-contact responses (TOFU without pinning).
中文:从未经验证的首次接触响应中接受服务器公钥(TOFU 无固定)。
References / 参考¶
- MCP: Security Best Practices) (MCP:安全最佳实践)
- MCP: Authorization) (MCP:授权)
- MCP: Streamable HTTP) (MCP:流式 HTTP)