AI Security

MCP Security: Securing the Connection Between AI Agents and Tools

A practical hardening guide for security teams responsible for MCP deployments: how to assess MCP servers, configure clients, apply the protocol's authorisation model, sandbox local servers and monitor tool use, with a checklist you can use in reviews.

By EthicalHat Research · · 3 min read
Infographic: Defence in depth for MCP deployments

Last week we covered what happens when LLMs can call your APIs: how the Model Context Protocol works and the attacks researchers have demonstrated against it. This follow-up is for the people who have to make MCP deployments safe in practice: security architects, platform teams and reviewers.

1. Assess servers before approval

Treat every MCP server as third-party software with privileged access to your systems. Before approving one, review:

  • Provenance: who publishes it? Is it official from the vendor of the system it connects to, community-maintained or internal?
  • Code: for open-source servers, review how inputs are handled, especially anything that reaches a shell, file system or query builder.
  • Permissions: what credentials does it need, and are they the minimum necessary?
  • Tools exposed: which are read-only and which have side effects? Are descriptions short and factual, with no hidden instructions?
  • Data flows: what data leaves your environment, and to where?
  • Update behaviour: does it change tool definitions dynamically? How are updates delivered?

Record approved servers in an inventory with an owner, the pinned version and the approved scopes.

2. Configure clients safely

  • Pin versions of local servers and hash tool definitions, alerting on changes so a server cannot silently alter its tools after approval.
  • Require confirmation for tools with side effects. Reserve "always allow" for read-only, low-risk tools.
  • Show full arguments in confirmation prompts, not just tool names.
  • Separate profiles for different trust levels. Do not connect servers that ingest untrusted public content alongside servers with access to sensitive data and outbound channels in the same session unless cross-boundary actions require approval.
  • Manage configuration centrally in enterprise deployments, so users cannot add arbitrary servers to managed clients.

3. Use the protocol's authorisation model

The March 2025 revision of the MCP specification introduced an authorisation framework for HTTP-based servers built on OAuth 2.1. Use it rather than static API keys:

  • Users authorise each server through your identity provider, and the server receives a per-user token.
  • Tokens are scoped to what the server needs and short-lived.
  • Servers should validate that tokens were issued for them (audience) and not accept tokens meant for other services.
  • Servers must not simply pass the user's token through to arbitrary downstream APIs.
sequenceDiagram
  participant U as User
  participant C as MCP client (host)
  participant IdP as Identity provider
  participant S as Remote MCP server
  participant API as Backend API
  C->>S: connect
  S-->>C: 401 + authorisation metadata
  C->>IdP: OAuth authorisation (PKCE), user consents to scopes
  IdP-->>C: access token (audience = this MCP server)
  C->>S: tools/call + bearer token
  S->>S: validate issuer, audience, scopes, expiry
  S->>API: call with server's own scoped credential for this user
  API-->>S: result
  S-->>C: tool result

4. Harden server implementations

For teams building MCP servers:

python
import os
import re
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("repo-reader")
REPO_ROOT = "/srv/repos/allowed-project"
SAFE_PATH = re.compile(r"^[\w./-]{1,200}$")

@mcp.tool()
def read_file(path: str) -> str:
    """Read a text file from the allowed project repository (read-only)."""
    if not SAFE_PATH.match(path) or ".." in path:
        raise ValueError("path must be a relative path inside the project")
    full = os.path.realpath(os.path.join(REPO_ROOT, path))
    if not full.startswith(REPO_ROOT + os.sep):
        raise ValueError("path escapes the project root")
    with open(full, encoding="utf-8", errors="replace") as f:
        return f.read(200_000)    # cap output size
  • Validate every argument strictly, and canonicalise paths.
  • Never pass model-supplied strings to a shell; use argument arrays and allow-lists.
  • Cap result sizes and rate-limit calls.
  • Keep tool descriptions minimal and version them.
  • Log every call with the calling user's identity.
  • Mark returned content from untrusted sources so hosts and users can treat it accordingly.

5. Sandbox local servers

Local stdio servers run with the user's privileges by default, which often includes SSH keys, cloud credentials and browser data. Reduce that:

  • Run servers in containers or other sandboxes with only the directories they need mounted, read-only where possible.
  • Provide no ambient credentials; inject only the specific token each server needs.
  • Restrict network egress to the destinations the server legitimately calls.
  • Prefer vendor-hosted or centrally hosted remote servers over arbitrary local ones for sensitive systems.

6. Monitor and respond

  • Send tool-call logs (user, host, server, tool, arguments, result size) to your SIEM.
  • Alert on first-time use of write tools, unusual volumes, new servers, and data flowing from sensitive sources to outbound tools.
  • Include MCP in incident response: how to revoke tokens, disable a server and preserve evidence.

Review checklist

  • Server is on the approved list, with an owner and pinned version
  • Tool definitions hashed; changes trigger re-approval
  • Remote servers use OAuth with per-user, audience-bound, short-lived tokens
  • Credentials are least-privilege and not stored in plain-text config
  • Write tools require confirmation showing full arguments
  • Local servers are sandboxed with restricted file system and egress
  • Inputs validated; no shell interpolation; outputs capped
  • Untrusted-content servers separated from sensitive-data servers
  • Every tool call logged centrally and monitored

The bottom line

MCP's security depends far more on how it is deployed than on the protocol itself. With governed servers, proper authorisation, hardened implementations, sandboxing and monitoring, organisations can give AI agents useful access to tools without handing attackers a new route into their systems.

Our application security testing includes MCP server and deployment reviews against this checklist.

EthicalHat Research

The EthicalHat research team: penetration testers, SOC analysts and engineers writing about security, software engineering and AI.

Want an expert opinion on your own systems?

Email info@ethicalhat.com or call +1-844-838-4422. Our security engineers are happy to talk it through.