金融情感分析服务器

一个轻量级的MCP服务器,提供FinBERT模型用于金融文本的情感分析,支持批量处理和异步操作。

  • by Traia-IO
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 Traia-IO/finbert-mcp-server-prod

使用说明

FinBERT MCP服务器

这是一个轻量级的MCP(模型上下文协议)服务器,提供对FinBERT模型的访问,用于金融情绪分析。它使AI代理和LLM能够使用专门的BERT模型分析财务文本,该模型对财务数据进行了微调,并针对生产使用进行了优化。

特性

  • 🔧 MCP协议:基于模型上下文协议构建,实现无缝的人工智能集成
  • 🤖 轻量级FinBERT模型:专注于情绪分析以获得最佳性能
  • 🚀 懒加载:模型按需加载,以实现高效的内存使用
  • 📊 批处理:通过批量情感操作高效分析多个文本
  • 🐳 Docker支持:使用Docker和Docker Compose轻松部署
  • ⚡ 异步操作:内置FastMCP,实现高效异步处理
  • 🎯 生产就绪:针对生产环境进行了优化,资源使用率最低

模型信息

此服务器提供对轻量级FinBERT模型的访问:

  • 进程AI/finbert:用于金融情绪分析的原始FinBERT模型-针对生产用途进行了优化

可用工具

此轻量级服务器提供以下工具:

1. finbert_sentiment_analysis

使用FinBERT模型分析金融文本的情绪。

参数:

  • text (string):用于分析情绪的金融文本

退货:

  • sentiment:预测情绪(积极、消极、中性)
  • confidence:置信度得分(0-1)
  • all_scores:所有带有分数的情绪预测数组
  • model_used:用于分析的模型名称
  • timestamp:分析时间戳

2. analyze_financial_text_batch

批量分析多个财务文本,提高情绪分析效率。

参数:

  • texts (array):要分析的财务文本列表(最多50个)
  • analysis_type (string):分析类型(仅支持“情绪”)

退货:

  • results:每个文本的情绪分析结果数组
  • analysis_type:分析类型(“情绪”)
  • model_used:用于分析的模型名称
  • total_texts:处理的文本数量

3. get_finbert_model_info

获取有关FinBERT模型及其当前状态的信息。

退货:

  • models:有关可用型号的信息
  • supported_operations:可用工具列表
  • limitations:型号限制(文本长度、批量大小)
  • documentation:相关文件的链接

安装

使用Docker(推荐)

  1. 克隆此存储库:

    git clone https://github.com/Traia-IO/finbert-mcp-server.git
    cd finbert-mcp-server
    
  2. 使用Docker运行:

    ./run_local_docker.sh
    

使用Docker Compose

  1. 创建一个 .env 使用您的配置文件:

    PORT=8000
    LOG_LEVEL=INFO
    
  2. 启动服务器:

    docker-compose up
    

手动安装

  1. 使用安装依赖项 uv:

    uv pip install -e .
    
  2. 运行服务器:

    uv run python -m server
    

用法

健康检查

测试服务器是否正在运行:

python mcp_health_check.py

使用CrewAI

from traia_iatp.mcp.traia_mcp_adapter import create_mcp_adapter

# Connect to the MCP server
with create_mcp_adapter(
    url="http://localhost:8000/mcp/"
) as tools:
    # Use the tools
    for tool in tools:
        print(f"Available tool: {tool.name}")
        
    # Example: Analyze sentiment of financial text
    result = await tool.finbert_sentiment_analysis(
        text="The company reported strong quarterly earnings, exceeding expectations."
    )
    print(f"Sentiment: {result['sentiment']}, Confidence: {result['confidence']}")
    
    # Example: Batch sentiment analysis
    texts = [
        "Revenue growth exceeded forecasts this quarter.",
        "The company faces regulatory challenges ahead.",
        "Investment in new technology platforms shows promise."
    ]
    result = await tool.analyze_financial_text_batch(
        texts=texts,
        analysis_type="sentiment"
    )
    print(f"Batch sentiment analysis completed for {result['total_texts']} texts")
    
    # Example: Get model information
    result = await tool.get_finbert_model_info()
    print(f"Available models: {list(result['models'].keys())}")

示例响应

情感分析

{
  "status": "success",
  "sentiment": "positive",
  "confidence": 0.8945,
  "all_scores": [
    {"label": "positive", "score": 0.8945},
    {"label": "neutral", "score": 0.0892},
    {"label": "negative", "score": 0.0163}
  ],
  "text_length": 67,
  "model_used": "ProsusAI/finbert",
  "timestamp": "2024-01-15T10:30:00Z"
}

批次分析

{
  "status": "success",
  "analysis_type": "sentiment",
  "model_used": "ProsusAI/finbert",
  "total_texts": 3,
  "results": [
    {
      "index": 0,
      "text": "Revenue growth exceeded forecasts this quarter.",
      "prediction": "positive",
      "confidence": 0.9234,
      "all_scores": [...]
    },
    // ... more results
  ],
  "timestamp": "2024-01-15T10:30:00Z"
}

发展

测试服务器

  1. 在本地启动服务器
  2. 运行健康检查: python mcp_health_check.py
  3. 使用CrewAI适配器测试单个工具

模型加载

模型在首次使用时延迟加载,并缓存以备后续请求。服务器在启动后启动一个后台线程来预加载模型,以获得更好的性能。

添加新工具

要添加新工具,请编辑 server.py 以及:

  1. 如果需要,创建新的模型初始化函数
  2. 添加 @mcp.tool() 装饰功能
  3. 使用新工具更新此README
  4. 更新 deployment_params.json 工具名称位于功能数组中

部署

部署配置

这 deployment_params.json 文件包含此MCP服务器的部署配置:

{
  "github_url": "https://github.com/Traia-IO/finbert-mcp-server",
  "mcp_server": {
    "name": "finbert-mcp",
    "description": "Lightweight pre-trained BERT-based model fine‑tuned on financial text for sentiment analysis (positive/negative/neutral), optimized for production use.",
    "server_type": "streamable-http",
    "requires_api_key": false,
    "capabilities": [
      "finbert_sentiment_analysis",
      "analyze_financial_text_batch",
      "get_finbert_model_info"
    ]
  },
  "deployment_method": "cloud_run",
  "gcp_project_id": "traia-mcp-servers",
  "gcp_region": "us-central1",
  "tags": ["finbert", "financial", "sentiment", "analysis", "bert", "nlp", "ai", "text-processing", "lightweight", "production-ready"],
  "ref": "main"
}

重要:始终更新 capabilities 添加或删除工具时使用数组!

谷歌云运行

此服务器旨在部署在Google Cloud Run上。部署将:

  1. 从Dockerfile构建一个容器
  2. 使用指定配置部署到Cloud Run
  3. 暴露 /mcp 客户端连接的端点

环境变量

  • PORT:服务器端口(默认值:8000)
  • STAGE:环境阶段(默认:MAINNET,选项:MAINNET、TESTNET)
  • LOG_LEVEL:日志记录级别(默认值:INFO)

型号规格

文本限制

  • 最大文本长度:512个字符(文本会自动截断)
  • 批量大小限制:每批请求50条文本
  • 支持的语言:仅限英语

演出

  • 模型按需加载(延迟加载)
  • 后台预加载以获得更好的性能
  • 为后续请求缓存模型
  • 高效的批处理

故障排除

  1. 服务器未启动:使用以下命令检查Docker日志 docker logs <container-id>
  2. 连接错误:确保服务器在预期端口上运行
  3. 工具错误:检查服务器日志以获取详细的错误消息
  4. 模型加载问题:确保有足够的内存和磁盘空间用于模型下载
  5. CUDA错误:GPU支持是可选的;如果CUDA不可用,模型将在CPU上运行

贡献

  1. 分叉存储库
  2. 创建要素分支
  3. 实施新工具或改进
  4. 更新README和deployment_params.json
  5. 提交拉取请求

许可证

MIT许可证