Skip to content

7.9 Security best practices for MCP Client

Clients are the gatekeepers: they decide which servers to trust, which tools to invoke, and how much of the model’s output to accept. If the client is lax, it may hand over power to the wrong server or execute malicious requests unchecked.

The following practices ensure clients enforce trust, maintain oversight, and protect against server abuse.

7.9.1 Secure discovery & trust anchoring

Clients that don’t verify servers risk connecting to impostors. A fake MCP server can capture tokens or trick the client into harmful actions.

Best practices: - Verify servers using signed metadata: MCP servers should publish a metadata file (e.g., JSON) along with a digital signature (Ed25519, RSA, etc.). Clients verify the signature against a known public key before trusting the server. - Use trusted registries: only connect to MCP servers or tools listed in vetted registries that enforce namespace isolation and signature checks, reducing the risk of rogue endpoints. - Enforce TLS/mTLS on all connections to protect against man-in-the-middle attacks. - Refuse connections to servers that lack cryptographic verification or are not part of an approved allowlist.

Note that these practices follow the same pattern used in package ecosystems like PyPI or Docker Hub, where integrity checks and signing are critical to prevent supply-chain attacks.

7.9.2 Authorization & trust

Even if a server is authentic, a compromised or spoofed authorization flow can trick clients into granting excessive access. If OAuth flows aren’t validated correctly, malicious servers may escalate privileges.

Best practices: - Implement OAuth 2.1 client flows strictly (e.g., Authorization Code with PKCE). - Verify authorization server metadata and keys before trusting tokens. - Enforce scope checks locally. Reject requests outside approved scopes. - Validate tokens on every request. - Fail closed: if verification fails, deny access.

7.9.3 Connection security

Transport-layer weaknesses let attackers eavesdrop, tamper with, or impersonate servers. Clients that don’t enforce TLS are vulnerable to man-in-the-middle attacks.

Best practices: - Always require TLS 1.2+ and above for all MCP communications. - Enforce mutual TLS (mTLS) where possible to authenticate both server and client. - Reject self-signed or untrusted certificates. - Use certificate pinning for high-value environments.

7.9.4 Tool integrity

Malicious servers can advertise tool definitions that are tampered with or unsafe. If clients don’t validate these definitions, they may execute dangerous or corrupted logic. Best practices: - Validate tool definitions against schemas (e.g., JSON Schema). - Require digital signatures on tool metadata. - Maintain a whitelist of trusted tools; reject unknown or unrecognized ones. - Warn users when tool definitions change unexpectedly.

7.9.5 Input validation & prompt safety

Prompt injection can make models request tools in unsafe ways. Without validation, the client becomes a blind executor of malicious instructions.

Best practices: - Validate model requests against JSON Schema or Protobuf contracts. - Reject unknown fields, excessive nesting, or overly long strings. - Sandbox untrusted completions before execution. - Require user confirmation for high-impact requests.

7.9.6 Token management & local isolation

If tokens are stored in plaintext on a client device, malware or rogue processes can steal them.

Best practices: - Store tokens in system keychains (e.g., macOS Keychain, Windows DPAPI). - Use ephemeral storage when possible. - Isolate IPC with UNIX domain sockets or restricted gRPC channels. - Rotate tokens automatically on short intervals (minutes, not days).

7.9.7 Session integrity & replay protection

Attackers can replay captured session identifiers to impersonate valid clients.

Best practices: - Bind sessions to nonces or unique transaction IDs. - Use short-lived session cookies with SameSite/HttpOnly flags. - Reject stale requests and enforce clock skew tolerance. - Regenerate session keys on re-authentication.

7.9.8 Tool use transparency & human oversight

If tool use happens silently, users can’t tell when models are overstepping. Best practices: - Display tool invocations in a user-visible UI before execution. - Provide an audit dashboard of recent tool calls. - Require explicit confirmation for destructive actions. - Log completions with model + version identifiers for traceability.

7.9.9 Execution sandboxing

Clients may need to perform local operations, such as file handling, shell commands, or sampling requests. If these run without isolation, a single malicious request could compromise the environment. Best practices: - Run local executions in sandboxed environments (VMs, containers, seccomp profiles). - Apply schema checks, policy filters, and user confirmations as layered defenses. - Limit file system and network access from sandboxed processes. - Use OS-level capabilities (AppArmor, SELinux) for extra isolation.

7.9.10 Sampling controls

MCP servers can request clients to perform sampling (LLM completions). Malicious servers could abuse this to inject prompts or drain budgets.

Best practices: - Show completions to users for review/edit/reject before execution. - Filter completions with content moderation or sanitization layers. - Let users select the model family and version used. - Apply rate limits, timeouts, and cost caps specific to sampling requests.