[BUG]: UI - API Tokens - Last Used and Usage Stats not showing any data
🐞 Bug Summary
API Tokens Last Used and Usage Stats is not showing any data even though where used.
🧩 Affected Component
Select the area of the project impacted:
- [ ]
mcpgateway- API - [X]
mcpgateway- UI (admin panel) - [ ]
mcpgateway.wrapper- stdio wrapper - [ ] Federation or Transports
- [ ] CLI, Makefiles, or shell scripts
- [ ] Container setup (Docker/Podman/Compose)
- [ ] Other (explain below)
🔁 Steps to Reproduce
- Go to http://localhost:8000/admin/#tokens
- Check Last Used
- Click Usage Stats
🤔 Results
Last Used is showing always Never. Usage Stats is not showing any data.
🤔 Expected Behavior
Last Used should be updated each time the token is used. Usage Stats should show the correct data.
📓 Logs / Error Output
Paste any relevant stack traces or logs here. ⚠️ Do not paste secrets, credentials, or tokens.
🧠 Environment Info
You can retrieve most of this from the /version endpoint.
| Key | Value |
|---|---|
| Version or commit | v0.128.0 |
| Runtime | Python 3.12.3 |
| Platform / OS | Linux 6.6.87.2-microsoft-standard-WSL2 (x86_64) |
| Container | none |
🧩 Additional Context (optional)
Related with #2390
Thank you for the report.
Root cause identified:
-
last_usednot updating: API tokens created via/tokensare JWT tokens. When used, they pass JWT validation directly (auth.py:612) and the auth flow never reaches_lookup_api_token_sync()(auth.py:836) wherelast_usedis updated. The database lookup was designed as a fallback for non-JWT tokens, but all API tokens are JWTs. -
Usage Stats empty: The
log_token_usage()method that populatesTokenUsageLogis defined but never called from production code.
Proposed Fix:
- Add helper function in
auth.pyafter line 264:
def _update_api_token_last_used_sync(jti: str) -> None:
"""Update last_used timestamp for an API token."""
with fresh_db_session() as db:
from sqlalchemy import select
from mcpgateway.db import EmailApiToken, utc_now
result = db.execute(select(EmailApiToken).where(EmailApiToken.jti == jti))
api_token = result.scalar_one_or_none()
if api_token:
api_token.last_used = utc_now()
- Call from
_set_auth_method_from_payload()when API token is detected (~line 458):
if auth_provider == "api_token":
request.state.auth_method = "api_token"
jti = payload.get("jti")
if jti:
await asyncio.to_thread(_update_api_token_last_used_sync, jti)
return
-
Same for legacy API token branch (~line 472).
-
For Usage Stats: Add middleware that calls
log_token_usage()after request completion whenrequest.state.auth_method == "api_token".