Skip to content

mcp: add bearer and OAuth2 authentication support to $mcp client#1770

Merged
nmaguiar merged 3 commits intot8from
codex/add-oauth2-authentication-to-mcp-client-4bfn54
Mar 12, 2026
Merged

mcp: add bearer and OAuth2 authentication support to $mcp client#1770
nmaguiar merged 3 commits intot8from
codex/add-oauth2-authentication-to-mcp-client-4bfn54

Conversation

@nmaguiar
Copy link
Collaborator

@nmaguiar nmaguiar commented Mar 12, 2026

Motivation

  • Enable authenticated connections for remote/http MCP servers by supporting static bearer tokens and OAuth2 flows so MCP clients can call protected endpoints.
  • Provide a flexible auth configuration that supports client credentials, authorization code flows, token refresh, and optional browser-based authorization.

Description

  • Added aOptions.auth parsing and defaults to $mcp and extended the odoc header with usage examples for bearer and OAuth2 configurations.
  • Implemented internal auth state and helpers: _auth, _urlEnc, _openAuthBrowser, _getAuthorizationCode, and _getAuthHeaders to obtain and refresh access tokens (including refresh token handling and authorization_code flow support with optional browser launch and paste prompt).
  • Introduced _execWithAuth wrapper that injects Authorization headers into REST calls for remote/http types and delegates to the underlying JSON-RPC client, and replaced direct _jsonrpc.exec calls with _execWithAuth in initialize, notifications, listTools, callTool, listPrompts, getPrompt, listAgents, getAgent, sendToAgent, exec, and the initialized notification path.
  • Merged auth headers into execOptions.restOptions.requestHeaders when present and added handling of auth.tokenType, refreshWindowMs, extraParams, and other OAuth2 options.

Testing

  • No automated tests were added or executed as part of this change; please run the repository test suite and integration tests against an MCP server requiring authentication to validate OAuth2 and bearer flows.

- Introduce  option to  and  for Server‑Sent Events.\n- Update connection type logic to handle  and /.\n- Default  map to  and add guard for empty auth.\n- Enhance error handling for MCP initialization.\n- Minor refactor of auth header construction.
Add a new test case for the MCP client remote SSE functionality, ensuring that the client can initialize, list tools, and call the ping tool over SSE. This test verifies that the SSE MCP server correctly handles notifications and tool calls.
@nmaguiar nmaguiar marked this pull request as ready for review March 12, 2026 05:08
@nmaguiar nmaguiar self-assigned this Mar 12, 2026
@nmaguiar nmaguiar merged commit b8300b3 into t8 Mar 12, 2026
1 check passed
@github-project-automation github-project-automation bot moved this from Backlog to Done in Continuous Enhancement Mar 12, 2026
@nmaguiar nmaguiar deleted the codex/add-oauth2-authentication-to-mcp-client-4bfn54 branch March 12, 2026 05:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Development

Successfully merging this pull request may close these issues.

1 participant