How do I lock down an MCP server so an agent cannot do damage?
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.
Cite
GitShowcase 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.- Nils HaugenStaffAPIs 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:*oradmin:*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-Authenticatechallenge with ascopevalue. - Do not publish every possible scope in
scopes_supported, and avoid wildcard or catch-all scopes like*orfull-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.
3. If you proxy a third-party API, get consent per client
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_uriby exact string (no wildcards), and use a single-usestatevalue. 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:
- Prefer the
stdiotransport for local servers, so only the client that launched it can talk to it. - If you use HTTP locally, require an authorization token or use a restricted unix socket.
- Read the exact command before adding a server from someone else's config.
- 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.
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.