Good news: The MCP server handles all token management automatically. This guide is for understanding what happens behind the scenes.
Token Types
The eBay MCP Server works with two types of OAuth tokens:Access Token
Lifetime: ~2 hoursPurpose: Used for API requestsRefreshed: Automatically when expired
Refresh Token
Lifetime: 18 monthsPurpose: Obtain new access tokensRenewed: Each time it’s used
How Token Management Works
Automatic Token Lifecycle
1
Initial Authorization
When you complete OAuth setup, eBay provides:
- Access token (valid ~2 hours)
- Refresh token (valid 18 months)
- Token expiry timestamp
.env file automatically.2
Token Storage
The MCP server loads tokens from environment variables:
3
Token Validation
Before each API call, the server checks:
- Is the access token still valid?
- Has it expired based on the expiry timestamp?
4
Automatic Refresh
When the access token expires:
- Server uses refresh token to request new access token
- eBay returns new access token + new refresh token
- Server updates
.envfile with new tokens - API call proceeds with new access token
5
Continuous Operation
This cycle repeats automatically:
- Every ~2 hours, tokens refresh
- Each refresh renews the refresh token
- As long as the server runs occasionally, tokens never truly expire
You never need to manually refresh tokens - the MCP server does it all for you!
Token Expiry Details
Access Token Expiration
Typical lifetime: 7,200 seconds (2 hours) Expiry behavior:- Server checks expiry before each API call
- Automatically refreshes if expired
- Uses cached token if still valid (performance optimization)
.env:
The expiry timestamp is in ISO 8601 format with UTC timezone (the
Z suffix).Refresh Token Expiration
Typical lifetime: 540 days (18 months) Important: Each time you use a refresh token to get a new access token, eBay also provides a new refresh token with a fresh 18-month expiry. This means:- If you use the MCP server at least once every 18 months, your refresh token effectively never expires
- Long periods of inactivity (>18 months) require re-authorization
- Regular Use
- Inactive Account
Scenario: You use the MCP server weeklyWhat happens:
- Access tokens refresh every ~2 hours when needed
- Each refresh renews the refresh token
- Refresh token expiry resets to 18 months from last use
- Result: Tokens never expire ✅
Token Refresh Process
When Refresh Happens
The server refreshes tokens in these situations:- Before API calls - If access token is expired
- On server startup - If access token is expired
- Proactively - If token expires within next 5 minutes (configurable)
How Refresh Works
1
Detect Expiration
Server compares current time with
EBAY_USER_TOKEN_EXPIRY:2
Call eBay Token Endpoint
Server makes a POST request to eBay’s token endpoint:With Authorization header containing Client ID and Secret.
3
Receive New Tokens
eBay responds with:
4
Update Environment
Server updates Expiry is calculated as:
.env file with new values:currentTime + expires_in seconds.5
Continue Operation
The API call proceeds with the new access token.
Monitoring Token Status
Check Current Token Status
View your current token information:- Current access token
- Current refresh token
- When access token expires
Validate Tokens
Ensure your tokens are valid:- ✅ Tokens exist
- ✅ Format is correct
- ✅ Tokens work with eBay API
- ✅ Expiry timestamp is valid
Run this after setup or if you suspect token issues.
Server Logs
The MCP server logs token-related events:Manual Token Operations
While automatic management is recommended, you can manually manage tokens if needed:Manually Refresh Tokens
Force a token refresh:- Uses current refresh token
- Obtains new access and refresh tokens
- Updates
.envfile
Regenerate Tokens
If tokens are corrupted or invalid, regenerate them:- Opens browser for authorization
- Exchanges code for new tokens
- Saves to
.env
This is the same process as initial OAuth setup.
Revoke Tokens
To revoke all tokens and start fresh:1
Remove from .env
Delete or comment out token variables:
2
Revoke in eBay Portal (Optional)
- Sign in to eBay Developer Portal
- Navigate to User Tokens
- Find your application
- Click Revoke to invalidate tokens
3
Re-authorize
Run setup to get new tokens:
Token Security
Protecting Your Tokens
Never Expose Tokens
Never Expose Tokens
Tokens are credentials - protect them like passwords:✅ Do:
- Store in
.envfile (add to.gitignore) - Use environment variables in production
- Set file permissions:
chmod 600 .env - Use secret management in cloud deployments
- Commit to version control
- Share via email/chat
- Include in screenshots
- Log to console in production
- Hardcode in source files
Monitor Token Usage
Monitor Token Usage
Regular security checks:
- Review API usage in eBay Developer Portal
- Check for unexpected token refreshes
- Monitor for unusual activity patterns
- Set up alerts for rate limit spikes
The eBay Developer Portal shows detailed API usage by token.
Rotate Tokens Periodically
Rotate Tokens Periodically
Best practices for production:
- Re-authorize every 6-12 months
- Immediately revoke if compromised
- Test new tokens before switching
- Keep audit logs of rotations
- Run
npm run setupto get new tokens - Verify new tokens work
- (Optional) Revoke old tokens in eBay portal
- Update production deployment
Handle Token Compromise
Handle Token Compromise
If you suspect tokens are compromised:
- Immediately revoke in eBay Developer Portal
- Remove from
.envand any backups - Review recent API activity for unauthorized use
- Generate new tokens via
npm run setup - Update all deployments with new tokens
- Document the incident and response
Troubleshooting Token Issues
Common Problems
Token Refresh Fails
Token Refresh Fails
Symptoms:
- “Token refresh failed” errors in logs
- Server falls back to client credentials
- 401 Unauthorized responses
- Refresh token expired (>18 months inactive)
- Refresh token invalid or corrupted
- Client ID/Secret changed
- Network connectivity issues
- Re-run OAuth setup:
- Verify Client ID and Secret haven’t changed
- Check network connectivity to eBay APIs
- Review server logs for specific error messages
Token Expiry Keeps Resetting
Token Expiry Keeps Resetting
Symptoms:
- Token expiry is always in the past
- Constant refresh attempts
- Performance degradation
- System clock is incorrect
- Timezone mismatch
.envfile not being updated
- Check system time:
date - Ensure system clock is accurate
- Verify
.envfile permissions (must be writable) - Check disk space (file updates might fail)
.env File Not Updating
.env File Not Updating
Symptoms:
- Tokens refresh but
.envstill has old values - Manual edits to
.envget reverted - Token expiry timestamp doesn’t update
- File permissions issue
- Multiple instances running
.envfile locked by another process
- Check file permissions:
- Ensure only one instance is running
- Restart the MCP server
- Check for file system errors
Server Falls Back to Client Credentials
Server Falls Back to Client Credentials
Symptoms:
- Server starts with user tokens but switches to client credentials
- Limited API access after running
- Higher rate limit errors
- User tokens became invalid
- Token refresh failed
- Access token expired and refresh token missing
- Check server logs for token errors
- Verify all token variables in
.env: - Re-run OAuth setup if tokens are missing
- Ensure refresh token is valid
Invalid Token Format
Invalid Token Format
Symptoms:
- “Invalid token format” errors
- Token validation fails
- API calls return 401
- Token string got truncated
- Extra spaces or newlines in
.env - Token got corrupted during copy/paste
- Re-run OAuth setup to get fresh tokens:
- Check
.envfor formatting issues - Ensure no quotes around token values
- Verify no line breaks in token strings
Token Management Best Practices
For Development
- Local Development
- Team Development
Recommended setup:
- Use Sandbox environment
- Store tokens in
.envfile - Add
.envto.gitignore - Use interactive setup wizard
- Let automatic refresh handle tokens
.env:For Production
- Cloud Deployment
- Monitoring
- Disaster Recovery
Use environment variables, not
.env files:AWS:- AWS Secrets Manager
- Systems Manager Parameter Store
- Environment variables in ECS/Lambda
- Azure Key Vault
- App Service Configuration
- Secret Manager
- Cloud Run environment variables
Advanced Token Management
Token Caching
The MCP server caches access tokens in memory for performance: Benefits:- Reduces file I/O
- Faster API calls
- Less disk wear
- Access token cached on first load
- Cache invalidated on expiry
- New token cached after refresh
Restart the server to clear the token cache.
Preemptive Refresh
The server can refresh tokens before they expire: Configuration:- Prevents mid-request expiry
- Smoother operation
- Reduced API call failures
Multiple Environments
Managing tokens across environments: Structure:Next Steps
OAuth Setup
Learn how to set up OAuth tokens
Client Credentials
Understand the fallback authentication method
Rate Limits
Optimize your API usage
Troubleshooting
Solve common token issues