How to Secure an MCP Server
How to secure an MCP server: authenticate every client, accept only tokens issued for that server, and check each tool call.
How to secure an MCP server starts by treating it as a resource server that runs tools, which means it runs code. Authenticate every client, accept only a token that was issued for this server, and authorize each tool call before it runs. The protocol states the obligations and leaves enforcement to you. A server that trusts its network, forwards the caller's token upstream, or lists every tool to every agent has already given the model more agency than the job needs.
What are you protecting?
An MCP server exposes tools, and often resources, to a model that chooses what to call next. The protocol's security principles say tools are arbitrary code execution, and that descriptions of tool behavior should be treated as untrusted unless they come from a server you trust. Hosts are told to get explicit consent before a tool runs, and to make clear what the tool does. The specification is plain that MCP itself cannot enforce those principles. Implementors build the consent flow, the access control, and the log.
A local stdio server often keeps the upstream secret in the client config. The trust boundary is that laptop and that file, and nothing central revokes the secret when the person leaves. A remote HTTP server is shared. NIST SP 800-207 assumes no implicit trust from network location or from who owns the machine. Authentication and authorization are separate steps, done before a session to the resource. "On the VPC" and "bound to localhost" describe where the process listens. The grant is a different fact.
How to secure AI agent access in your company is the same problem once several people each add a server. One careful server can still sit in a fleet, each with its own credential and, with luck, its own log.
Who is allowed to connect?
For a remote server, serve the endpoint over HTTPS. The authorization specification requires the client to send a bearer token on every HTTP request, in the Authorization header, and to keep access tokens out of the query string. The server, acting as the resource server, validates that token and checks that it was issued for this server as the audience. An invalid or expired token gets a 401. A token minted for some other API is the wrong credential, even when the signature is good.
If you run the authorization server, follow the authorization security considerations. Clients use PKCE with the S256 challenge and refuse a server that does not advertise PKCE. Redirect URIs are registered exactly and checked against that list. Prefer short-lived access tokens. A token written into a log is a token an attacker can replay.
An editor and a CI job are different callers. Give the job its own credential, with an expiry, so revoke does not depend on a file left on a laptop. A personal token pasted into a pipeline is a second copy of a human session, and it outlives the offboarding.
Local stdio can skip this for a tool you run yourself, against files already on that machine. The moment that server holds a production password, the laptop is the credential store. Put the server behind the same rules, or put a gateway in front and stop handing out the password.
Which tools should a caller see?
OWASP LLM06:2025, Excessive Agency, splits the failure into excessive functionality, excessive permissions, and excessive autonomy. The MCP version is a server that lists delete_page, refund, and a shell beside the one read the job needed. The model will sometimes pick the wide tool. LLM01:2025, Prompt Injection, is one reason: untrusted text, including text returned by a tool, can redirect the next call. The tool's own description is untrusted text of the same kind, and it can change after you approved it. How to prevent MCP tool poisoning and rug pulls is that review. Limit the damage by limiting what the model is allowed to invoke.
The tools specification puts the same duty on the server: validate inputs, implement access controls, rate-limit invocations, and sanitize outputs. Clients should prompt on sensitive operations, show the inputs before the call, check results before they go back into the model, time out, and log. A server that relies on the client to hide dangerous tools has delegated its access control to every editor that connects.
Build the tool list per caller:
- A support agent sees
orders.readandstripe.refund. It does not see a general SQL tool and a shell. - A database answers reads unless you have granted writes.
- A CI agent that deploys sees the deploy tool, on its own credential, until the date you set. It does not see the engineer's mail or the billing admin tools.
RBAC vs ABAC for AI agents is the longer version of that split. The role, team, or project decides which tools appear. Attributes of the call decide which of those calls may run.
Why must the server refuse someone else's token?
Token passthrough is the anti-pattern the security best practices document forbids. The server accepts a token that was not issued for it, then forwards that token to a downstream API. Downstream rate limits and audit logs key off the token's audience. A forwarded token skips them, and the downstream log names a caller that is not your server. A stolen token for one service then works anywhere that accepts it.
The mitigation is a single rule. The server accepts only tokens explicitly issued for itself. When the server calls an upstream API, it uses a different credential, issued for that API. The authorization security considerations repeat the rule: the inbound token stays at the MCP server, and the upstream token is a separate token from the upstream authorization server.
A proxy in front of a third-party API has a second duty, the confused deputy. One static OAuth client id for every MCP client lets a consent cookie from an earlier approval skip the third-party consent screen on a later request. The security best practices document requires the proxy to record consent per client before it sends the user to that authorization server.
The inbound token names who may talk to this server. The upstream credential names what this server may do for them. Keep the two apart. How to stop MCP token passthrough and confused deputy attacks is that split in full: the audience check, the separate upstream credential, and the per-client consent a proxy records before it redirects.
What do you check before a tool runs?
Validate arguments against the tool's schema before any work. Reject a missing or mistyped required field. The tools specification pairs that with the access check, a rate limit, and sanitized output. A tool result is text the model reads next. Raw HTML, or a line that tells the model to call another tool, extends whatever the upstream system stored into the agent. Return only what the job needed.
Then decide the action, which is a narrower question than "may this identity call this tool?" A support role may hold stripe.refund. A refund of $4,000 is still a different action from a refund of $40. How to write AI agent guardrails that fail closed is the pattern: name the action, the resource, and the condition, in a sentence a reviewer can test.
- "Nobody force-pushes to main."
- "Refunds over $500 need Finance."
- "Deny deleting any file under
/reports/q3."
Run a new rule in monitor mode first. Matches are allowed and flagged, so you can see false positives on real traffic. When the rule behaves, enforce it. An enforcing check fails closed: if the checker is down, or the answer is unusable, the call is denied and the denial is logged. A retry is cheaper than a call that ran with no decision.
Sensitive calls that are sometimes legitimate wait for a person. How to require human approval for MCP tool calls is that hold in detail: which calls wait, what the approver sees, and why a timeout denies the call. The tools specification asks clients to confirm those operations and to show the inputs first. On a shared server, send that class of call to an approver in the right role, and keep the requester from approving their own request. A ticket read can proceed. A delete, a refund over the threshold, or a force-push waits.
Rate-limit invocations per caller, and time each call out, as the tools specification requires of servers. A tight loop of writes is an outage and a bill. OAuth discovery follows URLs the server supplies, and the security best practices treat that as server-side request forgery: a malicious server can name internal addresses. Connect clients to servers you trust, and keep discovery off arbitrary internal URLs.
Where should secrets live?
Upstream credentials belong on the server that calls the upstream API. They do not belong in the client config that every developer copies. This is the failure mode in a hand-maintained .mcp.json: the database URL, including the password, sits next to the command that starts the server.
{
"mcpServers": {
"orders": {
"command": "npx",
"args": ["orders-mcp", "postgres://admin:pw@prod-db:5432/app"]
}
}
}
Anyone who can read the file can read production, and rotating the password means finding every copy.
Put one URL in the client, and keep the password on the server that injects it at call time. The plaintext reaches the database. It does not reach the model, and it does not sit in the editor config:
{
"mcpServers": {
"smartplane": {
"url": "https://app.smartplane.dev/mcp"
}
}
}
Clients that support MCP sign-in need that URL. The editor opens a browser, the person signs in with their work account, and access ends when the membership does. For CI and older clients, issue a separate token and send it as a bearer token. Show it once, store only a hash, and put an expiry on it. Revoking that token, or removing the grant, stops the next call without a hunt through laptops.
What belongs in the log?
Log the caller, the tool, the decision, and the time, and log grant changes, approvals, and sign-ins on the same trail. The tools specification asks clients to log tool use. A server on the shared path should log it too, or the record depends on every editor.
Make the log append-only, each event carrying the hash of the one before it, so an edit or a deletion breaks the chain. Export a window when an auditor asks, and log the export. Leave secret values out. A delivery can record which secret, to whom, and when. The amount that tripped a refund rule belongs in the decision. A credential pasted into an argument is a reason to keep log access tight, and a reason to keep secrets out of arguments in the first place.
What does a secure server check off?
| Check | What good looks like |
|---|---|
| Transport | A remote server is reached over HTTPS |
| Caller | Every request carries a bearer token issued for this server; anything else is a 401 |
| Upstream | The server calls APIs with its own credential, and does not forward the caller's token |
| Consent | A proxy records approval per client before it sends the user to a third-party authorization server |
| Tool list | The caller sees only the tools their role, team, or project was granted |
| Arguments | Inputs are validated, and a missing required field denies the call |
| Sensitive calls | A person sees the inputs, or the call waits for an approver who is not the requester |
| Limits | Invocations are rate-limited, and each call has a timeout |
| Output | Tool results are sanitized before they go back to the model |
| Secrets | Upstream passwords stay on the server, not in the client config |
| Failure | A check that does not answer denies the call, and the denial is logged |
| Record | Caller, tool, decision, and time are on an append-only log, with no secret values |
For a server you operate, work down the list. For a server you only connect to, you still choose which server you trust, what goes in the config, and whether one gateway covers the other rows.
Frequently asked questions
Is a local stdio server safe because it is local?
It is as safe as the laptop and the file that holds its secrets. NIST SP 800-207 withholds trust based on location alone, and localhost is a location. A personal tool against local files can live there. A production password should not.
Does a bearer token replace tool authorization?
The token names the caller. A separate decision names the tool and the arguments. The authorization specification covers the first. The tools specification's access controls, and a rule on the arguments, cover the second. You want the 401 and the denial in the same log.
Should every tool call wait for a person?
Reads the role already allows can run. Deletes, payments, and anything hard to undo should wait, which is the tools specification's guidance on sensitive operations. Approve the calls that are sometimes right, and hard-deny the calls that never are, so the queue stays short enough to read.
What if the policy check is down?
Deny the call and log the reason. How to write AI agent guardrails that fail closed is the difference between a retry and an action taken while the check was blind. Enforce again when the check is answering.
Do you secure each server, or one gateway?
Secure a server you own against the checklist above. When the fleet is many servers and many laptops, put one gateway in front: register each upstream once, grant tools by person, team, role, or project, and keep the upstream secrets on the gateway. Best MCP gateways for enterprises compares the products built for that job. RBAC vs ABAC for AI agents is how to split the grant and the per-call rule once that gateway exists.
Where does a gateway fit?
A gateway is the shared path the checklist assumes. Agents connect to one MCP endpoint. Administrators register tools, MCP servers, skills, databases, and APIs once, and grant them to roles, teams, and projects. When a member's agent lists tools, it sees only what that member may use. The gateway injects the upstream credential, runs the call, and returns the result. The plaintext stays off the laptop and out of the model.
Smartplane is that gateway. Sign-in uses the work account, and access ends when the membership does. Directory offboarding, over SSO and SCIM, revokes the membership, the tokens, the grants, the teams, and the personal connected accounts together. Guard rules are plain-English checks before the action, in monitor mode until you enforce them, and they fail closed. A call that should sometimes proceed waits for approval from someone who is not the requester. The audit log is append-only and hash-chained.
The same engineering can live on each server you run. A gateway is how a team does it once, with one grant list and one log for a review to point at. The security page is the control list. The product is the URL you put in the client, in place of the password.
Related articles
How to Stop MCP Token Passthrough and Confused Deputy Attacks
How to stop MCP token passthrough and confused deputy attacks: accept only a token issued for this server, and call upstream with a separate credential.
How to Prevent MCP Tool Poisoning and Rug Pulls
How to prevent MCP tool poisoning and rug pulls: read the full tool definition, pin a hash of it, and refuse the tool when the hash changes.
Best MCP Gateways for Enterprises (2026)
Best MCP gateways for enterprises: the controls to require, who each current product fits, and what their public pages actually say.
