Authenticating remote servers with OAuth 2.1
Authenticating remote servers with OAuth 2.1, step by step
Level: from scratch — no prior experience needed. You've already built MCP servers and tested them in the Inspector. Now that a server can live on the network (not just on your computer), a new question shows up: who can use it? Watch the video above and follow this guide at your own pace.
When an MCP server runs on your own machine over stdio, security is simple: if you made it to your computer, you're already inside. But the moment that server is published on the internet as a remote server, anyone with the URL could try to talk to it. You need a doorway: a standard way to ask for credentials, hand them over, and verify them. That doorway is OAuth 2.1. Mind the nuance: authentication itself is optional — a remote server can be public — but when a remote server decides to authenticate, OAuth 2.1 is the framework that MCP requires (you can't invent your own scheme). In this lesson you'll understand it end to end, still without code: first the "how it works," which is what makes everything else fall into place.
What you'll learn
- Why a remote MCP server needs authentication (and a local one doesn't).
- The beginner's OAuth 2.1 flow: 401 → discovery → token → validate, step by step.
- What a JWT (the "token") is and what the server looks at when it receives one.
- Why the discovery endpoints stay open, without asking for credentials.
The flow, explained as a conversation
Think of OAuth 2.1 as entering a building with a reception desk. You don't knock on the office door: first you pass by reception, show who you are, they give you a temporary credential, and only with that credential do they let you through. These are the five steps, in order:
- The client knocks on the door. Your MCP client sends a first request to the remote server, still without credentials. It doesn't yet know what it will be asked for.
- The server responds
401 Unauthorized. It's a "no, not yet." But it's not a door slammed shut: alongside the 401 comes aWWW-Authenticateheader that points to the metadata of the protected resource. In other words, the server tells you "you need permission, and here's the address to find out how to get it." - The client does discovery. Following that clue, the client queries the
/.well-known/oauth-protected-resourceendpoint. That "well-known" file tells it who the authorization server is — the one to ask for the token. These discovery endpoints stay always open, without authentication, precisely so a new client can get started (bootstrap) without knowing anything in advance. - The client obtains a token (a JWT). With that information, the client goes to the authorization server, proves its identity, and receives an access token in the form of a JWT.
- The client retries, now with the credential. It repeats the original request to the server,
but this time it adds the
Authorization: Bearer <token>header. The server validates the JWT and, if everything checks out, responds. You're in.
What a JWT (the "token") is, in plain words
JWT stands for JSON Web Token. It's a token that carries signed information inside: who issued it, who it's for, when it expires, and what permissions it includes. Think of it as an event wristband: there's no need to call the organization on the phone to confirm you're valid — the wristband carries the data and a seal that can't be forged. That "seal" is the signature: the server can check that the token is legitimate without keeping a copy of every credential it issued.
When the server receives the Bearer <token>, it doesn't accept it blindly: it validates it. It
checks three key things:
- Issuer (
iss) — who issued it: does it come from the authorization server I trust? - Audience (
aud) — who it's for: was this token issued for this server, and not for another? (A token valid for another service should not work here.) - Scope — what it allows: does it include the permission needed for the action being requested?
If any of that doesn't check out, the server rejects the request. This verification is the heart of security first: a remote server must never trust a token without validating it.
Key moments in the video
- 5 — The first OAuth 2.1 flow. Alejandro shows the full journey: the
request without credentials, the
401, and the retry with the token. As you watch, hold on to the rhythm 401 → discovery → token → validate; it's the map of the whole lesson. - 11 — The
well-knowndiscovery. Here you see how the client discovers on its own which authorization server to ask for the token, by querying/.well-known/oauth-protected-resource. Pause and notice that this endpoint responds without asking for credentials: it's the bootstrap. - 27 — Validating the JWT. The key security moment: the server receives the token and checks issuer, audience, and scope before letting it through. If you take a single image from the video, let it be this one.
Quick glossary
- OAuth 2.1: the authorization framework that MCP requires when a remote server decides to authenticate (authenticating is optional; if it authenticates, it has to be with OAuth 2.1).
- 401 Unauthorized: a response that says "you need credentials"; it carries the header that points to the path to get them.
WWW-Authenticate: the 401 header that points to the metadata of the protected resource.- Discovery /
well-known: open endpoints (/.well-known/oauth-protected-resource) that tell the client who to ask for the token, with no authentication involved. - Authorization server: the "one that issues the credentials"; it hands over the token.
- JWT (JSON Web Token): a signed token that carries issuer, audience, expiration, and scope inside.
Authorization: Bearer <token>: the header with which the client presents the token.- Issuer / Audience / Scope: who issued the token / who it's for / what it allows. What the server validates.
To download
- 📄 OAuth 2.1 guide for remote MCP servers (1-page PDF with the flow as a numbered sequence, the step/what-happens/who table, and a "to remember" to keep beside you).
- 📄 OAuth exercises for MCP (PDF with 4–6 conceptual exercises to trace the flow, spot the missing step, and reason through validation — with solutions at the end).
Before moving on
If you keep one idea, let it be this: a remote server never trusts; it always
verifies. The client asks, the server responds 401 with a clue, the client does discovery,
gets a JWT and retries with Bearer, and the server validates issuer, audience, and scope before
opening the door. In the next lesson we continue with the conceptual security reading (the
checklist for MCP servers); the implementation of OAuth in code you'll do in the
Module 5 capstone.
