Skip to content

Conversation

@bvolpato-dd
Copy link

@bvolpato-dd bvolpato-dd commented Jan 21, 2026

Summary

This PR adds OAuth 2.0 authentication support for HTTP-based MCP servers that require authorization.

  • Implements OAuth 2.0 Authorization Code flow with PKCE (RFC 7636)
  • Adds dynamic client registration support
  • Persists tokens to ~/.config/mcp/oauth_tokens.json for reuse across sessions
  • Includes automatic token refresh using refresh tokens
  • Provides local callback server for OAuth redirects (tries port 80, falls back to 8080/3000)
  • Opens browser automatically for user authorization

Usage

Add "oauth": true to any HTTP server configuration:

{
  "mcpServers": {
    "my-server": {
      "url": "https://example.com/mcp",
      "oauth": true
    }
  }
}

On first connection, the browser opens for authorization. Tokens are cached for subsequent connections.

New Files

  • src/auth/token-storage.ts - Persistent OAuth token storage
  • src/auth/callback-server.ts - Local HTTP server for OAuth callbacks
  • src/auth/oauth-provider.ts - OAuthClientProvider implementation for the MCP SDK
  • src/auth/index.ts - Re-exports

Modified Files

  • src/config.ts - Added OAuthConfig type and oauth field to HttpServerConfig
  • src/client.ts - Added OAuth support for HTTP transports with proper flow handling
  • src/index.ts - Updated help text with OAuth documentation

Test plan

  • Lint passes (bun run lint)
  • TypeScript compiles (bun run typecheck)
  • Unit tests pass (bun test)
  • Manual testing with OAuth-protected MCP server
  • Verified token caching works for subsequent connections
  • Verified tool calls work after OAuth authentication

@philschmid
Copy link
Owner

Thank you for the PR. Do you have an MCP server i could use to test? Especially how this work with an LLM using the cli

Add support for OAuth 2.0 authentication with PKCE for HTTP-based MCP
servers that require authorization.

Features:
- OAuth 2.0 Authorization Code flow with PKCE (RFC 7636)
- Dynamic client registration support
- Persistent token storage in ~/.config/mcp/oauth_tokens.json
- Automatic token refresh using refresh tokens
- Local callback server for OAuth redirects (port 80 by default)
- Browser-based authorization flow

Usage:
Add "oauth": true to HTTP server config:
```json
{
  "mcpServers": {
    "my-server": {
      "url": "https://example.com/mcp",
      "oauth": true
    }
  }
}
```

On first connection, the browser opens for authorization. Tokens are
cached for subsequent connections.

New files:
- src/auth/token-storage.ts - Persistent OAuth token storage
- src/auth/callback-server.ts - Local HTTP server for OAuth callbacks
- src/auth/oauth-provider.ts - OAuthClientProvider implementation
- src/auth/index.ts - Re-exports

Modified files:
- src/config.ts - Added OAuthConfig type and oauth field
- src/client.ts - Added OAuth support for HTTP transports
- src/index.ts - Updated help text with OAuth documentation
@bvolpato-dd bvolpato-dd force-pushed the feat/oauth-authentication branch from dbce5f7 to 55c0670 Compare January 23, 2026 14:42
@bvolpato-dd
Copy link
Author

Thank you for the PR. Do you have an MCP server i could use to test? Especially how this work with an LLM using the cli

https://github.com/bvolpato/mcp-server-oauth-toy

Deployed to https://mcp-server-oauth-toy.brunovolpato.com/mcp

So just add

{
  "mcpServers": {
    "oauth-toy": {
      "url": "https://mcp-server-oauth-toy.brunovolpato.com/mcp",
      "oauth": true
    }
  }
}

And give that a try

mcp-cli oauth-toy/whoami '{}'

- Make redirectUrl getter use actual port from callback server
- Update clientMetadata to dynamically resolve redirect_uris
- Fixes OAuth flow when port 80 is unavailable (common on macOS/Linux)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants