Skip to content
GitShowcase Forum

How do I lock down an MCP server so an agent cannot do damage?

Staff answerWritten by Nils Haugen · MCP Servers and Agent Tooling0 replies

Short answer

Assume the agent will eventually call every tool you expose with the worst arguments it can be talked into. So limit what the server can do: read-only by default, narrow scopes, tokens issued only for your server, local servers sandboxed, and a human confirmation for anything destructive. Do not rely on the prompt to keep the agent safe.

Written by Nils Haugen · APIs and tooling editor: LLM, MCP, payments and portfolios ·
CiteGitShowcase Forum, "How do I lock down an MCP server so an agent cannot do damage?", https://gitshowcase.com/forum/discuss/how-do-i-lock-down-an-mcp-server-so-an-agent-cannot-do-damage/, accessed 2026-11-21.
  1. APIs and tooling editor: LLM, MCP, payments and portfolios ·

    Start from the threat, not the tool list

    An MCP server is code that runs with real credentials, driven by a model that reads untrusted text. A web page, an issue comment or a document the agent reads can contain instructions, and the model may follow them. The server cannot tell the difference between you asking for a delete and an injected instruction asking for it. So the safe design is to make the damaging call impossible or gated, not to hope the model refuses. If you want the basics of what an MCP server is first, see what an MCP server is and when to build one.

    The MCP security best practices page (current as of the November 2026 docs) lists concrete attacks and fixes. The checklist below follows it, plus a few habits that are common sense rather than protocol rules.

    1. Give the server the smallest scopes you can

    The docs call out scope minimization: broad scopes such as files:*, db:* or admin:* make a stolen token far more damaging. Their recommended model is progressive and least-privilege:

    • Start with a minimal set of scopes for low-risk read and discovery operations.
    • Ask for more only when a privileged operation is first attempted, using a targeted WWW-Authenticate challenge with a scope value.
    • Do not publish every possible scope in scopes_supported, and avoid wildcard or catch-all scopes like * or full-access.
    • Do not treat a scope claimed in a token as enough on its own. Check authorization in server code too.

    The same idea applies to the credentials behind the server. If a tool only reads rows, connect with a database role that can only read them.

    2. Never pass tokens through

    Token passthrough means accepting a token from the client and forwarding it unchanged to another API. The docs say this is explicitly forbidden: MCP servers must not accept tokens that were not issued for the MCP server itself. Check the audience claim (RFC 9068 describes it) so a token minted for some other service is rejected. Passthrough bypasses your rate limits and audit trail, and it lets a stolen token use your server as a proxy.

    A proxy that uses one static client ID with a third-party OAuth server, and also allows dynamic client registration, can be tricked into a "confused deputy" attack where a consent cookie lets an attacker skip the consent screen. The docs say such proxies must keep a registry of approved client IDs per user, show their own consent page before forwarding, match redirect_uri by exact string (no wildcards), and use a single-use state value. If your server is not a proxy, you can skip this one.

    4. Treat local servers as code running on your machine

    A local MCP server runs with the privileges of whoever launched it. The docs warn about malicious startup commands in client configs and about servers left running on localhost. Practical rules:

    1. Prefer the stdio transport for local servers, so only the client that launched it can talk to it.
    2. If you use HTTP locally, require an authorization token or use a restricted unix socket.
    3. Read the exact command before adding a server from someone else's config.
    4. Run it in a container or sandbox with restricted file system and network access.

    5. Block server-side request forgery in clients

    If your client fetches URLs supplied by a server (OAuth discovery, metadata), a hostile server can point it at http://169.254.169.254/ or an internal address. The docs recommend requiring HTTPS, blocking private and link-local ranges, validating redirect targets, and using an egress proxy. They also warn not to hand-roll IP checks, because encoding tricks defeat custom parsers.

    6. Bind any state handle to the caller

    If a tool returns an ID such as a cart or workflow handle, the docs say possession of that handle must not count as authentication. Use random handles and store them keyed by the verified user ID, so another user presenting the same handle is refused.

    7. Add your own guard rails

    These come from experience, not the spec, so adapt them:

    • Split tools into read and write, and expose only the read tools in the default configuration.
    • Make destructive tools (delete, send, pay) require an explicit confirmation step in the client.
    • Validate every argument server-side. Assume the model will send surprising values.
    • Log each tool call with who, what and when, so you can reconstruct an incident.

    A tiny example of a guard that refuses writes unless the server was started in write mode:

    const WRITE_ENABLED = process.env.MCP_ALLOW_WRITES === "1";
    
    function requireWrite(toolName: string) {
      if (!WRITE_ENABLED) {
        throw new Error(`${toolName} is disabled: start the server with MCP_ALLOW_WRITES=1`);
      }
    }
    

    For a broader review of server risks, our MCP server security guide goes through typical failures.

    0
    ReplyLink

Plain text or simple Markdown. Links are nofollow. Your email is never shown.

GitShowcase Forum. Questions and answers on shipping developer projects: dev tools, APIs, infrastructure and portfolios.