Skip to main content
Glama

LangGraph RAG MCP

一个通过模型上下文协议(Model Context Protocol,MCP)提供 LangGraph 文档服务的检索增强生成(RAG)系统。

概述

本系统构建了一个文档检索系统,它可以:

  1. 从官方网站收集和处理 LangGraph 文档

  2. 按照本文档创建向量数据库,用于语义搜索

  3. 通过模型上下文协议(MCP)公开这些知识

  4. 与 MCP 兼容的主机集成,如 VS Code、Cursor、Claude Desktop 或 Windsurf

Related MCP server: groq-docs-mcp

工作原理

1. 文档收集与处理(上下文)

  • 使用 RecursiveUrlLoader 和 BeautifulSoup,从多个网站 URL 递归抓取并清洗 LangGraph 文档。

  • 借助带 tiktoken 的 RecursiveCharacterTextSplitter 将文本切分为适当的块,从而实现准确的 token 计数。

  • 使用 BAAI/bge-large-en-v1.5 嵌入模型,将文本块转换为向量表示。

  • 将向量存储在 SKLearnVectorStore 中,以便高效检索。

2. 检索系统(工具)

  • 实现一个检索函数,为给定的查询找出最相关的文档块。

  • 将该函数与 Claude 等语言模型集成,以提供基于上下文的响应。

  • 返回包含来源说明和相关上下文的格式化响应。

3. MCP 服务器集成

  • 使用 fastmcp 库将检索工具封装到 MCP 服务器中。

  • 将检索函数作为工具公开,使 MCP 兼容的主机得以调用。

  • 提供对检索系统以及附加资源(如完整文档文件)的访问。

环境要求

  • Python 3.10 或更高版本

  • Docker 和 Docker Compose(推荐)

  • Anthropic API 密钥(用于 Claude 模型)

安装与设置

你可以使用 Docker(推荐)或本地 Python 环境来运行本系统。

使用 Docker(推荐)

  1. 克隆仓库:

git clone https://github.com/yourusername/langraph-rag-mcp.git
cd langraph-rag-mcp
  1. 在 .env 文件中设置 API 密钥:

在项目根目录创建一个 .env 文件,并填入你的 Anthropic API 密钥:

echo "ANTHROPIC_API_KEY=your_api_key_here" > .env

docker-compose.yml 文件会自动加载该环境变量。

本地环境(无 Docker)

  1. 克隆仓库:

git clone https://github.com/yourusername/langraph-rag-mcp.git
cd langraph-rag-mcp
  1. 创建并激活虚拟环境:

conda create -n mcp python=3.13
conda activate mcp
  1. 安装所需依赖包:

pip install -r requirements.txt
  1. 在 .env 文件中设置 API 密钥:

echo "ANTHROPIC_API_KEY=your_api_key_here" > .env

使用方法

整个过程主要包含两个步骤:第一步生成向量存储,第二步运行 MCP 服务器。

第 1 步:生成向量存储

你只需要执行一次,或者当你想更新文档时再执行。

  1. 在 Jupyter 环境中打开并运行 rag-tool.ipynb 笔记本。

  2. 该过程将:

    • 下载最新的 LangGraph 文档。

    • 将完整文档保存到 llms_full.txt。

    • 将文档拆分为多个文本块。

    • 在 sklearn_vectorstore.parquet 处创建并保存向量存储。

第 2 步:运行 MCP 服务器

使用 Docker

运行服务器最简单的方式,是使用随附的 shell 脚本(它封装了 Docker Compose)。

bash run-mcp-docker.sh

该脚本会在镜像不存在时构建 Docker 镜像、启动容器,然后在容器内执行 MCP 服务器,并正确支持 MCP 通信所需的输入输出。

不使用 Docker

如果你不使用 Docker,可以通过以下命令,以开发模式直接运行 MCP 服务器:

mcp dev langgraph-mcp.py

配置 MCP 主机

若要在兼容的编辑器中使用该服务器,你需要对其进行配置。

VS Code

  1. 打开你的 VS Code settings.json 文件(可通过命令面板打开:Preferences: Open User Settings (JSON))。

  2. 将以下配置添加到文件中。请务必将 <path-to-your-project> 替换为你机器上 langgraph-rag-mcp 目录的绝对路径。

"mcp.servers": [
    {
        "name": "langgraph-mcp",
        "command": [
            "/bin/bash",
            "<path-to-your-project>/run-mcp-docker.sh"
        ],
        "env": {
            "ANTHROPIC_API_KEY": "<your-anthropic-api-key>"
        }
    }
]

不使用 Docker 时,可把 command 改为:

"command": [
    "<path-to-your-conda-env>/bin/python",
    "<path-to-your-project>/langgraph-mcp.py"
]

系统架构

(Phase 1: Data Ingestion - Performed once on Host Machine)
┌─────────────┐      ┌──────────────────┐      ┌───────────────────────────────┐
│ LangGraph   │      │ Jupyter Notebook │      │ Vector Store & Full Docs      │
│ Docs (Web)  │───▶  │ (rag-tool.ipynb) │───▶  │ (.parquet & .txt files)       │
└─────────────┘      └──────────────────┘      └───────────────────────────────┘


(Phase 2: Live RAG System - Request/Response Flow)
┌──────────────┐                             ┌─────────────┐
│   VS Code    │                             │  .env file  │
│(User Interface)│                             │ (API KEY)   │
└──────┬───────┘                             └──────┬──────┘
       │ 1. User Query                             │ (provides)
       ▼                                           │
┌──────┴───────────────────────────────────────────┴───────────────────────────────┐
│ Host Machine Boundary                                                            │
│                                                                                  │
│      ┌────────────────────┐          ┌─────────────────────────────────────────┐ │
│      │ run-mcp-docker.sh  │ 2. Execs │  🐳 Docker Container                      │ │
│      │ (Entrypoint Script)│────▶     │                                         │ │
│      └────────────────────┘          │  ┌───────────────────────────────────┐  │ │
│                                      │  │   🐍 Python MCP Server              │  │ │
│      ▲                             ◀─┼──│   (langgraph-mcp.py)              │  │ │
│      │ 7. Final Response             │  └───────────────┬───────────────────┘  │ │
│      │                               │                  │ 3. Reads Data From  │ │
│      └───────────────────────────────│──────────────────│─────────────────────┘ │
│                                      │                  │                       │
│                                      │                  ▼                       │
│                                      │  ┌───────────────────────────────────┐  │
│ (files mounted from Host) ············  │   Mounted Vector Store & Docs     │  │
│                                      │  └───────────────────────────────────┘  │
│                                      └─────────────────────────────────────────┘ │
│                                                                                  │
└──────────────────────────────────────────────────────────────────────────────────┘
                                                        │ 4. API Call
                                                        │    (sends augmented prompt)
                                                        ▼
                                             ┌────────────────────┐
                                             │ ☁️ Anthropic API   │
                                             │   (Claude LLM)     │
                                             └──────────┬─────────┘
                                                        │ 5. Generation
                                                        │
                                                        ◀························
                                                          6. Returns Response
                                                          (to MCP Server)

参考资料

MCP From Scratch

Related MCP Connectors

Related MCP Servers

  • F
    license
    B
    quality
    C
    maintenance
    Retrieval-Augmented Generation system serving LangGraph documentation through the Model Context Protocol, enabling semantic search and context-aware responses.
    2
    17
    -
  • F
    license
    Not graded
    quality
    C
    maintenance
    Provides MCP-compatible hosts with retrieval-augmented access to LangGraph documentation, enabling Claude and other assistants to answer questions with relevant, source-attributed context.
    -