知识存储服务器

一个基于Spring Boot构建的MCP知识服务器,提供智能访问知识存储的功能,具备丰富的元数据能力,支持高效文件发现和大文件处理。

  • by korshak1962
  • Java
  • 本地部署
  • 更新于
在 GitHub 查看 korshak1962/mcp-knowledge-server

使用说明

具有增强元数据的MCP知识服务器

使用Spring Boot构建的模型上下文协议(MCP)服务器,提供对具有丰富元数据功能的知识库的智能访问,以实现高效的文件发现和大型文件处理。

🚀 主要特点

  • 智能元数据系统:丰富的文件描述、标签、类别和摘要
  • 大文件智能:自动检测和处理超出上下文限制的文件
  • 多格式支持:使用Apache Tika处理文本文件、PDF、图像等
  • MCP协议:完整的MCP 2024-11-05协议实施
  • WebSocket接口:通过WebSocket进行实时通信
  • REST API:用于测试和文件上传的其他HTTP端点
  • 智能搜索:按内容或元数据(标签、描述、类别)搜索
  • 类别自动检测:自动文件分类
  • Spring Boot:基于强大的Spring Boot框架构建

🎯 已解决的问题:大文件上下文管理

此服务器智能地处理超出Claude上下文窗口的大文件的挑战:

  • 元数据优先:Claude可以在不阅读完整内容的情况下查看文件描述和摘要
  • 尺寸警告:大于50KB的文件会被标记为建议先阅读摘要
  • 智能发现:在内容搜索之前通过元数据搜索查找相关文件
  • 类别导航:按类型组织和发现文件

🚀 有两种运输方式可供选择

📋 请参阅双重运输指南 详细设置

选项1:Web/Webocket版本 (用于测试/开发)

  • WebSocket: ws://localhost:8080/mcp
  • REST API: http://localhost:8080/api/knowledge/
  • 命令: start-web-server.bat

选项2:标准输入/标准输出版本 (适用于克劳德桌面)

  • 直接stdin/stdout通信
  • 添加到Claude Desktop MCP配置
  • 命令: start-stdin-server.bat

快速开始

先决条件

  • Java 17或更高版本
  • Maven 3.6或更高版本

建立和运行

  1. 克隆或导航到项目目录:

    cd D:\mcp-knowledge-server
    
  2. 构建项目:

    mvn clean install
    
  3. 运行服务器:

    mvn spring-boot:run
    

    或者直接运行JAR:

    java -jar target/mcp-knowledge-server-1.0.0.jar
    
  4. 服务器将在端口8080上启动

测试服务器

健康检查:

curl http://localhost:8080/api/knowledge/health

列出文件:

curl http://localhost:8080/api/knowledge/files

上传文件:

curl -X POST -F "file=@your-file.txt" http://localhost:8080/api/knowledge/upload

MCP协议使用

WebSocket端点

ws://localhost:8080/mcp

可用工具

  1. 列表文件:列出知识库中的所有文件

    {
      "jsonrpc": "2.0",
      "id": "1",
      "method": "tools/call",
      "params": {
        "name": "list_files",
        "arguments": {}
      }
    }
    
  2. read_file:读取特定文件的内容

    {
      "jsonrpc": "2.0",
      "id": "2",
      "method": "tools/call",
      "params": {
        "name": "read_file",
        "arguments": {
          "filename": "welcome.md"
        }
      }
    }
    
  3. 搜索文件:搜索包含特定文本的文件

    {
      "jsonrpc": "2.0",
      "id": "3",
      "method": "tools/call",
      "params": {
        "name": "search_files",
        "arguments": {
          "query": "your search term"
        }
      }
    }
    
  4. 获取_文件_信息:获取文件的元数据

    {
      "jsonrpc": "2.0",
      "id": "4",
      "method": "tools/call",
      "params": {
        "name": "get_file_info",
        "arguments": {
          "filename": "welcome.md"
        }
      }
    }
    
  5. write_file:将内容写入文件

    {
      "jsonrpc": "2.0",
      "id": "5",
      "method": "tools/call",
      "params": {
        "name": "write_file",
        "arguments": {
          "filename": "new-file.txt",
          "content": "Your content here"
        }
      }
    }
    

项目结构

D:\mcp-knowledge-server\
├── src/
│   ├── main/
│   │   ├── java/com/example/mcpserver/
│   │   │   ├── McpKnowledgeServerApplication.java
│   │   │   ├── McpWebSocketHandler.java
│   │   │   ├── controller/
│   │   │   │   └── KnowledgeStoreController.java
│   │   │   ├── handler/
│   │   │   │   └── McpProtocolHandler.java
│   │   │   ├── model/
│   │   │   │   ├── McpMessage.java
│   │   │   │   ├── McpError.java
│   │   │   │   ├── Tool.java
│   │   │   │   └── ToolCallResult.java
│   │   │   └── service/
│   │   │       └── KnowledgeStoreService.java
│   │   └── resources/
│   │       └── application.properties
│   └── test/
├── knowledgeStore/          # Your knowledge files go here
│   ├── welcome.md
│   └── setup-instructions.txt
├── pom.xml
└── README.md

配置

编辑 src/main/resources/application.properties 自定义:

  • server.port:服务器端口(默认值:8080)
  • knowledge.store.path:知识库路径(默认:./knowledgeStore)
  • 日志记录级别和其他Spring Boot设置

支持的文件类型

  • 文本文件:.txt、.md
  • PDF文件:.pdf(使用Apache PDFBox)
  • 图像:.jpg、.jpeg、.png、.gif(元数据提取)
  • 办公文档:.docx、.xlsx、.pptx(使用Apache Tika)
  • 更多 通过Apache Tika的广泛格式支持

依赖项

  • 弹簧靴3.2.0
  • Apache PDFBox 3.0.1(PDF处理)
  • Apache Tika 2.9.1(多格式文档处理)
  • Apache Commons IO 2.15.1(文件操作)
  • Jackson(JSON处理)

发展

要扩展服务器,请执行以下操作:

  1. 在中添加新工具 McpProtocolHandler
  2. 在中实现业务逻辑 KnowledgeStoreService
  3. 在中添加REST端点 KnowledgeStoreController 如有需要
  4. 更新中的工具架构 handleToolsList 方法

许可证

本项目按原样提供,用于教育和发展目的。