portfolio-mcp
A portfolio analysis MCP server powered by mcp-refcache for building AI agent tools that handle financial data efficiently.
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 weightsget_portfolio- Retrieve portfolio details and metricslist_portfolios- List all stored portfoliosdelete_portfolio- Remove a portfolioupdate_portfolio_weights- Modify portfolio weightsclone_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 returnsget_correlation_matrix- Asset correlation analysisget_covariance_matrix- Variance-covariance structureget_individual_stock_metrics- Per-asset statisticsget_drawdown_analysis- Maximum drawdown and recovery analysiscompare_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 curverun_monte_carlo- Monte Carlo simulation for portfolio analysisapply_optimization- Apply optimization and update stored portfolio
Data Tools (8 tools)
generate_price_series- Generate synthetic GBM price datagenerate_portfolio_scenarios- Create multiple scenario datasetsget_sample_portfolio_data- Get sample data for testingget_trending_coins- Trending cryptocurrencies from CoinGeckosearch_crypto_coins- Search for crypto assetsget_crypto_info- Detailed cryptocurrency informationlist_crypto_symbols- Available crypto symbol mappingsget_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:
- Large results are cached - When a tool returns data that exceeds the preview size, it's stored in the cache
- References are returned - The tool returns a
ref_idand a preview/sample of the data - 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
- mcp-refcache - Reference-based caching for MCP servers
- fastmcp-template - Template this project was built from
- FinQuant - Financial portfolio analysis library
Metadata
Release files for portfolio-mcp 0.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| portfolio_mcp-0.0.5.tar.gz | 220.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| portfolio_mcp-0.0.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 261.4 kB
Release files / portfolio_mcp-0.0.5.tar.gz
| Download URL | portfolio_mcp-0.0.5.tar.gz |
|---|---|
| Size | 220.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1265337563a31f4f82be4675df3f06b8802efd465a8f22d9131bf408ca6d2e31
|
|
BLAKE2b-256 checksum How to use checksums |
b506ec12c39e688bd94ba8a57ebce10bfde9419599a079dad992e50968212816
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 15, 2025.
Transparency logRelease files / portfolio_mcp-0.0.5-py3-none-any.whl
| Download URL | portfolio_mcp-0.0.5-py3-none-any.whl |
|---|---|
| Size | 41.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bed500ad5f6ee1fbf3431e3812470071f5811b9d049bd4917d2eeb6c1444d679
|
|
BLAKE2b-256 checksum How to use checksums |
2abace483a75b88289d526a389892364f4f7d8762a768228afbea74fe0348953
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 15, 2025.
Transparency log