
一个为模型上下文协议(MCP)客户端提供OAuth2认证工具的TypeScript库。
概览
一个为模型上下文协议(MCP)客户端提供OAuth2认证工具的TypeScript库。该库简化了为MCP客户端实现添加OAuth认证的过程。 MCP OAuth 则更复杂,因为典型的 OAuth 实现不支持动态客户端注册和元数据发现步骤。该库简化为两个函数调用。 如果你喜欢这个项目,请考虑为它加主角,并在X/Twitter上关注我。本项目由Aomni赞助。
概述(AI 翻译)
一个为模型上下文协议(MCP)客户端提供OAuth2认证工具的TypeScript库。该库简化了为MCP客户端实现添加OAuth认证的过程。
MCP OAuth 则更复杂,因为典型的 OAuth 实现不支持动态客户端注册和元数据发现步骤。该库简化为两个函数调用。
如果你喜欢这个项目,请考虑为它加主角,并在X/Twitter上关注我。本项目由Aomni赞助。
特色
主要能力:
- 🔐 完整的OAuth2流程:实现标准的OAuth2授权码流程,使用PKCE
- 🚀 MCP 客户端集成:MCP 客户端的直接开放式支持
- 📦 零配置:自动服务器元数据发现和动态客户端注册
- 🌐 智能传输选择:自动从StreamableHTTP退回SSE传输以实现最大兼容性
- 🔄 令牌管理:自动令牌刷新和安全存储(通过可插拔存储接口)
- 🎯 TypeScript First:完整的类型安全和IntelliSense支持。
安装
快速启动
使用高级MCP客户端
本页由 AI 翻译,技术细节请以原文为准。
README
以下为原文 README 节选,完整内容请查看原文。
MCP Client Auth
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
- 🔄 Token Management: Automatic token refresh and secure storage (via pluggable storage interface)
- 🎯 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. TheauthorizationRequestcontains 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:
安装
This server does not publish a one-line install command.
Open the repository installation guide 新手?先看 mcpskill.org 教程 →