Spaces:
Sleeping
Sleeping
File size: 3,162 Bytes
4dbe519 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | # Agent Framework
A flexible framework for building AI agents with tool support, MCP integration, and multi-step reasoning.
## Structure
```
agent_framework/
βββ __init__.py # Package exports
βββ models.py # Core data models (Message, ToolCall, Event, ExecutionContext)
βββ tools.py # BaseTool and FunctionTool classes
βββ llm.py # LlmClient and request/response models
βββ agent.py # Agent and AgentResult classes
βββ mcp.py # MCP tool loading utilities
βββ utils.py # Helper functions for tool definitions
```
## Quick Start
```python
from agent_framework import Agent, LlmClient, FunctionTool
# Define a tool
def calculator(expression: str) -> float:
"""Calculate mathematical expressions."""
return eval(expression)
# Create the agent
agent = Agent(
model=LlmClient(model="gpt-5-mini"),
tools=[FunctionTool(calculator)],
instructions="You are a helpful assistant.",
)
# Run the agent
result = await agent.run("What is 1234 * 5678?")
print(result.output) # "7006652"
```
## Components
### Models (`models.py`)
- `Message`: Text messages in conversations
- `ToolCall`: LLM's request to execute a tool
- `ToolResult`: Result from tool execution
- `Event`: Recorded occurrence during agent execution
- `ExecutionContext`: Central storage for execution state
### Tools (`tools.py`)
- `BaseTool`: Abstract base class for all tools
- `FunctionTool`: Wraps Python functions as tools
### LLM (`llm.py`)
- `LlmClient`: Client for LLM API calls using LiteLLM
- `LlmRequest`: Request object for LLM calls
- `LlmResponse`: Response object from LLM calls
### Agent (`agent.py`)
- `Agent`: Main agent class that orchestrates reasoning and tool execution
- `AgentResult`: Result of an agent execution
### MCP (`mcp.py`)
- `load_mcp_tools()`: Load tools from MCP servers
### Utils (`utils.py`)
- `function_to_input_schema()`: Convert function signature to JSON Schema
- `format_tool_definition()`: Format tool definition in OpenAI format
- `tool`: Decorator to convert functions to tools
## Usage Examples
### Basic Tool Usage
```python
from agent_framework import FunctionTool
def my_function(x: int, y: int) -> int:
"""Add two numbers."""
return x + y
tool = FunctionTool(my_function)
result = await tool.execute(context, x=5, y=3) # 8
```
### Using the @tool Decorator
```python
from agent_framework import tool
@tool
def multiply(a: float, b: float) -> float:
"""Multiply two numbers."""
return a * b
# multiply is now a FunctionTool instance
```
### MCP Tool Integration
```python
from agent_framework import load_mcp_tools
import os
connection = {
"command": "npx",
"args": ["-y", "tavily-mcp@latest"],
"env": {"TAVILY_API_KEY": os.getenv("TAVILY_API_KEY")}
}
mcp_tools = await load_mcp_tools(connection)
agent = Agent(
model=LlmClient(model="gpt-5-mini"),
tools=mcp_tools,
)
```
## Installation
The framework uses:
- `pydantic` for data validation
- `litellm` for LLM API calls
- `mcp` for MCP server integration
Install dependencies:
```bash
pip install pydantic litellm mcp
```
|