|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# /// script |
| 3 | +# dependencies = [ |
| 4 | +# "crewai>=0.11.0", |
| 5 | +# "crewai-tools[mcp]>=0.0.5", |
| 6 | +# "pydantic>=2.11.0", |
| 7 | +# "python-dotenv>=1.0.0", |
| 8 | +# ] |
| 9 | +# /// |
| 10 | + |
| 11 | +""" |
| 12 | +Example script demonstrating how to use CrewAI with Unstructured MCP to configure sources. |
| 13 | +This script creates a CrewAI agent that connects to the Unstructured MCP server |
| 14 | +and configures data sources. |
| 15 | +
|
| 16 | +1. Configure UNSTRUCTURED_API_KEY, ANTHROPIC_API_KEY, AWS_KEY, AWS_SECRET keys in .env file |
| 17 | +2. Start MCP server with: |
| 18 | +``` |
| 19 | +make sse-server |
| 20 | +``` |
| 21 | +
|
| 22 | +3. Run example with: |
| 23 | +``` |
| 24 | +uv run example_clients/crew_ai_agent.py |
| 25 | +``` |
| 26 | +""" |
| 27 | + |
| 28 | +import os |
| 29 | +from typing import Optional |
| 30 | + |
| 31 | +from crewai import LLM, Agent, Crew, Task |
| 32 | +from crewai_tools import MCPServerAdapter |
| 33 | +from dotenv import load_dotenv |
| 34 | +from pydantic import BaseModel, Field |
| 35 | +from rich.pretty import pprint |
| 36 | + |
| 37 | +load_dotenv() |
| 38 | + |
| 39 | + |
| 40 | +class SourceConfigurationResult(BaseModel): |
| 41 | + source_id: Optional[str] = Field( |
| 42 | + default=None, |
| 43 | + description="The ID of the configured data source", |
| 44 | + ) |
| 45 | + source_type: Optional[str] = Field( |
| 46 | + default=None, |
| 47 | + description="The type of data source configured", |
| 48 | + ) |
| 49 | + source_name: Optional[str] = Field( |
| 50 | + default=None, |
| 51 | + description="The name of the configured data source", |
| 52 | + ) |
| 53 | + source_config: Optional[dict] = Field( |
| 54 | + default=None, |
| 55 | + description="The configuration details of the data source", |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +def main(): |
| 60 | + |
| 61 | + with MCPServerAdapter( |
| 62 | + {"url": os.getenv("MCP_SERVER_URL", "http://127.0.0.1:8080/sse")}, |
| 63 | + ) as tools: |
| 64 | + llm = LLM(model="anthropic/claude-3-opus-20240229", temperature=0.7, max_tokens=4096) |
| 65 | + |
| 66 | + agent = Agent( |
| 67 | + role="Source Configuration Specialist", |
| 68 | + goal="Configure and manage data sources for the MCP server", |
| 69 | + backstory="""You are an expert in data source configuration and management. |
| 70 | + You specialize in setting up and configuring various types of data sources |
| 71 | + including AWS S3, Google Drive, and other storage systems. You ensure |
| 72 | + proper configuration and validation of data sources.""", |
| 73 | + tools=tools, |
| 74 | + llm=llm, |
| 75 | + verbose=True, |
| 76 | + ) |
| 77 | + |
| 78 | + task = Task( |
| 79 | + description="""Configure an S3 source with the following specifications: |
| 80 | + - Name: MCP-S3-Source |
| 81 | + - URI: s3://test/uri |
| 82 | + - Recursive: true |
| 83 | + Ensure the source is properly configured and return the configuration details.""", |
| 84 | + agent=agent, |
| 85 | + expected_output="""A result containing: |
| 86 | + - source_id: The ID of the configured source |
| 87 | + - source_config: The configuration details |
| 88 | + - source_name: The name of the configured source |
| 89 | + - source_type: The type of data source configured |
| 90 | + """, |
| 91 | + output_pydantic=SourceConfigurationResult, |
| 92 | + ) |
| 93 | + |
| 94 | + crew = Crew(agents=[agent], tasks=[task], verbose=True) |
| 95 | + |
| 96 | + result = crew.kickoff() |
| 97 | + pprint("Task Result:") |
| 98 | + pprint(result.tasks_output[0].pydantic) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + main() |
0 commit comments