Tushare MCP Server

Tushare MCP Server

Provides access to Tushare financial data through the Model Context Protocol, enabling users to query and retrieve Chinese stock market and financial information.

Category
访问服务器

README

Tushare MCP Server

This is a Model Context Protocol (MCP) server that provides access to Tushare financial data.

Prerequisites

  • Python 3.10 or higher
  • A Tushare Token (Get it from Tushare.pro)

Installation

  1. Install the dependencies:
pip install -r requirements.txt

Configuration

  1. Create a .env file in the project root directory:
    TUSHARE_TOKEN=your_tushare_token_here
    

Project Layout

  • server/: MCP entrypoint (server.py) plus logs.
  • src/: application code split into strategies/ (wheel backtest, etc.) and utils/ (shared math + performance helpers).
  • temp_data/: generated artifacts such as wheel_report.html, wheel_dashboard.html, and portfolio CSV/JSON outputs.
  • tests/: standalone scripts for strategy prototyping (wheel_strategy.py, portfolio_rebalance.py, etc.).

Using with GitHub Copilot in VS Code

To use this MCP server with GitHub Copilot in VS Code, you need to configure the mcp.json file.

  1. Open Configuration:

    • Open the Command Palette (Ctrl+Shift+P or F1).
    • Search for and select MCP: Configure MCP Servers.
    • This will open the mcp.json file (typically located in %APPDATA%\Code\User\mcp.json on Windows).
  2. Add via Command Palette (quick way):

  • Press Ctrl+Shift+P again and pick MCP: Add MCP Server.
  • When the Enter Command prompt appears, paste:
```
C:\Users\lochen\AppData\Local\Microsoft\WindowsApps\python3.13.exe c:\Users\lochen\tushare-mcp\server\server.py
```
  • Accept the suggested server name (for example tushare) and save.
  1. Add Server Configuration manually: Add the tushare-server configuration to the JSON file. Make sure to use absolute paths for both the Python executable and the script.

    {
      "mcpServers": {
        "tushare-server": {
          "command": "C:\\path\\to\\your\\python.exe",
          "args": [
            "C:\\path\\to\\tushare_mcp_server\\server\\server.py"
          ]
        }
      }
    }
    
    • Replace C:\\path\\to\\your\\python.exe with your actual Python interpreter path (e.g., C:\\Users\\username\\AppData\\Local\\Programs\\Python\\Python311\\python.exe).
    • Replace C:\\path\\to\\tushare_mcp_server\\server\\server.py with the absolute path to this project's server entry point.
  2. Restart VS Code: After saving mcp.json, restart VS Code for the changes to take effect.

Usage

Testing with MCP Inspector

You can test the server using the MCP Inspector:

npx @modelcontextprotocol/inspector python server/server.py

Price Volatility Tool

After the server is running (Inspector, Copilot, etc.), invoke the get_price_volatility tool to compute recent volatility for a stock. It supports frequency values daily, monthly, or yearly, letting you measure 波动率 on日线/月线/年线 data.

{
  "tool": "get_price_volatility",
  "args": {
    "identifier": "000001.SZ",
    "window": 30,
    "frequency": "daily",
    "annualize": true
  }
}

Key fields returned:

  • frequency: 数据频率(daily/monthly/yearly)。
  • window_periods: 实际使用的周期数(交易日/月份/年份)。
  • period_volatility: 该频率下的标准差。
  • annualized_volatility: 根据频率自动换算后的年化波动率(252/12/1)。
  • mean_period_return: 平均单周期收益。

Option Reference & Daily Data

基于 Tushare 文档 #157 新增了两个期权工具:

  • get_option_basic(exchange, fields): 返回上/深交所上市期权的合约元数据(行权价、类型、到期日等)。
  • get_option_daily(ts_code, trade_date, start_date, end_date, exchange): 返回期权日线行情。

示例(通过 MCP 调用 get_option_daily 查询科创50期权在单日的 K 线):

{
  "tool": "get_option_daily",
  "args": {
    "exchange": "SSE",
    "trade_date": "20251203"
  }
}

返回值为 JSON 数组,字段与 Tushare opt_daily 接口一致,可配合 get_price_volatility 等工具进一步分析期权策略。

ETF/Fund Daily Quotes

使用新的 get_fund_daily 工具(Tushare 文档 #127 对应接口)可直接拉取 ETF/场内基金的日线行情:

{
  "tool": "get_fund_daily",
  "args": {
    "ts_code": "159915.SZ",
    "start_date": "20250101",
    "end_date": "20251203",
    "fields": "ts_code,trade_date,open,high,low,close,vol"
  }
}

fields 为空时会返回默认全部列。可搭配 get_option_dailyget_price_volatility 等工具构建 ETF+期权策略分析。

Wheel Strategy Backtest

tests/wheel_strategy.py 利用 159915.SZ(创业板ETF)及其期权,ETF 行情通过 Tushare fund_daily 接口(文档 #127)获取,模拟“车轮饼”策略:

  1. 每个自然月首个交易日:
  • 若空仓,卖出 5%~10% OTM 的认沽合约(call_put='P')。
  • 若持仓,卖出 5%~10% OTM 的认购合约(call_put='C')。
  1. 到期日根据标的收盘价判断是否被指派,按轮动逻辑更新持仓。
  2. 统计权利金、被指派次数、最大保证金占用,并估算收益率。
  3. 报告会显示每笔交易的隐含波动率:若 Tushare 返回该字段则直接使用,否则基于 Black-Scholes(假定 2% 无风险利率)用成交价反推出一个参考值。

运行:

python tests/wheel_strategy.py

输出包含总收益、占用保证金、近10期交易记录等。所有生成的 wheel_report.htmlwheel_dashboard.html 等文件都会写入 temp_data/,便于统一清理。若需调整标的、时间区间或虚值区间,可编辑脚本顶部的常量(UNDERLYINGSTART_DATEOTM_RANGE 等)。

Wheel Strategy MCP Tool

无需运行脚本,也可直接通过 MCP 调用 backtest_wheel_strategy

{
  "tool": "backtest_wheel_strategy",
  "args": {
    "underlying": "159915.SZ",
    "start_date": "20230101",
    "end_date": "20251203",
    "otm_min": 0.05,
    "otm_max": 0.10,
    "initial_capital": 30000
  }
}

返回 JSON 中包含:

  • ending_valuereturn_on_capitalannualized_return 等指标。
  • recent_trades:近 12 期的期权选择、权利金、行权价及是否被指派。

可通过参数更换标的(只要该 ETF 有挂牌期权)、调整虚值区间或回测时间窗。确保 Tushare Token 对 fund_dailyopt_basicopt_daily 接口有权限。

Multi-ETF Portfolio Backtest

tests/portfolio_rebalance.py 按照截图中的 10 只 ETF 及固定权重构建组合,并在每个自然月首个交易日动态再平衡:

python tests/portfolio_rebalance.py

脚本会:

  • 自动获取所有 ETF 的可用历史区间,并截取重叠部分;
  • 计算每日组合净值并输出 temp_data/portfolio_equity_curve.csv,再平衡明细写入 temp_data/portfolio_rebalances.csv
  • 生成 temp_data/portfolio_vs_benchmarks.csv,其中包含组合与沪深300/中证500/创业板指的归一化指数曲线;
  • temp_data/portfolio_summary.json 中汇总收益率、年化波动率、最大回撤、Sharpe Ratio 及各基准指数的对比指标。

Using with Claude Desktop

Add the following configuration to your claude_desktop_config.json:

{
  "mcpServers": {
    "tushare": {
      "command": "python",
      "args": ["C:\\Users\\lochen\\tushare_mcp_server\\server\\server.py"],
      "env": {
        "TUSHARE_TOKEN": "your_token_here"
      }
    }
  }
}

Make sure to replace your_token_here with your actual Tushare token.

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选