本项目借助 MCP(Multi - Modal Protocol)协议,实现 AI 助手与智兔数服所提供的沪深股票数据服务的无缝对接。通过该方案,AI 能够直接调用实时股票数据和市场信息,为投资分析与决策提供有力支持。
在项目的主文件中,初始化一个 MCP 代理实例:
from mcp import Agent
# 创建 MCP 代理实例
agent = Agent(
name="TradingAssistant",
description="A comprehensive trading assistant integrating with MCP protocol.",
tools=TOOLS.values()
)
在 mcp_z兔_server.py 文件中定义可使用的工具:
# 工具列表
TOOLS = {
"get_stock_list": get_stock_list,
"get_new_stock_calendar": get_new_stock_calendar,
"get_company_profile": get_company_profile,
"get_capital_daily_trend": get_capital_daily_trend,
"get_all_announcements": get_all_announcements
}
启动 MCP 代理服务:
if __name__ == "__main__":
agent.run()
mcp - client 库以实现与 AI 代理的通信。# 安装 MCP 客户端库
pip install mcp - client
# 克隆本项目仓库
git clone https://github.com/your - username/mcp - trading.git
cd mcp - trading
在项目中使用 MCP 协议与智兔数服 API 集成的基础流程如下:
# 初始化 MCP 服务
from mcp import Agent
# 创建 MCP 代理实例
agent = Agent(
name="TradingAssistant",
description="A comprehensive trading assistant integrating with MCP protocol.",
tools=TOOLS.values()
)
# 定义可用工具
TOOLS = {
"get_stock_list": get_stock_list,
"get_new_stock_calendar": get_new_stock_calendar,
"get_company_profile": get_company_profile,
"get_capital_daily_trend": get_capital_daily_trend,
"get_all_announcements": get_all_announcements
}
# 启动服务
if __name__ == "__main__":
agent.run()
当前已实现的工具如下:
| 工具名 | 描述 | 参数要求 |
|---|---|---|
get_stock_list |
获取基础 A 股股票列表(代码、名称、交易所) | 无 |
get_new_stock_calendar |
获取新股日历(申购信息、上市日期等) | 无 |
get_company_profile |
获取指定股票代码的上市公司简介 | stock_code: 股票代码 |
get_capital_daily_trend |
获取指定股票代码的每日资金流入趋势(近十年) | stock_code: 股票代码 |
get_all_announcements |
获取指定股票代码的历史所有公告列表 | stock_code: 股票代码 |
访问 智兔数服 API 文档 查找所需功能的 API 端点。
在 mcp_z兔_server.py 中,按照现有工具函数的模式,开发新的 Python 函数以调用目标 API。确保处理好参数和返回值,并添加清晰的注释。
将新工具注册到 TOOLS 字典中:
# 新工具示例
def new_tool_function(params):
# 实现逻辑
pass
TOOLS["new_tool"] = new_tool_function
创建一个配置文件 config.json:
{
"mcp": {
"host": "localhost",
"port": 5000,
"timeout": 30
},
"api_key": "your_api_key_here"
}
TOOLS 字典中。本项目遵循 MIT 协议。请查看 LICENSE 文件获取详细信息。
欢迎贡献!请参考 CONTRIBUTING.md 获取参与方式。
通过以上步骤,您可以轻松地将 MCP 协议与智兔数服 API 集成,为您的 AI 项目增添强大的金融数据处理能力。