Overview
mcp-agent provides two powerful ways to define agent logic:- Workflow Class: For complex, stateful agent workflows
- Tool Decorators: For simple, stateless functions
The Workflow Class
TheWorkflow
class is the foundation for building complex agent behaviors. It provides:
- Type-safe input/output handling
- Automatic MCP tool registration
- Support for both asyncio and Temporal execution
- Built-in error handling and retries
- Workflow state management
Basic Workflow Definition
Generic Type Parameters
Workflows use Python generics to specify return types:Workflow Properties
Every workflow has access to important properties:Error Handling
Workflows provide structured error handling:Tool Decorators
For simpler use cases, mcp-agent provides decorator-based tool definition:@app.tool - Synchronous Tools
The@app.tool
decorator creates tools that return results immediately:
- Returns final result directly
- No workflow ID or polling needed
- Best for quick operations
- Supports optional
app_ctx
parameter for context access
@app.async_tool - Asynchronous Tools
The@app.async_tool
decorator creates tools that start workflows asynchronously:
- Returns workflow/run IDs immediately
- Client polls for results using
workflows-get_status
- Best for long-running operations
- Enables progress tracking
Tool Naming and Description
Control how your tools appear to MCP clients:Advanced Workflow Patterns
Workflow Composition
Compose complex workflows from simpler ones:Workflow with Agents
Integrate agents into workflows:Parallel Workflow Execution
Execute multiple workflows in parallel:Stateful Workflows
Maintain state across workflow executions:Temporal Integration
Workflows seamlessly support Temporal for durable execution:MCP Server Integration
Exposing Workflows as MCP Tools
Workflows and tools are automatically exposed when creating an MCP server:Tool Discovery
MCP clients can discover available tools:Best Practices
Choose the Right Abstraction
Choose the Right Abstraction
- Use
@app.tool
for simple, stateless operations - Use
@app.async_tool
for long-running operations that need polling - Use
Workflow
class for complex, multi-step processes
Type Hints and Documentation
Type Hints and Documentation
Always provide type hints and docstrings:
Error Handling
Error Handling
Handle errors gracefully:
Resource Management
Resource Management
Use context managers for resources:
Logging and Observability
Logging and Observability
Use structured logging: