Skip to main content
Glama

基于 MCP 的 Agentic RAG 系统

一个本地、模块化的检索增强生成(RAG)系统,使用模型上下文协议(MCP)将 LLM 连接到向量数据库和文档加载器等外部工具。

概述

本项目实现了一个 Agentic RAG 系统,它可以:

  • 检索:从本地向量数据库(ChromaDB)中检索相关文档

  • 增强:使用检索到的上下文增强提示词

  • 生成:使用本地 LLM(Ollama)生成信息丰富的回答

  • 暴露:通过 FastAPI 的 REST API 提供功能

Related MCP server: MCP RAG with ChromaDB

技术栈

组件

工具/库

详情

语言模型

Ollama

本地 LLM 推理(mistral, llama3 等)

代理框架

mcp + FastAPI

带有工具注册功能的 API 服务器

RAG 流水线

LangChain + 自定义

上下文检索和提示词工程

向量存储

ChromaDB

本地、持久化向量数据库

嵌入模型

SentenceTransformers

all-MiniLM-L6-v2 模型

文件处理

pypdf, python-docx

PDF 和文档加载

前端(可选)

Streamlit

交互式 Web UI

环境

Python 3.10+

virtualenv 或 Conda

项目结构

agentic-rag-mcp/
├── main.py                    # FastAPI MCP server
├── rag_agent.py              # Agent query logic and RAG orchestration
├── mcp_config.yaml           # Configuration file
├── requirements.txt          # Python dependencies
├── vector_store/             # Persisted ChromaDB vector store
├── data/
│   └── sample_docs/          # Sample documents for ingestion
└── tools/
    └── chromadb_tool.py      # Vector search tool implementation

安装与设置

1. 克隆并创建虚拟环境

cd agentic-rag-mcp
python -m venv .venv

# On Windows
.venv\Scripts\activate

# On macOS/Linux
source .venv/bin/activate

2. 安装依赖

pip install -U pip
pip install -r requirements.txt

3. 设置 Ollama

从官网下载并安装 Ollama

启动 Ollama 服务器:

# On the system terminal (not in virtual environment)
ollama serve

在另一个终端中,拉取一个模型:

ollama pull mistral    # Recommended for RAG
# or
ollama pull llama3

验证服务器是否正在运行:

curl http://localhost:11434/api/tags

运行系统

选项 1:聊天界面(交互式)

运行交互式聊天循环:

python rag_agent.py

这将:

  1. 将示例文档加载到向量存储中

  2. 启动一个可以提问的交互式聊天

  3. 代理将检索相关文档并生成答案

交互示例:

You: What is MCP?
Agent: The Model Context Protocol (MCP) enables modular tool use for AI agents by providing a standardized way to connect language models to external services...

[Used 2 retrieved documents as context]

选项 2:API 服务器

启动 FastAPI MCP 服务器:

python main.py

服务器将在以下地址可用:http://localhost:8000

API 端点

健康检查

GET /health

查询代理

POST /query
Content-Type: application/json

{
  "query": "What is artificial intelligence?",
  "use_context": true,
  "n_results": 3
}

搜索文档

POST /search
Content-Type: application/json

{
  "query": "MCP protocol",
  "n_results": 5
}

添加文档

POST /documents
Content-Type: application/json

{
  "documents": [
    "Document text 1",
    "Document text 2"
  ],
  "ids": ["doc1", "doc2"],
  "metadata": [
    {"source": "file1.txt"},
    {"source": "file2.txt"}
  ]
}

获取统计信息

GET /stats

Python 使用示例

from rag_agent import RAGAgent

# Initialize agent
agent = RAGAgent(
    ollama_url="http://localhost:11434",
    model="mistral"
)

# Get a response
result = agent.get_response("What is RAG?")
print(result["response"])
print(f"Retrieved {len(result['retrieved_documents'])} documents")

配置

编辑 mcp_config.yaml 以进行自定义:

  • LLM 设置:模型、温度、最大 token 数

  • 向量存储:嵌入模型、集合名称

  • RAG:检索文档数量、相似度度量

  • 服务器:主机、端口、日志级别

  • 安全:API 速率限制、身份验证

添加自定义文档

以编程方式

from tools.chromadb_tool import ChromaTool

tool = ChromaTool()
documents = [
    "Your document text 1",
    "Your document text 2"
]
tool.add_documents(documents, ids=["id1", "id2"])

通过 API

curl -X POST http://localhost:8000/documents \
  -H "Content-Type: application/json" \
  -d '{
    "documents": ["Document 1", "Document 2"],
    "ids": ["doc1", "doc2"]
  }'

可选的 Streamlit 前端

创建 streamlit_app.py

import streamlit as st
import requests

st.set_page_config(page_title="RAG Agent", layout="wide")
st.title("MCP-Powered Agentic RAG")

query = st.text_input("Ask a question:")

if query:
    response = requests.post(
        "http://localhost:8000/query",
        json={"query": query}
    )
    result = response.json()
    
    st.subheader("Response")
    st.write(result["response"])
    
    st.subheader("Retrieved Context")
    for i, doc in enumerate(result["retrieved_documents"], 1):
        st.write(f"**Doc {i}**: {doc[:200]}...")

运行 Streamlit:

streamlit run streamlit_app.py

扩展与未来工作

  • ✅ 基于 ChromaDB 的基础 RAG

  • ⬜ 网络搜索工具集成

  • ⬜ PDF 文档摄入 UI

  • ⬜ 代理记忆(对话历史)

  • ⬜ 多模态支持(图像、表格)

  • ⬜ 针对特定领域数据进行微调

  • ⬜ 结构化输出(JSON 模式)

  • ⬜ 实时流式响应

故障排除

Ollama "Connection refused"

  • 确保 Ollama 服务器正在运行:ollama serve

  • 检查其是否可访问:curl http://localhost:11434/api/tags

ChromaDB 嵌入错误

  • 确保已安装 sentence-transformers:pip install sentence-transformers

  • 首次运行时会下载嵌入模型(约 30MB)

向量存储未持久化

  • 检查 ./vector_store/ 目录是否存在且可写

  • 验证配置中的 persist_dir 是否与实际路径匹配

许可证

MIT 许可证 - 详情请参阅 LICENSE 文件

贡献

欢迎贡献!请:

  1. Fork 本仓库

  2. 创建功能分支

  3. 提交更改

  4. 推送并开启 Pull Request

参考资料

A
license - permissive license
Not graded
quality - not tested
C
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

  • F
    license
    Not graded
    quality
    D
    maintenance
    A FastAPI-based application that enables document embedding and semantic retrieval using Qdrant vector database, allowing users to convert documents into embeddings and retrieve relevant content through natural language queries.
  • A
    license
    Not graded
    quality
    D
    maintenance
    Provides retrieval-augmented generation (RAG) capabilities by ingesting various document formats into a persistent ChromaDB vector store. It enables semantic search and retrieval using either OpenAI or Ollama embeddings for processing local files, directories, and URLs.
    1
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    Provides token-efficient semantic search and document retrieval by indexing PDFs, text, and markdown files into local notebooks using ChromaDB. It enables AI agents to query relevant passages from large documents through local embedding models like Hugging Face or Ollama.
    1
    MIT
  • F
    license
    Not graded
    quality
    D
    maintenance
    A fully offline local RAG server that utilizes ChromaDB and Ollama to index and query PDF, text, and Markdown documents. It allows users to manage local knowledge bases and perform semantic searches with AI-generated responses.

View all related MCP servers

Related MCP Connectors

  • Persistent semantic memory for AI agents: store and recall text by meaning (RAG). x402

  • Persistent memory and knowledge management for AI agents with semantic search and 50+ tools.

  • Long-term memory for AI assistants. Hybrid retrieval, query expansion, auto-topics.

View all MCP Connectors

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/EimanTahir027/MCP-powered-Agentic-RAG'

If you have feedback or need assistance with the MCP directory API, please join our Discord server