"""A remote MCP call must not borrow another user's process environment.""" import asyncio from pathlib import Path def test_explicit_context_overrides_process_environment(monkeypatch, tmp_path): monkeypatch.setenv("SMETA_USER", "wrong-user") monkeypatch.setenv("SMETA_TAB", "wrong-tab") monkeypatch.setenv("SMETA_WORK_DIR", str(tmp_path / "wrong")) import smeta_mcp from mcp_context import ToolContext, bind_tool_context own = tmp_path / "alice" with bind_tool_context(ToolContext("alice", "alice-tab", own, "client-a", frozenset())): assert smeta_mcp._user_tab() == ("alice", "alice-tab") assert smeta_mcp._work_dir("alice", "alice-tab") == own assert smeta_mcp._user_tab() == ("wrong-user", "wrong-tab") def test_parallel_contexts_do_not_mix(monkeypatch, tmp_path): monkeypatch.setenv("SMETA_USER", "wrong-user") import smeta_mcp from mcp_context import ToolContext, bind_tool_context async def one(user): with bind_tool_context(ToolContext(user, "tab", Path(tmp_path / user), "client", frozenset())): await asyncio.sleep(0) return await asyncio.to_thread(smeta_mcp._user_tab) async def both(): return await asyncio.gather(one("alice"), one("bob")) assert asyncio.run(both()) == [ ("alice", "tab"), ("bob", "tab")]