Add multi-tenancy TODO comments across MCP Python SDK codebase #1589
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Multi-Tenancy Code Review Summary
Overview
This PR adds
TODO: Multi-tenancycomments throughout the codebase to identify areas that need updates to support multi-tenancy. The review focused on code that maintains state between MCP server calls, configuration without tenant scoping, and client code that needs to specify tenant identifiers.Key Findings
🔴 Critical Issues
1. Shared State Managers
Files:
src/mcp/server/fastmcp/tools/tool_manager.pysrc/mcp/server/fastmcp/resources/resource_manager.pysrc/mcp/server/fastmcp/prompts/manager.pysrc/mcp/server/lowlevel/server.pyIssue: Tools, resources, and prompts are stored in shared dictionaries without tenant scoping. A tool/resource/prompt registered by Tenant A is accessible to Tenant B.
Required Changes:
tenant_idparameter to all manager methodsdict[tuple[tenant_id, name], Tool])2. Authentication & Authorization
Files:
src/mcp/server/auth/provider.pysrc/mcp/server/auth/middleware/auth_context.pyIssue:
AccessToken,RefreshToken, andAuthorizationCodelacktenant_idfields. Cannot enforce tenant-based access control.Required Changes:
tenant_idfield toAccessToken,RefreshToken, andAuthorizationCodeget_tenant_id()helper to extract tenant from auth contexttenant_idfrom token3. Session Management
Files:
src/mcp/server/session.pysrc/mcp/server/streamable_http_manager.pysrc/mcp/shared/context.pyIssue: Sessions and request contexts don't track
tenant_id. All requests processed in same global context.Required Changes:
tenant_idfield toServerSessiontenant_idfield toRequestContext_server_instancesby tenant inStreamableHTTPSessionManager(tenant_id, session_id)🟡 Medium Priority Issues
4. Configuration Management
Files:
src/mcp/server/fastmcp/server.pyIssue: Settings loaded from environment variables without tenant-specific overrides. Auth providers, event stores, and session managers are shared across tenants.
Required Changes:
tenant_idto prevent cross-tenant data leakage5. Client-Side Changes
Files:
src/mcp/client/session.pyIssue: Client doesn't track or send
tenant_idto server. Server cannot determine which tenant context to use.Required Changes:
tenant_idparameter toClientSession.__init__()tenant_idinInitializeRequestParamsor clientInfo metadatatenant_idwith each request (in metadata or headers)🔵 Testing Gaps
6. Test Coverage
Files:
tests/server/fastmcp/test_integration.pyIssue: No multi-tenant test scenarios.
Required Changes:
Add tests for:
Implementation Approaches
Option 1: Tenant-Scoped Storage (Recommended)
Pros: Simpler to implement, less architectural changes
Cons: Requires passing
tenant_idthrough all layersSteps:
tenant_idtoAccessToken,RefreshToken,AuthorizationCodetenant_idtoRequestContextandServerSession(tenant_id, resource_id)tenant_idfrom token in request handlerstenant_idOption 2: Tenant-Isolated Instances
Pros: Stronger isolation, cleaner separation
Cons: More complex, requires tenant registry/factory pattern
Steps:
Files Modified
Total: 12 source files + 1 test file
Server Core (4 files)
src/mcp/server/fastmcp/server.pysrc/mcp/server/lowlevel/server.pysrc/mcp/server/session.pysrc/mcp/server/streamable_http_manager.pyManagers (3 files)
src/mcp/server/fastmcp/resources/resource_manager.pysrc/mcp/server/fastmcp/tools/tool_manager.pysrc/mcp/server/fastmcp/prompts/manager.pyAuthentication (2 files)
src/mcp/server/auth/provider.pysrc/mcp/server/auth/middleware/auth_context.pyClient (1 file)
src/mcp/client/session.pyShared (1 file)
src/mcp/shared/context.pyTests (1 file)
tests/server/fastmcp/test_integration.pyHow to Find All TODOs
Search for
"TODO: Multi-tenancy"in the codebase to find all marked locations. Each TODO comment includes:Next Steps
tenant_idfields and update managersSecurity Considerations
Detailed TODO Locations
Authentication System
Session Management
State Managers
Client Integration