Authentication
MCP Gatekeeper uses token-based authentication to secure API endpoints.
Overview
Protected endpoints require an authentication token sent via a custom HTTP header. This provides a simple yet effective security layer for local development.
Configuration
Backend Setup
Configure authentication in apps/backend/.env:
# Authentication Token (IMPORTANT: Change this!)
AUTH_TOKEN=your-secret-token-here
# Authentication Header Name (default: X-MCPGK-Token)
AUTH_TOKEN_HEADER_NAME=X-MCPGK-Token
Generating a Secure Token
Generate a strong random token:
# Using openssl
openssl rand -hex 32
# Using Python
python3 -c "import secrets; print(secrets.token_hex(32))"
# Using Node.js
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
Frontend Setup
The frontend automatically reads the AUTH_TOKEN from the backend configuration when making API requests.
If you're using a custom token, ensure it matches the backend configuration.
How It Works
Authentication Flow
- Client sends HTTP request to protected endpoint
- Request includes
X-MCPGK-Tokenheader with the token - Backend validates the token
- If valid, request proceeds; if invalid, returns 401 Unauthorized
Example Request
curl -H "X-MCPGK-Token: your-secret-token-here" \
http://127.0.0.1:8000/api/servers/
Protected Endpoints
The following endpoints require authentication:
- POST
/api/servers/- Create a new server - POST
/api/servers/{id}/start- Start a server - POST
/api/servers/{id}/stop- Stop a server - DELETE
/api/servers/{id}- Delete a server - POST
/api/packages/install-npm- Install a package - DELETE
/api/packages/{id}- Uninstall a package - PUT
/api/agents/{agent_type}/servers- Update agent config - DELETE
/api/agents/{agent_type}/servers- Remove from agent config - DELETE
/api/logs/- Clear logs
Public Endpoints
These endpoints do NOT require authentication:
- GET
/api/health- Health check - GET
/api/servers/- List servers - GET
/api/servers/{id}- Get server details - GET
/api/metrics/*- Get metrics - GET
/api/logs/- Get logs - GET
/api/packages/- List packages - GET
/api/agents/- List agents - WS
/api/ws/metrics- WebSocket connection
Frontend Integration
The frontend automatically includes the authentication token in requests:
// apps/frontend/src/services/api.ts
const API_TOKEN = 'your-token-here';
const api = axios.create({
baseURL: import.meta.env.VITE_API_URL,
headers: {
'X-MCPGK-Token': API_TOKEN,
},
});
Security Considerations
Local-Only Access
The backend binds to 127.0.0.1 (localhost) by default, preventing external network access:
# In apps/backend/.env
HOST=127.0.0.1
Do NOT change this to 0.0.0.0 unless you understand the security implications.
Token Storage
- Backend: Token stored in
.envfile (NOT committed to git) - Frontend: Token embedded in build (local development only)
Best Practices
- Change Default Token: Always change the default token in production
- Use Strong Tokens: Generate tokens with at least 32 bytes of randomness
- Keep .env Secure: Never commit
.envfiles to version control - Rotate Tokens: Periodically change your authentication token
- Local-Only: Keep
HOST=127.0.0.1for local-only access
Troubleshooting
401 Unauthorized Error
If you receive a 401 error:
- Verify token in
apps/backend/.envmatches frontend - Check that
AUTH_TOKEN_HEADER_NAMEis correct - Ensure the token is included in the request header
Missing Authentication Token
Error: Missing authentication token in 'X-MCPGK-Token' header
Solution: Include the token in your request headers.
Invalid Authentication Token
Error: Invalid authentication token
Solution: Verify the token matches the AUTH_TOKEN in apps/backend/.env.
Custom Authentication
Changing Header Name
To use a different header name:
# In apps/backend/.env
AUTH_TOKEN_HEADER_NAME=X-Custom-Auth-Token
Update frontend accordingly:
// apps/frontend/src/services/api.ts
headers: {
'X-Custom-Auth-Token': API_TOKEN,
}
Implementing OAuth/JWT
For production deployments, consider implementing:
- OAuth 2.0
- JWT tokens
- API key management
- Rate limiting
See Development > Contributing for guidance on extending authentication.
Next Steps
- Architecture Overview - Understand the system
- API Reference - Authentication API details
- Security Best Practices - Security guidelines