Skip to content

API Reference parrot.mcp.oauth2_state

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

parrot.mcp.oauth2_state

View source

Shared in-process state for MCP OAuth2 callback coordination.

When a user starts an OAuth2 authorization code flow, the MCP transport creates an asyncio.Event and places it in _pending_mcp_callbacks keyed by the OAuth2 state parameter. Once the Navigator callback route receives the redirect from the authorization server, it looks up the event by state and signals it so the transport can complete the token exchange.

Both the transport layer (parrot.mcp.transports.http) and the callback route (parrot.auth.oauth2_routes) import from this module so they share the same _pending_mcp_callbacks dict.

Note: This is an in-process dict; it does not survive restarts and is not safe for multi-process deployments. A Redis-backed alternative can replace it without changing the public API.

Function deregister_pending_callback

Remove a pending callback entry without signalling it.

Call this when the transport times out waiting for the callback, to prevent the abandoned state entry from accumulating in _pending_mcp_callbacks indefinitely.

deregister_pending_callback(state: str) -> None

Parameter Type Default Description
state str OAuth2 state parameter to remove.

Function is_pending

Return True if there is a pending callback for the given state.

is_pending(state: str) -> bool

Parameter Type Default Description
state str OAuth2 state parameter.

Returns: bool — True if the state is registered, False otherwise.

Function register_pending_callback

Register a pending OAuth2 callback for the given state parameter.

The transport layer calls this to get the event/result pair before opening the browser. The callback route calls :func:resolve_pending_callback when the code arrives.

register_pending_callback(state: str) -> Tuple[asyncio.Event, Dict[str, str]]

Parameter Type Default Description
state str OAuth2 state parameter (must be unique per flow).

Returns: Tuple[asyncio.Event, Dict[str, str]] — Tuple of (asyncio.Event, result_dict). The event is set when the callback is received; result_dict is populated with {"code": ..., "state": ...}.

Function resolve_pending_callback

Resolve a pending OAuth2 callback by signalling the event.

Called by the Navigator callback route after validating the incoming redirect. Pops the entry from the dict (preventing replay) and sets the event so the waiting transport coroutine can continue.

resolve_pending_callback(state: str, code: str) -> bool

Parameter Type Default Description
state str OAuth2 state parameter identifying the pending flow.
code str Authorization code from the authorization server.

Returns: bool — True if the callback was found and resolved, False if the state was unknown or already consumed.

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