7.8 Security best practices for MCP Server
7.8.1 Authentication & authorization
Without strong identity checks, attackers can impersonate clients, use overbroad tokens, or replay credentials meant for another system. This is the classic “confused deputy” risk, where the server executes a malicious request under the guise of a legitimate client.
Best practices: - Enforce OAuth 2.1 with proper token validation. - Apply least-privilege scopes, mapping each tool to only what’s essential. - Use sender-constrained tokens such as mTLS or DPoP (Demonstration of Proof-of-Possession) to prevent token replay. - Audit client registration and discovery metadata to block spoofed auth servers. - Prevent confused deputy issues by enforcing consent for every OAuth proxy flow.
7.8.2 Input sanitization & prompt safety
Model-generated inputs are inherently untrusted. If a malicious string makes it through unchecked, it can trigger shell escapes, SQL injection, or other command exploits.
The solution is simple enough, which makes it surprising that we keep having vulnerabilities of this kind. OWASP’s SQL Injection Prevention Cheat Sheet has held the same #1 recommendation for over a decade: use parameterized queries.
Best practices: - Validate inputs against JSON Schema or Protobuf definitions. - Sanitize strings (escape shell characters, enforce length limits). - Run tool executions in isolated sandboxes or containers to limit blast radius. - Require human/policy approval for high-risk operations (e.g., file writes, deletes).
7.8.3 Token security & credential hygiene
Tokens often leak through logs, caches, or config files. If they’re long-lived and not scoped, an attacker can use them indefinitely.
Best practices: - Use short-lived JWTs (with exp claims) scoped to specific audiences. - Never log tokens or pass them in URLs (prefer headers like Authorization: Bearer). - Store tokens securely, away from userland processes. - Rotate and revoke credentials regularly. - Only accept tokens explicitly issued to the server and validate the audience (aud) claim rigorously.
7.8.4 Scope management & exposure control
Tools with broad scopes give attackers more leverage. A single compromised tool can cascade into cross-system exposure.
Best practices: - Audit tools regularly for scope creep. - Apply least privilege for every tool and endpoint. - Separate read and write scopes; avoid mixing them in one token. Define granular scopes (e.g., crm.read, crm.write) instead of broad crm.*. - Audit scopes in CI/CD with policy-as-code (e.g., OPA or Conftest). - Enforce deny by default, requiring explicit grants.
7.8.5 Endpoint verification & tool integrity
Spoofed MCP servers or tampered tools can masquerade as legitimate ones. Without integrity checks, servers load malicious code into trusted environments.
Best practices: - Use signed registries and cryptographic verification for tools. Verify tool registries with Ed25519 or RSA signatures. - Pin package versions and use hashes (sha256sum) in manifests to prevent silent tampering. - Enforce certificate pinning for TLS connections. - Alert users when tools are updated or replaced. - Reject endpoints that fail TLS or metadata verification. - Audit tools periodically to prevent malicious or “poisoned” versions from being substituted.
7.8.6 Observability & auditing
Without visibility, malicious actions can go unnoticed. Servers that fail to log, monitor, and enforce limits won’t detect abuse until damage is already done.
Best practices: - Centralize logging of prompts, tool invocations, and responses, for auditing and incident response. - Monitor for anomalies like repeated failures or unusual sequences. - Correlate logs with client IDs and timestamps for forensic clarity. - Review logs regularly and feed them into SIEM/SOC pipelines for real-time monitoring. - Apply regular patches and updates to both server runtime and dependencies. - Apply rate limiting, timeouts, and cost controls to prevent resource abuse and runaway tool use.
7.8.7 Supply-chain security
MCP servers depend on libraries, packages, and tool modules that may be tampered with or maliciously substituted. A single poisoned dependency can compromise the server at build time or runtime.
Best practices: - Use signed and verified packages for all server components. - Integrate SAST (Static Application Security Testing) and SCA (Software Composition Analysis) into CI/CD pipelines. - Monitor for new CVEs in dependencies and patch quickly. - Prefer trusted, vetted registries; avoid pulling unpinned packages from public sources.