Skip to content

MCP Security and Trust

Security is paramount when integrating Large Language Models (LLMs) with external tools and data sources via MCP. This chapter outlines crucial security considerations, the MCP trust model, and best practices to ensure safe and reliable operation.

MCP is designed with the following core security principles in mind:

Users must have explicit control over what data an MCP server can access and what tools it can invoke on their behalf. Consent should be granular and clearly presented. Users should be able to review and revoke permissions easily.

Data exchanged via MCP, especially sensitive information accessed through resources or used in prompts, must be protected. This includes data in transit (e.g., using TLS) and data at rest where applicable. Minimize the collection and exposure of personally identifiable information (PII).

Tools exposed by MCP servers can perform actions. It’s critical to ensure these tools operate within defined boundaries and do not pose a risk of unintended consequences. This involves careful design of tool functionalities, input validation, and clear documentation of their effects.

When MCP is used to provide context to LLMs, controlling how the LLM samples or uses this context is important. While MCP facilitates providing the context, the client or host application managing the LLM interaction should implement safeguards against the LLM misusing or leaking sensitive context information in its responses.

Adhering to these guidelines will help you build secure MCP integrations:

Section titled “Building Robust Consent and Authorization Flows”

Implement clear and auditable consent mechanisms. Before an MCP client can access resources or invoke tools that require permission, the server (or a managing host) must obtain explicit user approval. OAuth 2.0 or similar authorization frameworks can be adapted for managing access tokens and scopes for MCP servers.

Providing Clear Security Impact Documentation

Section titled “Providing Clear Security Impact Documentation”

For each MCP server, and particularly for each tool it exposes, document the potential security implications. What data does it access? What actions can it perform? What are the risks if it’s misused? This helps users and client developers make informed decisions.

Implementing Access Control and Data Protection

Section titled “Implementing Access Control and Data Protection”

Servers must enforce access controls rigorously. Only authenticated and authorized clients should be able to access sensitive resources or execute privileged tools. Employ standard data protection techniques like encryption for data in transit (HTTPS/WSS) and consider encryption for sensitive data at rest if the server stores it.

Potential Security Risks and Mitigation Measures

Section titled “Potential Security Risks and Mitigation Measures”

Risk: If access tokens used by clients to authenticate with MCP servers are compromised, an attacker could impersonate the user and access their data or tools.

Mitigation:

  • Use short-lived tokens with refresh mechanisms.
  • Store tokens securely (e.g., using system keychain services).
  • Transmit tokens only over encrypted channels (TLS).
  • Implement token revocation capabilities.

Risk: An attacker compromises the MCP server itself, gaining access to its configuration, underlying data sources, or the ability to modify its behavior.

Mitigation:

  • Follow secure coding practices when developing servers.
  • Keep server software and dependencies updated.
  • Run servers with the least privilege necessary.
  • Implement robust logging and monitoring to detect suspicious activity.
  • Regularly audit server configurations and code.

Risk: Maliciously crafted inputs (potentially from users or external data sources fed into prompts) cause the LLM (or the tools invoked via MCP based on LLM output) to perform unintended or harmful actions.

Mitigation:

  • Sanitize and validate all inputs used in prompts, especially those that form tool parameters or database queries.
  • Use structured input formats for tools rather than relying on LLMs to generate arbitrary code or commands.
  • Clearly demarcate instructions, context, and user input within prompts.
  • Implement strict parsing and validation of LLM outputs before they are used to invoke tools.
  • Educate users about the risks of prompt injection if they are crafting complex prompts.

Risk: An MCP server or a specific tool within it is granted more permissions than necessary to perform its function, increasing the potential damage if it’s compromised or misused.

Mitigation:

  • Adhere to the principle of least privilege. Grant only the minimum necessary permissions for each resource provider and tool.
  • Implement granular permission scopes that users can approve individually.
  • Regularly review and audit the permissions granted to servers and their components.