An extension of the Model Context Protocol (MCP) SDK to enable MCP servers to function as workflow engines. Build complex, multi-step workflows with activities that have lifecycle hooks, automatic state management, and seamless integration with MCP tools.
- Activity Tools: Wrap individual units of work with lifecycle hooks (onSuccess, onFailure, onComplete)
- Workflow Orchestration: Chain activities together into sequential workflows
- State Management: Automatic session management with shared memory across workflow steps
- Validation: Built-in input/output validation using Zod schemas
- Error Handling: Robust error handling with retry logic and optional steps
- Conditional Execution: Skip steps based on runtime conditions
- Branching Workflows: Activities can suggest multiple next steps, allowing for dynamic workflow paths.
- Persistent Storage: Pluggable storage layer for session persistence (default is in-memory).
- MCP Integration: Seamlessly register workflows as MCP tools
The mcp-workflow library provides a structured way to define and execute complex, multi-step processes on top of the Model Context Protocol. It introduces the concepts of "Workflows" and "Activities" to orchestrate a series of tool calls, manage state, and handle errors.
- McpWorkflow: The main class that orchestrates a workflow. It manages a sequence of "activities" and registers itself as a set of MCP tools (_start and _continue).
- McpActivityTool: Represents a single unit of work within a workflow. It has lifecycle hooks (onSuccess, onFailure, onComplete) and can be configured with input/output schemas, timeouts, and retries.
- WorkflowSessionManager: Manages the lifecycle of workflow sessions, including creation, retrieval, and updates. It uses a WorkflowStore for persistence.
- WorkflowStore: An abstract class that defines the interface for storing and retrieving workflow session data. This allows for plugging in different storage backends like Redis, PostgreSQL, etc.
- InMemoryWorkflowStore: The default, in-memory implementation of WorkflowStore. Good for development and testing, but data is lost on restart.
- Define Activities: Create McpActivityTool instances for each step in your process. Each activity encapsulates a specific task, with its own input/output validation, execution logic, and lifecycle callbacks.
- Define a Workflow: Create an McpWorkflow instance, providing a sequence of steps. Each step references an activity and can have its own configuration, such as input mapping, conditions for execution, and branching logic.
- Attach to MCP Server: The McpWorkflow instance is attached to your MCP server. This automatically registers two tools:
- {workflow_name}_start: To begin a new workflow execution.
- {workflow_name}_continue: To proceed to the next step in an ongoing workflow.
- Execution Flow:
- An MCP client calls the _start tool, which creates a new workflow session and executes the first step.
- The result of the step, along with suggestions for the next step (if any), is returned to the client.
- The client then calls the _continue tool to execute the next step, and so on, until the workflow is complete.
Activity tools are individual units of work that can be executed independently or as part of a workflow.
Workflows orchestrate multiple activities in sequence, managing state between steps.
Activities can suggest multiple next steps, allowing for dynamic workflow paths.
You can provide your own storage implementation by extending WorkflowStore and passing it to the WorkflowSessionManager.
The public API is exposed through the main index.ts file and includes:
- McpWorkflow
- McpActivityTool
- WorkflowSessionManager
- WorkflowStore
- InMemoryWorkflowStore
- And all related types and interfaces.
See the examples directory for complete working examples.
Contributions are welcome! Please open an issue or pull request on GitHub.
.png)

