MCP server connection fails with HTTP 401 even when Bearer token is provided
Summary
Connecting an MCP server that requires OAuth/Bearer auth fails with HTTP 401 even after the user manually adds the Authorization header with a Bearer token in the Connect dialog.
Problem
What happened: When connecting the Notion MCP server (https://notion.run.tools), the connect dialog shows:
MCP unauthorized for 'https://notion.run.tools' (HTTP 401;
resource metadata: https://notion.run.tools/.well-known/oauth-protected-resource)
The user had added a Authorization: Bearer <value> custom header in the dialog, but the connection still fails with 401. The server remains in "Disconnected" state.
What was expected: The Bearer token provided in custom headers should be sent with the connection request and accepted by the server. If the token is invalid or blank, the error should clearly say so rather than silently failing with a generic 401.
Steps to reproduce:
- Go to Connections → MCP Servers.
- Select Notion MCP server → Connect.
- Add
Authorization: Bearer <token>as a custom header. - Click Connect — observe HTTP 401 error.
Platform: macOS desktop · Beta build v0.58.0.
Solution (optional)
- Verify the custom headers from the Connect dialog are actually included in the MCP handshake/connection request.
- If the server requires OAuth flow (not just a static token), surface that clearly — with a "Sign in with Notion" flow rather than a raw header field.
- Check if the
/.well-known/oauth-protected-resourcemetadata is being fetched and handled correctly for OAuth-protected MCP servers.
Acceptance criteria
- [ ] Bearer token sent — Custom headers provided in the Connect dialog are included in all MCP connection requests.
- [ ] OAuth-protected servers — MCP servers requiring OAuth are handled with an OAuth flow, not a raw Bearer header field.
- [ ] Clear auth error — If auth fails, the error distinguishes between wrong token, missing token, and OAuth-required.
- [ ] Regression safety — MCP servers that need no auth unaffected.
- [ ] Diff coverage ≥ 80% — Fix PR meets the changed-lines coverage gate.
Related
MCP end-to-end task: #4272 npx spawn bug: #4279
Status / scope note
Traced this end-to-end on current main. Most of the acceptance criteria here already landed in #3495/#3648 (feat(mcp): MCP servers usable by the agent end-to-end — auth, OAuth) and #4120 (MCP catalog … data-driven auth):
- Bearer token sent — custom headers from the Connect dialog are forwarded onto the MCP
initializerequest. A single header maps toMcpAuthConfig::Header, multiple toMcpAuthConfig::Headers, and both are written onto the outbound POST in the HTTP transport'sapply_auth. Not dropped. - OAuth-protected servers — there's already a full browser-OAuth path: a
detect_authprobe reads the.well-known/oauth-protected-resource→ authorization-server metadata, and if anauthorization_endpointis present the dialog shows a "Sign in" button (PKCE + dynamic client registration + a local/oauth/mcp/callbackroute), instead of a raw token field. So for a genuinely OAuth-only server like Notion, the intended path is Sign in, not pasting aBearerheader — a manually pasted static token will still be rejected by an OAuth-only server.
So the repro in this issue (paste a Bearer header into an OAuth-only server → 401) is partly expected: the static header is sent, the server just refuses it because it wants OAuth.
Genuine residual gaps I plan to address (scoped PR):
- Clear auth error (criterion 3) — a rejected auth currently surfaces the generic
MCP unauthorized … HTTP 401 … oauth-protected-resource. It should distinguish token rejected (wrong/expired) vs this server needs Sign-in, not a pasted token vs missing token. - Test coverage — only the
BearerTokenvariant has a wire-level forwarding test; the custom-headerHeader/Headersarms have none. Adding regression coverage there.
@YellowSnnowmann — looks like this is assigned to you; flagging so we don't double up. I'll keep my change narrow to (1)+(2) above unless you're already on the error-clarity piece.