Skip to main content

Portfolio analysis MCP server powered by mcp-refcache - Comprehensive portfolio metrics, optimization, and risk analysis

Project description

portfolio-mcp

A portfolio analysis MCP server powered by mcp-refcache for building AI agent tools that handle financial data efficiently.

Tests Coverage Python

Features

  • Portfolio Management: Create, read, update, delete portfolios with persistent storage
  • Data Sources: Yahoo Finance (stocks/ETFs), CoinGecko (crypto), Synthetic (GBM simulation)
  • Analysis Tools: Returns, volatility, Sharpe ratio, Sortino ratio, VaR, drawdowns, correlations
  • Optimization: Efficient Frontier, Monte Carlo simulation, weight optimization
  • Reference-Based Caching: Large datasets cached via mcp-refcache to avoid context bloat

Installation

Using uv (recommended)

# Clone the repository
git clone https://github.com/l4b4r4b4b4/portfolio-mcp
cd portfolio-mcp

# Install dependencies
uv sync

# Run the server
uv run portfolio-mcp stdio

Using pip

pip install portfolio-mcp
portfolio-mcp stdio

Quick Start

Connect to Claude Desktop

Add to your Claude Desktop configuration (~/.config/claude/claude_desktop_config.json):

{
  "mcpServers": {
    "portfolio-mcp": {
      "command": "uv",
      "args": ["run", "--directory", "/path/to/portfolio-mcp", "portfolio-mcp", "stdio"]
    }
  }
}

Basic Usage

Once connected, you can use natural language to:

"Create a portfolio called 'tech_stocks' with AAPL, GOOG, and MSFT"
"Analyze the returns and volatility of my tech_stocks portfolio"
"Optimize my portfolio for maximum Sharpe ratio"
"Show me the efficient frontier with 20 points"
"Compare my portfolios by Sharpe ratio"

Available Tools

Portfolio Management (6 tools)

  • create_portfolio - Create a new portfolio with symbols and weights
  • get_portfolio - Retrieve portfolio details and metrics
  • list_portfolios - List all stored portfolios
  • delete_portfolio - Remove a portfolio
  • update_portfolio_weights - Modify portfolio weights
  • clone_portfolio - Create a copy with optional new weights

Analysis Tools (8 tools)

  • get_portfolio_metrics - Comprehensive metrics (return, volatility, Sharpe, Sortino, VaR)
  • get_returns - Daily, log, or cumulative returns
  • get_correlation_matrix - Asset correlation analysis
  • get_covariance_matrix - Variance-covariance structure
  • get_individual_stock_metrics - Per-asset statistics
  • get_drawdown_analysis - Maximum drawdown and recovery analysis
  • compare_portfolios - Side-by-side portfolio comparison

Optimization Tools (4 tools)

  • optimize_portfolio - Optimize weights (max Sharpe, min volatility, target return/vol)
  • get_efficient_frontier - Generate efficient frontier curve
  • run_monte_carlo - Monte Carlo simulation for portfolio analysis
  • apply_optimization - Apply optimization and update stored portfolio

Data Tools (8 tools)

  • generate_price_series - Generate synthetic GBM price data
  • generate_portfolio_scenarios - Create multiple scenario datasets
  • get_sample_portfolio_data - Get sample data for testing
  • get_trending_coins - Trending cryptocurrencies from CoinGecko
  • search_crypto_coins - Search for crypto assets
  • get_crypto_info - Detailed cryptocurrency information
  • list_crypto_symbols - Available crypto symbol mappings
  • get_cached_result - Retrieve cached large results by reference ID

Architecture

portfolio-mcp/
├── app/
│   ├── __init__.py
│   ├── __main__.py      # Typer CLI entry point
│   ├── config.py        # Pydantic settings
│   ├── server.py        # FastMCP server setup
│   ├── storage.py       # RefCache-based portfolio storage
│   ├── models.py        # Pydantic models for I/O
│   ├── data_sources.py  # Yahoo Finance + CoinGecko APIs
│   └── tools/           # MCP tool implementations
│       ├── portfolio.py
│       ├── analysis.py
│       ├── optimization.py
│       └── data.py
└── tests/               # 163 tests, 81% coverage

Reference-Based Caching

This server uses mcp-refcache to handle large results efficiently:

  1. Large results are cached - When a tool returns data that exceeds the preview size, it's stored in the cache
  2. References are returned - The tool returns a ref_id and a preview/sample of the data
  3. Full data on demand - Use get_cached_result(ref_id=...) to retrieve the complete data

This prevents context window bloat when working with large datasets like price histories or Monte Carlo simulations.

Development

Prerequisites

  • Python 3.12+
  • uv (recommended) or pip

Setup

# Clone and install
git clone https://github.com/l4b4r4b4b4/portfolio-mcp
cd portfolio-mcp
uv sync

# Run tests
uv run pytest --cov

# Lint and format
uv run ruff check .
uv run ruff format .

Running Locally

# stdio mode (for MCP clients)
uv run portfolio-mcp stdio

# SSE mode (for web clients)
uv run portfolio-mcp sse --port 8080

# Streamable HTTP mode
uv run portfolio-mcp streamable-http --port 8080

Configuration

Environment variables:

Variable Description Default
LOG_LEVEL Logging level INFO
CACHE_TTL Default cache TTL in seconds 3600

License

MIT License - see LICENSE for details.

Related Projects

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

portfolio_mcp-0.0.4.tar.gz (220.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

portfolio_mcp-0.0.4-py3-none-any.whl (41.2 kB view details)

Uploaded Python 3

File details

Details for the file portfolio_mcp-0.0.4.tar.gz.

File metadata

  • Download URL: portfolio_mcp-0.0.4.tar.gz
  • Upload date:
  • Size: 220.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for portfolio_mcp-0.0.4.tar.gz
Algorithm Hash digest
SHA256 6d2814e3a34a744ed6f75f816b06aa341ac0bf8781c55cc83504d1eaa74fa988
MD5 5b5e3e1e44a4a4216a3f75558e120dc4
BLAKE2b-256 6a3f1ed878b09d9f11c4c92e84bd006501fb1b7e54a5ac745dc187e08d3827f0

See more details on using hashes here.

Provenance

The following attestation bundles were made for portfolio_mcp-0.0.4.tar.gz:

Publisher: publish.yml on l4b4r4b4b4/portfolio-mcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file portfolio_mcp-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: portfolio_mcp-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 41.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for portfolio_mcp-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 31eb7697cca62dbdbf5e1bc4c91eeee415a86aa897a3c8391d80e7e6df6a0ed3
MD5 fdb587050b5404eb6ebca7569815d5b8
BLAKE2b-256 a4a8674a3a156a520e63bc75a5164363ce52297bb7ed4bac5f4c6dea2a5dfe4d

See more details on using hashes here.

Provenance

The following attestation bundles were made for portfolio_mcp-0.0.4-py3-none-any.whl:

Publisher: publish.yml on l4b4r4b4b4/portfolio-mcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page