As an open-source project, dzhng/mcp-client-auth has picked up 39 stars on GitHub (TypeScript). A TypeScript library providing OAuth2 authentication utilities for Model Context Protocol (MCP) clients. This library simplifies the process of adding OAuth authentication to MCP client implementations.
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
A TypeScript library providing OAuth2 authentication utilities for Model Context Protocol (MCP) clients. This library simplifies the process of adding OAuth authentication to MCP client implementations.
MCP OAuth is extra tricky because the dynamic client registration and metadata discovery steps are not supported by typical oauth implementations. This library simplifies everything to 2 function calls.
If you like this project, please consider starring it and giving me a follow on X/Twitter. This project is sponsored by Aomni.
Features
Key capabilities:
🔐 Complete OAuth2 Flow: Implements the standard OAuth2 authorization code flow with PKCE
🚀 MCP Client Integration: Drop-in OAuth support for MCP clients
📦 Zero Configuration: Automatic server metadata discovery and dynamic client registration
🌐 Smart Transport Selection: Automatic fallback from StreamableHTTP to SSE transport for maximum compatibility
🎯 TypeScript First: Full type safety and IntelliSense support
Installation
npm install mcp-client-auth
Quick Start
Using the High-Level MCP Client
Init the client
import { McpClient } from 'mcp-client-auth';
const client = new McpClient({
url: 'https://mcp.example.com',
oauthRedirectUri: 'localhost:3000/mcp/oauth/callback',
// store: -- add your own database store here --
});
There are only 2 methods that are needed to connect to a MCP server (and handle OAuth if needed).
Checking for auth status
The isAuthRequired() method returns an AuthStatus object that indicates the authentication state. This status can be one of three types:
{ isRequired: true, isAuthenticated: false, authorizationRequest: AuthorizationRequest } - Authentication is required and not yet completed. The authorizationRequest contains the URL and state needed to start the OAuth flow.
{ isRequired: false, isAuthenticated: true } - No authentication is needed for this server.
{ isRequired: true, isAuthenticated: true } - Authentication is required and has already been completed successfully.
This status object helps you determine whether to redirect the user to the OAuth authorization page or proceed with using the client directly.
// Check if authentication is required
const authStatus = await client.isAuthRequired();
if (authStatus.isRequired && !authStatus.isAuthenticated) {
console.log('Please visit:', authStatus.authorizationRequest.url);
// ... REDIRECT USER ...
}
Note you should save the AuthorizationRequest object for the next step.
Handling the OAuth callback
The handleAuthByCode method is used to complete the OAuth flow by exchanging the authorization code for access tokens. It takes two parameters:
code: The authorization code received from the OAuth server after user authorization
authRequest: The original authorization request object containing the state and code verifier needed for PKCE
This method should be called in your server callback route, as defined by the oauthRedirectUri.
function callback() {
// After user authorizes, exchange code for token
// Realistically - this would be in a different callback route
const token = await client.handleAuthByCode(
code,
authStatus.authorizationRequest,
);
}
If a store is provided (ideally connected to a database), the token returned will be automatically saved via store, which means next time isAuthRequired is called it will automatically return isAuthenticated of true, and no redirect will be needed.
Using MCP tools
// Use the client - auth is handled automatically
const tools = await client.listTools();
const result = await client.callTool('search', { query: 'example' });
Using OAuth Directly
This is normally not needed - only do this if you have some custom auth logic you want to implement.
import { McpOAuth } from 'mcp-client-auth';
// Create OAuth instance
const oauth = new McpOAuth({
serverUrl: 'https://mcp.example.com',
clientId: 'your-client-id', // Optional
redirectUri: 'http://localhost:3334/callback', // Optional
});
// Initialize (discovers metadata, registers client if needed)
await oauth.init();
// Generate authorization URL with PKCE
const authRequest = await oauth.createAuthorizationRequest();
console.log('Visit:', authRequest.url);
// Exchange authorization code for tokens
const token = await oauth.exchangeCodeForToken(
code,
state,
authRequest.codeVerifier,
);
// Get authenticated HTTP client
const ky = await oauth.ky();
const data = await ky.get('api/data').json();
API Reference
McpClient
High-level MCP client with integrated OAuth support.
class McpClient {
constructor(options: McpClientOptions);
// Check if auth is required and get auth status
isAuthRequired(): Promise<AuthStatus>;
// Get OAuth instance for manual auth flow
getOAuth(): Promise<McpOAuth | undefined>;
// MCP operations
listTools(): Promise<McpTool[]>;
callTool(name: string, args: any): Promise<any>;
disconnect(): Promise<void>;
}
If you are curious about some of the business logic.
OAuth Authentication Process
The library handles the complete OAuth flow automatically:
Server Discovery 🔍 → Discovers OAuth endpoints via /.well-known/oauth-authorization-server
Dynamic Registration 📝 → Registers as a public client if no client ID is provided
Authorization 🔐 → Generates authorization URL with PKCE code challenge
Token Exchange 🔄 → Exchanges authorization code for access/refresh tokens
Token Refresh ♻️ → Automatically refreshes expired tokens when available
Transport Selection
The MCP client automatically selects the best transport method for connecting to MCP servers:
StreamableHTTP Transport 🚀 → Primary choice for optimal performance with bidirectional streaming
SSE Transport Fallback 📡 → Automatic fallback when StreamableHTTP is not supported
This ensures maximum compatibility across different server implementations and network configurations without requiring manual transport configuration.
License
MIT License - feel free to use and modify as needed.
Does dzhng/mcp-client-auth have a project website?
No homepage URL was recorded for dzhng/mcp-client-auth in TopGit's last sync. The README tab above frequently contains screenshots and demo links, or check the repository description on GitHub.
Does dzhng/mcp-client-auth have any tags?
TopGit's last sync did not record any GitHub topics for dzhng/mcp-client-auth. GitHub topics appear in the right sidebar of a repository page; that's the authoritative place to check.
How active is development on dzhng/mcp-client-auth?
The most recent commit recorded on dzhng/mcp-client-auth was 1.4 years ago, based on the GitHub push timestamp. The repository has 3 forks — one of the better signals of community interest.
Is dzhng/mcp-client-auth open source?
Yes — dzhng/mcp-client-auth ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/dzhng/mcp-client-auth.
What license does dzhng/mcp-client-auth use?
dzhng/mcp-client-auth is released under the MIT license. Always verify the LICENSE file directly on GitHub for the authoritative terms — license strings can be edited out of sync with a project's actual stance.
Where do I read more about dzhng/mcp-client-auth?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/dzhng/mcp-client-auth is the definitive source.
Read full README in the tab above.
Want a second opinion on mcp-client-auth?
Ask an AI that can read this page — one click and you get its take on mcp-client-auth.