Skip to content

Add pagination for token listing - #2833

Merged
crivetimihai merged 1 commit into
mainfrom
token-pagination
Feb 13, 2026
Merged

crivetimihai merged 1 commit into
mainfrom
token-pagination

Conversation

@madhav165

@madhav165 madhav165 commented Feb 11, 2026 •

Copy link
Copy Markdown
Contributor

✨ Feature / Enhancement PR

🔗 Epic / Issue

Closes #2764


Summary

  • Add server-side paginated HTMX endpoint (GET /admin/tokens/partial) for the tokens tab, matching the existing pattern used by teams, tools, and other admin tables
  • Extract token listing into a dedicated tokens_partial.html template with OOB pagination controls
  • Fix token list not auto-refreshing after create (clicking "I've Saved It") or revoke — switched from htmx.ajax() to element-based htmx.trigger() which properly processes OOB swaps
  • Add count_user_tokens() to TokenCatalogService and return accurate total in the REST API's TokenListResponse

Changes

Backend

  • mcpgateway/admin.py: New GET /admin/tokens/partial endpoint with pagination, team filtering, inactive filtering, and render=controls mode
  • mcpgateway/services/token_catalog_service.py: Added count_user_tokens() for accurate total counts
  • mcpgateway/routers/tokens.py: Use count_user_tokens() in list endpoint so total reflects all matching tokens, not just the current page

Frontend

  • mcpgateway/templates/tokens_partial.html: New partial template (bare cards + OOB pagination div), follows the same pattern as teams_partial.html
  • mcpgateway/templates/admin.html: Tokens table uses innerHTML swap with refreshTokens custom trigger; show-inactive checkbox also uses innerHTML
  • mcpgateway/static/admin.js: loadTokensList() sets hx-get on #tokens-table and fires htmx.trigger(el, "refreshTokens") instead of htmx.ajax(); modal dismiss triggers list refresh

Tests

  • tests/unit/mcpgateway/routers/test_tokens.py: Added test for count_user_tokens

🧪 Checks

  • make lint passes
  • make test passes
  • CHANGELOG updated (if user-facing)

@madhav165
madhav165 marked this pull request as draft February 11, 2026 08:30
@crivetimihai crivetimihai added this to the Release 1.0.0-GA milestone Feb 11, 2026
@madhav165
madhav165 force-pushed the token-pagination branch 4 times, most recently from 3015bb3 to d224d1e Compare February 13, 2026 06:52
@madhav165
madhav165 marked this pull request as ready for review February 13, 2026 08:23

@crivetimihai crivetimihai left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR, @madhav165! The pagination implementation is solid — correct total_count via separate count queries, good use of paginate_query in the admin partial, and the team visibility change is well-justified. A few items:

1. SQL wildcard injection via ilike (medium severity)
admin.py new code: EmailApiToken.name.ilike(f"%{q}%") — the q parameter is passed directly without escaping LIKE wildcards (%, _). A user searching for % or _ gets unintended broad matches. The codebase already has _escape_like() in email_auth_service.py:709-718. Please sanitize q the same way:

safe_q = q.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")

2. list_all_tokens admin endpoint: total_count stays 0 when user_email is None
The else branch sets tokens = [] and total_count remains 0. This means the admin endpoint cannot paginate all tokens — it returns total: 0 when no user_email filter is provided.

3. N+1 revocation queries in API endpoints
You added get_token_revocations_batch and use it in admin_tokens_partial_html and admin_search_tokens — great improvement. But list_tokens, list_all_tokens, and list_team_tokens still call get_token_revocation per token in a loop. Consider using the batch method there too.

@crivetimihai crivetimihai self-assigned this Feb 13, 2026
@crivetimihai crivetimihai added the ica ICA related issues label Feb 13, 2026
- Add server-side paginated HTMX endpoint (GET /admin/tokens/partial)
- Extract token listing into tokens_partial.html template with OOB pagination
- Fix token list auto-refresh after create/revoke
- Add count_user_tokens(), list_user_and_team_tokens() to TokenCatalogService
- Add /tokens/search API for server-side token search by name
- Add alembic migration for token permissions on team_admin role
- Batch fetch revocation info to eliminate N+1 queries
- Allow team admins to view and manage team tokens

Closes #2764

Signed-off-by: Madhav Kandukuri <[email protected]>
Signed-off-by: Mihai Criveti <[email protected]>
@crivetimihai

Copy link
Copy Markdown
Member

Rebase & Review Notes

Rebased onto main and squashed 23 commits into a single clean commit. Applied the following fixes during rebase:

Fixes applied

  1. Alembic migration down_revision: Updated from c1c2c3c4c5c6 to 207d8bbddd61 (current main head after the gateway_mode PR was merged). Without this fix, alembic heads would show multiple heads and all migrations would fail.

  2. Removed dead displayTokensList() function from admin.js (~105 lines): This function was never called — the old #tokens-list DOM element no longer exists (replaced by HTMX-driven #tokens-table). The eslint-disable comment claimed it was "Called from admin.html" but it wasn't.

  3. Fixed stale docstring in routers/tokens.py for list_team_tokens: Said "only team owners can do this" / "must be team owner", but the underlying service was changed to allow any active team member. Updated to "requires active team membership" / "must be an active member of the team" to match actual behavior.

Review summary

Reviewed for security, performance, correctness, and consistency with existing pagination patterns (teams, tools, servers, prompts, a2a). All looks good:

  • Security: Clean. revoke_token team-owner fallback is properly gated, LIKE wildcards are escaped, token data uses explicit allowlist (no hash/JTI leakage), allow_admin_bypass=False is consistent with all other admin endpoints.
  • Performance: N+1 query elimination via get_token_revocations_batch() is a nice improvement across all token list endpoints.
  • Consistency: Follows the established *_partial.html + OOB pagination + HTMX trigger pattern used by other admin sections.
  • Tests: 932 tests pass.

@crivetimihai
crivetimihai merged commit 8120da6 into main Feb 13, 2026
47 checks passed
@crivetimihai
crivetimihai deleted the token-pagination branch February 13, 2026 22:17
suciu-daniel pushed a commit that referenced this pull request Feb 16, 2026
- Add server-side paginated HTMX endpoint (GET /admin/tokens/partial)
- Extract token listing into tokens_partial.html template with OOB pagination
- Fix token list auto-refresh after create/revoke
- Add count_user_tokens(), list_user_and_team_tokens() to TokenCatalogService
- Add /tokens/search API for server-side token search by name
- Add alembic migration for token permissions on team_admin role
- Batch fetch revocation info to eliminate N+1 queries
- Allow team admins to view and manage team tokens

Closes #2764

Signed-off-by: Madhav Kandukuri <[email protected]>
Signed-off-by: Mihai Criveti <[email protected]>
Co-authored-by: Mihai Criveti <[email protected]>
vishu-bh pushed a commit that referenced this pull request Feb 18, 2026
- Add server-side paginated HTMX endpoint (GET /admin/tokens/partial)
- Extract token listing into tokens_partial.html template with OOB pagination
- Fix token list auto-refresh after create/revoke
- Add count_user_tokens(), list_user_and_team_tokens() to TokenCatalogService
- Add /tokens/search API for server-side token search by name
- Add alembic migration for token permissions on team_admin role
- Batch fetch revocation info to eliminate N+1 queries
- Allow team admins to view and manage team tokens

Closes #2764

Signed-off-by: Madhav Kandukuri <[email protected]>
Signed-off-by: Mihai Criveti <[email protected]>
Co-authored-by: Mihai Criveti <[email protected]>
Signed-off-by: Vishu Bhatnagar <[email protected]>
kcostell06 pushed a commit to kcostell06/mcp-context-forge that referenced this pull request Feb 24, 2026
- Add server-side paginated HTMX endpoint (GET /admin/tokens/partial)
- Extract token listing into tokens_partial.html template with OOB pagination
- Fix token list auto-refresh after create/revoke
- Add count_user_tokens(), list_user_and_team_tokens() to TokenCatalogService
- Add /tokens/search API for server-side token search by name
- Add alembic migration for token permissions on team_admin role
- Batch fetch revocation info to eliminate N+1 queries
- Allow team admins to view and manage team tokens

Closes IBM#2764

Signed-off-by: Madhav Kandukuri <[email protected]>
Signed-off-by: Mihai Criveti <[email protected]>
Co-authored-by: Mihai Criveti <[email protected]>
brian-hussey pushed a commit to brian-hussey/mcp-context-forge that referenced this pull request Oct 20, 2026
- Add server-side paginated HTMX endpoint (GET /admin/tokens/partial)
- Extract token listing into tokens_partial.html template with OOB pagination
- Fix token list auto-refresh after create/revoke
- Add count_user_tokens(), list_user_and_team_tokens() to TokenCatalogService
- Add /tokens/search API for server-side token search by name
- Add alembic migration for token permissions on team_admin role
- Batch fetch revocation info to eliminate N+1 queries
- Allow team admins to view and manage team tokens

Closes IBM#2764

Signed-off-by: Madhav Kandukuri <[email protected]>
Signed-off-by: Mihai Criveti <[email protected]>
Co-authored-by: Mihai Criveti <[email protected]>
Signed-off-by: Vishu Bhatnagar <[email protected]>
brian-hussey pushed a commit to brian-hussey/mcp-context-forge that referenced this pull request Oct 20, 2026
- Add server-side paginated HTMX endpoint (GET /admin/tokens/partial)
- Extract token listing into tokens_partial.html template with OOB pagination
- Fix token list auto-refresh after create/revoke
- Add count_user_tokens(), list_user_and_team_tokens() to TokenCatalogService
- Add /tokens/search API for server-side token search by name
- Add alembic migration for token permissions on team_admin role
- Batch fetch revocation info to eliminate N+1 queries
- Allow team admins to view and manage team tokens

Closes IBM#2764

Signed-off-by: Madhav Kandukuri <[email protected]>
Signed-off-by: Mihai Criveti <[email protected]>
Co-authored-by: Mihai Criveti <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ica ICA related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][UI]: API Tokens page missing pagination and team filter not updating

2 participants