Skip to content

Agents, Sessions & Workflows API

API reference for litestar_mcp.agent: declarative Agent and AgentGroup specs, AgentRuntime and TurnBudget, AgentChatController, pluggable SessionStore implementations (MemorySessionStore, LitestarStoreSessionStore), model adapters (ModelClient, GoogleGenAIClient, MockModelClient), SSE AgentStreamFrame streaming, and in-process DynamicWorkflow / WorkflowEngine DAG execution.

For guides and patterns, see Agent Runtime & Turn Budgets, Agent Groups & Handoffs, Sessions & SSE Streaming, and Durable DAG Workflows.


Agent & Group Specifications

Agent

Agent definition with instructions, tools, skills, and an optional model binding.

Parameters:

Name Type Description Default
name str

Unique identifier of the agent within an application or AgentGroup.

required
description str

Human-readable summary of the agent's purpose and capabilities.

''
instructions str

Base system instructions guiding the agent's behavior.

''
tools Sequence[Tool | Callable[..., Any]]

Tools or plain callables exposed directly to the agent.

()
skills Sequence[SkillController | type[SkillController]]

SkillController classes or instances contributing tools and instructions.

()
model ModelClient | str | None

Bound ModelClient instance or model identifier string.

None

Attributes:

Name Type Description
namestr

Unique identifier of the agent within an application or AgentGroup.

descriptionstr

Human-readable summary of the agent's purpose and capabilities.

instructionsstr

Base system instructions guiding the agent's behavior.

toolsSequence[Tool | Callable[..., Any]]

Tools or plain callables exposed directly to the agent.

skillsSequence[SkillController | type[SkillController]]

SkillController classes or instances contributing tools and instructions.

modelModelClient | str | None

Bound ModelClient instance or model identifier string.

tool_settuple[Tool, ...]

Return the agent's own tools followed by skill tools.

skill_instancestuple[SkillController, ...]

Return the instantiated skill controllers attached to this agent.

combined_instructionsstr

Return agent instructions joined with skill instructions by blank lines.

AgentGroup

Coordinator and specialist agents with a group-owned transfer_to_agent tool.

Attributes:

Name Type Description
specialistsdict[str, Agent]
Default: {s.name: s for s in specialists}

tools_for

tools_for(agent)

Return tools available to an agent in this group.

get_agent

get_agent(name)

Retrieve an agent by name from coordinator or specialist roster.

resolve_transfer

resolve_transfer(call)

Resolve a tool call into an agent transfer target if applicable.

AgentMessage

Bases: Struct

Provider-neutral conversation message.

Parameters:

Name Type Description Default
role Literal['system', 'user', 'assistant', 'tool']

Message author role (system, user, assistant, or tool).

required
content str

Primary text content of the message.

''
tool_calls list[ToolCall]

Built-in mutable sequence.

If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

<dynamic>
tool_results list[ToolResult]

Built-in mutable sequence.

If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

<dynamic>
agent_name str | None

Optional name of the agent that produced or handled this message.

None

Attributes:

Name Type Description
roleLiteral['system', 'user', 'assistant', 'tool']

Message author role (system, user, assistant, or tool).

contentstr

Primary text content of the message.

tool_callslist[ToolCall]

Tool invocations requested by the assistant in this message.

tool_resultslist[ToolResult]

Tool execution results returned in a tool role message.

agent_namestr | None

Optional name of the agent that produced or handled this message.


Agent Runtime & Turn Budgets

AgentRuntime

Bases: InitPluginProtocol

Runs agent turns with budgets, owner-scoped sessions, and streamed frames.

Register the runtime in Litestar(plugins=[...]) to provide it as the runtime dependency used by AgentChatController.

on_app_init

on_app_init(app_config)

Provide this runtime as the application's runtime dependency.

Returns:

Type Description
AppConfig

The updated application configuration.

get_history

get_history(context, session_id)

Return conversation history scoped to connection owner.

produce

produce(turn, send)

Execute turn loop as a producer coroutine emitting frames to the send stream.

open_stream

open_stream(turn)

Open a background task group streaming frames to an in-memory receive stream.

stream

stream(turn)

Yield stream frames sequentially from open_stream.

run_turn

run_turn(turn)

Run turn to completion and return TurnResponse.

TurnRequest

Input parameters for one agent turn execution.

Parameters:

Name Type Description Default
user_message str

User prompt text initiating the agent turn.

''
session_id str | None

Optional session identifier to resume an existing conversation.

None
turn_id str | None

Optional caller-supplied identifier for the turn.

None
dynamic_context str | None

Optional per-turn context prepended to the active agent's instructions.

None
context ToolContext | None

Request-scoped tool execution and principal ownership context.

None

Attributes:

Name Type Description
user_messagestr

User prompt text initiating the agent turn.

session_idstr | None

Optional session identifier to resume an existing conversation.

turn_idstr | None

Optional caller-supplied identifier for the turn.

dynamic_contextstr | None

Optional per-turn context prepended to the active agent's instructions.

contextToolContext | None

Request-scoped tool execution and principal ownership context.

TurnResponse

Aggregated result of a completed agent turn.

Parameters:

Name Type Description Default
session_id str

Session identifier under which the turn history was persisted.

required
turn_id str

Unique identifier for the completed turn.

required
output str

Concatenated assistant text output produced across the turn.

required
agent_name str

Name of the active agent at the end of the turn.

required
messages list[AgentMessage]

Built-in mutable sequence.

If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

<dynamic>
token_usage dict[str, int]

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)

<class 'dict'>

Attributes:

Name Type Description
session_idstr

Session identifier under which the turn history was persisted.

turn_idstr

Unique identifier for the completed turn.

outputstr

Concatenated assistant text output produced across the turn.

agent_namestr

Name of the active agent at the end of the turn.

messageslist[AgentMessage]

Full conversation history including the newly appended turn messages.

token_usagedict[str, int]

Aggregated token usage counts for prompt, completion, and thought tokens.

contentstr

Alias for output text produced by the turn.

TurnBudget

Configurable execution limits for a single agent turn.

Parameters:

Name Type Description Default
max_turns int

Maximum number of model-invocation iterations allowed in a single turn.

15
max_tool_calls int

Maximum total tool calls allowed across all iterations of a single turn.

50
timeout_seconds float

Wall-clock timeout in seconds for the entire turn execution.

300.0

Attributes:

Name Type Description
max_turnsint

Maximum number of model-invocation iterations allowed in a single turn.

max_tool_callsint

Maximum total tool calls allowed across all iterations of a single turn.

timeout_secondsfloat

Wall-clock timeout in seconds for the entire turn execution.

BudgetExceededError

Bases: LitestarMCPError

Raised when an agent turn exceeds its configured turn, tool-call, or timeout budget.


HTTP & SSE Chat Controller

AgentChatController

Bases: Controller

SSE chat, synchronous turns, and owner-scoped history over an injected AgentRuntime.

Attributes:

Name Type Description
ping_intervalfloat
Default: 15.0

chat

chat(request, data, runtime)

Stream an agent turn over Server-Sent Events from a typed chat payload.

create_turn

create_turn(request, data, runtime)

Execute an agent turn synchronously returning final TurnResult.

get_session

get_session(request, session_id, runtime)

Return conversation history for a session scoped to caller identity.

ChatRequest

Bases: Struct

Body payload for agent chat streaming and turn creation.

Parameters:

Name Type Description Default
message str

User prompt text for the agent turn.

required
session_id str | None

Optional existing session identifier to continue a conversation.

None
dynamic_context str | None

Optional per-turn context prepended to the agent's system instructions.

None

Attributes:

Name Type Description
messagestr

User prompt text for the agent turn.

session_idstr | None

Optional existing session identifier to continue a conversation.

dynamic_contextstr | None

Optional per-turn context prepended to the agent's system instructions.

TurnResult

Bases: Struct

Aggregated turn result returned by POST /agent/turns.

Parameters:

Name Type Description Default
session_id str

Identifier of the conversation session for this turn.

required
turn_id str

Unique identifier assigned to this turn execution.

required
agent_name str

Name of the agent that produced the final turn output.

required
output str

Concatenated assistant text output produced during the turn.

required
messages list[AgentMessage]

Updated conversation message history after the turn completes.

required
token_usage dict[str, int]

Accumulated token usage counts keyed by token category.

required

Attributes:

Name Type Description
session_idstr

Identifier of the conversation session for this turn.

turn_idstr

Unique identifier assigned to this turn execution.

agent_namestr

Name of the agent that produced the final turn output.

outputstr

Concatenated assistant text output produced during the turn.

messageslist[AgentMessage]

Updated conversation message history after the turn completes.

token_usagedict[str, int]

Accumulated token usage counts keyed by token category.

SessionHistory

Bases: Struct

Conversation history payload returned by GET /agent/sessions/{session_id}.

Parameters:

Name Type Description Default
session_id str

Identifier of the retrieved conversation session.

required
messages list[AgentMessage]

Ordered list of messages stored in the session.

required

Attributes:

Name Type Description
session_idstr

Identifier of the retrieved conversation session.

messageslist[AgentMessage]

Ordered list of messages stored in the session.

SessionNotFoundError

Bases: NotFoundException

Exception raised when a requested session is not found.


Session Stores

SessionStore

Bases: Protocol

Persist conversation history keyed by owner and session id.

load

load(owner, session_id)

Load messages for an owner and session id, returning None if nonexistent.

save

save(owner, session_id, messages)

Persist messages for an owner and session id.

lock

lock(owner, session_id)

Acquire a per-session lock context manager.

MemorySessionStore

Process-local LRU session store with TTL expiry and per-session locks.

lock

lock(owner, session_id)

Acquire per-session lock.

load

load(owner, session_id)

Load messages if entry exists and has not expired.

save

save(owner, session_id, messages)

Store messages and evict oldest when capacity exceeded.

LitestarStoreSessionStore

Session store over a Litestar Store backend.

lock

lock(owner, session_id)

Acquire per-session lock.

load

load(owner, session_id)

Load and decode conversation messages from storage backend.

save

save(owner, session_id, messages)

Encode and persist conversation messages to storage backend.


Model Clients & Streaming Frames

ModelClient

Bases: Protocol

Asynchronous protocol implemented by all LLM model adapters.

stream_turn

stream_turn(*, system_instruction, messages, tools)

Stream an agent turn yielding text, thoughts, tool calls, and usage deltas.

GoogleGenAIClient

First-party Google GenAI model client wrapping google.genai with httpx2 support.

Attributes:

Name Type Description
provider_namestr

Return canonical OpenTelemetry provider name.

stream_turn

stream_turn(*, system_instruction, messages, tools)

Stream an agent turn using Google GenAI SDK.

MockModelClient

Deterministic scriptable model client for fast offline unit tests.

Attributes:

Name Type Description
responseslist[list[ModelDelta]]
Default: responses or []
call_historylist[dict[str, Any]]
Default: []

stream_turn

stream_turn(*, system_instruction, messages, tools)

Record turn inputs and yield queued scripted responses.

ModelDelta

Incremental chunk emitted by a ModelClient during an agent turn.

Parameters:

Name Type Description Default
event_type ModelEventType

Kind of incremental model event (delta, thought, tool_call, or usage).

required
text str | None

Incremental assistant text or thought content.

None
thought str | None

Reasoning or thinking trace text when emitted by a thinking model.

None
tool_name str | None

Name of the tool requested by the model for a tool_call event.

None
call_id str | None

Unique identifier correlating a tool call with its result.

None
arguments dict[str, Any] | None

Decoded JSON arguments mapping passed to the requested tool.

None
prompt_tokens int | None

Input prompt token count reported in a usage event.

None
completion_tokens int | None

Output completion token count reported in a usage event.

None
provider_metadata dict[str, dict[str, Any]]

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)

<class 'dict'>
thought_tokens int | None

Reasoning token count reported in a usage event.

None

Attributes:

Name Type Description
event_typeModelEventType

Kind of incremental model event (delta, thought, tool_call, or usage).

textstr | None

Incremental assistant text or thought content.

thoughtstr | None

Reasoning or thinking trace text when emitted by a thinking model.

tool_namestr | None

Name of the tool requested by the model for a tool_call event.

call_idstr | None

Unique identifier correlating a tool call with its result.

argumentsdict[str, Any] | None

Decoded JSON arguments mapping passed to the requested tool.

prompt_tokensint | None

Input prompt token count reported in a usage event.

completion_tokensint | None

Output completion token count reported in a usage event.

provider_metadatadict[str, dict[str, Any]]

Provider-specific metadata such as call identifiers and thought signatures.

thought_tokensint | None

Reasoning token count reported in a usage event.

AgentStreamFrame

Bases: Struct

Typed SSE frame emitted during agent execution.

Parameters:

Name Type Description Default
event_type Literal['session', 'thought', 'delta', 'tool_call', 'tool_result', 'agent_transfer', 'ping', 'complete', 'error']

Frame event discriminator (session, thought, delta, tool_call, tool_result, agent_transfer, ping, complete, or error).

required
turn_id str

Unique identifier of the turn that emitted this frame.

required
seq int

Monotonically increasing 1-based sequence number within the turn stream.

0
agent_name str | None

Name of the active agent associated with this frame.

None
text str | None

Incremental text chunk for delta or thought frames.

None
tool_name str | None

Tool name for tool_call and tool_result frames.

None
call_id str | None

Tool call correlation identifier for tool_call and tool_result frames.

None
payload dict[str, Any] | None

Structured event payload for session, tool, transfer, completion, or error frames.

None

Attributes:

Name Type Description
event_typeStreamEventType

Frame event discriminator (session, thought, delta, tool_call, tool_result, agent_transfer, ping, complete, or error).

turn_idstr

Unique identifier of the turn that emitted this frame.

seqint

Monotonically increasing 1-based sequence number within the turn stream.

agent_namestr | None

Name of the active agent associated with this frame.

textstr | None

Incremental text chunk for delta or thought frames.

tool_namestr | None

Tool name for tool_call and tool_result frames.

call_idstr | None

Tool call correlation identifier for tool_call and tool_result frames.

payloaddict[str, Any] | None

Structured event payload for session, tool, transfer, completion, or error frames.

to_sse_message

to_sse_message(type_encoders=None)

Encode as an SSE message whose id is seq when non-zero and whose event is event_type.

from_sse

from_sse(event)

Decode an SSE event, taking seq from the event id when the payload omits it.

iter_agent_stream

iter_agent_stream(response, *, max_event_size=1048576)

Consume a streaming httpx2.Response using httpx2.EventSource and yield AgentStreamFrame objects.


DAG Workflows (DynamicWorkflow & WorkflowEngine)

DynamicWorkflow

DAG-based workflow connecting agents and discrete task handlers.

Attributes:

Name Type Description
nodesdict[str, WorkflowNode]
Default: {}

add_node

add_node(
    name_or_node,
    *,
    agent=None,
    handler=None,
    depends_on=None,
    dependencies=None,
    prompt=None,
    timeout_seconds=300.0,
    condition=None,
)

Add a step to the workflow with explicit dependencies.

get_ready_nodes

get_ready_nodes(completed)

Return names of all nodes whose dependencies are satisfied and not yet completed.

WorkflowNode

A single execution node within a DynamicWorkflow DAG.

Parameters:

Name Type Description Default
name str

Unique node name within the workflow DAG.

required
agent Agent | None

Optional Agent executed when this node runs.

None
handler Callable[..., Any] | Agent | None

Optional synchronous or asynchronous callable accepting WorkflowContext.

None
depends_on tuple[str, ...] | Sequence[str]

Names of upstream nodes that must complete before this node runs.

()
prompt Callable[list, str] | None

Optional callable building the prompt string for an agent node from WorkflowContext.

None
timeout_seconds float

Maximum execution time in seconds allowed for this node.

300.0
condition Callable[list, bool] | None

Optional predicate evaluated against WorkflowContext; when false, the node is skipped.

None

Attributes:

Name Type Description
namestr

Unique node name within the workflow DAG.

agentAgent | None

Optional Agent executed when this node runs.

handlerCallable[..., Any] | Agent | None

Optional synchronous or asynchronous callable accepting WorkflowContext.

depends_ontuple[str, ...] | Sequence[str]

Names of upstream nodes that must complete before this node runs.

promptCallable[[WorkflowContext], str] | None

Optional callable building the prompt string for an agent node from WorkflowContext.

timeout_secondsfloat

Maximum execution time in seconds allowed for this node.

conditionCallable[[WorkflowContext], bool] | None

Optional predicate evaluated against WorkflowContext; when false, the node is skipped.

dependencieslist[str]

Backward-compatible list representation of node dependencies.

WorkflowEngine

Concurrent in-process DAG execution engine for DynamicWorkflow pipelines.

execute_node

execute_node(workflow, node_name, context_or_state)

Execute a single workflow node against current workflow context or state.

validate_dependencies

validate_dependencies(workflow)

Validate that all node dependencies exist and graph is acyclic.

execute_run

execute_run(workflow, initial_state=None)

Execute workflow in DAG topological order via ready queue scheduling.

run

run(workflow, initial_state=None)

Execute the workflow to completion and return the final state dictionary.

WorkflowContext

Execution context tracking state and step outputs across a workflow run.

Parameters:

Name Type Description Default
run_id str

Unique identifier for the workflow execution run.

'32ea9688df6f47c9b049732194fc8c1b'
state dict[str, Any]

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)

<class 'dict'>
node_outputs dict[str, Any]

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)

<class 'dict'>
step_results dict[str, StepResult]

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)

<class 'dict'>

Attributes:

Name Type Description
run_idstr

Unique identifier for the workflow execution run.

statedict[str, Any]

Mutable shared state dictionary accessible to all workflow nodes.

node_outputsdict[str, Any]

Mapping of completed node names to their raw output values.

step_resultsdict[str, StepResult]

Mapping of executed node names to their StepResult records.

get

get(key, default=None)

Retrieve a value from workflow state or node outputs.

StepResult

Result record for a single executed workflow step.

Parameters:

Name Type Description Default
node_name str

Name of the workflow node that produced this result.

required
output Any

Value returned by the node handler or agent turn.

required
status Literal['completed', 'skipped', 'failed']

Terminal execution status of the step (completed, skipped, or failed).

'completed'
error str | None

Optional error message when status is failed.

None

Attributes:

Name Type Description
node_namestr

Name of the workflow node that produced this result.

outputAny

Value returned by the node handler or agent turn.

statusStepStatus

Terminal execution status of the step (completed, skipped, or failed).

errorstr | None

Optional error message when status is failed.