Skip to content

Fix: Refresh teams list automatically after team creation#2780

Merged
crivetimihai merged 2 commits intomainfrom
fix/2690-team-creation-list-refresh
Feb 10, 2026
Merged

Fix: Refresh teams list automatically after team creation#2780
crivetimihai merged 2 commits intomainfrom
fix/2690-team-creation-list-refresh

Conversation

@shoummu1
Copy link
Copy Markdown
Collaborator

@shoummu1 shoummu1 commented Feb 9, 2026

🐛 Bug-fix PR

📌 Summary

With this fix, teams list now updates automatically after creating a new team, eliminating the need for manual page refresh.

Why: The HTMX response after team creation wasn't triggering a list refresh, causing users to see an outdated teams list until they manually refreshed the page. This created a confusing UX where the create operation succeeded but the new team wasn't visible.

🔗 Related Issue

Closes #2690

🐞 Root Cause

The backend returned custom HTML that was inserted via hx-swap="afterbegin", but this approach had two problems:

  1. No actual refresh triggered: The HX-Trigger: adminTeamAction header sent resetTeamCreateForm: true but NOT refreshUnifiedTeamsList: true, so the JavaScript refresh logic never executed
  2. HTML structure mismatch: Backend-generated HTML had a different structure than template-rendered teams (missing relationship badges, action buttons, filtering attributes)

Location: mcpgateway/admin.py:4065-4100 and mcpgateway/templates/admin.html:14136

💡 Fix Description

Implemented solution 2 from suggestion provided: "A trigger to refresh the teams table after successful creation"

Key changes:

  1. Backend (admin.py:4066-4068): Return empty 201 response instead of custom HTML

    response = HTMLResponse(content="", status_code=201)
    return response
  2. Frontend (admin.html:14137-14139): Changed form target to error container

    hx-target="#create-team-error"
    hx-swap="innerHTML"
  3. Existing handler (admin.html:14737-14742): Leverages already-present htmx:afterRequest listener that refreshes the list via initializeTeamManagement()

Design rationale:

  • Eliminates HTML duplication (single source of truth: teams_partial.html)
  • Reuses existing refresh infrastructure
  • Cleaner than hx-swap-oob approach (no full table rendering in response)
  • All teams render consistently via template with proper structure

🧪 Verification

Check Command Status
Lint suite make lint
Unit tests make test
Coverage ≥ 90 % make coverage
Manual regression no longer fails Tested via Admin UI - team appears immediately after creation without F5

Test updates:

  • Updated test_admin_create_team_success to expect empty 201 response

📐 MCP Compliance (if relevant)

  • Matches current MCP spec
  • No breaking change to MCP clients

✅ Checklist

  • Code formatted (make black isort pre-commit)
  • No secrets/credentials committed

@crivetimihai
Copy link
Copy Markdown
Member

Nice fix, @shoummu1! Great root cause analysis — the existing htmx:afterRequest handler was already the right mechanism, and the old code was just bypassing it by swapping inline HTML directly. Removing the duplicated server-side HTML generation (-40 lines) in favor of the full refresh via initializeTeamManagement() is the right call. Clean and minimal. LGTM!

Copy link
Copy Markdown
Collaborator

@gcgoncalves gcgoncalves left a comment

Choose a reason for hiding this comment

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

Tested locally and worked well. Great seeing a fix with +5 -40 lines!

Signed-off-by: Shoumi <shoumimukherjee@gmail.com>
Signed-off-by: Shoumi <shoumimukherjee@gmail.com>
@crivetimihai crivetimihai force-pushed the fix/2690-team-creation-list-refresh branch from baee585 to 6c5a453 Compare February 10, 2026 20:53
@crivetimihai
Copy link
Copy Markdown
Member

Code Review

Verdict: Correct and well-designed

Flow analysis:

  1. Form submits POST to /admin/teams
  2. Backend creates team, returns HTMLResponse(content="", status_code=201)
  3. HTMX swaps empty content into #create-team-error (clears any prior errors)
  4. Global htmx:afterRequest handler (admin.html:14756) catches status === 201 + elt.id === 'create-team-form'
  5. Calls closeCreateTeamModal() (hides modal + resets form)
  6. Calls initializeTeamManagement() (fetches /teams/partial and re-renders entire list)

Consistency: This pattern is consistent with how delete, leave, and cancel-join work (lines 14662-14680) — they all call initializeTeamManagement() after completion. The other team operations (edit, add/remove members) use the adminTeamAction HX-Trigger event system, but those are more complex operations with success HTML feedback. Using the afterRequest listener for create is simpler and appropriate.

Error handling: Error paths (validation, integrity, generic) all properly use HX-Retarget="#create-team-error" + HX-Reswap="innerHTML", which is now redundant (form already targets that div), but harmless and defensive.

No issues found

  • Security: No issues. Removes 30+ lines of server-side HTML generation (which had html.escape but was still a risk surface). The new approach delegates rendering entirely to Jinja templates via teams_partial.html.
  • Performance: Slightly better — one fewer response payload (empty vs. HTML blob). The full list refresh is the same cost as before (since the old code never triggered it properly).
  • Logic: Correct. The afterRequest check for status === 201 is precise enough since only successful creation returns 201.
  • Test: Updated correctly — asserts empty body and 201 status.

Minor observation (not a blocker)

The removed code previously triggered resetTeamCreateForm via the adminTeamAction HX-Trigger header. The resetTeamCreateForm() function still exists in admin.js (line 21294) and is still referenced in handleAdminTeamAction (line 21852). It's not dead code — other parts of the system could still trigger adminTeamAction with resetTeamCreateForm: true — but since team creation no longer sends that trigger, you may want to clean it up in a follow-up if nothing else uses it.

Net change: +5 / -40 lines — a good simplification.

@crivetimihai crivetimihai merged commit 29b2742 into main Feb 10, 2026
51 checks passed
@crivetimihai crivetimihai deleted the fix/2690-team-creation-list-refresh branch February 10, 2026 21:20
kcostell06 pushed a commit to kcostell06/mcp-context-forge that referenced this pull request Feb 24, 2026
* fix: refresh teams list automatically after team creation

Signed-off-by: Shoumi <shoumimukherjee@gmail.com>

* test fixes

Signed-off-by: Shoumi <shoumimukherjee@gmail.com>

---------

Signed-off-by: Shoumi <shoumimukherjee@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ui User Interface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Teams - Newly created team is not displayed until the page is refreshed

3 participants