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 |
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]]
|
|
()
|
model
|
ModelClient | str | None
|
Bound |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
name | str | Unique identifier of the agent within an application or |
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]] |
|
model | ModelClient | str | None | Bound |
tool_set | tuple[Tool, ...] | Return the agent's own tools followed by skill tools. |
skill_instances | tuple[SkillController, ...] | Return the instantiated skill controllers attached to this agent. |
combined_instructions | str | 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 |
|---|---|---|
specialists | dict[str, Agent] | Default: {s.name: s for s in specialists} |
AgentMessage
¶
Bases: Struct
Provider-neutral conversation message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
role
|
Literal['system', 'user', 'assistant', 'tool']
|
Message author role ( |
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 |
|---|---|---|
role | Literal['system', 'user', 'assistant', 'tool'] | Message author role ( |
content | str | Primary text content of the message. |
tool_calls | list[ToolCall] | Tool invocations requested by the assistant in this message. |
tool_results | list[ToolResult] | Tool execution results returned in a |
agent_name | str | 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
¶
Provide this runtime as the application's runtime dependency.
Returns:
| Type | Description |
|---|---|
AppConfig
|
The updated application configuration. |
get_history
¶
Return conversation history scoped to connection owner.
produce
¶
Execute turn loop as a producer coroutine emitting frames to the send stream.
open_stream
¶
Open a background task group streaming frames to an in-memory receive stream.
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_message | str | User prompt text initiating the agent turn. |
session_id | str | None | Optional session identifier to resume an existing conversation. |
turn_id | str | None | Optional caller-supplied identifier for the turn. |
dynamic_context | str | None | Optional per-turn context prepended to the active agent's instructions. |
context | ToolContext | 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_id | str | Session identifier under which the turn history was persisted. |
turn_id | str | Unique identifier for the completed turn. |
output | str | Concatenated assistant text output produced across the turn. |
agent_name | str | Name of the active agent at the end of the turn. |
messages | list[AgentMessage] | Full conversation history including the newly appended turn messages. |
token_usage | dict[str, int] | Aggregated token usage counts for prompt, completion, and thought tokens. |
content | str | 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_turns | int | Maximum number of model-invocation iterations allowed in a single turn. |
max_tool_calls | int | Maximum total tool calls allowed across all iterations of a single turn. |
timeout_seconds | float | 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_interval | float | Default: 15.0 |
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 |
|---|---|---|
message | str | User prompt text for the agent turn. |
session_id | str | None | Optional existing session identifier to continue a conversation. |
dynamic_context | str | 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_id | str | Identifier of the conversation session for this turn. |
turn_id | str | Unique identifier assigned to this turn execution. |
agent_name | str | Name of the agent that produced the final turn output. |
output | str | Concatenated assistant text output produced during the turn. |
messages | list[AgentMessage] | Updated conversation message history after the turn completes. |
token_usage | dict[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_id | str | Identifier of the retrieved conversation session. |
messages | list[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.
MemorySessionStore
¶
Process-local LRU session store with TTL expiry and per-session locks.
LitestarStoreSessionStore
¶
Model Clients & Streaming Frames¶
ModelClient
¶
Bases: Protocol
Asynchronous protocol implemented by all LLM model adapters.
stream_turn
¶
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_name | str | Return canonical OpenTelemetry provider name. |
stream_turn
¶
Stream an agent turn using Google GenAI SDK.
MockModelClient
¶
Deterministic scriptable model client for fast offline unit tests.
Attributes:
| Name | Type | Description |
|---|---|---|
responses | list[list[ModelDelta]] | Default: responses or [] |
call_history | list[dict[str, Any]] | Default: [] |
stream_turn
¶
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 ( |
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 |
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 |
None
|
completion_tokens
|
int | None
|
Output completion token count reported in a |
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 |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
event_type | ModelEventType | Kind of incremental model event ( |
text | str | None | Incremental assistant text or thought content. |
thought | str | None | Reasoning or thinking trace text when emitted by a thinking model. |
tool_name | str | None | Name of the tool requested by the model for a |
call_id | str | None | Unique identifier correlating a tool call with its result. |
arguments | dict[str, Any] | None | Decoded JSON arguments mapping passed to the requested tool. |
prompt_tokens | int | None | Input prompt token count reported in a |
completion_tokens | int | None | Output completion token count reported in a |
provider_metadata | dict[str, dict[str, Any]] | Provider-specific metadata such as call identifiers and thought signatures. |
thought_tokens | int | None | Reasoning token count reported in a |
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 ( |
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 |
None
|
tool_name
|
str | None
|
Tool name for |
None
|
call_id
|
str | None
|
Tool call correlation identifier for |
None
|
payload
|
dict[str, Any] | None
|
Structured event payload for session, tool, transfer, completion, or error frames. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
event_type | StreamEventType | Frame event discriminator ( |
turn_id | str | Unique identifier of the turn that emitted this frame. |
seq | int | Monotonically increasing 1-based sequence number within the turn stream. |
agent_name | str | None | Name of the active agent associated with this frame. |
text | str | None | Incremental text chunk for |
tool_name | str | None | Tool name for |
call_id | str | None | Tool call correlation identifier for |
payload | dict[str, Any] | None | Structured event payload for session, tool, transfer, completion, or error frames. |
iter_agent_stream
¶
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 |
|---|---|---|
nodes | dict[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
¶
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 |
None
|
handler
|
Callable[..., Any] | Agent | None
|
Optional synchronous or asynchronous callable accepting |
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 |
None
|
timeout_seconds
|
float
|
Maximum execution time in seconds allowed for this node. |
300.0
|
condition
|
Callable[list, bool] | None
|
Optional predicate evaluated against |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
name | str | Unique node name within the workflow DAG. |
agent | Agent | None | Optional |
handler | Callable[..., Any] | Agent | None | Optional synchronous or asynchronous callable accepting |
depends_on | tuple[str, ...] | Sequence[str] | Names of upstream nodes that must complete before this node runs. |
prompt | Callable[[WorkflowContext], str] | None | Optional callable building the prompt string for an |
timeout_seconds | float | Maximum execution time in seconds allowed for this node. |
condition | Callable[[WorkflowContext], bool] | None | Optional predicate evaluated against |
dependencies | list[str] | Backward-compatible list representation of node dependencies. |
WorkflowEngine
¶
Concurrent in-process DAG execution engine for DynamicWorkflow pipelines.
execute_node
¶
Execute a single workflow node against current workflow context or state.
validate_dependencies
¶
Validate that all node dependencies exist and graph is acyclic.
execute_run
¶
Execute workflow in DAG topological order via ready queue scheduling.
run
¶
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_id | str | Unique identifier for the workflow execution run. |
state | dict[str, Any] | Mutable shared state dictionary accessible to all workflow nodes. |
node_outputs | dict[str, Any] | Mapping of completed node names to their raw output values. |
step_results | dict[str, StepResult] | Mapping of executed node names to their |
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'
|
error
|
str | None
|
Optional error message when |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
node_name | str | Name of the workflow node that produced this result. |
output | Any | Value returned by the node handler or agent turn. |
status | StepStatus | Terminal execution status of the step ( |
error | str | None | Optional error message when |