part…
Check what stuck.
A random exam drawn from every lesson quiz, and a glossary of every term in the course.
A random exam drawn from every lesson quiz, and a glossary of every term in the course.
The authorization code grant is the main OAuth 2.1 flow when there is a human behind the client. This lesson walks through it request by request, with PKCE and the resource parameter as MCP uses them, explains the front channel and the back channel, and shows why the authorization server issues a code instead of the token directly.
In this lesson
That is why there is an intermediate code: whatever goes through the browser is worth little (single use, a few minutes of life, useless without the PKCE code_verifier).
Authorization code + PKCE, request by request
1. Preparation (internal to the client)
The client generates:
state (to tie the return trip to this outbound trip).code_verifier and its code_challenge = BASE64URL(SHA256(verifier)).It keeps both secrets in memory.
2. Redirect the user to the AS
Client → User / browser
It opens the browser at the authorization_endpoint:
GET /authorize?response_type=code
&client_id=https%3A%2F%2Fapp.example.com%2Fclient.json
&redirect_uri=http%3A%2F%2F127.0.0.1%3A33418%2Fcallback
&scope=files%3Aread
&state=af0ifjsldkj
&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
&code_challenge_method=S256
&resource=https%3A%2F%2Fmcp.example.com%2Fmcp HTTP/1.1
Host: auth.example.com
None of these values is secret: the code_challenge is a hash.
3. Login and consent
User / browser → Authorization server
The user signs in on the AS's website (password, MFA, corporate SSO…) and sees: "App X wants to read your files. Allow?". The client does not take part in or see any of this.
4. Return with the code
Authorization server → Client
The AS redirects the browser to the redirect_uri:
HTTP/1.1 302 Found
Location: http://127.0.0.1:33418/callback?code=SplxlOBeZQQYbYS6WxSbIA
&state=af0ifjsldkj
&iss=https%3A%2F%2Fauth.example.com
The client, which was listening on that local port, receives the code. (iss appears with servers that support RFC 9207; MCP adopts it in 2026-07-28.)
5. Client checks
state match the one I generated? If not, abort.iss is present, does it match the AS I sent the user to? If not, abort.6. Code exchange (back channel)
Client → Authorization server
POST /token HTTP/1.1
Host: auth.example.com
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&code=SplxlOBeZQQYbYS6WxSbIA
&redirect_uri=http%3A%2F%2F127.0.0.1%3A33418%2Fcallback
&client_id=https%3A%2F%2Fapp.example.com%2Fclient.json
&code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk
&resource=https%3A%2F%2Fmcp.example.com%2Fmcp
Now the code_verifier is sent: the AS computes its hash and compares it with the code_challenge from step 2.
7. Token response
Authorization server → Client
{ "access_token": "eyJhbGciOi...", "token_type": "Bearer",
"expires_in": 3600, "refresh_token": "8xLOxBtZp8...", "scope": "files:read" }
8. Using the token
Client → MCP server
POST /mcp HTTP/1.1
Host: mcp.example.com
Authorization: Bearer eyJhbGciOi...
Content-Type: application/json
{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"read_file","arguments":{"path":"notes.md"}}}
9. The server validates and responds
MCP server → Client
It checks signature, expiry, audience (https://mcp.example.com/mcp) and scope. All good → 200 with the tool result.
| Parameter | Purpose |
|---|---|
response_type=code |
"I want an authorization code" |
client_id |
Who the application is (see lesson 3.1) |
redirect_uri |
Where to return the code; must match the registered one exactly |
scope |
Requested permissions, separated by spaces |
state |
Random value tying the outbound and return trips together (anti-CSRF) |
code_challenge, code_challenge_method |
PKCE |
resource |
Which MCP server the token is for (RFC 8707, mandatory in MCP since 2025-06-18) |
Put the authorization code + PKCE flow in order.
state, code_verifier and code_challenge/authorize with the code_challengeredirect_uri with code and statestatePOST /token with the code and the code_verifierAuthorization: BearerSummary
1. Why doesn't the AS return the access token directly in the step 4 redirect?
Returning the token in the redirect was the implicit grant, removed in OAuth 2.1 precisely because it exposed the token in the browser.
2. In the code exchange (POST /token), which value lets the AS confirm that the caller is the same client that started the flow?
The AS hashes the code_verifier and compares it with the code_challenge received in the authorization request. state is checked by the client on the redirect, not by the AS at the token endpoint.