Skip to content

When the MCP server is the attacker

Everybody hardens the server, but almost nobody asks what the client does when a server tells it to go fetch something. This post shows how a hostile server can turn your client against your own network, and which defenses hold up.

Say you add an MCP server to your agent, and it is one you didn't write, which is the usual case. On the very first request the server answers with a 401 and a URL that is supposed to point at its authorization metadata. Your client follows it, as the protocol tells it to, but the URL points at 169.254.169.254, your cloud's metadata endpoint, so the request goes out from inside your network, sent by your own software.

Nobody broke into anything here: the server asked, and your client fetched. This is server-side request forgery (SSRF), with your MCP client as the vehicle. We spend a lot of effort asking whether a server can be trusted with our data, and very little asking what our client will do simply because a server told it to.

Why the client is the weak point

The server holds the data and runs the tools, so it is the server that gets the authentication, the audit and the security review. The client, on the other hand, is written to cooperate: it follows the server's discovery documents, its redirects and its tool descriptions, because that is its job.

The difficulty lies in who it is cooperating with. When you connect to a server you found in a registry or in somebody's README, you usually don't know who runs it, and yet by connecting you give that party two things: your client's position inside your network, and whatever credentials your client carries.

The usual advice is to connect only to servers you trust, but that doesn't go far enough, because trust is not all-or-nothing. A server from a vendor you like is still a collection of tools, and a single poisoned or quietly edited tool description is enough to cause harm. You can trust the company and still have no reason to trust every URL its server hands you.

PUBLIC INTERNETMALICIOUS MCP SERVERyou don’t know who runs itYOUR INTERNAL NETWORKINTERNAL SERVICES169.254.169.254, databases…YOUR CLIENTon both networksconnects outtalked into going incan’t reach it directlyPUBLIC INTERNETMALICIOUS MCP SERVERyou don’t know who runs itYOUR CLIENTon both networksINTERNAL SERVICES169.254.169.254, databases…YOUR INTERNAL NETWORKconnects outtalked into going incan’t reachit directly
The setup The attacker's server can't reach your internal network, but your client can, so the attack consists of getting your client to do the reaching.

SSRF through OAuth discovery

This attack follows directly from how OAuth discovery works. A protected MCP server answers the first request with a 401, and a header in that response tells the client where the server's metadata lives. The client is supposed to follow that pointer to find out where to log in, and a naive client will follow it wherever it leads.

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer resource_metadata="http://169.254.169.254/latest/meta-data/"

→ your client GETs that URL, from inside your network
YOUR CLIENTMALICIOUS SERVERINTERNAL METADATA1 POST /mcp2 401, metadata is at 169.254.169.2543 GET that URL, from inside your network4 whatever lives thereYOURCLIENTMALICIOUSSERVERINTERNALMETADATA1 POST /mcp2 401, metadata is at 169.254.169.2543 GET that URL, from inside your network4 whatever lives there
The mechanism The server chooses the URL, and the client fetches it without checking whether it is a place it should ever go.

In client code the slip is easy to miss, because it looks like simply doing what the protocol says:

# following discovery, the naive version
# the server answers this request with a 401
r = httpx.post(f"{server}/mcp", json=request)
hdr = parse_www_authenticate(r.headers["WWW-Authenticate"])
# fetches whatever URL the server named
meta = httpx.get(hdr["resource_metadata"]).json()

That header is not the only opening. The server controls three sets of URLs during discovery, and the MCP security guide lists all of them:

  • the resource_metadata URL in the 401,
  • the authorization_servers list inside that metadata,
  • the token_endpoint, authorization_endpoint and the other URLs inside the authorization server's metadata.

Each of these is a field where a hostile server can write an address that your client will then request.

What the attacker gets out of it

A fair question at this point is how the server ever sees the answer, given that it was your client that did the fetch. There are three ways it can benefit:

  • Reading data through error messages. Your client expected OAuth metadata and received something else. If the error it reports, logs or passes on includes what it received, the internal response ends up travelling back toward the attacker.
  • Mapping the network without seeing any response. Which addresses answer, which time out and how long each one takes is enough to map a network the attacker has no direct view of.
  • Triggering writes. The token endpoint is a URL your client sends a POST to, so if it points at an internal endpoint that changes state on POST, your client has just made that change.

Why the obvious fixes fail one after another

This is the part I find most interesting: each obvious fix is correct as far as it goes, and each has a counter-move.

  1. DefenseRequire HTTPS for every OAuth URL. This already stops the textbook example, because the cloud metadata endpoint only speaks plain HTTP.
    BypassThe attacker targets an internal service that does speak HTTPS, and most networks have plenty of those.
  2. DefenseResolve the hostname and refuse private, loopback and link-local addresses.
    BypassThe attacker gives you a public URL that passes the check and then answers it with a 307 redirect to an internal one, which many HTTP libraries follow by default.
  3. DefenseTurn off automatic redirects and apply the same checks to every hop.
    BypassThe attacker moves to DNS and gives you a hostname whose record lives for one second, so that it resolves to a public IP when you check it and to an internal IP a moment later, when you connect.
YOUR CLIENTATTACKER’S DNSINTERNAL HOST1 resolve host (for the safety check)2 203.0.113.10, public, passes the check3 resolve again (to open the connection)4 10.0.0.5, internal5 connect to 10.0.0.5YOURCLIENTATTACKER’SDNSINTERNALHOST1 resolve host (for the safety check)2 203.0.113.10, public, passes the check3 resolve again (to open the connection)4 10.0.0.5, internal5 connect to 10.0.0.5
DNS rebinding The check and the connection each query DNS, and each gets a different answer. This is known as a time-of-check to time-of-use (TOCTOU) problem.

Notice what happened in that last step: the check was correct at the moment it ran and wrong by the time it mattered. A smarter check can't fix this, because the weakness is the gap between checking and connecting, and it is that gap which has to be closed.

Closing the gap with an egress proxy

We usually guard only one direction of traffic. A reverse proxy such as Nginx stands in front of your services and filters what comes in, whereas SSRF works in the opposite direction: your client is talked into sending requests out. The control therefore belongs on the outbound side.

An egress proxy sits between your client and everything it calls. For each request it resolves the hostname once, checks that one address against the ranges you forbid, and then connects to exactly that IP. Because the same lookup is used for both the check and the connection, there is nothing left to rebind, and an internal address is never dialed, whether or not a redirect is involved.

YOUR CLIENTeverything outboundEGRESS PROXYresolve, check, pinone lookup for bothPUBLIC SERVERallowedINTERNAL HOST10.0.0.5, blockedYOUR CLIENTeverything outboundEGRESS PROXYresolve, check, pinone lookup for bothPUBLIC SERVERallowedINTERNAL HOST10.0.0.5, blocked
Resolve, check, pin A single lookup is used for the check and for the connection, so the address that was checked is the one that gets dialed.

The MCP security guide reaches the same conclusion: it advises anyone running a client on a server to consider an egress proxy. I agree with that advice for two practical reasons. First, it gives you a single place to get this right, rather than every HTTP call in every client. Second, it also covers the requests you forgot about, such as the POST to the token endpoint.

The same three steps can also be implemented inside the client's HTTP layer, and in a desktop app, where you can't put a proxy on the user's machine, that is the only option you have. It is a weaker control, though, and it is worth being clear about why. A check inside the client protects only the requests that pass through it, so any other HTTP call in the application, in a dependency or in a subprocess goes around it, and the internal address is still reachable from the machine. An egress proxy is different only when it is combined with a network policy that blocks every other outbound path: the rule is then enforced outside the process and holds whatever the code does. On a server I would use both, with the in-client check as a first layer and the proxy as the one that is actually enforced.

# resolve once, check that address, then connect to it
# (the name is never resolved a second time)
import ipaddress, socket

def pin(host: str) -> str:
    ip = socket.getaddrinfo(host, None)[0][4][0]
    addr = ipaddress.ip_address(ip)
    # ::ffff:10.0.0.5 is really 10.0.0.5
    if addr.version == 6 and addr.ipv4_mapped:
        addr = addr.ipv4_mapped
    # False for private, loopback, link-local, shared,
    # reserved and unspecified addresses
    if not addr.is_global:
        raise PermissionError(f"blocked: {addr} is not public")
    # connect to this address, not to host
    return str(addr)

The check uses the library's own is_global test rather than a hand-written list of ranges, for the reason given in the note above: a list is easy to get wrong, and addresses such as 0.0.0.0 or the 100.64.0.0/10 shared range are easy to leave out. There is also one practical detail when you connect to the returned address over HTTPS. You still have to use the original hostname for the TLS handshake (SNI and certificate verification) and for the Host header, because connecting to a bare IP address otherwise fails certificate validation, and switching validation off to get around that would be a worse problem than the one being fixed.

The trace below illustrates the difference. It shows a client talking to a hostile server, first with the check off and then with it on:

## check off
POST /mcp
     ← 401
# the server chose this URL
GET  http://169.254.169.254/latest/meta-data/…
     ← 200  {"AccessKeyId":"ASIA…", …}
     the client now holds a response
     it should never have requested

## check on (resolve, check, pin)
POST /mcp
     ← 401
     blocked: 169.254.169.254 is not public
     the request was never sent

Other attacks, and who has to fix them

SSRF is not the only thing a hostile server can attempt. The table below lists four more attacks, together with who actually has to fix each of them.

AttackWho has to fix itWhat the client should do
Hostile authorization URLThe client.The authorization_endpoint comes from the server, and your client opens it. A javascript: URL, or one containing shell metacharacters, can turn opening the login page into code execution. Allow only https:// (and plain http:// for loopback during development), and never open a URL through a shell.
Authorization server mix-upThe client.Record which issuer you started the flow with, and compare the iss value in the authorization response against it before you redeem the code (RFC 9207).
Token at the wrong audienceMostly the server, which must reject tokens that were not issued for it.Request every token with the resource parameter (RFC 8707), store tokens per server, and never send a token to a server it was not issued for.
Confused deputyThe server. It affects proxy servers that use one fixed client ID at a third party and show no consent screen of their own.There is nothing to fix on the client side.

The mix-up attack deserves a short explanation, because it is often assumed that PKCE covers it. It does not: in a mix-up, your client itself sends both the authorization code and the PKCE verifier to the attacker's token endpoint. The defense is simple bookkeeping, which is to remember which authorization server you started the flow with and to check iss before handing anything over.

As for the confused deputy, I looked for a client-side fix and did not find one. The attack takes place between a victim's browser, a proxy server and a third-party authorization server, and your client is not part of that exchange. What you can do is learn the pattern, so that you recognise a server built this way before you connect your users to it.

Checklist

  • Treat every URL from a server as untrusted. Require HTTPS, resolve the name, check the address, and connect to the address you checked.
  • Don't let your HTTP library follow redirects automatically, and apply the same checks to every hop.
  • Route outbound requests through an egress proxy if your client runs on a server, and block every other outbound path so that the proxy can't be bypassed.
  • Open only https:// authorization URLs, and never through a shell.
  • Record the issuer and check iss before you redeem the authorization code.
  • Keep one token per server, bound with resource, and request the narrowest scopes that work.
  • Trust tools individually. Pin tool definitions, and ask the user again when one of them changes.

Conclusion

Agentic AI and MCP are developing quickly, but none of these defenses is new. Checking where a request really goes, validating every redirect, filtering outbound traffic and verifying who issued a response are all practices we inherited from the web applications and OAuth deployments that came before MCP. The only change is to apply them to the server at the other end of the connection as well, since it is just as untrusted as any user input.

It is also worth noticing that none of the attacks in this post is a coding mistake. The naive client shown earlier contains no bug in the usual sense: it does what the discovery flow asks, and it is vulnerable because of what it trusts. That matters now that more and more code is written by AI. Whether or not AI already writes better code than we do, the number of CVEs published every year suggests that humans were producing slop long before the word was applied to AI. Either way, better code does not help here. As implementation bugs become rarer, what remains is the question of whom your software trusts, and of how you find the places where that trust can be abused. An AI agent asked to implement discovery will write the client, mock the server in its tests and watch them all pass, because a mocked server only ever returns what the test expects. Nothing in that process asks what happens when the real server is hostile, unless somebody thinks to ask. Asking it is threat modeling, and somebody still has to decide that it gets done.

Checked against the 2026-07-28 revision of the MCP specification.

← Back to Writing