openhuman icon indicating copy to clipboard operation
openhuman copied to clipboard

MCP server connection fails with HTTP 401 even when Bearer token is provided

Open Al629176 opened this issue 5 months ago • 1 comments

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:

  1. Go to Connections → MCP Servers.
  2. Select Notion MCP server → Connect.
  3. Add Authorization: Bearer <token> as a custom header.
  4. 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-resource metadata 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

Al629176 avatar Jun 29 '26 11:06 Al629176

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 initialize request. A single header maps to McpAuthConfig::Header, multiple to McpAuthConfig::Headers, and both are written onto the outbound POST in the HTTP transport's apply_auth. Not dropped.
  • OAuth-protected servers — there's already a full browser-OAuth path: a detect_auth probe reads the .well-known/oauth-protected-resource → authorization-server metadata, and if an authorization_endpoint is present the dialog shows a "Sign in" button (PKCE + dynamic client registration + a local /oauth/mcp/callback route), instead of a raw token field. So for a genuinely OAuth-only server like Notion, the intended path is Sign in, not pasting a Bearer header — 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):

  1. 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.
  2. Test coverage — only the BearerToken variant has a wire-level forwarding test; the custom-header Header/Headers arms 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.

oxoxDev avatar Jun 29 '26 11:06 oxoxDev