|
| 1 | +# Claude CLI Integration Improvements |
| 2 | + |
| 3 | +This document summarizes the enhancements made to improve the Supabase MCP server's integration with Claude CLI and other MCP clients. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The improvements focus on three main areas: |
| 8 | +1. **Enhanced Authentication & Token Handling** |
| 9 | +2. **Claude CLI-Specific Error Messaging** |
| 10 | +3. **Improved User Experience & Debugging** |
| 11 | + |
| 12 | +## Key Improvements |
| 13 | + |
| 14 | +### 1. Enhanced Token Validation (`src/auth.ts`) |
| 15 | + |
| 16 | +**New Features:** |
| 17 | +- **Token Format Validation**: Validates Supabase token format (`sbp_*`) with proper regex patterns |
| 18 | +- **Token Sanitization**: Removes quotes, whitespace, and other common formatting issues |
| 19 | +- **Early Validation**: Validates tokens at startup rather than waiting for API calls |
| 20 | +- **Flexible Token Length**: Supports various Supabase token lengths while maintaining security |
| 21 | + |
| 22 | +**Code Example:** |
| 23 | +```typescript |
| 24 | +const result = validateAndSanitizeToken(' "sbp_1234567890abcdef" '); |
| 25 | +// Returns: { isValid: true, sanitizedToken: 'sbp_1234567890abcdef' } |
| 26 | +``` |
| 27 | + |
| 28 | +### 2. Client Detection & Context-Aware Messaging |
| 29 | + |
| 30 | +**New Features:** |
| 31 | +- **Claude CLI Detection**: Automatically detects when running under Claude CLI |
| 32 | +- **Context-Aware Errors**: Provides different error messages based on the detected client |
| 33 | +- **User Agent Analysis**: Uses client info and user agent for better detection |
| 34 | + |
| 35 | +**Code Example:** |
| 36 | +```typescript |
| 37 | +const clientContext = detectClientContext(clientInfo, userAgent); |
| 38 | +if (clientContext.isClaudeCLI) { |
| 39 | + // Provide Claude CLI-specific guidance |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +### 3. Enhanced Error Handling (`src/management-api/index.ts`) |
| 44 | + |
| 45 | +**Improvements:** |
| 46 | +- **Detailed Debug Logging**: Enhanced 401 error logging with client context |
| 47 | +- **Progressive Error Messages**: Structured error messages with actionable steps |
| 48 | +- **Client-Specific Guidance**: Different troubleshooting steps for Claude CLI vs other clients |
| 49 | + |
| 50 | +**Before:** |
| 51 | +``` |
| 52 | +Unauthorized. Please provide a valid access token to the MCP server via the --access-token flag or SUPABASE_ACCESS_TOKEN. |
| 53 | +``` |
| 54 | + |
| 55 | +**After:** |
| 56 | +``` |
| 57 | +Unauthorized: Invalid or expired access token. |
| 58 | +
|
| 59 | +For Claude CLI users: |
| 60 | +1. Ensure SUPABASE_ACCESS_TOKEN is set in your environment |
| 61 | +2. Restart Claude CLI after setting the environment variable |
| 62 | +3. Check your MCP server configuration in Claude CLI settings |
| 63 | +
|
| 64 | +Token validation issues: |
| 65 | +- Supabase access tokens must start with "sbp_" |
| 66 | +- Generate a new token at https://supabase.com/dashboard/account/tokens |
| 67 | +
|
| 68 | +General troubleshooting: |
| 69 | +- Verify the token at https://supabase.com/dashboard/account/tokens |
| 70 | +- Ensure the token has not expired |
| 71 | +- Check that the token has appropriate permissions |
| 72 | +``` |
| 73 | + |
| 74 | +### 4. Startup Authentication Validation (`src/transports/stdio.ts`) |
| 75 | + |
| 76 | +**New Features:** |
| 77 | +- **Startup Token Resolution**: Validates tokens before server initialization |
| 78 | +- **Multiple Token Sources**: CLI flags, environment variables with proper priority |
| 79 | +- **Warning System**: Provides warnings for suboptimal configurations |
| 80 | +- **Graceful Failure**: Clear error messages when authentication fails |
| 81 | + |
| 82 | +### 5. Comprehensive Testing (`src/auth.test.ts`) |
| 83 | + |
| 84 | +**Test Coverage:** |
| 85 | +- Token format validation and sanitization |
| 86 | +- Client context detection for Claude CLI |
| 87 | +- Error message generation for different scenarios |
| 88 | +- Token resolution with multiple sources |
| 89 | +- Authentication setup validation |
| 90 | + |
| 91 | +## Usage Examples |
| 92 | + |
| 93 | +### For Claude CLI Users |
| 94 | + |
| 95 | +1. **Set Environment Variable:** |
| 96 | + ```bash |
| 97 | + export SUPABASE_ACCESS_TOKEN="sbp_your_token_here" |
| 98 | + ``` |
| 99 | + |
| 100 | +2. **Restart Claude CLI** to pick up the new environment variable |
| 101 | + |
| 102 | +3. **The server will automatically:** |
| 103 | + - Detect Claude CLI usage |
| 104 | + - Validate token format |
| 105 | + - Provide Claude CLI-specific error messages if issues occur |
| 106 | + |
| 107 | +### For Other MCP Clients |
| 108 | + |
| 109 | +The improvements are backward compatible and provide enhanced error messaging for all MCP clients, with specific optimizations for Claude CLI. |
| 110 | + |
| 111 | +## Configuration Files Updated |
| 112 | + |
| 113 | +### `server.json` |
| 114 | +- Enhanced environment variable description with token format information |
| 115 | +- Added link to Supabase token generation page |
| 116 | + |
| 117 | +### `README.md` |
| 118 | +- New "Claude CLI Configuration" section |
| 119 | +- Detailed troubleshooting guide |
| 120 | +- Enhanced setup instructions |
| 121 | + |
| 122 | +## Security Considerations |
| 123 | + |
| 124 | +- **Token Validation**: Prevents malformed tokens from reaching the API |
| 125 | +- **Input Sanitization**: Safely handles user input with proper validation |
| 126 | +- **Error Information**: Avoids leaking sensitive information in error messages |
| 127 | +- **Debug Logging**: Comprehensive logging for security monitoring without exposing secrets |
| 128 | + |
| 129 | +## Migration Guide |
| 130 | + |
| 131 | +These improvements are **fully backward compatible**. Existing MCP server configurations will continue to work without any changes. |
| 132 | + |
| 133 | +**Optional Improvements:** |
| 134 | +- Set `SUPABASE_ACCESS_TOKEN` as an environment variable for better Claude CLI experience |
| 135 | +- Update MCP client configurations to use environment variables instead of CLI flags |
| 136 | + |
| 137 | +## Testing |
| 138 | + |
| 139 | +Run the comprehensive test suite: |
| 140 | +```bash |
| 141 | +pnpm test:unit -- src/auth.test.ts |
| 142 | +``` |
| 143 | + |
| 144 | +All existing tests continue to pass, with additional coverage for the new authentication features. |
| 145 | + |
| 146 | +## Future Enhancements |
| 147 | + |
| 148 | +Potential areas for future improvement: |
| 149 | +1. **Token Expiration Detection**: Check token expiration before API calls |
| 150 | +2. **Credential Refresh**: Automatic token refresh mechanisms |
| 151 | +3. **Multiple Token Support**: Support for different token types |
| 152 | +4. **Advanced Client Detection**: More sophisticated client detection logic |
| 153 | +5. **Metrics & Analytics**: Usage analytics for different client types |
0 commit comments