MCP Agent - AI Expense Tracker

MCP Agent - AI Expense Tracker

Enables AI agents to manage personal expenses through natural language conversations. Supports adding, searching, and analyzing transactions with automatic categorization and financial insights.

Category
访问服务器

README

MCP Agent - AI Expense Tracker

License: MIT Python 3.11+ GitHub stars Presentation

A practical demonstration of AI Agent implementation with custom MCP server.

This project showcases how to build intelligent AI agents using the Model Context Protocol (MCP). Through a real-world expense tracking application, you'll see how AI agents can interact with tools, databases, and APIs to perform complex tasks through natural conversation.

🎯 What This Demonstrates

  • Custom MCP Server: Build your own MCP server using FastAPI
  • AI Agent Integration: Connect AI agents to tools via MCP protocol
  • Real-world Application: Practical expense tracking use case
  • Natural Language Interface: Chat with AI to manage your data
  • Tool Discovery: AI automatically discovers and uses available tools

🏗️ Architecture

graph TB
    subgraph "Backend"
        API[API Server<br/>FastAPI + SQLite<br/>Port: 8002]
        MCP[MCP Server<br/>FastAPI-MCP<br/>Port: 9002]
    end

    subgraph "AI Layer"
        Agent[AI Agent<br/>Agno Framework<br/>Port: 7777]
        LLM[LLM]
    end

    subgraph "Client Layer"
        UI[Web UI<br/>Next.js + React<br/>Port: 3000]
        Telegram[Telegram Bot<br/>Python Telegram Bot]
        AnyClient[Any MCP Client]
    end

    subgraph "End Users"
        User1[User]
        User2[User]
        User3[User]
    end

    API -->|Exposes REST API| MCP
    MCP -->|MCP Protocol| Agent
    MCP -.->|MCP Protocol| AnyClient
    Agent -->|API Calls| LLM
    Agent -->|Serves| UI
    Agent -->|Serves| Telegram
    UI -->|Interacts| User1
    Telegram -->|Interacts| User2
    AnyClient -.->|Interacts| User3

    style API fill:#0066CC,color:#fff
    style MCP fill:#00AA66,color:#fff
    style Agent fill:#FF6600,color:#fff
    style LLM fill:#8B5CF6,color:#fff
    style UI fill:#06B6D4,color:#fff
    style Telegram fill:#06B6D4,color:#fff

📖 For detailed architecture documentation, request flows, and deployment options, see ARCHITECTURE.md

🚀 Features

  • AI-Powered Agent: Natural language expense tracking using OpenAI GPT-4
  • SQLite Database: Persistent storage for all transactions
  • Auto-Initialization: Automatic database setup with seed data
  • MCP Integration: Extensible tool system for AI agents
  • REST API: Full CRUD operations for expense management
  • Multiple Clients: Web UI, Telegram bot, and direct agent interface
  • Smart Categorization: Automatic expense categorization and insights
  • Currency-Agnostic: Clean numerical formatting without currency symbols

📁 Project Structure

MCPAgent/
├── .env.example       # Environment variables template
├── .env               # Your configuration (create from .env.example)
├── agent/             # AI Agent with Agno framework
│   ├── agent.py       # Main agent with system prompts
│   └── agno.db        # Agent's SQLite database
├── server/            # FastAPI backend with MCP server
│   ├── main.py        # API routes and endpoints
│   ├── store.py       # SQLite data store
│   ├── models.py      # Pydantic data models
│   ├── config.py      # Configuration settings
│   ├── mcp_server.py  # MCP protocol server
│   ├── start.py       # Server initialization & startup
│   └── expenses.db    # Transactions database
└── client/
    ├── agent-ui/      # Next.js web interface
    └── telegram-bot/  # Telegram bot client

🏃 Quick Start

1. Install Dependencies

# Install Python dependencies
pip install -r server/requirements.txt
pip install agno openai python-dotenv

2. Set Environment Variables

# Copy example and add your API key
cp .env.example .env
# Edit .env and add your OPENAI_API_KEY

3. Initialize and Start Servers

cd server

# Check dependencies and initialize database with seed data
python start.py

# Start MCP server (in one terminal)
python start.py --mcp

# Start API server (in another terminal)
python start.py --api

4. Run the AI Agent

cd agent
python agent.py

Access the agent at: http://localhost:7777

💬 Usage Examples

Chat with the AI agent:

  • "Add a 50 grocery expense"
  • "I spent 75 on dinner last night"
  • "How much did I spend on food this month?"
  • "Show me my financial summary"
  • "What's my biggest expense category?"
  • "Add income of 5000 from salary"

🛠️ Tech Stack

  • Protocol: Model Context Protocol (MCP) - Custom server implementation
  • Agent Framework: Agno
  • AI Model: OpenAI GPT-4
  • MCP Server: FastAPI-MCP (converts REST API to MCP tools)
  • Backend: FastAPI + SQLite
  • Frontend: Next.js + React
  • Bot: Python Telegram Bot

🔌 How MCP Works Here

  1. FastAPI Backend (server/main.py) - Standard REST API with CRUD operations
  2. MCP Server (server/mcp_server.py) - Wraps the API and exposes it as MCP tools
  3. AI Agent (agent/agent.py) - Connects to MCP server and automatically discovers tools
  4. Natural Language - User chats with agent, agent uses tools to complete tasks
User Input → AI Agent → MCP Server → FastAPI → SQLite
              ↓
         Tool Selection & Execution
              ↓
         Natural Language Response

📊 API Endpoints

  • GET /transactions - List all transactions
  • POST /transactions - Create new transaction
  • PUT /transactions/{id} - Update transaction
  • DELETE /transactions/{id} - Delete transaction
  • GET /transactions/search?q= - Search transactions
  • GET /summary - Financial summary
  • GET /summary/categories - Category breakdown
  • GET /health - Health check

Full API docs: http://localhost:8002/docs

🎯 Server Commands

The start.py script manages server initialization and startup:

# Check dependencies and initialize database
python start.py

# Start MCP server only
python start.py --mcp

# Start API server only  
python start.py --api

# Custom ports
python start.py --api --port 8000
python start.py --mcp --port 9000

What start.py does:

  • ✅ Checks all required dependencies
  • ✅ Verifies environment variables
  • ✅ Initializes SQLite database
  • ✅ Seeds database with sample transactions (first run only)
  • ✅ Starts requested server(s)

🤖 Agent Capabilities

The AI agent can:

  • Create, read, update, and delete expenses
  • Search transactions by keyword
  • Generate financial summaries and insights
  • Analyze spending patterns by category
  • Provide budgeting recommendations
  • Filter transactions by date, type, or category

🔧 Configuration

Environment Variables (.env)

OPENAI_API_KEY=your_key_here    # Required for AI agent
HOST=localhost                  # Server host
PORT=8002                       # API server port
MCP_HOST=localhost              # MCP server host
MCP_PORT=9002                   # MCP server port

Server Configuration (server/config.py)

  • Server host/port settings
  • Database path
  • MCP server configuration

Agent Configuration (agent/agent.py)

  • AI model selection (default: gpt-4.1)
  • System prompt customization
  • Agent behavior settings
  • Database location

🔍 Troubleshooting

Dependencies missing?

pip install -r server/requirements.txt
pip install agno openai python-dotenv

Database not initialized?

cd server && python start.py

Port already in use?

python start.py --api --port 8003
python start.py --mcp --port 9003

Agent can't connect to MCP?

📊 Presentation

This project includes a presentation about practical AI agent implementation:

🌐 Live Demo

View Slides Online →

📁 Local Files

Open the slides to learn more about AI agents and MCP protocol.

📚 Resources

This project is built with and inspired by amazing open-source projects:

Special thanks to these projects and their maintainers for making AI agent development accessible and enjoyable! 🙏

📝 License

MIT


Built with ❤️ using AI agents and MCP

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选