{"slug": "the-mcp-python-sdk-advertises-client-secret-basic-then-rejects-it", "title": "The MCP Python SDK advertises client_secret_basic, then rejects it", "summary": "The MCP Python SDK's built-in OAuth authorization server advertises client_secret_basic in its metadata but rejects clients that use it, returning a 401 \"invalid_client: Missing client_id\" error, and the bug remains unfixed in version 2.3.0 on PyPI. The SDK's ClientAuthenticator in src/mcp/server/auth/middleware/client_auth.py reads client_id from the form body before checking the Authorization header, so clients following RFC 6749 section 2.3.1 are turned away; the SDK's own client masks the problem by always sending client_id in the body, per issue #3138. The defect is tracked in open issues #3545 and #1315 (from August 2025), and a fix, PR #3550, was closed without being merged.", "body_md": "# The MCP Python SDK advertises client_secret_basic, then rejects it\n\nOur MCP server's OAuth token endpoint turned away every client that sent its credentials the standard way. The cause is in the MCP Python SDK itself (still in 2.3.0), how to tell if your server has it, and a small fix you can add today.\n\nFor 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.\n\nThe 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.\n\n```\nPress Run to call the API.\n```\n\n[Get your free API key](https://nestdaddy.com/mcp)\n\n## What the logs showed\n\nThe whole sign-in flow, in order:\n\n```\nGET  /.well-known/oauth-authorization-server/memory   200\nGET  /authorize?response_type=code&client_id=...        302\nPOST /token                                             401\n```\n\nThe token endpoint answered:\n\n```\n{\"error\": \"invalid_client\", \"error_description\": \"Missing client_id\"}\n```\n\nBut the client had sent its client_id. It was in the `Authorization` header.\n\n## The standard says that is allowed\n\nOAuth 2.0 ([RFC 6749, section 2.3.1](https://datatracker.ietf.org/doc/html/rfc6749#section-2.3.1)) gives a confidential client two ways to send its credentials to the token endpoint:\n\n- **client_secret_basic** :`Authorization: Basic base64(client_id:client_secret)` , and nothing about the client in the form body.\n- **client_secret_post** :`client_id` and`client_secret` as form fields.\n\nBasic is the one the RFC says servers must support. Many OAuth libraries use it by default.\n\n## What the SDK does\n\nThe SDK's server side does two things that do not fit together:\n\n1. Its metadata route announces both methods. In `src/mcp/server/auth/routes.py` the default is`token_endpoint_auth_methods_supported=[\"client_secret_post\", \"client_secret_basic\"]` .\n2. Its `ClientAuthenticator` (`src/mcp/server/auth/middleware/client_auth.py` ) reads`client_id` from the**form body** first, and raises`Missing client_id` if it is not there, before it ever looks at the Basic header.\n\nSo 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.\n\nWhy doesn't everyone notice? Because the SDK's own client sends `client_id` in the body even when it uses Basic ([issue #3138](https://github.com/modelcontextprotocol/python-sdk/issues/3138)). SDK client talking to SDK server works. A client built on anything else may not.\n\nThis is already reported: [issue #3545](https://github.com/modelcontextprotocol/python-sdk/issues/3545) (open), and an older [issue #1315](https://github.com/modelcontextprotocol/python-sdk/issues/1315) from August 2025 (open). A fix, [PR #3550](https://github.com/modelcontextprotocol/python-sdk/pull/3550), was closed without being merged. As of today the newest release on PyPI, 2.3.0, still has it.\n\n## Does your server have it?\n\nYou are affected if all of these are true:\n\n- you use the SDK's built-in authorization server (`mcp.server.auth` ),\n- your metadata lists `client_secret_basic` (it does by default),\n- clients register themselves (dynamic client registration) and some of them choose Basic.\n\nTest 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:\n\n```\ncurl -s -X POST https://your-server.example/token \\\n  -u \"$CLIENT_ID:$CLIENT_SECRET\" \\\n  -d grant_type=authorization_code -d code=fake \\\n  -d redirect_uri=https://your-client.example/callback -d code_verifier=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n```\n\n- `invalid_grant` (\"authorization code does not exist\"): client authentication passed. You are fine.\n- `invalid_client` (\"Missing client_id\"): you have the bug.\n\nIn 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.\n\n## The fix we run\n\nUntil 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.\n\n``` python\nimport base64\nimport urllib.parse\n\nfrom starlette.responses import JSONResponse\n\nclass BasicClientIdFix:\n    \"\"\"RFC 6749 2.3.1: with client_secret_basic the client_id may be only in the\n    Authorization header. Copy it into the form so the SDK's authenticator finds it.\n    The SDK still verifies the secret in full.\"\"\"\n    PATHS = (\"/token\", \"/revoke\")\n    MAX_BODY = 65536\n\n    def __init__(self, app):\n        self.app = app\n\n    async def __call__(self, scope, receive, send):\n        if scope.get(\"type\") != \"http\" or scope.get(\"method\") != \"POST\" or scope.get(\"path\") not in self.PATHS:\n            return await self.app(scope, receive, send)\n        headers = {k.lower(): v for k, v in scope.get(\"headers\", [])}\n        auth = headers.get(b\"authorization\", b\"\").decode(\"latin-1\")\n        ctype = headers.get(b\"content-type\", b\"\").decode(\"latin-1\").lower()\n        if not auth.lower().startswith(\"basic \") or \"application/x-www-form-urlencoded\" not in ctype:\n            return await self.app(scope, receive, send)\n\n        body, more = b\"\", True\n        while more:\n            msg = await receive()\n            if msg.get(\"type\") != \"http.request\":\n                return\n            body += msg.get(\"body\", b\"\")\n            more = msg.get(\"more_body\", False)\n            if len(body) > self.MAX_BODY:\n                return await JSONResponse({\"error\": \"invalid_request\"}, status_code=413)(scope, receive, send)\n\n        form = urllib.parse.parse_qs(body.decode(\"utf-8\", \"replace\"), keep_blank_values=True)\n        if not form.get(\"client_id\"):\n            try:\n                user = base64.b64decode(auth[6:].strip(), validate=True).decode(\"utf-8\").split(\":\", 1)[0]\n                client_id = urllib.parse.unquote(user)\n            except Exception:\n                client_id = \"\"\n            if client_id:\n                extra = urllib.parse.urlencode({\"client_id\": client_id}).encode()\n                body = body + b\"&\" + extra if body else extra\n\n        new_headers = [(k, v) for k, v in scope.get(\"headers\", []) if k.lower() != b\"content-length\"]\n        new_headers.append((b\"content-length\", str(len(body)).encode()))\n        scope = dict(scope, headers=new_headers)\n        sent = False\n\n        async def replay():\n            nonlocal sent\n            if not sent:\n                sent = True\n                return {\"type\": \"http.request\", \"body\": body, \"more_body\": False}\n            return await receive()\n\n        await self.app(scope, replay, send)\n\napp = BasicClientIdFix(mcp.streamable_http_app())\n```\n\nWhat we tested after the change:\n\n| Request | Before | After | \n|---|---|---|\n| Basic, no client_id in body, fake code | 401 Missing client_id | 400 invalid_grant (auth passed) | \n| Basic with a wrong secret | 401 | 401 Invalid client_secret | \n| Form only, client_secret_post | unchanged | unchanged | \n| MCP call without a token | 401 | 401 | \n\nAnd the real test: the client that had failed for four days connected on the next try, and its health check went green.\n\n## What our registrations looked like\n\nOf 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.\n\nIf you run the SDK's auth server, run the curl test above. It takes a minute.\n\n*We found this while running [NestDaddy Memory](https://nestdaddy.com/memory), a hosted MCP memory server built on the MCP Python SDK. We will update this post when the SDK ships a fix.*", "url": "https://wpnews.pro/news/the-mcp-python-sdk-advertises-client-secret-basic-then-rejects-it", "canonical_source": "https://nestdaddy.com/blog/mcp-python-sdk-rejects-client-secret-basic", "published_at": "2026-11-24 23:45:34.459699+00:00", "updated_at": "2026-11-24 23:45:38.123532+00:00", "lang": "en", "topics": ["ai-agents", "agent-protocols", "developer-tools", "ai-tools"], "entities": ["MCP Python SDK", "Glama", "RFC 6749", "PyPI", "modelcontextprotocol/python-sdk"], "also_reported_by": [], "takeaway": "MCP Python SDK 2.3.0's ClientAuthenticator reads client_id from the form body before the Basic Authorization header, so servers advertising client_secret_basic reject RFC 6749-compliant clients with \"Missing client_id\"; test with a Basic-auth token request and a fake code, where invalid_grant means ", "recommendation": {"advice": "If your MCP server uses the SDK's built-in OAuth, test the token endpoint with a Basic-auth request and no body client_id; invalid_client confirms the bug, so patch client_auth.py to parse the Basic header before rejecting.", "playbook": "agent-security", "playbook_url": "https://wpnews.pro/playbooks/agent-security"}, "alternates": {"html": "https://wpnews.pro/news/the-mcp-python-sdk-advertises-client-secret-basic-then-rejects-it", "markdown": "https://wpnews.pro/news/the-mcp-python-sdk-advertises-client-secret-basic-then-rejects-it.md", "text": "https://wpnews.pro/news/the-mcp-python-sdk-advertises-client-secret-basic-then-rejects-it.txt", "jsonld": "https://wpnews.pro/news/the-mcp-python-sdk-advertises-client-secret-basic-then-rejects-it.jsonld"}}