Skip to content

refactor(managers): O(1) tenant lookups and missing APIs#10

Merged
andylim-duo merged 5 commits intomainfrom
fix/issue-8-manager-followups
Mar 17, 2026
Merged

refactor(managers): O(1) tenant lookups and missing APIs#10
andylim-duo merged 5 commits intomainfrom
fix/issue-8-manager-followups

Conversation

@andylim-duo
Copy link
Copy Markdown
Owner

@andylim-duo andylim-duo commented Mar 17, 2026

Summary

Addresses all follow-up items from #8 for the tenant-scoped manager storage introduced in PR #5.

  • Nested dict storage: Convert ToolManager, ResourceManager, and PromptManager from flat dict[tuple[str | None, str], T] to nested dict[str | None, dict[str, T]], giving O(1) per-tenant lookups instead of O(n) scans across all tenants
  • Duplicate template warnings: ResourceManager.add_template() now warns on duplicates and returns the existing template, matching the behavior of add_resource() and ToolManager.add_tool()
  • Remove APIs: Add ResourceManager.remove_resource() and PromptManager.remove_prompt() for parity with ToolManager.remove_tool()
  • Thread-safety documentation: All three manager classes document they are not thread-safe and are designed for single-threaded async event loops
  • Test fixture: Centralize Context() construction in a make_context conftest fixture so tests won't break if the Context.__init__ signature changes
  • Test cleanup: Replace internal _templates dict access with public add_template() API, add type annotations for pyright compliance

Test plan

  • All 33 manager tests pass (pytest tests/server/mcpserver/)
  • Pyright reports 0 errors on changed test files
  • Coverage verified on all three manager source files

This PR closes #8

…missing APIs

Convert ToolManager, ResourceManager, and PromptManager from flat
tuple-keyed dicts to nested `{tenant_id: {name: T}}` dicts, giving O(1)
per-tenant lookups instead of O(n) scans. Add duplicate-warning support
for resource templates, add remove_resource and remove_prompt methods,
document thread-safety constraints, and centralize Context construction
in tests via a make_context fixture.

Github-Issue:#8
@andylim-duo andylim-duo self-assigned this Mar 17, 2026
@andylim-duo
Copy link
Copy Markdown
Owner Author

andylim-duo commented Mar 17, 2026

Review Notes

1. MakeContext type alias duplicated across test files ✅ Resolved

Resolved in cd16fd0 — moved MakeContext into conftest.py as the single source of truth. Both test files now import it from there.

2. Empty inner dicts accumulate after removes ✅ Resolved

Resolved in a6fa5c0 — all three remove methods (remove_tool, remove_resource, remove_prompt) now delete the inner dict from the outer storage when it becomes empty after the last entry is removed. Test assertions verify the cleanup.

Move the duplicated MakeContext type alias from test_multi_tenancy_managers.py
and test_resource_manager.py into conftest.py as the single source of truth.

Github-Issue:#8
Remove the inner dict from the outer storage when the last entry for a
tenant is deleted, preventing unbounded accumulation of empty dicts in
long-running servers with transient tenants.

Github-Issue:#8
Add tests for ResourceManager and PromptManager where a tenant has
multiple items and only one is removed, exercising the branch where the
scope dict persists. Fixes 100% branch coverage requirement in CI.

Github-Issue:#8
@andylim-duo andylim-duo merged commit ca281f8 into main Mar 17, 2026
26 checks passed
@andylim-duo andylim-duo deleted the fix/issue-8-manager-followups branch March 17, 2026 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Follow-up improvements from PR #5 (tenant-scoped manager storage)

1 participant