AKShare MCP服务器
使用AKShare库提供财务数据分析功能的模型上下文协议(MCP)服务器。
特性
- 通过AKShare访问中国和全球金融市场数据
- 通过MCP协议与Claude Desktop集成
- 支持各种财务数据查询和分析
安装
使用紫外线(推荐)
# Clone the repository
git clone https://github.com/yourusername/akshare_mcp_server.git
cd akshare_mcp_server
# Create and activate a virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies with uv
uv pip install -e .使用pip
# Clone the repository
git clone https://github.com/yourusername/akshare_mcp_server.git
cd akshare_mcp_server
# Create and activate a virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -e .用法
运行服务器
# Activate the virtual environment
source venv/bin/activate # On Windows: venv\Scripts\activate
# Run the server
python run_server.py与Claude Desktop集成
- 将以下配置添加到您的Claude Desktop配置中:
"mcpServers": {
"akshare-mcp": {
"command": "uv",
"args": [
"--directory",
"/path/to/akshare_mcp_server",
"run",
"akshare-mcp"
],
"env": {
"AKSHARE_API_KEY": ""
}
}
}- 重新启动克劳德桌面
- 从可用工具中选择AKShare MCP服务器
可用工具
AKShare MCP服务器提供以下工具:
- 股票数据查询
- 资金数据查询
- 债券数据查询
- 期货数据查询
- 外汇数据查询
- 宏观经济数据查询
- 还有更多。..
添加新工具
要向MCP服务器添加新工具,请执行以下步骤:
- 在中添加新的API函数
src/mcp_server_akshare/api.py:
async def fetch_new_data_function(param1: str, param2: str = "default") -> List[Dict[str, Any]]:
"""
Fetch new data type.
Args:
param1: Description of param1
param2: Description of param2
"""
try:
df = ak.akshare_function_name(param1=param1, param2=param2)
return dataframe_to_dict(df)
except Exception as e:
logger.error(f"Error fetching new data: {e}")
raise- 将新工具添加到中的枚举
src/mcp_server_akshare/server.py:
class AKShareTools(str, Enum):
# Existing tools...
NEW_TOOL_NAME = "new_tool_name"- 将新功能导入
src/mcp_server_akshare/server.py:
from .api import (
# Existing imports...
fetch_new_data_function,
)- 将工具定义添加到
handle_list_tools()函数:
types.Tool(
name=AKShareTools.NEW_TOOL_NAME.value,
description="Description of the new tool",
inputSchema={
"type": "object",
"properties": {
"param1": {"type": "string", "description": "Description of param1"},
"param2": {"type": "string", "description": "Description of param2"},
},
"required": ["param1"], # List required parameters
},
),- 在中添加工具处理程序
handle_call_tool()函数:
case AKShareTools.NEW_TOOL_NAME.value:
param1 = arguments.get("param1")
if not param1:
raise ValueError("Missing required argument: param1")
param2 = arguments.get("param2", "default")
result = await fetch_new_data_function(
param1=param1,
param2=param2,
)- 测试新工具 通过运行服务器并向新工具发出请求。
发展
# Install development dependencies
uv pip install -e ".[dev]"
# Run tests
pytest码头工人
您还可以使用Docker运行服务器:
# Build the Docker image
docker build -t akshare-mcp-server .
# Run the Docker container
docker run -p 8000:8000 akshare-mcp-server许可证
麻省理工学院
