Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748"""Tests for session-based task ID isolation (CRITICAL SECURITY).
Ensures that tasks are properly scoped to sessions and clients cannotaccess each other's tasks."""
import pytest
from fastmcp import FastMCPfrom fastmcp.client import Client
@pytest.fixtureasync def task_server(): """Create a server with background tasks enabled.""" mcp = FastMCP("security-test-server")
@mcp.tool(task=True) # Enable background execution async def secret_tool(data: str) -> str: """A tool that processes sensitive data.""" return f"Secret result: {data}"
return mcp
async def test_same_session_can_access_all_its_tasks(task_server): """A single session can access all tasks it created.""" async with Client(task_server) as client: # Submit multiple tasks task1 = await client.call_tool( "secret_tool", {"data": "first"}, task=True, task_id="task-1" ) task2 = await client.call_tool( "secret_tool", {"data": "second"}, task=True, task_id="task-2" )
# Wait for both to complete await task1.wait(timeout=2.0) await task2.wait(timeout=2.0)
# Should be able to access both result1 = await task1.result() result2 = await task2.result()
assert "first" in str(result1.data) assert "second" in str(result2.data)