Integrate tenant_id into MCPServer server layer, completing the plumbing
from request context through to tenant-scoped storage in managers.
- Add tenant_id property to Context class, reading from request context
- Update all 7 private _handle_* methods to pass ctx.tenant_id to public methods
- Add keyword-only tenant_id parameter to all public methods (list_tools,
call_tool, list_resources, read_resource, list_resource_templates,
list_prompts, get_prompt, add_tool, remove_tool, add_resource, add_prompt)
- All new parameters default to None for full backward compatibility
This is iteration 4 of 6 in the multi-tenancy implementation plan.
Summary
Integrates
tenant_idinto the MCPServer server layer (iteration 4 of 6 in the multi-tenancy implementation plan), completing the plumbing from request context through to tenant-scoped storage in managers.Context: Multi-Tenancy Implementation Plan
This is part of a 6-iteration plan to add multi-tenant support to the MCP Python SDK:
tenant_idto authentication tokens (done, PR feat(auth): add tenant_id field to authentication token models #2)tenant_idto session and request context (done, PR feat(auth): add tenant_id to session and request context #3)tenant_idfrom handlers to managers (this PR)tenant_id, partition sessionsChanges
src/mcp/server/mcpserver/context.py: Addtenant_idproperty toContextclass that reads from the underlyingServerRequestContext. Also threadtenant_idthrough theread_resourceconvenience method.src/mcp/server/mcpserver/server.py:_handle_*methods to passctx.tenant_idto their corresponding public methodstenant_id: str | None = Noneparameter to all public query methods (list_tools,call_tool,list_resources,read_resource,list_resource_templates,list_prompts,get_prompt)tenant_idparameter to mutation methods (add_tool,remove_tool,add_resource,add_prompt)None, preserving full backward compatibilitytests/server/mcpserver/test_multi_tenancy_server.py(new): 13 tests coveringContext.tenant_idproperty, tenant-scoped operations across all public methods, and backward compatibilityBackward Compatibility
All changes are fully backward compatible. Every new
tenant_idparameter defaults toNone, which maps to the global scope — identical to the pre-existing single-tenant behavior. Existing tests (386 total in the MCPServer + client suites) pass without modification.Test plan
test_multi_tenancy_server.pyall passpyrighttype checking passes with 0 errorsruffformatting and linting pass