# MCP Authentication Patterns Complete guide to authentication methods for MCP servers in Claude Code plugins. ## Overview MCP servers support multiple authentication methods depending on the server type and service requirements. Choose the method that best matches your use case and security requirements. ## OAuth (Automatic) ### How It Works Claude Code automatically handles the complete OAuth 2.0 flow for SSE and HTTP servers: 1. User attempts to use MCP tool 2. Claude Code detects authentication needed 3. Opens browser for OAuth consent 4. User authorizes in browser 5. Tokens stored securely by Claude Code 6. Automatic token refresh ### Configuration ```json { "service": { "type": "sse", "url": "https://mcp.example.com/sse" } } ``` No additional auth configuration needed! Claude Code handles everything. ### Supported Services **Known OAuth-enabled MCP servers:** - Asana: `https://mcp.asana.com/sse` - GitHub (when available) - Google services (when available) - Custom OAuth servers ### OAuth Scopes OAuth scopes are determined by the MCP server. Users see required scopes during the consent flow. **Document required scopes in your README:** ```markdown ## Authentication This plugin requires the following Asana permissions: - Read tasks and projects - Create and update tasks - Access workspace data ``` ### Token Storage Tokens are stored securely by Claude Code: - Not accessible to plugins - Encrypted at rest - Automatic refresh - Cleared on sign-out ### Troubleshooting OAuth **Authentication loop:** - Clear cached tokens (sign out and sign in) - Check OAuth redirect URLs - Verify server OAuth configuration **Scope issues:** - User may need to re-authorize for new scopes - Check server documentation for required scopes **Token expiration:** - Claude Code auto-refreshes - If refresh fails, prompts re-authentication ## Token-Based Authentication ### Bearer Tokens Most common for HTTP and WebSocket servers. **Configuration:** ```json { "api": { "type": "http", "url": "https://api.example.com/mcp", "headers": { "Authorization": "Bearer ${API_TOKEN}" } } } ``` **Environment variable:** ```bash export API_TOKEN="your-secret-token-here" ``` ### API Keys Alternative to Bearer tokens, often in custom headers. **Configuration:** ```json { "api": { "type": "http", "url": "https://api.example.com/mcp", "headers": { "X-API-Key": "${API_KEY}", "X-API-Secret": "${API_SECRET}" } } } ``` ### Custom Headers Services may use custom authentication headers. **Configuration:** ```json { "service": { "type": "sse", "url": "https://mcp.example.com/sse", "headers": { "X-Auth-Token": "${AUTH_TOKEN}", "X-User-ID": "${USER_ID}", "X-Tenant-ID": "${TENANT_ID}" } } } ``` ### Documenting Token Requirements Always document in your README: ```markdown ## Setup ### Required Environment Variables Set these environment variables before using the plugin: \`\`\`bash export API_TOKEN="your-token-here" export API_SECRET="your-secret-here" \`\`\` ### Obtaining Tokens 1. Visit https://api.example.com/tokens 2. Create a new API token 3. Copy the token and secret 4. Set environment variables as shown above ### Token Permissions The API token needs the following permissions: - Read access to resources - Write access for creating items - Delete access (optional, for cleanup operations) \`\`\` ``` ## Environment Variable Authentication (stdio) ### Passing Credentials to Server For stdio servers, pass credentials via environment variables: ```json { "database": { "command": "python", "args": ["-m", "mcp_server_db"], "env": { "DATABASE_URL": "${DATABASE_URL}", "DB_USER": "${DB_USER}", "DB_PASSWORD": "${DB_PASSWORD}" } } } ``` ### User Environment Variables ```bash # User sets these in their shell export DATABASE_URL="postgresql://localhost/mydb" export DB_USER="myuser" export DB_PASSWORD="mypassword" ``` ### Documentation Template ```markdown ## Database Configuration Set these environment variables: \`\`\`bash export DATABASE_URL="postgresql://host:port/database" export DB_USER="username" export DB_PASSWORD="password" \`\`\` Or create a `.env` file (add to `.gitignore`): \`\`\` DATABASE_URL=postgresql://localhost:5432/mydb DB_USER=myuser DB_PASSWORD=mypassword \`\`\` Load with: \`source .env\` or \`export $(cat .env | xargs)\` \`\`\` ``` ## Dynamic Headers ### Headers Helper Script For tokens that change or expire, use a helper script: ```json { "api": { "type": "sse", "url": "https://api.example.com", "headersHelper": "${CLAUDE_PLUGIN_ROOT}/scripts/get-headers.sh" } } ``` **Script (get-headers.sh):** ```bash #!/bin/bash # Generate dynamic authentication headers # Fetch fresh token TOKEN=$(get-fresh-token-from-somewhere) # Output JSON headers cat <