Skip to content

eddiepiper/04-03-MCP-agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧠 SmartPortfolio AI – A MCP - Driven Rebalancing Agent System

SmartPortfolio AI is a modular, intelligent AI agent system designed to manage and optimize investment portfolios using Model Context Protocol (MCP).

This project showcases how an AI agent can evaluate, evolve, and recommend investment strategies dynamically, based on performance and policy constraints β€” while separating the decision logic (MCP) from execution workflows.


🎯 Project Purpose

To demonstrate Model Context Protocol (MCP) in action β€” where the AI system evolves internal capabilities (e.g., portfolio strategies) and adapts its behavior to changing conditions, user feedback, and performance results.


🧠 Core Concepts

  • Model Context Protocol (MCP): Tracks and manages evolving capabilities (investment strategies), scoring them based on past outcomes and adapting over time.
  • Modular AI Agent Design: Each agent performs a dedicated role in the portfolio rebalancing process.
  • Separation of Concerns: MCP recommends the best strategy, but a lightweight controller orchestrates the execution.

🧱 Agent Architecture

πŸ” System Flow

User (Telegram)
     ↓
Conversational Agent
     ↓
β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’
↓                 ↓
Calls Execution Controller    ←──────────────
(a lightweight orchestrator)                  |
     ↓                                         |
MCP Engine ⇄ Capability Manager Layer         |
     ↓                                         |
Sends recommendation:                         |
- Use "Risk Parity"                           |
- Deactivate "Equal Weight"                  ↑
     ↓                                        ↑
Execution Controller calls agents as needed:  ↑
- Portfolio Analysis                          ↑
- Bank Policy                                  ↑
- Rebalance Strategy                           ↑
- Trade Execution                              ↑
β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’β†’

πŸ‘₯ Agents in the System

Agent Role
Conversational Interface Agent Handles Telegram input/output and user interactions
MCP Engine Recommends strategies based on historical performance & context
Portfolio Analysis Agent Assesses portfolio structure and current asset distribution
Bank Policy Agent Applies internal or regulatory constraints on strategies
Rebalance Strategy Agent Executes chosen portfolio rebalancing logic
Trade Execution Agent Simulates or dispatches trade instructions

πŸ“‚ Directory Structure

SmartPortfolioMCP/
β”œβ”€β”€ agents/
β”‚   β”œβ”€β”€ analysis_agent.py
β”‚   β”œβ”€β”€ rebalance_agent.py
β”‚   β”œβ”€β”€ bank_policy_agent.py
β”‚   β”œβ”€β”€ trade_execution_agent.py
β”‚   └── MCP_engine.py          # The brain – recommends & evolves strategy capabilities
β”‚
β”œβ”€β”€ MCP/
β”‚   β”œβ”€β”€ capabilities.json      # Tracks all strategies, scores, and metadata
β”‚   └── performance_log.json   # Logs real-world or simulated performance results
β”‚
β”œβ”€β”€ interface/
β”‚   └── telegram_bot.py        # Telegram Conversational Agent
β”‚
β”œβ”€β”€ controller/
β”‚   └── execution_controller.py # Orchestrates agents based on MCP output
β”‚
β”œβ”€β”€ data/
β”‚   └── sample_portfolio.json
β”‚
β”œβ”€β”€ run.py                     # Main entry point
β”œβ”€β”€ requirements.txt
└── README.md

βš™οΈ How It Works

  1. User interacts via Telegram.
  2. Conversational Agent parses the query and forwards to the Execution Controller.
  3. The Controller calls the MCP Engine to get a recommended strategy.
  4. The MCP Engine evaluates current capability states (via capabilities.json).
  5. Based on recommendation, the Controller:
    • Calls Portfolio Analysis
    • Applies Bank Policies
    • Executes Strategy
    • Triggers Trade Execution
  6. Results are fed back to the MCP Engine for scoring and capability adjustment.

πŸ§ͺ Example Strategies (Capabilities)

  • equal_weight
  • risk_parity
  • volatility_weighted
  • dividend_focus

Each strategy has:

{
  "confidence_score": 0.74,
  "times_used": 3,
  "last_performance": 0.021,
  "active": true
}

βœ… Getting Started

git clone https://github.com/yourusername/SmartPortfolioMCP.git
cd SmartPortfolioMCP
pip install -r requirements.txt
python run.py

To start the Telegram bot, configure your bot token in interface/telegram_bot.py and run it.


πŸš€ Future Enhancements

  • Real-time price feed via Alpha Vantage or Yahoo Finance
  • Backtesting engine
  • Real trade execution via Alpaca API
  • Visual dashboard (Streamlit/Gradio)
  • Capability graph visualization

🧠 Designed With MCP in Mind

This project keeps the MCP Engine pure β€” responsible only for capability evaluation, evolution, and strategy recommendation, not system control.

The execution controller handles orchestration, maintaining modularity and showcasing how agent-based AI systems can be both adaptive and interpretable.


Dynamic Capital Management (MCP) Project

A sophisticated portfolio management system with AI-powered analysis and Telegram bot interface.

Features

  • Portfolio Management

    • Track holdings and positions
    • Calculate portfolio metrics
    • Monitor performance and risk metrics
    • Rebalancing suggestions
  • Market Analysis

    • Real-time market data integration
    • Sentiment analysis
    • Technical indicators
    • News integration
  • Telegram Bot Interface

    • View portfolio status
    • Get real-time alerts
    • Execute trades
    • Receive recommendations

Project Structure

MCP-project/
β”œβ”€β”€ agents/                 # AI agents for different analysis tasks
β”œβ”€β”€ MCP/                   # Core MCP functionality
β”‚   β”œβ”€β”€ alerts.py         # Portfolio alerts system
β”‚   β”œβ”€β”€ conditions.py     # Alert conditions
β”‚   β”œβ”€β”€ MCP_engine.py     # Main engine
β”‚   β”œβ”€β”€ holdings.py       # Portfolio holdings
β”‚   β”œβ”€β”€ market_data.py    # Market data integration
β”‚   └── portfolio.py      # Portfolio management
β”œβ”€β”€ interface/            # User interfaces
β”‚   └── telegram_bot.py   # Telegram bot implementation
β”œβ”€β”€ tests/               # Test suite
β”‚   β”œβ”€β”€ agents/         # Agent tests
β”‚   β”œβ”€β”€ MCP/           # Core functionality tests
β”‚   └── interface/     # Interface tests
β”œβ”€β”€ .env.example        # Example environment variables
β”œβ”€β”€ requirements.txt    # Python dependencies
└── README.md          # Project documentation

Setup

  1. Clone the repository:
git clone <repository-url>
cd MCP-project
  1. Create and activate a virtual environment:
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Copy .env.example to .env and configure your environment variables:
cp .env.example .env
  1. Configure your environment variables in .env:
TELEGRAM_BOT_TOKEN=your_telegram_bot_token
ALPHA_VANTAGE_API_KEY=your_alpha_vantage_key

Running Tests

Run all tests:

python -m pytest

Run specific test files:

python -m pytest tests/interface/test_telegram_bot.py

Usage

  1. Start the Telegram bot:
python -m interface.telegram_bot
  1. Available commands in Telegram:
  • /start - Start the bot
  • /help - Show help message
  • /portfolio - View current portfolio
  • /risk - View risk metrics
  • /performance - View performance metrics
  • /recommendations - Get recommendations
  • /rebalance - Rebalance portfolio
  • /alerts - Manage alerts
  • /strategies - View available strategies

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Financial data providers
  • Open-source libraries
  • Contributors and maintainers

About

πŸ€– Dynamic Context Management agent for AI applications

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages