@makersclub/mcp-auth-core
v0.6.0
Published
Core de autenticación para clientes MCP: OAuth2 (PKCE/Client Credentials), API Key, Bearer, Basic, HMAC. Transportes HTTP/SSE/WebSocket.
Maintainers
Readme
@makersclub/mcp-auth-core
Core de autenticación para clientes MCP (Model Context Protocol):
- OAuth2 (Authorization Code + PKCE, Client Credentials)
- API Key, Bearer, Basic, HMAC
- Adaptadores HTTP / SSE / WebSocket
- TypeScript, ESM/CJS, isomórfico (browser/Node>=18)
Instalación
npm install @makersclub/mcp-auth-core
# ó
pnpm add @makersclub/mcp-auth-core
# ó
yarn add @makersclub/mcp-auth-coreUso Básico
import { createAuthProvider, AuthScheme } from '@makersclub/mcp-auth-core';
import type { McpAuthConfig } from '@makersclub/mcp-auth-core';
// OAuth2 PKCE (recomendado para SPAs)
const oauth = await createAuthProvider({
serverId: 'my-server',
transport: 'http',
auth: {
scheme: AuthScheme.OAUTH2_PKCE,
discovery: { issuer: 'https://login.microsoftonline.com/tenant-id/v2.0' },
client: {
clientId: 'your-client-id',
redirectUri: 'http://localhost:3000/callback',
scopes: ['openid', 'profile', 'email']
},
storage: { type: 'memory' }
}
});
// API Key
const apiKey = await createAuthProvider({
serverId: 'my-server',
transport: 'http',
auth: { scheme: AuthScheme.API_KEY, in: 'header', name: 'X-API-Key', value: 'your-api-key' }
});
// Bearer Token
const bearer = await createAuthProvider({
serverId: 'my-server',
transport: 'http',
auth: { scheme: AuthScheme.BEARER, token: 'your-bearer-token' }
});
// Basic Auth
const basic = await createAuthProvider({
serverId: 'my-server',
transport: 'http',
auth: { scheme: AuthScheme.BASIC, username: 'user', password: 'password' }
});Adaptadores de Transporte
HTTP (fetch)
import { withAuthFetch } from '@makersclub/mcp-auth-core';
const client = withAuthFetch(oauth);
const response = await client.fetch('https://api.example.com/data');Server-Sent Events (SSE)
import { withAuthSSE } from '@makersclub/mcp-auth-core';
const sse = withAuthSSE(oauth);
const stream = await sse.open('https://api.example.com/events');WebSocket
import { withAuthWS } from '@makersclub/mcp-auth-core';
const ws = withAuthWS(oauth);
const socket = await ws.connect('wss://api.example.com/ws');Demo
Consulta el directorio demo/react para ver un ejemplo completo con React + Vite incluyendo:
- Autenticación OAuth2 PKCE con Microsoft
- Modo popup y redirect configurable
- Manejo de tokens y renovación automática
cd demo/react
npm install
npm run dev