Skip to content

API Reference parrot.mcp.context

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.mcp.context

View source

MCP Context Module.

Provides ReadonlyContext for context-aware tool access and MCPSessionManager for session lifecycle management with retry logic.

Attribute F

Class MCPSessionManager

Manages session lifecycle and retry logic for MCP connections.

Features:

  • Session caching keyed by headers (for multi-user scenarios)
  • Automatic retry with exponential backoff on transient failures
  • Configurable max retries
Parameter Type Default Description
config Any
max_retries int 3
base_wait float 2.0

Attributes:

Attribute Type Description
sessions Dict[int, Any]

Method MCPSessionManager.create_session

Create or retrieve cached session with retry.

create_session(headers: Optional[Dict[str, str]] = None, force_new: bool = False) -> Any

Parameter Type Default Description
headers Optional[Dict[str, str]] None Optional headers that affect session identity
force_new bool False If True, create new session even if cached

Returns: Any — MCP session object

Method MCPSessionManager.invalidate_session

Invalidate a cached session.

invalidate_session(headers: Optional[Dict[str, str]] = None) -> None

Parameter Type Default Description
headers Optional[Dict[str, str]] None

Method MCPSessionManager.invalidate_all

Invalidate all cached sessions.

invalidate_all() -> None

Class ReadonlyContext

Immutable context passed to tool operations.

This context provides information about the agent, user, and organizational context for tool execution. It enables:

  • Tool filtering based on user roles/scopes
  • Dynamic header generation
  • Multi-tenant isolation
  • Rate limiting by user/organization

Fields:

Field Type Default Description
agent_id str
user_id Optional[str] None
organization_id Optional[str] None
roles List[str] []
scopes List[str] []
conversation_id Optional[str] None
metadata Dict[str, Any] {}

Class TransientMCPError

Transient MCP errors that should be retried.

Use this for errors like connection timeouts, temporary server unavailability, rate limiting, etc. The retry_on_errors decorator will automatically retry operations that raise this exception.

Function retry_on_errors

Decorator for automatic retry on transient errors with exponential backoff.

retry_on_errors(max_retries: int = 3, base_wait: float = 2.0) -> Callable[[F], F]

Parameter Type Default Description
max_retries int 3 Maximum number of retry attempts (default: 3)
base_wait float 2.0 Base wait time in seconds for exponential backoff (default: 2.0)

Returns: Callable[[F], F] — Decorated async function with retry logic

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally