|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Example: Creating a multi-step workflow for writing code. |
| 4 | +""" |
| 5 | + |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from mcp_workflows.builder import WorkflowBuilder |
| 9 | + |
| 10 | +# Define task prompts (you can put these in separate .md files) |
| 11 | +requirements_md = """# Requirements Analysis |
| 12 | +
|
| 13 | +- Gather all functional requirements |
| 14 | +- Identify constraints and edge cases |
| 15 | +- Clarify input/output specifications |
| 16 | +- Note dependencies and prerequisites""" |
| 17 | + |
| 18 | +design_md = """# Design Phase |
| 19 | +
|
| 20 | +- Architect the solution structure |
| 21 | +- Plan algorithms and data flow |
| 22 | +- Define interfaces and modules |
| 23 | +- Consider error handling""" |
| 24 | + |
| 25 | +implementation_md = """# Code Implementation |
| 26 | +
|
| 27 | +- Write clean, readable code |
| 28 | +- Follow language best practices |
| 29 | +- Add meaningful comments |
| 30 | +- Handle exceptions properly""" |
| 31 | + |
| 32 | +testing_md = """# Testing and Review |
| 33 | +
|
| 34 | +- Write unit tests for key functions |
| 35 | +- Test edge cases and error conditions |
| 36 | +- Review code for bugs and optimization |
| 37 | +- Suggest improvements""" |
| 38 | + |
| 39 | +def create_code_writing_workflow(): |
| 40 | + """Create a multi-step workflow for writing code.""" |
| 41 | + |
| 42 | + builder = ( |
| 43 | + WorkflowBuilder.start() |
| 44 | + .with_goal("Write production-ready code for the specified task") |
| 45 | + .memory("code_memory.md") |
| 46 | + ) |
| 47 | + |
| 48 | + # Register tasks |
| 49 | + builder.register_task("requirements", text=requirements_md) |
| 50 | + builder.register_task("design", text=design_md) |
| 51 | + builder.register_task("implement", text=implementation_md) |
| 52 | + builder.register_task("test", text=testing_md) |
| 53 | + |
| 54 | + # Add steps in sequence |
| 55 | + builder.add_step( |
| 56 | + "Gather Requirements", |
| 57 | + doc="Collect and analyze all requirements for the coding task", |
| 58 | + uses=["requirements"], |
| 59 | + ) |
| 60 | + |
| 61 | + builder.add_step( |
| 62 | + "Design Solution", |
| 63 | + doc="Plan the architecture and approach for implementation", |
| 64 | + uses=["requirements", "design"], |
| 65 | + ) |
| 66 | + |
| 67 | + builder.add_step( |
| 68 | + "Implement Code", |
| 69 | + doc="Write the actual code following the design plan", |
| 70 | + uses=["design", "implement"], |
| 71 | + ) |
| 72 | + |
| 73 | + builder.add_step( |
| 74 | + "Test and Review", |
| 75 | + doc="Test the code, review for quality, and suggest improvements", |
| 76 | + uses=["implement", "test"], |
| 77 | + ) |
| 78 | + |
| 79 | + return builder |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + # Create and emit the workflow |
| 83 | + builder = create_code_writing_workflow() |
| 84 | + output_path = Path("workflows/code_workflow.yaml") |
| 85 | + builder.emit_yaml(output_path) |
| 86 | + |
| 87 | + print(f"Multi-step code writing workflow created: {output_path}") |
| 88 | + print("\nYou can run it with:") |
| 89 | + print(f"python -m mcp_workflows.main --goal 'Write a Python function to sort a list' --out {output_path} --memory code_memory.md --run") |
0 commit comments