|
| 1 | +# GitLab MCP Passthrough Authentication Mode |
| 2 | + |
| 3 | +This guide explains how to configure the GitLab MCP server to use passthrough authentication mode, where users provide their own GitLab Personal Access Token (PAT) with each request. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Passthrough mode allows multiple users to use the same MCP server instance with their own GitLab credentials. Each user provides their GitLab Personal Access Token via the `Gitlab-Token` header, and the server uses that token for all GitLab API requests. |
| 8 | + |
| 9 | +This mode is ideal for: |
| 10 | +- Multi-user environments |
| 11 | +- Scenarios where you don't want to store tokens on the server |
| 12 | +- Testing with different access levels |
| 13 | + |
| 14 | +## Configuration |
| 15 | + |
| 16 | +To enable passthrough mode, set the following environment variable: |
| 17 | + |
| 18 | +```bash |
| 19 | +GITLAB_PAT_PASSTHROUGH=true |
| 20 | +``` |
| 21 | + |
| 22 | +**Important:** When using passthrough mode, do not set: |
| 23 | +- `GITLAB_PERSONAL_ACCESS_TOKEN` |
| 24 | +- `GITLAB_OAUTH2_CLIENT_ID` (or any OAuth2 configuration) |
| 25 | + |
| 26 | +## How It Works |
| 27 | + |
| 28 | +1. The MCP server starts without any pre-configured authentication |
| 29 | +2. Each client request must include a `Gitlab-Token` header with a valid GitLab PAT |
| 30 | +3. The server uses the provided token for that specific request |
| 31 | +4. No tokens are stored or cached by the server |
| 32 | + |
| 33 | +## Client Configuration |
| 34 | + |
| 35 | +### Using with MCP Client |
| 36 | + |
| 37 | +When connecting to a passthrough-enabled server, clients must provide the GitLab token with each request: |
| 38 | + |
| 39 | +```typescript |
| 40 | +// Example client configuration |
| 41 | +const client = new MCPClient({ |
| 42 | + url: 'http://localhost:3000', |
| 43 | + headers: { |
| 44 | + 'Gitlab-Token': 'your-gitlab-personal-access-token' |
| 45 | + } |
| 46 | +}); |
| 47 | +``` |
| 48 | + |
| 49 | +### Using with cURL |
| 50 | + |
| 51 | +```bash |
| 52 | +curl -H "Gitlab-Token: your-gitlab-personal-access-token" \ |
| 53 | + http://localhost:3000/your-endpoint |
| 54 | +``` |
| 55 | + |
| 56 | +### Using with HTTP Libraries |
| 57 | + |
| 58 | +```javascript |
| 59 | +// Node.js with fetch |
| 60 | +const response = await fetch('http://localhost:3000/your-endpoint', { |
| 61 | + headers: { |
| 62 | + 'Gitlab-Token': 'your-gitlab-personal-access-token' |
| 63 | + } |
| 64 | +}); |
| 65 | +``` |
| 66 | + |
| 67 | +## Creating a GitLab Personal Access Token |
| 68 | + |
| 69 | +1. Go to GitLab (e.g., https://gitlab.com) |
| 70 | +2. Navigate to **User Settings** → **Access Tokens** |
| 71 | +3. Create a new token with: |
| 72 | + - **Token name**: Descriptive name (e.g., "MCP Client") |
| 73 | + - **Expiration date**: Set as needed |
| 74 | + - **Scopes**: Select based on your needs: |
| 75 | + - `api` - Full API access (recommended) |
| 76 | + - `read_api` - Read-only API access |
| 77 | + - `read_repository` - Read repository content |
| 78 | + - `write_repository` - Write repository content |
| 79 | + |
| 80 | +4. Copy the generated token and use it in the `Gitlab-Token` header |
| 81 | + |
| 82 | +## Security Considerations |
| 83 | + |
| 84 | +1. **Token Transmission**: Tokens are sent with every request |
| 85 | + - Always use HTTPS in production to encrypt tokens in transit |
| 86 | + - Never log or store tokens on the client side in plain text |
| 87 | + |
| 88 | +2. **No Server Storage**: The server does not store any tokens |
| 89 | + - Each request is authenticated independently |
| 90 | + - No session management or token caching |
| 91 | + |
| 92 | +3. **Token Scope**: Users control their own access levels |
| 93 | + - Each user's token determines what they can access |
| 94 | + - Server has no control over permissions |
| 95 | + |
| 96 | +## Error Handling |
| 97 | + |
| 98 | +### Missing Token |
| 99 | +If a request is made without the `Gitlab-Token` header: |
| 100 | +``` |
| 101 | +Status: 401 Unauthorized |
| 102 | +Body: "Please set a Gitlab-Token header in your request" |
| 103 | +``` |
| 104 | + |
| 105 | +### Invalid Token Format |
| 106 | +If the token is not a string or is sent multiple times: |
| 107 | +``` |
| 108 | +Status: 401 Unauthorized |
| 109 | +Body: "Gitlab-Token must only be set once" |
| 110 | +``` |
| 111 | + |
| 112 | +### Invalid GitLab Token |
| 113 | +If GitLab rejects the token: |
| 114 | +``` |
| 115 | +Status: 401 Unauthorized |
| 116 | +Body: GitLab API error message |
| 117 | +``` |
| 118 | + |
| 119 | +## Starting the Server |
| 120 | + |
| 121 | +Start the server with passthrough mode enabled: |
| 122 | + |
| 123 | +```bash |
| 124 | +GITLAB_PAT_PASSTHROUGH=true npm dev |
| 125 | +``` |
| 126 | + |
| 127 | +The server will log: |
| 128 | +``` |
| 129 | +Configuring GitLab PAT passthrough authentication. Users must set the Gitlab-Token header in their requests |
| 130 | +``` |
| 131 | + |
| 132 | +## Comparison with Other Modes |
| 133 | + |
| 134 | +| Feature | Passthrough | Static PAT | OAuth2 Proxy | |
| 135 | +|---------|------------|------------|--------------| |
| 136 | +| Multi-user support | ✅ Yes | ❌ No | ✅ Yes | |
| 137 | +| Token storage | ❌ None | ✅ Server | ✅ Database | |
| 138 | +| Setup complexity | Low | Low | High | |
| 139 | +| Transport support | All | All | SSE/HTTP only | |
| 140 | +| User token control | ✅ Full | ❌ None | ⚠️ Limited | |
| 141 | + |
| 142 | +## Example: Full Request Flow |
| 143 | + |
| 144 | +1. User creates a GitLab PAT with necessary scopes |
| 145 | +2. User configures their MCP client with the token: |
| 146 | + ```javascript |
| 147 | + const client = new MCPClient({ |
| 148 | + url: 'http://localhost:3000', |
| 149 | + headers: { |
| 150 | + 'Gitlab-Token': 'glpat-xxxxxxxxxxxxxxxxxxxx' |
| 151 | + } |
| 152 | + }); |
| 153 | + ``` |
| 154 | +3. Client makes a request to list projects |
| 155 | +4. MCP server receives request with token header |
| 156 | +5. Server forwards the token to GitLab API |
| 157 | +6. GitLab validates token and returns data |
| 158 | +7. Server returns data to client |
| 159 | + |
| 160 | +## Troubleshooting |
| 161 | + |
| 162 | +### Token Not Working |
| 163 | +- Verify the token has not expired |
| 164 | +- Check that the token has the required scopes |
| 165 | +- Ensure the token is from the correct GitLab instance |
| 166 | +- Try the token directly with GitLab API to verify it works |
| 167 | + |
| 168 | +### Multiple Users Issues |
| 169 | +- Each user must use their own token |
| 170 | +- Tokens should not be shared between users |
| 171 | +- Consider OAuth2 mode for better multi-user management |
| 172 | + |
| 173 | +## Best Practices |
| 174 | + |
| 175 | +1. **Token Rotation**: Regularly rotate PATs for security |
| 176 | +2. **Minimal Scopes**: Use tokens with only necessary scopes |
| 177 | +3. **HTTPS Only**: Always use HTTPS in production |
| 178 | +4. **Client Security**: Store tokens securely on client side |
| 179 | +5. **Monitoring**: Log request counts but never log tokens |
| 180 | + |
| 181 | +## Note |
| 182 | + |
| 183 | +Passthrough mode is ideal for development and multi-user scenarios where each user manages their own credentials. For production deployments with many users, consider using OAuth2 proxy mode for better token management and security. |
0 commit comments