|
| 1 | +import asyncio |
| 2 | +from typing import Any, Dict |
| 3 | + |
| 4 | +from mcp.server.fastmcp.tools.base import Tool as FastMCPTool |
| 5 | + |
| 6 | +from fast_agent.agents.agent_types import AgentConfig |
| 7 | +from fast_agent.agents.tool_agent_sync import ToolAgentSynchronous |
| 8 | +from fast_agent.core import Core |
| 9 | +from fast_agent.llm.model_factory import ModelFactory |
| 10 | + |
| 11 | + |
| 12 | +# Example 1: Simple function that will be wrapped |
| 13 | +async def search_web(query: str, max_results: int = 5) -> str: |
| 14 | + """Search the web for information. |
| 15 | +
|
| 16 | + Args: |
| 17 | + query: The search query |
| 18 | + max_results: Maximum number of results to return |
| 19 | +
|
| 20 | + Returns: |
| 21 | + Search results as a formatted string |
| 22 | + """ |
| 23 | + # Mock implementation |
| 24 | + return f"Found {max_results} results for '{query}': [Result 1, Result 2, ...]" |
| 25 | + |
| 26 | + |
| 27 | +# Example 2: Create a FastMCP Tool directly for more control |
| 28 | +def create_calculator_tool() -> FastMCPTool: |
| 29 | + """Create a calculator tool with explicit schema.""" |
| 30 | + |
| 31 | + def calculate(operation: str, a: float, b: float) -> float: |
| 32 | + """Perform a calculation.""" |
| 33 | + operations = { |
| 34 | + "add": lambda x, y: x + y, |
| 35 | + "subtract": lambda x, y: x - y, |
| 36 | + "multiply": lambda x, y: x * y, |
| 37 | + "divide": lambda x, y: x / y if y != 0 else float("inf"), |
| 38 | + } |
| 39 | + |
| 40 | + if operation not in operations: |
| 41 | + raise ValueError(f"Unknown operation: {operation}") |
| 42 | + |
| 43 | + return operations[operation](a, b) |
| 44 | + |
| 45 | + # Create the tool with explicit configuration |
| 46 | + return FastMCPTool.from_function( |
| 47 | + fn=calculate, |
| 48 | + name="calculator", |
| 49 | + description="Perform basic arithmetic operations", |
| 50 | + # FastMCP will still generate the schema, but we could override if needed |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +# Example 3: Complex async tool with side effects |
| 55 | +async def send_email(to: str, subject: str, body: str) -> Dict[str, Any]: |
| 56 | + """Send an email (mock implementation). |
| 57 | +
|
| 58 | + Args: |
| 59 | + to: Recipient email address |
| 60 | + subject: Email subject |
| 61 | + body: Email body content |
| 62 | +
|
| 63 | + Returns: |
| 64 | + Dictionary with send status and message ID |
| 65 | + """ |
| 66 | + # Mock async operation |
| 67 | + await asyncio.sleep(0.1) |
| 68 | + |
| 69 | + return { |
| 70 | + "status": "sent", |
| 71 | + "message_id": f"msg_{hash((to, subject))}", |
| 72 | + "timestamp": "2024-01-01T12:00:00Z", |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | +async def main(): |
| 77 | + core: Core = Core() |
| 78 | + await core.initialize() |
| 79 | + |
| 80 | + # Create agent configuration |
| 81 | + config = AgentConfig(name="assistant", model="haiku") |
| 82 | + |
| 83 | + # Mix different tool types |
| 84 | + tools = [ |
| 85 | + search_web, # Async function |
| 86 | + create_calculator_tool(), # Pre-configured FastMCP Tool |
| 87 | + send_email, # Complex async function |
| 88 | + ] |
| 89 | + |
| 90 | + # Create tool agent |
| 91 | + tool_agent = ToolAgentSynchronous(config, tools=tools, context=core.context) |
| 92 | + |
| 93 | + # Attach the LLM |
| 94 | + await tool_agent.attach_llm(ModelFactory.create_factory("haiku")) |
| 95 | + |
| 96 | + # Test various tools |
| 97 | + print("Testing search:") |
| 98 | + await tool_agent.send("Search for information about Python FastMCP") |
| 99 | + |
| 100 | + print("\nTesting calculator:") |
| 101 | + await tool_agent.send("What is 42 multiplied by 17?") |
| 102 | + |
| 103 | + print("\nTesting email:") |
| 104 | + await tool_agent.send( |
| 105 | + "Send an email to [email protected] with subject 'Hello' and body 'Test message'" |
| 106 | + ) |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + asyncio.run(main()) |
0 commit comments