The MCP Python SDK advertises client_secret_basic, then rejects it
For four days, every health check Glama ran against our MCP server failed with a 401. The sign-in page worked. The consent screen worked. The user pressed Allow. Then the last step, swapping the authorization code for a
For four days, every health check Glama ran against our MCP server failed with a 401. The sign-in page worked. The consent screen worked. The user pressed Allow. Then the last step, swapping the authorization code for a token, failed, and nothing on screen said why.
The cause turned out not to be in our code. It is in the MCP Python SDK, and if you run an MCP server with the SDK's built-in OAuth, your server very likely has it too.
What the logs showed
The whole sign-in flow, in order:
GET /.well-known/oauth-authorization-server/memory 200
GET /authorize?response_type=code&client_id=... 302
POST /token 401
The token endpoint answered:
{"error": "invalid_client", "error_description": "Missing client_id"}
But the client had sent its client_id. It was in the Authorization header.
The standard says that is allowed
OAuth 2.0 (RFC 6749, section 2.3.1) gives a confidential client two ways to send its credentials to the token endpoint:
-
client_secret_basic:
Authorization: Basic base64(client_id:client_secret), and nothing about the client in the form body. -
client_secret_post:
client_idandclient_secretas form fields.
Basic is the one the RFC says servers must support. Many OAuth libraries use it by default.
What the SDK does
The SDK's server side does two things that do not fit together:
- Its metadata route announces both methods. In
src/mcp/server/auth/routes.pythe default istoken_endpoint_auth_methods_supported=["client_secret_post", "client_secret_basic"]. - Its
ClientAuthenticator(src/mcp/server/auth/middleware/client_auth.py) readsclient_idfrom the form body first, and raisesMissing client_idif it is not there, before it ever looks at the Basic header.
So a client that reads the metadata, picks client_secret_basic, and follows the RFC is rejected. The Basic header is only checked when the client also repeats its client_id in the body, which the RFC does not ask it to do.
Why doesn't everyone notice? Because the SDK's own client sends client_id in the body even when it uses Basic (issue #3138). SDK client talking to SDK server works. A client built on anything else may not.
This is already reported: issue #3545 (open), and an older issue #1315 from August 2025 (open). A fix, PR #3550, was closed without being merged. As of today the newest release on PyPI, 2.3.0, still has it.
Does your server have it?
You are affected if all of these are true:
- you use the SDK's built-in authorization server (
mcp.server.auth), - your metadata lists
client_secret_basic(it does by default), - clients register themselves (dynamic client registration) and some of them choose Basic.
Test it in one line. Register a client, then send a token request with Basic and no client_id in the body, using a code you know is fake:
curl -s -X POST https://your-server.example/token \
-u "$CLIENT_ID:$CLIENT_SECRET" \
-d grant_type=authorization_code -d code=fake \
-d redirect_uri=https://your-client.example/callback -d code_verifier=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-
invalid_grant("authorization code does not exist"): client authentication passed. You are fine. -
invalid_client("Missing client_id"): you have the bug.
In your access log the pattern is a successful GET /authorize followed by a 401 on POST /token from the same client, and the user never gets connected.
The fix we run
Until the SDK changes, a small ASGI middleware in front of the app does the job. For POST /token and POST /revoke with a Basic header and no client_id in the form, it copies the client_id from the header into the form. The SDK then checks the secret exactly as before, so nothing gets weaker.
import base64
import urllib.parse
from starlette.responses import JSONResponse
class BasicClientIdFix:
"""RFC 6749 2.3.1: with client_secret_basic the client_id may be only in the
Authorization header. Copy it into the form so the SDK's authenticator finds it.
The SDK still verifies the secret in full."""
PATHS = ("/token", "/revoke")
MAX_BODY = 65536
def __init__(self, app):
self.app = app
async def __call__(self, scope, receive, send):
if scope.get("type") != "http" or scope.get("method") != "POST" or scope.get("path") not in self.PATHS:
return await self.app(scope, receive, send)
headers = {k.lower(): v for k, v in scope.get("headers", [])}
auth = headers.get(b"authorization", b"").decode("latin-1")
ctype = headers.get(b"content-type", b"").decode("latin-1").lower()
if not auth.lower().startswith("basic ") or "application/x-www-form-urlencoded" not in ctype:
return await self.app(scope, receive, send)
body, more = b"", True
while more:
msg = await receive()
if msg.get("type") != "http.request":
return
body += msg.get("body", b"")
more = msg.get("more_body", False)
if len(body) > self.MAX_BODY:
return await JSONResponse({"error": "invalid_request"}, status_code=413)(scope, receive, send)
form = urllib.parse.parse_qs(body.decode("utf-8", "replace"), keep_blank_values=True)
if not form.get("client_id"):
try:
user = base64.b64decode(auth[6:].strip(), validate=True).decode("utf-8").split(":", 1)[0]
client_id = urllib.parse.unquote(user)
except Exception:
client_id = ""
if client_id:
extra = urllib.parse.urlencode({"client_id": client_id}).encode()
body = body + b"&" + extra if body else extra
new_headers = [(k, v) for k, v in scope.get("headers", []) if k.lower() != b"content-length"]
new_headers.append((b"content-length", str(len(body)).encode()))
scope = dict(scope, headers=new_headers)
sent = False
async def replay():
nonlocal sent
if not sent:
sent = True
return {"type": "http.request", "body": body, "more_body": False}
return await receive()
await self.app(scope, replay, send)
app = BasicClientIdFix(mcp.streamable_http_app())
What we tested after the change:
| Request | Before | After |
|---|---|---|
| Basic, no client_id in body, fake code | 401 Missing client_id | 400 invalid_grant (auth passed) |
| Basic with a wrong secret | 401 | 401 Invalid client_secret |
| Form only, client_secret_post | unchanged | unchanged |
| MCP call without a token | 401 | 401 |
And the real test: the client that had failed for four days connected on the next try, and its health check went green.
What our registrations looked like
Of the eleven clients that registered with our server through dynamic registration, five chose client_secret_post, five chose none (public clients with PKCE), and one chose client_secret_basic. Only one in eleven, so the bug can sit there for a long time. But that one client was a directory that checks servers every hour, and it had marked us as failing every hour for days.
If you run the SDK's auth server, run the curl test above. It takes a minute.
We found this while running NestDaddy Memory, a hosted MCP memory server built on the MCP Python SDK. We will update this post when the SDK ships a fix.
Originally published on the NestDaddy blog. NestDaddy gives your AI today's internet: live web search and real-time breaking news from its own index. nestdaddy.com/mcp
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.