File_Operations_Agent
This project is an AI-powered agent designed for automating CRUD file operations, featuring a CLI interface and MCP integration. It utilizes GPT APIs for intelligent file content analysis and offers two operational modes: a custom ReAct agent and a Pydantic-AI agent. Users can efficiently perform file creation, reading, updating, and deletion, contributing to increased productivity in their workflows.
GitHub Stars
1
User Rating
Not Rated
Forks
0
Issues
0
Views
3
Favorites
0
AI-Powered File Operations Agent
An intelligent agent for CRUD file operations with CLI interface and MCP (Model Context Protocol) integration.
Overview
This project implements an autonomous agent capable of:
- Complete CRUD file operations (Create, Read, Update, Delete)
- Intelligent file content analysis using GPT APIs
- CLI interface for local interaction
- MCP server for integration with Claude Desktop and other clients
- Tool-based architecture with ReAct pattern
- Smart fallback system for query validation
The system offers two operational modes:
- custom ReAct Agent (GPT-4o + LLaMA 3 8B) - Advanced artificial intelligence
- Pydantic-AI Agent (Native framework) - Modern framework implementation
Architecture
The project implements two architectural approaches to meet different needs:
1. Custom ReAct Architecture (Main Implementation)
From-scratch implementation of the entire ReAct architecture (Reasoning + Acting) with manual tool orchestration and dual-model system. Uses GPT-4o for planning and LLaMA 3 for query validation. This approach ensures maximum control and transparency over the decision cycle, with a lightweight structure optimized for local execution, low latency, and custom MCP integration.
Technical advantages:
- Direct control over reasoning loops and tool execution
- Optimized for performance-critical scenarios
- Transparent debugging and customization
- Minimal framework overhead
2. Pydantic-AI Architecture (Modern Implementation)
Modern implementation using the Pydantic-AI framework for declarative tool orchestration, automatic structured output validation, and dependency injection. Demonstrates integration with modern agentic frameworks while maintaining the same user interface and functionality.
Technical advantages:
- Automatic tool orchestration and error handling
- Built-in structured output validation with Pydantic models
- Type-safe dependency injection
- Framework-managed conversation flow
Main Components
- Agent Core: Agent implementation with ReAct pattern
- Tool Registry: Centralized system for tool management
- CLI Interface: Command line interface for local use
- MCP Servers: Servers for integration with MCP clients
- Security Layer: Query validation and security controls
Available Tools
- list_files(): Lists all files in directory with metadata
- read_file(filename): Reads complete content of a file
- write_file(filename, content, mode): Writes content to a file
- delete_file(filename): Deletes a file from directory
- answer_question_about_files(query): Answers questions about files using GPT intelligence
⚡ Guaranteed Tool Usage (Assignment Compliance)
ALL agents strictly respect assignment requirements:
- ✅ Mandatory Tool Usage: Every file operation MUST use the appropriate tool
- ✅ Internal Planning Loop: ReAct pattern implemented with internal reasoning loop
- ✅ Tool-Based Architecture: Architecture completely based on CRUD tools
- ✅ Smart Validation: Automatic detection of queries requiring tools
- ✅ Forced Tool Execution: System that forces tool usage when necessary
Example of compliant behavior:
User: "list files"
❌ Direct response: "I can help you list files..."
✅ Correct response: Uses list_files() tool + provides results
User: "read config.json"
❌ Direct response: "I'll read that file for you..."
✅ Correct response: Uses read_file("config.json") tool + shows content
Installation
Prerequisites
- Python 3.8+
- Virtual environment (recommended)
Setup
# Clone repository
git clone <repository-url>
cd assignment
# Create and activate virtual environment
python -m venv .venv
source .venv/bin/activate # Linux/Mac
# or
.venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt
API Keys Configuration
Create a .env
file in the project root:
OPENAI_API_KEY=your_openai_api_key_here
GROQ_API_KEY=your_groq_api_key_here
Usage
The project offers 2 distinct agents for different operational needs:
1. Custom ReAct Agent (Advanced AI)
Features:
- GPT-4o for reasoning and main planning
- LLaMA 3 8B for query validation (economical)
- Advanced natural language understanding
- Intelligent multi-step reasoning
Prerequisites:
OPENAI_API_KEY=your_openai_api_key_here
GROQ_API_KEY=your_groq_api_key_here # Optional but recommended
Launch:
python chat_interface/llm_cli.py --directory /path/to/working/directory
For testing, sample files are created in the test_files folder:
python chat_interface/llm_cli.py --directory ./test_files
Advanced usage examples:
# Natural language queries
> "Show me all Python files and summarize what they do"
> "Find the most recently modified file and read its content"
> "Create a backup of all important files with timestamp"
> "Analyze the file types and tell me about the project structure"
2. Pydantic-AI Agent (Modern Framework)
Features:
- Native Pydantic-AI framework for tool orchestration
- Automatic structured output with Pydantic validation
- Advanced dependency injection
- GPT-4o + Groq for dual-model architecture
- Same interface as other agents but with modern technology
Prerequisites:
OPENAI_API_KEY=your_openai_api_key_here # REQUIRED
GROQ_API_KEY=your_groq_api_key_here # OPTIONAL
Launch:
python chat_interface/pydantic_cli.py --directory /path/to/working/directory
Usage examples:
# Same commands as other agents but with structured output
> "List all files and analyze their types"
> "Create a summary report of all JSON files"
> "Read the largest Python file and explain its structure"
MCP Integration with Claude Desktop
The project provides MCP (Model Context Protocol) server integration for seamless use with Claude Desktop and other MCP-compatible clients.
MCP Server Setup
Configure API Keys: Update the
mcp_config.json
file with your API keys:{ "mcpServers": { "llm-file-operations-agent": { "env": { "OPENAI_API_KEY": "your_openai_api_key_here", "GROQ_API_KEY": "your_groq_api_key_here" } } } }
Install Configuration: Copy the configuration to Claude Desktop:
# Create Claude Desktop config directory if it doesn't exist mkdir -p ~/Library/Application\ Support/Claude/ # Copy configuration file cp mcp_config.json ~/Library/Application\ Support/Claude/claude_desktop_config.json
Restart Claude Desktop: Close and reopen Claude Desktop to load the new configuration
Verify Connection: Check the MCP settings in Claude Desktop to ensure the server shows as connected
Available MCP Server
- llm-file-operations-agent: Advanced MCP server featuring the Custom ReAct Agent with GPT-4o reasoning and LLaMA 3.1-8B validation. Provides natural language file operations, intelligent analysis, and multi-step reasoning capabilities.
MCP Tools Available
- llm_query: Natural language queries for intelligent file operations
- list_files: List directory contents with metadata
- read_file: Read complete file content
- write_file: Create or modify files with append support
- delete_file: Safe file deletion
MCP Usage Examples
Once connected to Claude Desktop, you can use natural language commands:
"Show me all Python files and their purposes"
"Create a summary of all JSON configuration files"
"Find the largest file and analyze its content"
"Delete all temporary files that end with .tmp"
Advanced Features
Intelligent Fallback System
The agent implements a 3-level fallback system:
- LLaMA 3 8B (Groq): Primary validation (fast and economical)
- GPT-4o (OpenAI): Fallback if Groq unavailable
Security
- Query validation to prevent unsafe operations
- Path traversal controls to limit file access
- Input sanitization
- Safe error handling
Multi-tool Orchestration
The agent can execute tool sequences in a single request:
Query: "read the file that was modified most recently"
Execution:
1. list_files() -> gets file metadata
2. Identifies the most recent file
3. read_file(filename) -> reads the content
4. Returns the result
Testing
The project includes a comprehensive and optimized test suite with 40 tests (39 passing, 1 skipped) verifying all CRUD components, agent functionality, error scenarios, security and integration. The test suite has been streamlined for efficiency while maintaining complete coverage.
Optimized Test Structure
Core Test Files:
test_tools.py
(27 tests): Complete testing of all five CRUD tools (list, read, write, delete, answer_question). Includes functionality tests, error handling, security validation, and integration tests. Covers path traversal protection, binary file detection, Unicode support, and concurrent operations.test_agents.py
(13 tests): Comprehensive testing of Custom ReAct agent functionality and query validation system. Tests the fix for file analysis queries, validator behavior, tool usage decisions, and error handling scenarios for both Custom ReAct and Pydantic-AI agents.conftest.py
: Test configuration and shared fixtures. Provides temporary test directories with sample files, ensuring consistent test environments across all test modules.
Test Documentation:
TESTING_GUIDE.md
: Comprehensive guide for running tests, understanding test scenarios, and extending the test suite. Updated with current test structure and coverage metrics.
Test Coverage
- Test Success Rate: 39/40 tests PASSED (97.5% success rate)
- Tool Coverage: 100% (all 5 CRUD tools tested comprehensively)
- Agent Coverage: 100% (both Custom ReAct and Pydantic-AI agents)
- Error Paths: 100% (all error types and edge cases covered)
- Security Cases: 100% (path traversal protection, input validation)
- Assignment Requirements: 100% (complete compliance verification)
- Code Coverage: 38% overall (focused on critical functionality)
Running Tests
# Run all tests (verified working)
python -m pytest tests/ -v
# Run specific test files
python -m pytest tests/test_tools.py -v # CRUD operations (27 tests)
python -m pytest tests/test_agents.py -v # Both agents (13 tests)
# Run specific test classes
python -m pytest tests/test_tools.py::TestListFiles -v
python -m pytest tests/test_agents.py::TestCustomReActAgent -v
# Tests with coverage
python -m pytest tests/ --cov=. --cov-report=html
# Quick verification
python -m pytest tests/ -q
Expected Output
tests/test_agents.py::TestLLMValidator::test_file_analysis_queries_approved PASSED [ 2%]
tests/test_agents.py::TestLLMValidator::test_inappropriate_queries_rejected PASSED [ 5%]
tests/test_agents.py::TestCustomReActAgent::test_should_use_tools_file_queries PASSED [ 7%]
tests/test_agents.py::TestCustomReActAgent::test_should_not_use_tools_general_queries PASSED [ 10%]
tests/test_agents.py::TestPydanticAgent::test_pydantic_agent_initialization PASSED [ 12%]
tests/test_agents.py::TestPydanticAgent::test_pydantic_list_files_tool PASSED [ 15%]
tests/test_agents.py::TestPydanticAgent::test_pydantic_read_file_tool PASSED [ 17%]
tests/test_agents.py::TestPydanticAgent::test_pydantic_write_file_tool PASSED [ 20%]
tests/test_agents.py::TestPydanticAgent::test_pydantic_delete_file_tool PASSED [ 22%]
tests/test_agents.py::TestPydanticAgent::test_pydantic_answer_question_tool SKIPPED [ 25%]
tests/test_agents.py::TestErrorHandling::test_binary_file_error_messages PASSED [ 27%]
tests/test_agents.py::TestErrorHandling::test_nonexistent_file_error_messages PASSED [ 30%]
tests/test_agents.py::TestErrorHandling::test_path_traversal_protection PASSED [ 32%]
...
======================== 39 passed, 1 skipped in 3.70s ========================
Project Structure
assignment/
├── agent/ # Main agent logic
│ ├── llm_agent.py # Custom ReAct agent
│ ├── tool_registry.py # Tool registry
│ └── llm_validator.py # LLM validation
├── Pydantic-AI_Agent/ # Pydantic-AI implementation
│ ├── pydantic_agent.py # Agent with Pydantic-AI framework
│ ├── models.py # Pydantic models
│ ├── dependencies.py # Dependency injection
│ └── README_Pydantic.md # Specific documentation
├── tools/ # Tool implementations
│ ├── list_files.py
│ ├── read_file.py
│ ├── write_file.py
│ ├── delete_file.py
│ └── answer_question_about_files.py
├── chat_interface/ # User interfaces
│ ├── llm_cli.py # LLM CLI
│ └── pydantic_cli.py # Pydantic-AI CLI
├── server/ # MCP servers
│ └── llm_mcp_server.py # Enhanced MCP server with Custom ReAct Agent
├── tests/ # Optimized test suite (40 tests)
│ ├── test_tools.py # CRUD tools testing (27 tests)
│ ├── test_agents.py # Agent functionality testing (13 tests)
│ ├── conftest.py # Test configuration and fixtures
│ └── TESTING_GUIDE.md # Comprehensive test documentation
├── Guide_Documents/ # Extended documentation
├── test_files/ # Sample files for testing
├── mcp_config.json # MCP configuration
├── requirements.txt # Python dependencies
└── README.md # This file
Assignment Compliance
This project meets and exceeds all assignment requirements:
Mandatory Requirements
- Agent Design: Tool-based architecture with ReAct pattern
- CRUD Operations: All 5 required tools implemented
- Multi-tool Orchestration: Sequential execution of multiple tools
- Chat Interface: Functional CLI for interaction
- MCP Server: Fully functional MCP server
- Directory Constraints: All operations limited to base directory
Bonus Features
- Test Suite: Comprehensive and optimized test coverage with pytest (40 tests, 97.5% success rate)
- Safe Behavior: Query validation and inappropriate request rejection
- Lightweight Model: Fallback system with lighter models for validation
- MCP Integration: Production-ready MCP server for Claude Desktop integration
Extra Implementations
- Pydantic-AI Integration: Modern framework suggested in assignment
- Two distinct agents: Custom ReAct and Pydantic-AI
- Dual Architecture: Custom ReAct + Native framework
- Structured Output: Automatic validation with Pydantic
- Extended Documentation: Complete guides in
Guide_Documents/
Troubleshooting
Common Issues
- Import Errors: Make sure virtual environment is activated and dependencies are installed
- API Keys: Verify API keys are configured correctly in environment variables or MCP config
- Permissions: Check working directory permissions for file operations
- MCP Integration: Verify paths in MCP configuration file match your system
- Claude Desktop Connection: Ensure Claude Desktop configuration file is in the correct location
- Python Path: Verify Python interpreter path in MCP configuration matches your virtual environment
MCP Setup Verification
To test MCP server functionality:
# Test server startup
export OPENAI_API_KEY="your_key_here"
export GROQ_API_KEY="your_key_here"
python server/llm_mcp_server.py --directory ./test_files --name llm-file-operations-agent
# Test with sample request
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}' | python server/llm_mcp_server.py --directory ./test_files --name llm-file-operations-agent
Logs and Debug
MCP server generates logs in:
/tmp/llm_mcp_server.log
- Complete server activity and error logs- Console output (stderr) - Real-time server status and debugging information
Contributing
To contribute to the project:
- Fork the repository
- Create a feature branch
- Implement changes with tests
- Submit a pull request
License
This project is released under MIT license.
Contact
For questions or support, contact the developer: Andrea Belli Contarini - andreabcontarini@gmail.com
1
Followers
15
Repositories
0
Gists
14
Total Contributions