Skip to main content
Glama
test_mock_context.py2.02 kB
"""Mock Context for testing FastMCP Context state management.""" import uuid from typing import TYPE_CHECKING, Any, Protocol, cast if TYPE_CHECKING: from fastmcp import Context class ContextProtocol(Protocol): """Protocol defining the Context interface needed for type compatibility.""" @property def session_id(self) -> str: ... async def info(self, message: str) -> None: ... async def debug(self, message: str) -> None: ... async def error(self, message: str) -> None: ... async def warning(self, message: str) -> None: ... async def report_progress(self, progress: float) -> None: ... class MockContext: """Mock implementation of FastMCP Context for testing.""" def __init__( # type: ignore[explicit-any] # session_data is reaaaally amorphous self, session_id: str | None = None, session_data: dict[str, Any] | None = None ) -> None: self._session_id = session_id or uuid.uuid4().hex self._session_data = dict(session_data or {}) @property def session_id(self) -> str: """Return the session ID.""" return self._session_id async def info(self, message: str) -> None: """Mock info logging method.""" async def debug(self, message: str) -> None: """Mock debug logging method.""" async def error(self, message: str) -> None: """Mock error logging method.""" async def warning(self, message: str) -> None: """Mock warning logging method.""" async def report_progress(self, progress: float) -> None: """Mock progress reporting method.""" def create_mock_context( # type: ignore[explicit-any] # session_data is reaaaally amorphous session_id: str | None = None, session_data: dict[str, Any] | None = None, ) -> "Context": """Create a mock context with session data. Returns: MockContext cast to Context for type compatibility """ return cast("Context", MockContext(session_id=session_id, session_data=session_data))

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jonpspri/databeak'

If you have feedback or need assistance with the MCP directory API, please join our Discord server