加密货币新闻分析服务

一个基于Spring Boot的MCP服务器实现,提供加密货币新闻分析工具,支持AI助手如Cursor和Cline。

  • by dpolishuk
  • Java
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 dpolishuk/spring-boot-ai-mcp-server

使用说明

🚀 Spring Boot MCP服务器-加密新闻

模型上下文协议(MCP)服务器的Spring Boot实现,为Cursor和Cline等AI助手提供加密货币新闻分析工具。

📋 需求

  • Java 17+
  • Maven 3.6+
  • 端口8080可用

🏗️ 建筑

系统架构

graph TB
    subgraph "AI Assistants"
        A1[Cursor]
        A2[Cline]
    end
    
    subgraph "Transport Layer"
        T1[HTTP Transport]
        T2[SSE Transport]
    end
    
    subgraph "Spring Boot MCP Server"
        C1[McpJsonRpcController]
        C2[McpStandardSseController]
        S1[JsonRpcMcpServer]
        S2[NewsService]
        S3[TrackedTokenService]
    end
    
    subgraph "Data Layer"
        D1[SQLite Database]
        D2[Caffeine Cache]
    end
    
    A1 -->|HTTP| T1
    A2 -->|SSE| T2
    T1 --> C1
    T2 --> C2
    C1 --> S1
    C2 --> S1
    S1 --> S2
    S1 --> S3
    S2 --> D1
    S2 --> D2
    S3 --> D1

类图

classDiagram
    class JsonRpcMcpServer {
        -NewsService newsService
        -TrackedTokenService tokenService
        -ObjectMapper objectMapper
        -Map~String,SseEmitter~ sessions
        +handleMessage(sessionId, message)
        +createSession(sessionId) SseEmitter
        +initialize(params) Map
        +listTools() Map
        +callTool(name, arguments) Map
    }
    
    class McpStandardSseController {
        -JsonRpcMcpServer mcpServer
        +handleSse() SseEmitter
        +handleMessage(message, sessionId)
    }
    
    class NewsService {
        -NewsRepository newsRepository
        +searchNews(keyword) List~News~
        +getLatestNews(limit) List~News~
        +analyzeSentiment(token) SentimentAnalysis
    }
    
    class TrackedTokenService {
        -TrackedTokenRepository tokenRepository
        +trackToken(symbol) TrackedToken
        +getTrackedTokens() List~TrackedToken~
    }
    
    McpStandardSseController --> JsonRpcMcpServer
    JsonRpcMcpServer --> NewsService
    JsonRpcMcpServer --> TrackedTokenService

MCP协议序列图

sequenceDiagram
    participant AI as AI Assistant<br/>(Cursor/Cline)
    participant SSE as SSE Endpoint<br/>(/mcp/v1/sse)
    participant MSG as Message Endpoint<br/>(/mcp/v1/messages)
    participant MCP as JsonRpcMcpServer
    participant SVC as Services<br/>(News/Token)
    participant DB as SQLite DB

    Note over AI,DB: SSE Transport Flow
    
    AI->>SSE: GET /mcp/v1/sse
    SSE->>SSE: Create session
    SSE-->>AI: SSE Stream opened
    SSE-->>AI: event: endpoint<br/>data: /mcp/v1/messages?sessionId=xxx

    AI->>MSG: POST /mcp/v1/messages?sessionId=xxx<br/>{"method": "initialize", ...}
    MSG->>MCP: handleMessage(sessionId, message)
    MCP->>MCP: process initialize
    MCP-->>SSE: Send response via SSE
    SSE-->>AI: event: message<br/>data: {"result": {...}}

    AI->>MSG: POST {"method": "tools/list", ...}
    MSG->>MCP: handleMessage(sessionId, message)
    MCP->>MCP: listTools()
    MCP-->>SSE: Send tools list
    SSE-->>AI: event: message<br/>data: {"result": {"tools": [...]}}

    AI->>MSG: POST {"method": "tools/call",<br/>"params": {"name": "search_news",<br/>"arguments": {"keyword": "bitcoin"}}}
    MSG->>MCP: handleMessage(sessionId, message)
    MCP->>SVC: searchNews("bitcoin")
    SVC->>DB: SELECT * FROM news WHERE ...
    DB-->>SVC: News results
    SVC-->>MCP: List<News>
    MCP-->>SSE: Send results
    SSE-->>AI: event: message<br/>data: {"result": {"content": [...]}}

    Note over AI,DB: HTTP Transport Flow (Cursor)
    
    AI->>MSG: POST /mcp/messages<br/>{"method": "initialize", ...}
    MSG->>MCP: handleMessage(null, message)
    MCP->>MCP: process initialize
    MCP-->>MSG: Synchronous response
    MSG-->>AI: HTTP 200<br/>{"result": {...}}

🛠️ 快速开始

1.克隆和构建

git clone https://github.com/dpolishuk/spring-boot-ai-mcp-server.git
cd spring-boot-ai-mcp-server
mvn clean install

2.启动服务器

mvn spring-boot:run

3.验证安装

curl http://localhost:8080/actuator/health
# Should return: {"status":"UP"}

🔧 配置

对于光标

  1. 创建 .cursor/mcp.json 在项目根目录中:
{
  "mcpServers": {
    "crypto-news": {
      "url": "http://localhost:8080/mcp/messages",
      "transport": "http"
    }
  }
}
  1. 重新启动游标

  2. 聊天测试:

"What crypto news tools are available?"

对于Cline(VS代码)

  1. 打开设置: Cmd+Shift+P → “临床:打开设置”

  2. 添加到“MCP服务器”部分:

{
  "mcpServers": {
    "crypto-news": {
      "type": "sse",
      "url": "http://localhost:8080/mcp/v1/sse"
    }
  }
}
  1. 保存并重新启动Cline

📡 可用端点

1.HTTP传输(直接API)

  • 统一资源定位符: http://localhost:8080/mcp/messages
  • 方法:POST
  • 内容类型:应用程序/json

2.SSE传输(服务器发送事件)

  • SSE URL: http://localhost:8080/mcp/v1/sse
  • 邮件URL: http://localhost:8080/mcp/v1/messages?sessionId={sessionId}

🛠️ 可用工具

工具 说明 参数 示例用法
search_news 按关键字搜索加密新闻 keyword (字符串,必填) “搜索比特币新闻”
get_latest_news 获取最新的加密货币新闻 limit (整数,可选) “获取最新加密货币新闻”
track_token 追踪加密货币代币 symbol (字符串,必填) “追踪ETH代币”
get_tracked_tokens 列出跟踪的令牌 无 “显示跟踪的令牌”
analyze_sentiment 分析代币情绪 token (字符串,必填) “分析BTC情绪”

📝 使用示例

在光标/斜线中:

User: Find news about Ethereum
Assistant: I'll search for Ethereum news...
[Calls search_news with keyword="Ethereum"]

User: What's the sentiment for BTC?
Assistant: Let me analyze Bitcoin sentiment...
[Calls analyze_sentiment with token="BTC"]

🧪 测试

测试HTTP端点:

curl -X POST http://localhost:8080/mcp/messages \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","method":"initialize","params":{},"id":1}'

测试SSE端点:

curl http://localhost:8080/mcp/v1/sse -H "Accept: text/event-stream"

测试工具调用:

curl -X POST http://localhost:8080/mcp/messages \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "method": "tools/call",
    "params": {
      "name": "search_news",
      "arguments": {"keyword": "bitcoin"}
    },
    "id": 2
  }'

🌟 特性

  • ✅ 完全支持MCP协议(v1.0和v2025-03-26)
  • ✅ 多种传输机制(HTTP、SSE)
  • ✅ 用于持久存储的SQLite数据库
  • ✅ 咖啡因缓存提高性能
  • ✅ 用于直接访问的RESTful API
  • ✅ 加密货币的情绪分析
  • ✅ 实时SSE更新
  • ✅ 综合录井

🏛️ 项目结构

spring-boot-ai-mcp-server/
├── src/main/java/com/example/
│   ├── mcp/
│   │   └── JsonRpcMcpServer.java      # Core MCP implementation
│   ├── controller/
│   │   ├── McpJsonRpcController.java   # HTTP/SSE endpoint /mcp/*
│   │   ├── McpStandardSseController.java # SSE endpoint /mcp/v1/*
│   │   ├── NewsController.java         # REST API for news
│   │   └── TokenController.java        # REST API for tokens
│   ├── service/
│   │   ├── NewsService.java            # News business logic
│   │   └── TrackedTokenService.java    # Token management
│   └── entity/
│       ├── News.java                   # JPA entity for news
│       └── TrackedToken.java           # JPA entity for tokens
├── crypto_news.db                      # SQLite database
├── application.yml                     # Spring configuration
└── pom.xml                            # Maven configuration

🐛 故障排除

端口已在使用中

lsof -i :8080
kill -9 <PID>

SSE连接失败(临床)

  • 验证URL是否 http://localhost:8080/mcp/v1/sse
  • 检查服务器日志
  • 确保服务器正在运行

没有可用的工具

  • 确保服务器正在运行
  • 检查 crypto_news.db 文件存在
  • 验证是否调用了初始化

启用调试日志记录

在……里面 application.yml:

logging:
  level:
    com.example: DEBUG

📚 额外资源

🔒 协议支持

  • MCP协议版本:1.0和2025-03-26
  • JSON-RPC版本: 2.0
  • 运输类型:HTTP、SSE
  • 内容类型:应用程序/json、文本/事件流

内置于❤️ 使用Spring Boot和MCP