MCP ExplorerExplorer

Financial Mcp Agent

@24mlighton 10 months ago
8 GPL-3.0
FreeCommunity
AI Systems
Agent integrated with MCP tools.

Overview

What is Financial Mcp Agent

Financial-MCP-Agent is an integrated agent system designed for comprehensive analysis of A-share stocks, utilizing MCP tools and large language models (LLMs) to provide insights and investment recommendations.

Use cases

Use cases include analyzing stock performance, generating investment strategies based on technical and fundamental data, and providing summarized investment recommendations for traders and investors.

How to use

Users can interact with the Financial-MCP-Agent by accessing its various agents for technical analysis, value analysis, and fundamental analysis, which collectively analyze real-time A-share data to generate actionable insights.

Key features

Key features include multi-dimensional analysis from fundamental, technical, and valuation perspectives, data-driven decision support using real-time A-share data, and AI-driven summarization of analysis results to provide clear investment advice.

Where to use

Financial-MCP-Agent is primarily used in the financial sector, particularly for stock market analysis, investment strategy formulation, and decision-making processes related to A-share investments.

Content

License
Python
LangGraph
MCP Tools

⚠️ 免责声明:本项目仅用于教育目的,不构成任何投资建议。投资有风险,交易需谨慎。

🔍 项目概述

这是一个基于 LangGraph 的金融分析 Agent 系统,用于分析 A 股股票。系统包含四个 Agent:技术分析 Agent、价值分析 Agent、基本面分析 Agent 和总结 Agent。前三个 Agent 通过 MCP 工具获取 A 股相关数据并与大语言模型(LLM)交互;总结 Agent 综合上游数据,提供最终投资建议。


多维度分析

数据驱动

智能决策
系统同时从基本面、技术面和估值三个维度进行分析 利用实时A股数据,提供数据支持的决策建议 AI智能汇总分析结果,提供清晰的投资建议

✨ 功能特点


多 Agent 协作

MCP 工具集成

智能工具选择

数据流传递

投资建议生成

Markdown 报告

自然语言查询

交互式输入

实时数据更新
  • 多 Agent 协作:技术分析、价值分析、基本面分析和总结四个 Agent 协同工作
  • MCP 工具集成:通过langchain-mcp-adapters加载a-share-mcp-v2服务器上的多个工具
  • 智能工具选择:Agent 根据职能设计的 Prompt 智能选择工具,处理上游数据和 MCP 数据
  • 数据流传递:使用AgentState传递数据和元数据,确保信息流畅通
  • 投资建议生成:总结 Agent 综合上游数据,提供 A 股投资建议
  • Markdown 报告:自动生成格式化的 Markdown 分析报告并保存到文件
  • 🆕 自然语言查询:支持任意自然语言查询,无需特定格式
  • 🆕 交互式输入:未提供命令参数时自动进入交互模式

🏗️ 系统架构

系统基于 LangGraph 框架,包含以下组件:

组件 描述
AgentState 自定义的 TypedDict,用于在 Agent 之间传递数据
Agent 四个专业 Agent,各司其职
MCP 工具 通过 MultiServerMCPClient 加载 a-share-mcp-v2 服务器上的多个工具
LLM 交互 每个 Agent 使用绑定的 ChatOpenAI 模型与 LLM 交互
工作流 使用 StateGraph 定义 Agent 执行顺序

🚀 使用方法

环境设置

  1. 安装依赖:

    poetry install
    
    cp .env.example .env
    

或者手动将 .env.example 文件改名为 .env

  1. 设置环境变量(.env 文件):
OPENAI_COMPATIBLE_API_KEY=your_api_key
OPENAI_COMPATIBLE_BASE_URL=your_base_url
OPENAI_COMPATIBLE_MODEL=your_model
  1. 配置 MCP 服务器:

    编辑 src/tools/mcp_config.py 文件,修改 MCP 服务器路径:

    SERVER_CONFIGS = {
        "a_share_mcp_v2": {  # 重命名以提高清晰度,原名为 "a-share-mcp-v2"
            "command": "uv",  # 假设'uv'在PATH中或使用完整路径
            "args": [
                "run",  # uv run命令
                "--directory",
                r"a_share_mcp项目的绝对路径",  # 修改为您已部署好的MCP服务器项目路径,如未部署好,请git clone https://github.com/24mlight/a-share-mcp-is-just-i-need.git, 然后部署
                "python",  # 在uv中运行的命令
                "mcp_server.py"  # MCP服务器脚本
            ],
            "transport": "stdio",
        }
    }
    

    系统通过此配置连接到 A 股 MCP 服务器获取实时金融数据。

运行分析

运行方式

命令行参数模式

交互式模式

方式一:命令行参数模式

通过命令行运行:

poetry run python -m src.main --command "分析股票名称"

例如:

poetry run python -m src.main --command "分析贵州茅台"
poetry run python -m src.main --command "帮我看看比亚迪这只股票怎么样"
poetry run python -m src.main --command "我想了解一下腾讯的投资价值"

方式二:交互式模式 🆕

直接运行程序,系统将自动进入交互模式:

poetry run python -m src.main

系统会显示的欢迎界面和使用指南,然后等待您输入查询。

支持的自然语言查询示例:

  • “分析嘉友国际”
  • “帮我看看比亚迪这只股票怎么样”
  • “我想了解一下腾讯的投资价值”
  • “603871 这个股票值得买吗?”
  • “给我分析一下宁德时代的财务状况”
  • “中国平安现在的估值如何?”

注意: 必须使用 python -m src.main 的模块导入方式运行,而不是直接运行 python src/main.py,这样可以确保正确的导入路径。

输出

系统将在终端显示分析结果,并将完整的 Markdown 格式报告保存到reports目录。

📝 报告示例

报告章节 内容描述
执行摘要 对分析结果的整体概述和关键结论
基本面分析 公司财务状况、经营情况和行业地位分析
技术分析 股价走势、交易量和技术指标分析
估值分析 市盈率、市净率等估值指标分析
交叉验证 不同分析方法的对比验证
投资建议 基于综合分析的投资决策建议

📁 项目结构

Financial-MCP-Agent/
├── .venv/            # 虚拟环境
├── logs/             # 执行日志
├── reports/          # 生成的分析报告
├── src/
│   ├── agents/       # Agent实现
│   │   ├── fundamental_agent.py  # 基本面分析智能体
│   │   ├── technical_agent.py    # 技术面分析智能体
│   │   ├── value_agent.py        # 估值分析智能体
│   │   └── summary_agent.py      # 总结智能体
│   ├── tools/        # 工具实现
│   │   ├── mcp_client.py        # MCP客户端实现
│   │   ├── mcp_config.py        # MCP服务器配置
│   │   └── openrouter_config.py # OpenRouter配置
│   ├── utils/        # 工具函数
│   │   ├── execution_logger.py  # 执行日志系统
│   │   ├── log_viewer.py        # 日志查看器
│   │   ├── logging_config.py    # 日志配置
│   │   ├── llm_clients.py       # LLM客户端
│   │   └── state_definition.py  # 状态定义
│   └── main.py       # 主程序
├── tests/            # 测试
├── .env              # 环境变量
├── .env.example      # 环境变量示例
├── .gitignore        # Git忽略文件
├── logging_system.md # 日志系统说明
├── poetry.lock       # Poetry依赖锁定
├── pyproject.toml    # 项目配置
├── README.md         # 项目说明
└── test_agent_with_real_tools.py # 测试脚本

⚠️ 关于 terminal 打印的信息

Exception ignored in: <function BaseSubprocessTransport.__del__ at 0x00000217922E20C0>
Traceback (most recent call last):
  File "D:\python3.11\Lib\asyncio\base_subprocess.py", line 126, in __del__
    self.close()
  File "D:\python3.11\Lib\asyncio\base_subprocess.py", line 104, in close
    proto.pipe.close()
  File "D:\python3.11\Lib\asyncio\proactor_events.py", line 109, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "D:\python3.11\Lib\asyncio\base_events.py", line 761, in call_soon
    self._check_closed()
  File "D:\python3.11\Lib\asyncio\base_events.py", line 519, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

上述 error 可以忽略,这是由于异步执行未能正确关闭。不影响系统运行,可以忽视。

Tools

No tools

Comments

Recommend MCP Servers

View All MCP Servers