Sub topic 12
Remote servers and authorization
This chapter covers MCP servers that run somewhere other than your own machine, and how a client proves who it is to them. It assumes you have read the chapters on transports, client configuration, and security and trust. You should be comfortable with HTTP headers, status codes, and the idea of a bearer token.
Why remote servers exist
A stdio server is a child process of your assistant. It runs with your user account, reads your environment variables, and dies when the assistant exits. That model is simple, but it has costs:
- Every user installs and updates the server themselves.
- The server needs the runtime it was written for (Node, Python, and so on).
- Credentials end up in local config files and environment variables.
- A team cannot share one server instance or one set of audit logs.
A remote server is a long-running HTTP service. The client connects to a URL instead of launching a command. A vendor can host it once for everyone, and it can act on behalf of each user through that user's own account. Hosted servers for issue trackers, documentation search, and cloud dashboards usually work this way.
The trade-off is trust. A stdio server runs code on your machine. A remote server receives your requests and returns text that your model will read, so you need to decide who is allowed to call it and what the client must prove first.
Streamable HTTP in practice
Chapter on transports introduced Streamable HTTP. Here is the part that matters for remote use.
The server exposes a single endpoint, for example https://example.com/mcp.
- The client sends every JSON-RPC message as an HTTP
POSTto that endpoint. - The
Acceptheader must list bothapplication/jsonandtext/event-stream. The server picks one for its reply. - For a simple request, the server answers with one JSON body. For a long call that streams progress or needs to ask the client something, it answers with a Server-Sent Events stream.
- A client may also open a
GETto the same endpoint to receive server-initiated messages. Servers that never send these can reply405 Method Not Allowed. - The server may assign a session by returning an
Mcp-Session-Idheader on the initialize response. The client then sends that header on every later request. If the server forgets the session, it answers404, and the client must initialize again. - After initialization, the client also sends an
MCP-Protocol-Versionheader with the negotiated version.
You can watch this with curl. The following sends the initialize request to a local server and prints the response headers and body:
curl -i http://127.0.0.1:8000/mcp \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"curl-test","version":"0.1"}}}'If the server requires authorization, this request returns 401 instead. That response is the starting point for the rest of the chapter.
Serving the earlier example over HTTP
The calculator server from the chapter on writing a server needs only one changed line to listen on HTTP:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("calculator")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two integers."""
return a + b
if __name__ == "__main__":
mcp.run(transport="streamable-http") # serves on http://127.0.0.1:8000/mcpThe tool code does not change. Only the transport does, which is the point of the protocol's layering.
Connecting a client to a URL
Most assistants accept a URL in place of a command. The exact field names differ between products, so check your assistant's documentation. A common shape looks like this:
{
"mcpServers": {
"docs": {
"url": "https://mcp.example.com/mcp"
}
}
}Some assistants also let you add static headers for servers that accept a plain API key:
{
"mcpServers": {
"internal-api": {
"url": "https://mcp.example.com/mcp",
"headers": { "Authorization": "Bearer <set-in-your-environment>" }
}
}
}If your assistant only supports stdio, a bridge process such as mcp-remote can run locally and forward messages to the URL. This works, but it adds one more program that sees your traffic and tokens, so treat it with the same scrutiny as any other server you install.
Why remote servers need authorization
An HTTP endpoint on the internet can be called by anyone who finds it. The server must therefore answer two questions for every request:
- Who is calling?
- Is this caller allowed to use this tool with this data?
The MCP specification answers this with an optional authorization layer built on OAuth 2.1. It applies to HTTP transports. It does not apply to stdio, where the server should take credentials from its environment instead.
The OAuth roles in MCP
OAuth names the same three parties you already know under different labels.
| MCP term | OAuth role | What it does |
|---|---|---|
| MCP server | Resource server | Hosts the tools. Accepts or rejects access tokens. |
| MCP client (in the assistant) | OAuth client | Obtains a token on the user's behalf and attaches it to requests. |
| Authorization server | Authorization server | Signs the user in, asks for consent, and issues tokens. It may be a separate service or part of the same product. |
The user is the fourth party. They sign in and approve what the client may do, and the resulting token carries that approval.
The authorization flow
The flow starts when an unauthenticated request is refused.
Each step has a purpose:
- The 401. The server refuses the request and includes a
WWW-Authenticateheader that points to its protected resource metadata document. - Resource metadata. This JSON document (RFC 9728) tells the client which authorization server issues tokens for this MCP server.
- Authorization server metadata. A second document (RFC 8414) lists the authorization endpoint, token endpoint, and registration endpoint.
- Client registration. The client needs an identity at the authorization server. Because users add arbitrary servers, the specification supports dynamic client registration (RFC 7591), so a client can register itself automatically. Servers may also accept pre-registered clients.
- PKCE. The client creates a random secret, sends only its hash with the sign-in request, and reveals the secret when it exchanges the code. A stolen authorization code is useless without it. MCP clients are required to use it.
- Resource indicator. The client states which server the token is for (RFC 8707), so the token can be limited to that one audience.
- The bearer token. The client sends
Authorization: Bearer <token>on every request. The token goes in the header, never in the URL query string, because URLs end up in logs.
Your assistant performs all of this. You usually see only a browser window asking you to sign in and approve.
What the server must check
Accepting a token is more than checking that it exists. A resource server should:
- Validate the signature or introspect the token.
- Confirm the token has not expired.
- Confirm the audience matches this server. A token issued for a different service must be rejected.
- Check the scopes against the tool being called. A read-only token should not run a delete tool.
- Return
401for missing or invalid tokens and403for valid tokens that lack permission.
Token passthrough
A server must not accept a token issued for someone else, and it must not forward the client's token to a downstream API. If your MCP server calls another service, it needs its own credential for that service. Forwarding tokens creates a confused deputy: the downstream service believes it is talking to the original client, and audit trails and rate limits stop meaning anything.
Risks specific to remote servers
Authorization controls who can connect. It does not make the server's output trustworthy.
- Prompt injection still applies. A remote server that returns issue text or web pages passes attacker-written content to your model. Authenticated does not mean safe.
- Token scope. Approve the narrowest scopes offered. A token for "all repositories, read and write" is a larger loss if it leaks than one limited to a single project.
- Local HTTP servers. A server bound to
localhostcan be reached by web pages in your browser through DNS rebinding. It should bind to127.0.0.1rather than0.0.0.0, check theOriginheader, and authenticate requests. - Plain HTTP. Use HTTPS for any non-local URL. Tokens sent over plain HTTP can be read in transit.
- Revocation. Know where to remove access. Deleting a server from your config does not revoke the token at the authorization server. Revoke it in the provider's account settings.
Troubleshooting remote connections
| Symptom | Likely cause | What to check |
|---|---|---|
| `401` and the browser never opens | Client does not support OAuth for this server | Use a static header with a token, or a bridge |
| Sign-in succeeds, then `401` again | Token audience or scope does not match | Inspect the token's `aud` and `scope` claims |
| `403` on one tool only | Token lacks the scope for that tool | Re-authorize with broader scope, if appropriate |
| `404` after working for a while | Server dropped the session | Reconnect so the client re-initializes |
| `406 Not Acceptable` | Missing `Accept` header values | Send both `application/json` and `text/event-stream` |
| Works in `curl`, fails in the assistant | Redirect URI or registration problem | Read the assistant's MCP logs |
The MCP Inspector supports HTTP transports as well. Choose Streamable HTTP, enter the URL, and add any headers, and you can inspect the failing step without involving the assistant.
Choosing between local and remote
| Question | Prefer stdio | Prefer remote HTTP |
|---|---|---|
| Does it only touch local files? | Yes | No |
| Do many users need the same server? | No | Yes |
| Does it need per-user sign-in to a SaaS product? | Awkward | Natural |
| Do you want no local install? | No | Yes |
| Do you need the code to run in your own trust boundary? | Yes | Only if you host it |
Exercise
Your assistant connects to https://mcp.example.com/mcp and the server replies 401 with a WWW-Authenticate header. Name the first two documents the client fetches and what each tells it. Then explain why the server must reject a valid token that was issued for a different API.
Show answer
The client first fetches the protected resource metadata (RFC 9728) at the URL in the header, which names the authorization server for this MCP server. It then fetches the authorization server metadata (RFC 8414), which lists the authorization, token, and registration endpoints. The server must reject a token for a different audience because accepting it lets any service's token act as access here (and forwarding it onward creates a confused deputy). Each token should be valid only for the resource it was issued for.
Exercise
You run the calculator server from this chapter with mcp.run(transport="streamable-http") and test it with curl, but you get 406 Not Acceptable. What is the most likely fix?
Show answer
Add an Accept header that lists both application/json and text/event-stream, because the server may reply with either a single JSON body or an event stream.