Skip to main content
Glama

基于 FastAPI + MCP 的智能体 RAG 助手

一个内部文档问答助手,结合了招聘者当前正在重点考察的四项能力:

  • 智能体 AI —— 一种工具调用循环:由 LLM 决定是检索知识库、执行计算,还是直接回答。

  • FastAPI —— 一个简洁的 REST 后端(/ingest、/query、/agent/chat),自动生成 Swagger 文档。

  • RAG —— 文档先被分块、建立索引,并在 LLM 回答之前按语义相关性进行检索。

  • MCP(Model Context Protocol) —— 同样的工具(RAG 搜索 + 业务逻辑)也以 MCP 服务器的形式暴露,因此任何兼容 MCP 的客户端(Claude Desktop、Claude Code 等)都可以直接使用这些工具,而不只是通过这个 API。

架构

                     ┌────────────────────┐
                     │   FastAPI Service   │
                     │  (app/main.py)      │
                     └─────────┬───────────┘
                               │
                     ┌─────────▼───────────┐
                     │   Agent Loop         │◄──── Groq API (tool use)
                     │  (app/agent.py)      │
                     └─────────┬───────────┘
                               │ calls
                 ┌─────────────┼──────────────┐
                 ▼                             ▼
        ┌────────────────┐           ┌──────────────────┐
        │  RAG Engine      │           │  Business Tools    │
        │  (app/rag.py)    │           │  (app/tools.py)     │
        └────────────────┘           └──────────────────┘
                 ▲                             ▲
                 └─────────────┬───────────────┘
                                │  same tools, exposed via
                     ┌──────────▼───────────┐
                     │   MCP Server           │
                     │  (app/mcp_server.py)   │
                     └────────────────────────┘

Related MCP server: mcp-docpilot-server

安装配置

python -m venv venv
source venv/bin/activate       # Windows: venv\Scripts\activate
pip install -r requirements.txt
cp .env.example .env           # add your GROQ_API_KEY

运行 API

uvicorn app.main:app --reload --port 8000

打开 http://localhost:8000/docs 查看交互式 Swagger 文档。

试一试

# Pure retrieval, no LLM call
curl -X POST http://localhost:8000/query \
  -H "Content-Type: application/json" \
  -d '{"query": "how many days of leave do I get?"}'

# Full agentic chat — LLM decides which tool(s) to call
curl -X POST http://localhost:8000/agent/chat \
  -H "Content-Type: application/json" \
  -d '{"message": "I joined in March and have taken 4 days off. How much leave do I have left, and what is the WFH policy?"}'

第二个调用示例展示了多工具推理:模型在同一个对话中调用 rag_search 查找 WFH 政策,并调用 calculate_leave_balance 完成计算。

作为 MCP 服务器运行

python -m app.mcp_server

通过 stdio 将任何 MCP 宿主指向这个脚本(例如,把它作为自定义 MCP 服务器添加到 Claude Desktop 的 claude_desktop_config.json 中),它就会把 rag_search、get_current_datetime、calculate_leave_balance 暴露为可调用的工具。

面试中如何介绍这个项目

  • 为什么默认用 TF-IDF 而不是嵌入向量:这让 demo 无需任何 API 密钥、无需任何外部下载就能运行;VectorStore 类在设计上就支持把 FAISS/Chroma + 真实嵌入向量作为即插即用的替换方案,而不是重写。

  • 为什么工具都放在同一个文件(tools.py)里,并在两个地方暴露(agent.py 和 mcp_server.py):单一事实来源,HTTP 路径与 MCP 路径之间没有逻辑重复。

  • 智能体循环是工具调用模式的手动实现(不是黑盒框架),所以每一步都能讲清楚:模型请求工具 → 服务器执行它 → 结果被反馈回去 → 模型继续或直接回答。

可能的扩展方向(适合回答“你会如何改进”)

  • 把 TF-IDF 换成真正的嵌入向量(OpenAI/Voyage/本地 sentence-transformers),再加上持久化向量数据库。

  • 跨多轮对话添加会话记忆(目前每次 /agent/chat 调用都是无状态的)。

  • 通过 Server-Sent Events 添加流式响应。

  • 在公开部署之前添加身份验证(API key 或 JWT)。

  • 使用 Docker + docker-compose 进行容器化,实现一条命令启动。

Related MCP Connectors

Related MCP Servers

  • F
    license
    Not graded
    quality
    B
    maintenance
    Enables any MCP-compatible AI assistant to search, filter, and retrieve information from a local document collection using a hybrid search pipeline with vector, BM25, reranking, and LLM enrichment.
    4
    -
  • F
    license
    Not graded
    quality
    D
    maintenance
    An MCP server that exposes document retrieval as tools (semantic search and source listing) for any LLM, using a vector index built from DocPilot's ingestion pipeline.
    -
  • F
    license
    Not graded
    quality
    D
    maintenance
    Enables local document question-answering and retrieval via MCP, supporting multi-turn conversation, intent recognition, and tools for document search, Q&A, and summarization.
    5
    -