文档管理系统

一个基于模型上下文协议(MCP)和ChromaDB的客户端-服务器应用程序,提供文档添加和查询功能。

  • by bhavya314
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 bhavya314/mcp-rag-server

使用说明

MCP文件管理系统

此项目演示了使用模型上下文协议(MCP)构建的客户端-服务器应用程序。它提供了一个简单的文档管理系统,其中由ChromaDB驱动的服务器公开了添加和查询文档的工具。命令行客户端允许用户与这些工具进行交互。

特性

  • 基于MCP的通信: 利用模型上下文协议实现健壮的客户端-服务器交互。
  • 基于工具的体系结构: 服务器将功能作为“工具”公开(add_document, query_documents)客户端可以发现并调用。
  • 基于矢量的文档存储: 使用ChromaDB基于语义相似性存储和检索文档。
  • 异步: 使用Python构建 asyncio 实现高效、无阻塞的I/O。
  • 交互式CLI: 一个简单的命令行界面,供用户与文档管理系统交互。

先决条件

  • Python 3.11+

安装

  1. 克隆存储库:

    git clone <repository-url>
    cd <repository-directory>
    
  2. 创建并激活虚拟环境:

    python3 -m venv venv
    source venv/bin/activate
    
  3. 创建一个 requirements.txt 包含以下内容的文件:

    mcp.py
    chromadb
    sentence-transformers
    
  4. 安装依赖项:

    pip install -r requirements.txt
    

用法

您应该看到输出,指示ChromaDB客户端已初始化,FastMCP服务器正在运行。

运行客户端

客户端连接到服务器,发现可用的工具,并为您提供使用它们的交互式提示。

python client.py

示例会话

在运行示例之前,创建一个名为的目录 sample_data 并放置一些 .txt 里面的文件。

mkdir sample_data
echo "The Model Context Protocol (MCP) is for AI agent-to-tool communication." > sample_data/mcp.txt

现在,您可以与客户端交互:

$ python client.py
Connecting to the server...
# ... connection logs ...
Connected to the server successfully.
Starting chat loop. Type 'exit' or 'quit' to exit.
# ... tool information ...

Please choose a tool to use or type 'exit' to quit.

Query:
add_document
Enter collection name: my_docs
Enter document path: ./sample_data
Result from add_document: {'content': [], 'structuredContent': None, 'isError': False, 'result': "Documents added to collection 'my_docs' successfully."}

Please choose a tool to use or type 'exit' to quit.

Query:
query_documents
Enter collection name: my_docs
Enter query (comma-separated): what is mcp
Result from query_documents: {'content': [], 'structuredContent': {'Query 0': ['The Model Context Protocol (MCP) is for AI agent-to-tool communication.']}, 'isError': False, 'result': None}

Please choose a tool to use or type 'exit' to quit.

Query:
exit
Exiting chat loop.
Client session closed and exit stack cleaned up.
Client closed.

项目结构

.
├── client.py        # The MCP client application
├── server.py        # The MCP server application
└── chroma_db/       # Directory for ChromaDB persistent storage (auto-generated)