Skip to main content

Overview

mcp-agent gives you two complementary ways to expose agent behaviour:
  1. Decorator-based tools – mark a plain Python function with @app.tool or @app.async_tool to expose it as an MCP tool. This is the quickest way to add synchronous or long-running behaviour to your app.
  2. Workflow classes – build stateful, structured flows by subclassing Workflow[T]. Workflows give you fine-grained control over orchestration, retries, and Temporal integration.
Both options register MCP tools automatically, so any MCP client can invoke them. The high-level “workflow patterns” in examples/workflows (parallel, router, orchestrator, etc.) are built using these same primitives—they are patterns, not the Workflow base class itself. The rest of this page walks through the decorators first (because most apps start there) and then dives into the Workflow class.

Decorator-based tools

@app.tool – synchronous tools

Use @app.tool when the work can complete within a single MCP call. The return value is sent straight back to the client—no polling required.
Key points:
  • Works great for quick operations or simple glue code.
  • You can accept an optional app_ctx: Context parameter to access logging, server registry, etc.
  • The tool result is serialised and returned to the caller immediately.

@app.async_tool – long-running tools

Agents often need to run tasks that take longer than an MCP request allows (multi-step research, human-in-the-loop flows, durable Temporal runs). Decorate those entry points with @app.async_tool:
@app.async_tool starts a workflow in the background and returns identifiers that clients can poll via the built-in workflows-get_status tool. This pattern keeps your agent responsive even when the underlying work takes minutes or requires human decisions.
Tip: Agent servers rely heavily on these decorators—see Agent Servers for end-to-end examples.

The Workflow Class

The Workflow[T] base class lets you model multi-step or stateful logic while still exposing an MCP tool. Workflows are most useful when you need retries, shared state, or tight integration with the execution engine (asyncio or Temporal).

Basic workflow definition

Decorate the class with @app.workflow and the entry point with @app.workflow_run. Whatever you return from the method becomes the MCP tool result.

Useful workflow features

  • Access self.context for logging, MCP connections, and configuration.
  • Store reusable helpers or caches on self inside __init__.
  • Raise exceptions to trigger retries (Temporal) or propagate errors to the caller.
  • Combine with @app.workflow_task / @app.workflow_signal when you need durable activities or signal handlers.
See the sections below for more elaborate compositions.

Workflow patterns (examples/workflows)

The repository has an examples/workflows directory that demonstrates higher-level agent patterns: router, parallel fan-out, orchestrator, evaluator/optimizer, and more. These samples compose agents and AugmentedLLMs with helpers from mcp_agent.workflows.factory. They do not correspond one-to-one with the Workflow base class above—they are ready-made orchestration patterns you can adopt or customise. Use the patterns when you want opinionated orchestration, and drop down to the Workflow class (or @app.async_tool) when you need bespoke control flow.

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

  • 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
Always provide type hints and docstrings:
Handle errors gracefully:
Use context managers for resources:
Use structured logging:

Testing Workflows

Test your workflows locally:

Migration Guide

From Functions to Tools

From Scripts to Workflows

Next Steps

Workflow Patterns

Explore pre-built workflow patterns

Agent Server

Deploy workflows as MCP servers

Temporal Integration

Add durability with Temporal

Examples

See workflows in action