dzhng/mcp-client-auth là một trong những repo mã nguồn mở mà TopGit theo dõi, hiện có 39 sao, viết chủ yếu bằng 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.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
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.
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/dzhng/mcp-client-auth là nguồn chính thức.
dzhng/mcp-client-auth có phải mã nguồn mở không?
Có — dzhng/mcp-client-auth phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/dzhng/mcp-client-auth.
dzhng/mcp-client-auth có tag gì không?
Bản đồng bộ chưa ghi nhận topic GitHub nào cho dzhng/mcp-client-auth. GitHub topics hiển thị ở thanh bên phải trang repo — đó là nơi đáng kiểm tra nhất.
dzhng/mcp-client-auth có website riêng không?
TopGit chưa ghi nhận URL trang chủ cho dzhng/mcp-client-auth. Phần README ở tab phía trên thường có link demo, hoặc xem mô tả GitHub của repo.
dzhng/mcp-client-auth còn đang phát triển không?
Commit gần nhất trên dzhng/mcp-client-auth là 1.4 năm trước (theo timestamp GitHub). Repo có 3 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
dzhng/mcp-client-auth dùng license gì?
dzhng/mcp-client-auth phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
Đọc đầy đủ README ở tab phía trên.
Chưa chắc mcp-client-auth có hợp với bạn?
Để ChatGPT, Claude hoặc Perplexity tìm hiểu giúp — bấm bên dưới và xem AI nói gì về mcp-client-auth.