This guide helps resolve common Cloudflare authentication issues with the MCP Memory Service.
Cloudflare API tokens come in different types with varying scopes and verification methods. Understanding these differences is crucial for proper authentication.
What they are: Tokens with specific permissions limited to a particular Cloudflare account.
Required Permissions:
D1 Database:Edit- For D1 database operationsVectorize:Edit- For vector index operations
Verification Endpoint:
curl "https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/tokens/verify" \
-H "Authorization: Bearer {YOUR_TOKEN}"Success Response:
{
"result": {
"id": "token_id_here",
"status": "active",
"expires_on": "2026-04-30T23:59:59Z"
},
"success": true,
"errors": [],
"messages": [
{
"code": 10000,
"message": "This API Token is valid and active"
}
]
}What they are: Tokens with broader permissions across all accounts.
Verification Endpoint:
curl "https://api.cloudflare.com/client/v4/user/tokens/verify" \
-H "Authorization: Bearer {YOUR_TOKEN}"Cause: Using the wrong verification endpoint for your token type.
Solution:
- If using account-scoped token, use the account-specific endpoint
- If using global token, use the user endpoint
- Check token expiration date
- Verify token permissions
Example:
# ❌ Wrong: Testing account-scoped token with user endpoint
curl "https://api.cloudflare.com/client/v4/user/tokens/verify" \
-H "Authorization: Bearer account_scoped_token"
# Returns: {"success":false,"errors":[{"code":1000,"message":"Invalid API Token"}]}
# ✅ Correct: Testing account-scoped token with account endpoint
curl "https://api.cloudflare.com/client/v4/accounts/your_account_id/tokens/verify" \
-H "Authorization: Bearer account_scoped_token"
# Returns: {"success":true,...}Cause: Token lacks required permissions for specific operations.
Solution:
- Verify token has
D1 Database:Editpermission - Verify token has
Vectorize:Editpermission - Check if token has expired
- Ensure account ID matches token scope
Cause: Environment variables may not be properly loaded or token is invalid.
Debugging Steps:
-
Check environment variable loading:
python scripts/validation/diagnose_backend_config.py
-
Test token manually:
curl "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/tokens/verify" \ -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN"
-
Test D1 database access:
curl -X POST "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/d1/database/$CLOUDFLARE_D1_DATABASE_ID/query" \ -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ -H "Content-Type: application/json" \ -d '{"sql": "SELECT name FROM sqlite_master WHERE type='"'"'table'"'"';"}'
- Go to Cloudflare Dashboard
- Click "Create Token"
- Use "Custom token" template
- Set permissions:
Account→Cloudflare D1:EditAccount→Vectorize:Edit
- Set account resources to your specific account
- Add client IP restrictions (optional but recommended)
- Set expiration date
- Create and copy the token immediately
- ✅ Use account-scoped tokens with minimal required permissions
- ✅ Set expiration dates (e.g., 1 year maximum)
- ✅ Add IP restrictions when possible
- ✅ Store tokens securely (environment variables, not in code)
- ✅ Rotate tokens regularly
- ❌ Never commit tokens to version control
- ❌ Don't use global tokens unless absolutely necessary
# Account-scoped token (recommended)
CLOUDFLARE_API_TOKEN=your_account_scoped_token_here
CLOUDFLARE_ACCOUNT_ID=your_account_id_here
CLOUDFLARE_D1_DATABASE_ID=your_d1_database_id_here
CLOUDFLARE_VECTORIZE_INDEX=mcp-memory-index# Test all configuration
python scripts/validation/diagnose_backend_config.py
# Quick token test
curl "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/tokens/verify" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN"- Token is account-scoped and has correct permissions
- Using correct verification endpoint (
/accounts/{id}/tokens/verify) - Environment variables are loaded correctly
- Account ID matches token scope
- Token has not expired
- D1 database ID is correct
- Vectorize index exists
- MCP service has been restarted after configuration changes
If you're still experiencing issues:
- Run the diagnostic script:
python scripts/validation/diagnose_backend_config.py - Check the GitHub Issues
- Review the main README.md for setup instructions
- Check the CLAUDE.md for Claude Code specific guidance