TradingAssistantMCP
TradingAssistantMCP
一个学习项目:一个 MCP(Model Context Protocol)服务器,让代理(例如 Claude)能够获取股票数据,并最终运行回测和自定义交易策略。
设置
需要 Python 3.11+。目前还不需要 API 密钥(当前工具使用 yfinance,无需注册)。
python3 -m venv .venv
.venv/bin/pip install -e .这会在 .venv/ 中创建一个隔离环境,并将此项目的依赖安装到其中。.venv/ 已被 gitignore 忽略——任何克隆此仓库的人都需要自己运行这两条命令,一切才能正常工作。
Related MCP server: Yahoo Finance MCP Server
运行服务器
服务器(src/trading_assistant_mcp/server.py)并非设计为由你直接运行——它是由 MCP 客户端(代理)通过 stdio 启动的。有两种方式可以试用它:
1. MCP Inspector(手动测试,无需代理)——一个用于直接调用工具的浏览器 UI:
.venv/bin/mcp dev src/trading_assistant_mcp/server.py会打开一个本地 URL(例如 http://127.0.0.1:6274)。点击 Connect,进入 Tools 选项卡,然后手动调用工具。
需要安装 uv(mcp dev 在内部通过 uv run 启动服务器,与上面的 venv 无关)。
2. Claude Code(真实代理)——已在 .mcp.json 中注册为项目级 MCP 服务器。在此目录中打开 Claude Code 会话,直接提问,例如"AAPL 的价格是多少?"——Claude 会自行决定是否调用该工具。
可移植性说明:
.mcp.json硬编码了本机.venv/bin/python的绝对路径。如果你在其他位置克隆此仓库,请重新运行:claude mcp add -s project trading-assistant -- /absolute/path/to/.venv/bin/python /absolute/path/to/src/trading_assistant_mcp/server.py
目前已有的功能
get_quote(symbol)— 股票代码的当前价格、当日区间、成交量。
项目结构
src/trading_assistant_mcp/
├── server.py # MCP server: wraps data functions as tools
├── data/
│ └── yfinance_client.py # plain Python, no MCP dependency — reusable/testable on its own
└── analysis/ # reserved for indicators (SMA/EMA/RSI/MACD), not yet builtserver.py 有意设计为一个薄适配层:它是唯一导入 mcp 的文件。数据获取和(后续的)分析逻辑保留在普通 Python 模块中,以便它们独立于协议层进行测试和复用。
路线图
先进行当前数据分析(K线、基本面/比率、技术指标),然后再进行回测和自定义自然语言策略(例如"每次 AMD 下跌 1% 时买入,每次上涨 5% 时卖出")。完整计划请参阅项目决策。
Available Tools
4 toolsget_candlesA
Get historical OHLCV candlestick bars for a stock symbol.
Args: symbol: Stock ticker symbol, e.g. "AAPL" or "MSFT". period: How far back to fetch, e.g. "1mo", "3mo", "6mo". interval: Bar size, e.g. "1h", "1d".
Inputs: symbol (required), period, interval. Returns: result
get_fundamentalsA
Get valuation/financial ratios for a stock symbol (P/E, market cap, EPS, dividend yield, beta, 52-week range, margins, growth, etc.).
Fields may be null if not available for a given symbol/exchange.
Args: symbol: Stock ticker symbol, e.g. "AAPL" or "MSFT".
Inputs: symbol (required).
get_quoteA
Get the current price and day range for a stock symbol.
Args: symbol: Stock ticker symbol, e.g. "AAPL" or "MSFT".
Inputs: symbol (required).
get_technical_indicatorsA
Compute standard technical indicators (SMA-20/50, RSI-14, MACD, Bollinger Bands, ATR-14, volume vs its 20-period average) for a stock symbol.
Fetches its own candle data internally (not via get_candles - that tool's raw output is too large to round-trip through a tool call). Needs at least 50 candles for every indicator to be non-null (fewer still works, but indicators requiring more history than is available return null).
Args: symbol: Stock ticker symbol, e.g. "AAPL" or "MSFT". period: How far back to fetch, e.g. "1mo", "3mo", "6mo". interval: Bar size, e.g. "1h", "1d".
Inputs: symbol (required), period, interval.
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 4 tools
Each tool targets a distinct data domain: real-time quote, fundamentals, historical OHLCV, and computed indicators. Descriptions clearly separate concerns, so an agent should not confuse them.
All tools follow a consistent get_<noun> pattern with snake_case naming. The naming convention is uniform and predictable across the entire set.
Four tools is a focused, well-scoped set for a market-data-only server. Each tool covers a meaningful and non-redundant aspect of stock research.
The set covers the core read-only stock data needs: real-time price, fundamentals, historical price data, and technical indicators. Minor gaps exist, such as no symbol search/disambiguation or batch quote support, but these are workable around.
Maintenance
Related MCP Connectors
Live stock quotes, a stock screener, crypto prices and FX rates as agent tools.
Yahoo Finance stock quotes and multi-ticker comparison as agent tools.
Yahoo Finance for AI agents: quotes, price history, options with Greeks, financials, news, movers.
Live crypto + IDX stock market intelligence for AI agents — 19 tools, no API key.
Related MCP Servers
- FlicenseCqualityDmaintenanceEnables LLMs to retrieve real-time stock and cryptocurrency prices and news using ticker symbols through the yfinance API.25-
- AlicenseNot gradedqualityDmaintenanceEnables LLMs to retrieve stock market data and financial information from Yahoo Finance using the yfinance Python library. Supports querying stock prices, historical data, and other financial metrics through natural language.MIT
- AlicenseNot gradedqualityCmaintenanceProvides real-time stock quotes, historical data, and stock search via Yahoo Finance, enabling AI assistants to access and analyze financial market data.24 npm19MIT
- AlicenseNot gradedqualityDmaintenanceEnables AI assistants to access real-time stock, crypto, and market index data from Yahoo Finance, supporting queries for prices, history, company info, and comparisons.MIT