converse-mcp-server 1.3.0 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +117 -24
- package/README.md +118 -1
- package/docs/AUTHENTICATION.md +408 -0
- package/package.json +4 -1
- package/src/config.js +56 -1
- package/src/prompts/helpPrompt.js +10 -2
- package/src/providers/anthropic.js +5 -5
- package/src/providers/deepseek.js +2 -2
- package/src/providers/google.js +4 -4
- package/src/providers/mistral.js +3 -3
- package/src/providers/openai.js +5 -4
- package/src/providers/openrouter.js +9 -7
- package/src/tools/chat.js +30 -2
- package/src/tools/consensus.js +31 -3
- package/src/transport/auth/authMiddleware.js +444 -0
- package/src/transport/httpTransport.js +11 -3
package/.env.example
CHANGED
|
@@ -1,37 +1,130 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Converse MCP Server Environment Configuration
|
|
2
2
|
# Copy this file to .env and fill in your values
|
|
3
3
|
|
|
4
|
+
# ========================================
|
|
4
5
|
# API Keys - At least one is required
|
|
5
|
-
#
|
|
6
|
-
# IMPORTANT: Choose ONE approach:
|
|
7
|
-
# - Native APIs (Gemini/OpenAI/XAI) for direct access
|
|
8
|
-
# - DIAL for unified enterprise access
|
|
9
|
-
# - OpenRouter for unified cloud access
|
|
10
|
-
# Having multiple unified providers creates ambiguity about which serves each model.
|
|
11
|
-
#
|
|
12
|
-
# Option 1: Use native APIs (recommended for direct access)
|
|
13
|
-
# Get your Gemini API key from: https://makersuite.google.com/app/apikey
|
|
14
|
-
GEMINI_API_KEY=your_gemini_api_key_here
|
|
6
|
+
# ========================================
|
|
15
7
|
|
|
16
|
-
#
|
|
17
|
-
OPENAI_API_KEY=
|
|
8
|
+
# OpenAI - https://platform.openai.com/api-keys
|
|
9
|
+
OPENAI_API_KEY=sk-proj-...
|
|
18
10
|
|
|
19
|
-
#
|
|
20
|
-
|
|
11
|
+
# Google Gemini - https://makersuite.google.com/app/apikey
|
|
12
|
+
GOOGLE_API_KEY=AIzaSy...
|
|
21
13
|
|
|
22
|
-
#
|
|
23
|
-
|
|
14
|
+
# X.AI Grok - https://console.x.ai/
|
|
15
|
+
XAI_API_KEY=xai-...
|
|
24
16
|
|
|
25
|
-
#
|
|
26
|
-
|
|
17
|
+
# Anthropic Claude - https://console.anthropic.com/
|
|
18
|
+
ANTHROPIC_API_KEY=sk-ant-...
|
|
27
19
|
|
|
28
|
-
#
|
|
29
|
-
|
|
20
|
+
# Mistral - https://console.mistral.ai/
|
|
21
|
+
MISTRAL_API_KEY=...
|
|
30
22
|
|
|
31
|
-
#
|
|
32
|
-
|
|
23
|
+
# DeepSeek - https://platform.deepseek.com/
|
|
24
|
+
DEEPSEEK_API_KEY=...
|
|
33
25
|
|
|
34
|
-
# OpenRouter
|
|
26
|
+
# OpenRouter - https://openrouter.ai/keys
|
|
27
|
+
OPENROUTER_API_KEY=sk-or-...
|
|
35
28
|
OPENROUTER_REFERER=https://github.com/FallDownTheSystem/converse
|
|
36
29
|
|
|
30
|
+
# ========================================
|
|
31
|
+
# Server Configuration
|
|
32
|
+
# ========================================
|
|
33
|
+
|
|
34
|
+
# Transport mode: http or stdio (default: http)
|
|
35
|
+
MCP_TRANSPORT=http
|
|
36
|
+
|
|
37
|
+
# Server settings
|
|
38
|
+
PORT=3157
|
|
39
|
+
HOST=localhost
|
|
40
|
+
LOG_LEVEL=info
|
|
41
|
+
NODE_ENV=development
|
|
42
|
+
|
|
43
|
+
# ========================================
|
|
44
|
+
# Authentication Configuration (for remote deployments)
|
|
45
|
+
# ========================================
|
|
46
|
+
|
|
47
|
+
# Authentication strategy: none, bearer, api_key, oauth2 (default: none)
|
|
48
|
+
MCP_AUTH_STRATEGY=none
|
|
49
|
+
MCP_AUTH_REQUIRE=false
|
|
50
|
+
|
|
51
|
+
# JWT Configuration (for bearer token and OAuth2)
|
|
52
|
+
# Generate a secure secret: openssl rand -base64 32
|
|
53
|
+
MCP_JWT_SECRET=
|
|
54
|
+
MCP_JWT_ALGORITHM=HS256
|
|
55
|
+
MCP_JWT_EXPIRES_IN=24h
|
|
56
|
+
MCP_JWT_ISSUER=converse-mcp-server
|
|
57
|
+
|
|
58
|
+
# API Key Authentication
|
|
59
|
+
# Comma-separated list of valid API keys
|
|
60
|
+
MCP_API_KEYS=
|
|
61
|
+
|
|
62
|
+
# OAuth2 Configuration (example for Google)
|
|
63
|
+
MCP_OAUTH_CLIENT_ID=
|
|
64
|
+
MCP_OAUTH_CLIENT_SECRET=
|
|
65
|
+
MCP_OAUTH_AUTH_URL=https://accounts.google.com/o/oauth2/v2/auth
|
|
66
|
+
MCP_OAUTH_TOKEN_URL=https://oauth2.googleapis.com/token
|
|
67
|
+
MCP_OAUTH_CALLBACK_URL=https://your-server.com/oauth/callback
|
|
68
|
+
MCP_OAUTH_SCOPE=openid profile email
|
|
69
|
+
MCP_OAUTH_USERINFO_URL=https://www.googleapis.com/oauth2/v2/userinfo
|
|
70
|
+
|
|
71
|
+
# Session-based Authentication Control
|
|
72
|
+
MCP_SESSION_AUTH_ENABLED=true
|
|
73
|
+
MCP_SESSION_AUTH_REQUIRE_FOR_SESSION=false
|
|
74
|
+
MCP_SESSION_AUTH_REQUIRE_FOR_OPERATIONS=true
|
|
37
75
|
|
|
76
|
+
# ========================================
|
|
77
|
+
# HTTP Transport Configuration
|
|
78
|
+
# ========================================
|
|
79
|
+
|
|
80
|
+
# Request handling
|
|
81
|
+
HTTP_REQUEST_TIMEOUT=300000 # 5 minutes
|
|
82
|
+
HTTP_MAX_REQUEST_SIZE=10mb
|
|
83
|
+
|
|
84
|
+
# Session management
|
|
85
|
+
HTTP_SESSION_TIMEOUT=1800000 # 30 minutes
|
|
86
|
+
HTTP_SESSION_CLEANUP_INTERVAL=300000 # 5 minutes
|
|
87
|
+
HTTP_MAX_CONCURRENT_SESSIONS=100
|
|
88
|
+
|
|
89
|
+
# CORS configuration
|
|
90
|
+
HTTP_ENABLE_CORS=true
|
|
91
|
+
HTTP_CORS_ORIGINS=*
|
|
92
|
+
HTTP_CORS_METHODS=GET,POST,DELETE,OPTIONS
|
|
93
|
+
HTTP_CORS_HEADERS=Content-Type,mcp-session-id,Authorization,X-API-Key
|
|
94
|
+
HTTP_CORS_CREDENTIALS=false
|
|
95
|
+
|
|
96
|
+
# Security settings
|
|
97
|
+
HTTP_DNS_REBINDING_PROTECTION=false
|
|
98
|
+
HTTP_ALLOWED_HOSTS=127.0.0.1,localhost
|
|
99
|
+
HTTP_RATE_LIMIT_ENABLED=false
|
|
100
|
+
HTTP_RATE_LIMIT_WINDOW=900000 # 15 minutes
|
|
101
|
+
HTTP_RATE_LIMIT_MAX_REQUESTS=1000
|
|
102
|
+
|
|
103
|
+
# ========================================
|
|
104
|
+
# Advanced Configuration
|
|
105
|
+
# ========================================
|
|
106
|
+
|
|
107
|
+
# MCP Server settings
|
|
108
|
+
MCP_SERVER_NAME=converse-mcp-server
|
|
109
|
+
MCP_SERVER_VERSION=1.0.0
|
|
110
|
+
MAX_MCP_OUTPUT_TOKENS=25000
|
|
111
|
+
|
|
112
|
+
# Provider-specific settings
|
|
113
|
+
GOOGLE_LOCATION=us-central1
|
|
114
|
+
XAI_BASE_URL=https://api.x.ai/v1
|
|
115
|
+
|
|
116
|
+
# ========================================
|
|
117
|
+
# Production Example Configuration
|
|
118
|
+
# ========================================
|
|
119
|
+
|
|
120
|
+
# For production deployments, uncomment and modify:
|
|
121
|
+
#
|
|
122
|
+
# NODE_ENV=production
|
|
123
|
+
# MCP_AUTH_STRATEGY=oauth2
|
|
124
|
+
# MCP_AUTH_REQUIRE=true
|
|
125
|
+
# MCP_JWT_SECRET=<generate-with-openssl-rand-base64-32>
|
|
126
|
+
# HTTP_RATE_LIMIT_ENABLED=true
|
|
127
|
+
# HTTP_DNS_REBINDING_PROTECTION=true
|
|
128
|
+
# HTTP_CORS_ORIGINS=https://your-app.com
|
|
129
|
+
# HTTP_ENABLE_CORS=true
|
|
130
|
+
# LOG_LEVEL=warn
|
package/README.md
CHANGED
|
@@ -180,6 +180,30 @@ There are several ways to add the Converse MCP Server to Claude:
|
|
|
180
180
|
}
|
|
181
181
|
```
|
|
182
182
|
|
|
183
|
+
#### Option E: Local HTTP Development (Advanced)
|
|
184
|
+
|
|
185
|
+
For local development with HTTP transport:
|
|
186
|
+
|
|
187
|
+
1. **First, start the server manually**:
|
|
188
|
+
```bash
|
|
189
|
+
# In a terminal, navigate to the project directory
|
|
190
|
+
cd converse
|
|
191
|
+
npm run dev # Starts server on http://localhost:3157/mcp
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
2. **Then configure Claude to connect to it**:
|
|
195
|
+
```json
|
|
196
|
+
{
|
|
197
|
+
"mcpServers": {
|
|
198
|
+
"converse-local": {
|
|
199
|
+
"url": "http://localhost:3157/mcp"
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
**Important**: HTTP transport requires the server to be running before Claude can connect to it. Keep the terminal with the server open while using Claude.
|
|
206
|
+
|
|
183
207
|
#### Installation Steps
|
|
184
208
|
|
|
185
209
|
1. **For Claude Code**:
|
|
@@ -197,6 +221,14 @@ There are several ways to add the Converse MCP Server to Claude:
|
|
|
197
221
|
- macOS: `~/Library/Application Support/Claude/claude_desktop_config.json`
|
|
198
222
|
- Linux: `~/.config/Claude/claude_desktop_config.json`
|
|
199
223
|
|
|
224
|
+
**Windows Troubleshooting**: If `npx converse-mcp-server` doesn't work on Windows, try using:
|
|
225
|
+
```json
|
|
226
|
+
{
|
|
227
|
+
"command": "cmd",
|
|
228
|
+
"args": ["/c", "npx", "converse-mcp-server"]
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
200
232
|
For more detailed instructions, see the [official MCP configuration guide](https://docs.anthropic.com/en/docs/claude-code/mcp#configure-mcp-servers).
|
|
201
233
|
|
|
202
234
|
## 🛠️ Available Tools
|
|
@@ -349,7 +381,8 @@ npm start -- --transport=stdio
|
|
|
349
381
|
|
|
350
382
|
**Transport Modes**:
|
|
351
383
|
- **HTTP Transport** (default): `http://localhost:3157/mcp` - Better for development and debugging
|
|
352
|
-
- **
|
|
384
|
+
- **Note**: When using HTTP transport, the server must be started manually (e.g., `npm start` or `npm run dev`) as it runs as a standalone process, unlike stdio which is launched as a subprocess by Claude
|
|
385
|
+
- **Stdio Transport**: Use `--transport=stdio` or set `MCP_TRANSPORT=stdio` for traditional stdio communication (launched automatically by Claude)
|
|
353
386
|
|
|
354
387
|
### Testing with Real APIs
|
|
355
388
|
|
|
@@ -500,6 +533,90 @@ converse/
|
|
|
500
533
|
| `GOOGLE_LOCATION` | Google API region | `us-central1` | `us-central1` |
|
|
501
534
|
| `XAI_BASE_URL` | XAI API endpoint | `https://api.x.ai/v1` | Custom endpoint |
|
|
502
535
|
|
|
536
|
+
### 🔐 Authentication for Remote Deployments
|
|
537
|
+
|
|
538
|
+
The Converse MCP Server supports multiple authentication strategies for secure remote deployments:
|
|
539
|
+
|
|
540
|
+
#### Authentication Strategies
|
|
541
|
+
|
|
542
|
+
| Strategy | Description | Configuration |
|
|
543
|
+
|----------|-------------|---------------|
|
|
544
|
+
| `none` | No authentication (default) | No additional config needed |
|
|
545
|
+
| `bearer` | JWT Bearer token | Requires `MCP_JWT_SECRET` |
|
|
546
|
+
| `api_key` | API key authentication | Requires `MCP_API_KEYS` |
|
|
547
|
+
| `oauth2` | OAuth 2.0 flow | Requires OAuth provider config |
|
|
548
|
+
|
|
549
|
+
#### Environment Variables for Authentication
|
|
550
|
+
|
|
551
|
+
```bash
|
|
552
|
+
# Authentication Strategy
|
|
553
|
+
MCP_AUTH_STRATEGY=bearer # Options: none, bearer, api_key, oauth2
|
|
554
|
+
MCP_AUTH_REQUIRE=true # Require auth for all requests
|
|
555
|
+
|
|
556
|
+
# JWT Configuration (for bearer and oauth2)
|
|
557
|
+
MCP_JWT_SECRET=your-secret-key-at-least-32-chars
|
|
558
|
+
MCP_JWT_ALGORITHM=HS256 # Default: HS256
|
|
559
|
+
MCP_JWT_EXPIRES_IN=24h # Default: 24h
|
|
560
|
+
MCP_JWT_ISSUER=converse-mcp # Default: converse-mcp-server
|
|
561
|
+
|
|
562
|
+
# API Key Authentication
|
|
563
|
+
MCP_API_KEYS=key1,key2,key3 # Comma-separated list
|
|
564
|
+
|
|
565
|
+
# OAuth2 Configuration
|
|
566
|
+
MCP_OAUTH_CLIENT_ID=your-client-id
|
|
567
|
+
MCP_OAUTH_CLIENT_SECRET=your-client-secret
|
|
568
|
+
MCP_OAUTH_AUTH_URL=https://accounts.google.com/o/oauth2/v2/auth
|
|
569
|
+
MCP_OAUTH_TOKEN_URL=https://oauth2.googleapis.com/token
|
|
570
|
+
MCP_OAUTH_CALLBACK_URL=https://your-server.com/oauth/callback
|
|
571
|
+
MCP_OAUTH_SCOPE=openid profile email
|
|
572
|
+
MCP_OAUTH_USERINFO_URL=https://www.googleapis.com/oauth2/v2/userinfo
|
|
573
|
+
|
|
574
|
+
# Session-based Auth Control
|
|
575
|
+
MCP_SESSION_AUTH_ENABLED=true # Enable session auth
|
|
576
|
+
MCP_SESSION_AUTH_REQUIRE_FOR_SESSION=false # Allow session creation without auth
|
|
577
|
+
MCP_SESSION_AUTH_REQUIRE_FOR_OPERATIONS=true # Require auth for operations
|
|
578
|
+
```
|
|
579
|
+
|
|
580
|
+
#### Authentication Examples
|
|
581
|
+
|
|
582
|
+
**Bearer Token (Recommended for APIs)**
|
|
583
|
+
```bash
|
|
584
|
+
# Generate a token (you'll need to implement token generation)
|
|
585
|
+
curl -X POST http://localhost:3157/mcp \
|
|
586
|
+
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIs..."
|
|
587
|
+
```
|
|
588
|
+
|
|
589
|
+
**API Key**
|
|
590
|
+
```bash
|
|
591
|
+
curl -X POST http://localhost:3157/mcp \
|
|
592
|
+
-H "X-API-Key: your-api-key"
|
|
593
|
+
```
|
|
594
|
+
|
|
595
|
+
**OAuth2 Flow**
|
|
596
|
+
1. Direct user to `/oauth/authorize`
|
|
597
|
+
2. User authenticates with OAuth provider
|
|
598
|
+
3. Server exchanges code for token at `/oauth/callback`
|
|
599
|
+
4. Client receives MCP JWT token for subsequent requests
|
|
600
|
+
|
|
601
|
+
#### Security Best Practices
|
|
602
|
+
|
|
603
|
+
1. **Always use HTTPS** in production deployments
|
|
604
|
+
2. **Set strong JWT secrets** (minimum 32 characters)
|
|
605
|
+
3. **Use short token expiration** times (24h or less)
|
|
606
|
+
4. **Enable rate limiting** for additional protection
|
|
607
|
+
5. **Configure CORS** appropriately for your clients
|
|
608
|
+
|
|
609
|
+
Example production configuration:
|
|
610
|
+
```bash
|
|
611
|
+
NODE_ENV=production
|
|
612
|
+
MCP_AUTH_STRATEGY=oauth2
|
|
613
|
+
MCP_AUTH_REQUIRE=true
|
|
614
|
+
MCP_JWT_SECRET=$(openssl rand -base64 32)
|
|
615
|
+
HTTP_RATE_LIMIT_ENABLED=true
|
|
616
|
+
HTTP_DNS_REBINDING_PROTECTION=true
|
|
617
|
+
HTTP_CORS_ORIGINS=https://your-app.com
|
|
618
|
+
```
|
|
619
|
+
|
|
503
620
|
### Model Selection
|
|
504
621
|
|
|
505
622
|
Use `"auto"` for automatic model selection, or specify exact models:
|
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
# Authentication Guide for Converse MCP Server
|
|
2
|
+
|
|
3
|
+
This guide covers the authentication options available for securing remote deployments of the Converse MCP Server.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The Converse MCP Server supports multiple authentication strategies to secure remote deployments:
|
|
8
|
+
|
|
9
|
+
- **None** (default) - No authentication required
|
|
10
|
+
- **Bearer Token** - JWT-based authentication
|
|
11
|
+
- **API Key** - Simple shared key authentication
|
|
12
|
+
- **OAuth 2.0** - Full OAuth2 authorization code flow
|
|
13
|
+
|
|
14
|
+
## Configuration
|
|
15
|
+
|
|
16
|
+
All authentication is configured through environment variables. The primary variable is `MCP_AUTH_STRATEGY` which determines the authentication method.
|
|
17
|
+
|
|
18
|
+
## Authentication Strategies
|
|
19
|
+
|
|
20
|
+
### 1. No Authentication (Default)
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
MCP_AUTH_STRATEGY=none
|
|
24
|
+
MCP_AUTH_REQUIRE=false
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
This is the default configuration suitable for:
|
|
28
|
+
- Local development
|
|
29
|
+
- Internal networks with other security measures
|
|
30
|
+
- Testing environments
|
|
31
|
+
|
|
32
|
+
### 2. Bearer Token Authentication
|
|
33
|
+
|
|
34
|
+
JWT-based authentication suitable for API clients.
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
MCP_AUTH_STRATEGY=bearer
|
|
38
|
+
MCP_AUTH_REQUIRE=true
|
|
39
|
+
MCP_JWT_SECRET=your-secret-key-at-least-32-chars
|
|
40
|
+
MCP_JWT_ALGORITHM=HS256
|
|
41
|
+
MCP_JWT_EXPIRES_IN=24h
|
|
42
|
+
MCP_JWT_ISSUER=converse-mcp-server
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
**Client Usage:**
|
|
46
|
+
```bash
|
|
47
|
+
curl -X POST http://localhost:3157/mcp \
|
|
48
|
+
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIs..." \
|
|
49
|
+
-H "Content-Type: application/json" \
|
|
50
|
+
-d '{"jsonrpc":"2.0","method":"tools/list","id":1}'
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
**Token Generation:**
|
|
54
|
+
You'll need to implement token generation separately. Example Node.js code:
|
|
55
|
+
|
|
56
|
+
```javascript
|
|
57
|
+
import jwt from 'jsonwebtoken';
|
|
58
|
+
|
|
59
|
+
const token = jwt.sign(
|
|
60
|
+
{
|
|
61
|
+
sub: 'user123',
|
|
62
|
+
name: 'John Doe',
|
|
63
|
+
email: 'john@example.com'
|
|
64
|
+
},
|
|
65
|
+
process.env.MCP_JWT_SECRET,
|
|
66
|
+
{
|
|
67
|
+
algorithm: 'HS256',
|
|
68
|
+
expiresIn: '24h',
|
|
69
|
+
issuer: 'converse-mcp-server'
|
|
70
|
+
}
|
|
71
|
+
);
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### 3. API Key Authentication
|
|
75
|
+
|
|
76
|
+
Simple shared key authentication suitable for server-to-server communication.
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
MCP_AUTH_STRATEGY=api_key
|
|
80
|
+
MCP_AUTH_REQUIRE=true
|
|
81
|
+
MCP_API_KEYS=key1,key2,key3 # Comma-separated list
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
**Client Usage:**
|
|
85
|
+
```bash
|
|
86
|
+
curl -X POST http://localhost:3157/mcp \
|
|
87
|
+
-H "X-API-Key: key1" \
|
|
88
|
+
-H "Content-Type: application/json" \
|
|
89
|
+
-d '{"jsonrpc":"2.0","method":"tools/list","id":1}'
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### 4. OAuth 2.0 Authentication
|
|
93
|
+
|
|
94
|
+
Full OAuth2 authorization code flow suitable for web applications.
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
MCP_AUTH_STRATEGY=oauth2
|
|
98
|
+
MCP_AUTH_REQUIRE=true
|
|
99
|
+
|
|
100
|
+
# JWT for MCP tokens
|
|
101
|
+
MCP_JWT_SECRET=your-secret-key-at-least-32-chars
|
|
102
|
+
|
|
103
|
+
# OAuth Provider Configuration
|
|
104
|
+
MCP_OAUTH_CLIENT_ID=your-client-id
|
|
105
|
+
MCP_OAUTH_CLIENT_SECRET=your-client-secret
|
|
106
|
+
MCP_OAUTH_AUTH_URL=https://accounts.google.com/o/oauth2/v2/auth
|
|
107
|
+
MCP_OAUTH_TOKEN_URL=https://oauth2.googleapis.com/token
|
|
108
|
+
MCP_OAUTH_CALLBACK_URL=https://your-server.com/oauth/callback
|
|
109
|
+
MCP_OAUTH_SCOPE=openid profile email
|
|
110
|
+
MCP_OAUTH_USERINFO_URL=https://www.googleapis.com/oauth2/v2/userinfo
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
**OAuth Flow:**
|
|
114
|
+
|
|
115
|
+
1. **Initiate Authorization:**
|
|
116
|
+
```
|
|
117
|
+
GET /oauth/authorize?state=random-state
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
2. **User Authenticates:**
|
|
121
|
+
User is redirected to OAuth provider (Google, GitHub, etc.)
|
|
122
|
+
|
|
123
|
+
3. **Callback Handling:**
|
|
124
|
+
After authentication, provider redirects to:
|
|
125
|
+
```
|
|
126
|
+
GET /oauth/callback?code=auth-code&state=random-state
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
4. **Token Response:**
|
|
130
|
+
Server returns MCP JWT token:
|
|
131
|
+
```json
|
|
132
|
+
{
|
|
133
|
+
"access_token": "eyJhbGc...",
|
|
134
|
+
"token_type": "Bearer",
|
|
135
|
+
"expires_in": 86400,
|
|
136
|
+
"user": {
|
|
137
|
+
"sub": "user123",
|
|
138
|
+
"email": "user@example.com",
|
|
139
|
+
"name": "John Doe"
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
5. **Use Token:**
|
|
145
|
+
```bash
|
|
146
|
+
curl -X POST http://localhost:3157/mcp \
|
|
147
|
+
-H "Authorization: Bearer eyJhbGc..." \
|
|
148
|
+
-H "Content-Type: application/json" \
|
|
149
|
+
-d '{"jsonrpc":"2.0","method":"tools/list","id":1}'
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Session-Based Authentication
|
|
153
|
+
|
|
154
|
+
The server supports flexible session-based authentication:
|
|
155
|
+
|
|
156
|
+
```bash
|
|
157
|
+
# Enable session auth
|
|
158
|
+
MCP_SESSION_AUTH_ENABLED=true
|
|
159
|
+
|
|
160
|
+
# Allow creating sessions without auth (default: false)
|
|
161
|
+
MCP_SESSION_AUTH_REQUIRE_FOR_SESSION=false
|
|
162
|
+
|
|
163
|
+
# Require auth for operations (default: true)
|
|
164
|
+
MCP_SESSION_AUTH_REQUIRE_FOR_OPERATIONS=true
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
This allows:
|
|
168
|
+
- Clients to create sessions without authentication
|
|
169
|
+
- But requires authentication for actual operations
|
|
170
|
+
- Useful for allowing initial connection but securing data access
|
|
171
|
+
|
|
172
|
+
## Security Best Practices
|
|
173
|
+
|
|
174
|
+
### 1. Use HTTPS in Production
|
|
175
|
+
|
|
176
|
+
Always deploy with TLS/SSL encryption:
|
|
177
|
+
```nginx
|
|
178
|
+
server {
|
|
179
|
+
listen 443 ssl http2;
|
|
180
|
+
ssl_certificate /path/to/cert.pem;
|
|
181
|
+
ssl_certificate_key /path/to/key.pem;
|
|
182
|
+
|
|
183
|
+
location / {
|
|
184
|
+
proxy_pass http://localhost:3157;
|
|
185
|
+
proxy_set_header Authorization $http_authorization;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### 2. Generate Strong Secrets
|
|
191
|
+
|
|
192
|
+
```bash
|
|
193
|
+
# Generate a secure JWT secret
|
|
194
|
+
openssl rand -base64 32
|
|
195
|
+
|
|
196
|
+
# Generate API keys
|
|
197
|
+
openssl rand -hex 32
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### 3. Configure CORS Properly
|
|
201
|
+
|
|
202
|
+
```bash
|
|
203
|
+
HTTP_CORS_ORIGINS=https://your-app.com,https://app.your-domain.com
|
|
204
|
+
HTTP_CORS_CREDENTIALS=true # If using cookies
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### 4. Enable Rate Limiting
|
|
208
|
+
|
|
209
|
+
```bash
|
|
210
|
+
HTTP_RATE_LIMIT_ENABLED=true
|
|
211
|
+
HTTP_RATE_LIMIT_WINDOW=900000 # 15 minutes
|
|
212
|
+
HTTP_RATE_LIMIT_MAX_REQUESTS=1000 # Per window
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
### 5. Enable DNS Rebinding Protection
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
HTTP_DNS_REBINDING_PROTECTION=true
|
|
219
|
+
HTTP_ALLOWED_HOSTS=your-server.com,api.your-server.com
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Common OAuth Providers
|
|
223
|
+
|
|
224
|
+
### Google OAuth 2.0
|
|
225
|
+
|
|
226
|
+
1. Create project at https://console.cloud.google.com
|
|
227
|
+
2. Enable Google+ API
|
|
228
|
+
3. Create OAuth 2.0 credentials
|
|
229
|
+
4. Configure:
|
|
230
|
+
|
|
231
|
+
```bash
|
|
232
|
+
MCP_OAUTH_CLIENT_ID=your-google-client-id.apps.googleusercontent.com
|
|
233
|
+
MCP_OAUTH_CLIENT_SECRET=your-google-client-secret
|
|
234
|
+
MCP_OAUTH_AUTH_URL=https://accounts.google.com/o/oauth2/v2/auth
|
|
235
|
+
MCP_OAUTH_TOKEN_URL=https://oauth2.googleapis.com/token
|
|
236
|
+
MCP_OAUTH_USERINFO_URL=https://www.googleapis.com/oauth2/v2/userinfo
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### GitHub OAuth
|
|
240
|
+
|
|
241
|
+
```bash
|
|
242
|
+
MCP_OAUTH_CLIENT_ID=your-github-client-id
|
|
243
|
+
MCP_OAUTH_CLIENT_SECRET=your-github-client-secret
|
|
244
|
+
MCP_OAUTH_AUTH_URL=https://github.com/login/oauth/authorize
|
|
245
|
+
MCP_OAUTH_TOKEN_URL=https://github.com/login/oauth/access_token
|
|
246
|
+
MCP_OAUTH_USERINFO_URL=https://api.github.com/user
|
|
247
|
+
MCP_OAUTH_SCOPE=read:user user:email
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### Auth0
|
|
251
|
+
|
|
252
|
+
```bash
|
|
253
|
+
MCP_OAUTH_CLIENT_ID=your-auth0-client-id
|
|
254
|
+
MCP_OAUTH_CLIENT_SECRET=your-auth0-client-secret
|
|
255
|
+
MCP_OAUTH_AUTH_URL=https://your-domain.auth0.com/authorize
|
|
256
|
+
MCP_OAUTH_TOKEN_URL=https://your-domain.auth0.com/oauth/token
|
|
257
|
+
MCP_OAUTH_USERINFO_URL=https://your-domain.auth0.com/userinfo
|
|
258
|
+
MCP_OAUTH_SCOPE=openid profile email
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
## Custom Authentication
|
|
262
|
+
|
|
263
|
+
For custom authentication logic, implement a custom validator:
|
|
264
|
+
|
|
265
|
+
```javascript
|
|
266
|
+
const httpTransport = new HTTPTransportServer({
|
|
267
|
+
auth: {
|
|
268
|
+
strategy: 'custom',
|
|
269
|
+
customValidator: async (req) => {
|
|
270
|
+
// Your custom logic here
|
|
271
|
+
const token = req.headers['x-custom-token'];
|
|
272
|
+
|
|
273
|
+
// Validate token against your system
|
|
274
|
+
const user = await validateCustomToken(token);
|
|
275
|
+
|
|
276
|
+
return {
|
|
277
|
+
authenticated: !!user,
|
|
278
|
+
user: user,
|
|
279
|
+
// Additional data
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
## Troubleshooting
|
|
287
|
+
|
|
288
|
+
### Common Issues
|
|
289
|
+
|
|
290
|
+
1. **401 Unauthorized**
|
|
291
|
+
- Check auth strategy matches client implementation
|
|
292
|
+
- Verify token/API key is correct
|
|
293
|
+
- Check token expiration
|
|
294
|
+
|
|
295
|
+
2. **CORS Errors**
|
|
296
|
+
- Ensure `HTTP_CORS_ORIGINS` includes your client origin
|
|
297
|
+
- Add `Authorization` to `HTTP_CORS_HEADERS`
|
|
298
|
+
|
|
299
|
+
3. **OAuth Redirect Issues**
|
|
300
|
+
- Verify `MCP_OAUTH_CALLBACK_URL` matches OAuth provider config
|
|
301
|
+
- Check redirect URI is whitelisted in provider
|
|
302
|
+
|
|
303
|
+
4. **Session Issues**
|
|
304
|
+
- Ensure `mcp-session-id` header is included
|
|
305
|
+
- Check session timeout configuration
|
|
306
|
+
|
|
307
|
+
### Debug Mode
|
|
308
|
+
|
|
309
|
+
Enable debug logging for authentication:
|
|
310
|
+
```bash
|
|
311
|
+
LOG_LEVEL=debug
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
This will log:
|
|
315
|
+
- Authentication attempts
|
|
316
|
+
- Token validation results
|
|
317
|
+
- Session creation/cleanup
|
|
318
|
+
- OAuth flow steps
|
|
319
|
+
|
|
320
|
+
## Example Configurations
|
|
321
|
+
|
|
322
|
+
### Development (Local)
|
|
323
|
+
```bash
|
|
324
|
+
MCP_AUTH_STRATEGY=none
|
|
325
|
+
NODE_ENV=development
|
|
326
|
+
LOG_LEVEL=debug
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### Staging (API Key)
|
|
330
|
+
```bash
|
|
331
|
+
MCP_AUTH_STRATEGY=api_key
|
|
332
|
+
MCP_API_KEYS=staging-key-1,staging-key-2
|
|
333
|
+
MCP_AUTH_REQUIRE=true
|
|
334
|
+
HTTP_RATE_LIMIT_ENABLED=true
|
|
335
|
+
LOG_LEVEL=info
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
### Production (OAuth2)
|
|
339
|
+
```bash
|
|
340
|
+
NODE_ENV=production
|
|
341
|
+
MCP_AUTH_STRATEGY=oauth2
|
|
342
|
+
MCP_AUTH_REQUIRE=true
|
|
343
|
+
MCP_JWT_SECRET=$(openssl rand -base64 32)
|
|
344
|
+
MCP_OAUTH_CLIENT_ID=prod-client-id
|
|
345
|
+
MCP_OAUTH_CLIENT_SECRET=prod-client-secret
|
|
346
|
+
HTTP_RATE_LIMIT_ENABLED=true
|
|
347
|
+
HTTP_DNS_REBINDING_PROTECTION=true
|
|
348
|
+
HTTP_CORS_ORIGINS=https://app.example.com
|
|
349
|
+
LOG_LEVEL=warn
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
## Integration Examples
|
|
353
|
+
|
|
354
|
+
### JavaScript/TypeScript Client
|
|
355
|
+
|
|
356
|
+
```typescript
|
|
357
|
+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
358
|
+
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
359
|
+
|
|
360
|
+
const transport = new StreamableHTTPClientTransport(
|
|
361
|
+
new URL('https://your-server.com/mcp'),
|
|
362
|
+
{
|
|
363
|
+
headers: {
|
|
364
|
+
'Authorization': 'Bearer your-jwt-token'
|
|
365
|
+
// or 'X-API-Key': 'your-api-key'
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
);
|
|
369
|
+
|
|
370
|
+
const client = new Client({
|
|
371
|
+
name: 'my-client',
|
|
372
|
+
version: '1.0.0'
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
await client.connect(transport);
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
### Python Client
|
|
379
|
+
|
|
380
|
+
```python
|
|
381
|
+
import httpx
|
|
382
|
+
from mcp import Client
|
|
383
|
+
|
|
384
|
+
headers = {
|
|
385
|
+
"Authorization": "Bearer your-jwt-token"
|
|
386
|
+
# or "X-API-Key": "your-api-key"
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
async with httpx.AsyncClient(headers=headers) as http_client:
|
|
390
|
+
client = Client("my-client", "1.0.0")
|
|
391
|
+
await client.connect(
|
|
392
|
+
url="https://your-server.com/mcp",
|
|
393
|
+
http_client=http_client
|
|
394
|
+
)
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
## Deployment Checklist
|
|
398
|
+
|
|
399
|
+
- [ ] Choose appropriate authentication strategy
|
|
400
|
+
- [ ] Generate secure secrets (32+ characters)
|
|
401
|
+
- [ ] Configure OAuth provider (if using OAuth2)
|
|
402
|
+
- [ ] Set up HTTPS/TLS
|
|
403
|
+
- [ ] Configure CORS for your clients
|
|
404
|
+
- [ ] Enable rate limiting
|
|
405
|
+
- [ ] Set appropriate session timeouts
|
|
406
|
+
- [ ] Test authentication flow end-to-end
|
|
407
|
+
- [ ] Monitor authentication logs
|
|
408
|
+
- [ ] Document API keys/tokens for clients
|