Overview
Theswarms.schemas module provides Pydantic models and exception classes used for structured data validation across the framework. Everything importable from swarms.schemas falls into three groups:
These 17 names are the complete public surface of
swarms.schemas. All of them are re-exported from the top-level swarms package, so from swarms import AgentLLMError and from swarms.schemas import AgentLLMError give you the same class.MCP Schemas
MCPConnection
Defines connection parameters for a Model Context Protocol (MCP) server.Bearer token for accessing the MCP server. Accepts
"env:MY_VAR" or "${MY_VAR}" to read it from the environmentAPI key for the MCP server. Sent using
api_key_header / api_key_prefix. Accepts "env:MY_VAR" or "${MY_VAR}"Explicit auth mode. Inferred from the other fields when omitted
How long a single tool call may run before timing out. Separate from
timeout, which bounds HTTP requestsextra = "allow"), so additional keys will not raise a validation error, but only the fields above are read by the framework.
MCPOAuthConfig
OAuth 2.1 configuration for an MCP server, passed asMCPConnection.oauth. Three flavours are supported:
grant_type="authorization_code"(default) — the interactive browser flow. PKCE and RFC 7591 dynamic client registration are handled by the MCP SDK, soclient_idis optional. Tokens are cached on disk so the browser prompt only happens once.grant_type="client_credentials"— a headless machine-to-machine flow. Requiresclient_id/client_secret.access_token=...— a token obtained elsewhere. No flow is run; the token is sent as a bearer credential.
"env:MY_VAR" or "${MY_VAR}" to read the value from the environment instead of hardcoding a secret.
OAuth grant to use when no static
access_token is suppliedOAuth client id. Optional for
authorization_code when the server supports dynamic client registrationLoopback redirect URI used to capture the authorization code
Open the system browser for the authorization step. When
False the URL is logged insteadAgent Error Schemas
Exception classes raised by agents. All of them are plain Python exceptions — not Pydantic models — so they are caught with ordinarytry/except.
Two independent hierarchies are exported:
AgentMCPError inherits from Exception, not from AgentError. Catching AgentError will not catch MCP failures — catch both if you want blanket coverage.
Errors from outside this module that you may also see:
Planner/Worker Schemas
Schemas used byPlannerWorkerSwarm to represent tasks in the shared task queue.
TaskPriority
IntEnum of task priority levels: LOW = 0, NORMAL = 1, HIGH = 2, CRITICAL = 3.
PlannerTaskStatus
str, Enum of task statuses: PENDING, CLAIMED, RUNNING, COMPLETED, FAILED, CANCELLED.
Valid transitions:
PlannerTask
A single task in the shared planner-worker task queue. Created by planner agents, consumed by worker agents. Theversion field enables optimistic concurrency control.
PlannerTaskOutput
A single task definition as output from a planner agent.PlannerTaskSpec
Structured output from a planner agent: a plan narrative plus a list of concrete tasks.CycleVerdict
Structured output from the judge agent after evaluating a planning cycle.True if accumulated drift or systemic issues require a complete restart rather than incremental gap-filling. When True, all prior tasks are discarded and the planner begins from scratch with the original goal plus judge feedback
Example: MCP Connection Setup
Agent also takes mcp_config (one MCPConnection or dict) and mcp_configs (a list of them). Tools from every server are merged, and each call is routed to the server that owns the tool.
Example: Tracking Planner Tasks
Best Practices
- Type Safety: Use the provided schemas for type-safe data handling
- Validation: Leverage Pydantic’s validation to catch errors early
- Serialization: Use
.model_dump()and.model_dump_json()for serialization - Error Handling: Catch the narrowest agent exception that fits, and remember
AgentErrorandAgentMCPErrorare separate hierarchies - MCP Configuration: Use
MCPConnectionfor consistent tool integration, andMCPOAuthConfigwith"env:VAR"references instead of hardcoded secrets - Concurrency: Bump
PlannerTask.versionon every mutation so optimistic concurrency checks work
Schema Inheritance
The Pydantic models above (MCPConnection, MCPOAuthConfig, PlannerTask, PlannerTaskOutput, PlannerTaskSpec, CycleVerdict) inherit from Pydantic’s BaseModel, providing:
- Automatic validation
- JSON serialization/deserialization
- Schema generation
- IDE autocomplete support
- Type checking