MCP工具演示
一种模型上下文协议(MCP)服务器,使用LlamaIndex和Linkup提供网络搜索和RAG(检索增强生成)功能。
特性
- 网页搜索:使用Linkup API搜索网页,并提供来源答案
- RAG工作流程:使用带有Ollama和HuggingFace嵌入的RAG管道查询文档
- MCP协议:使用FastMCP的标准MCP服务器实现
先决条件
安装
构建Docker镜像
# Build the Docker image
docker build -t mcp-tools-demo .配置
- 复制示例环境文件:
cp .env.example .env- 编辑
.env并添加您的配置:
LINKUP_API_KEY=your_linkup_api_key_here
OLLAMA_HOST=http://localhost:11434
OLLAMA_MODEL=gemma:2b
OLLAMA_TIMEOUT=120
EMBEDDING_MODEL=BAAI/bge-small-en-v1.5
DATA_DIR=./data- 确保Olama正在运行:
ollama serve- 拉动所需的模型:
ollama pull gemma:2b注:使用 gemma:2b (更小,更快)。您还可以使用 llama3.2 如果你有更多的记忆。
- 将文档添加到
data/RAG功能目录。
用法
快速开始
- 设置环境:
cp .env.example .env
# Edit .env and add your LINKUP_API_KEY- 确保Ollama在主机上运行:
# Check if Ollama is running
curl http://localhost:11434/api/tags
# If not running, start it:
ollama serve
# Pull required model (if not already installed):
ollama pull gemma:2b
# Optional: Increase timeout if responses are long
export OLLAMA_TIMEOUT=120- 将文档添加到数据目录:
# Place your PDFs, text files, etc. in the data/ directory
# Example: cp your_document.pdf data/- 使用Docker运行MCP服务器:
docker run -it --rm mcp-tools-demo您应该看到:
Loading documents from: /app/data
Supported formats: PDF, TXT, MD, DOCX, and more...
Loaded 1 document(s) from /app/data
Document index created successfully
MCP server starting...运行MCP服务器(仅限Docker)
MCP服务器 必须 使用Docker运行:
docker run -it --rm mcp-tools-demo注:
- 这
.env文件和data/目录在构建过程中被复制到Docker镜像中 - 重要提示: 如果更改,请重建图像
.env或添加/更改文档:docker build -t mcp-tools-demo .
发生了什么:
- 从加载所有文档
data/目录(PDF、TXT、MD等) - 提取文本并生成嵌入
- 创建可搜索的向量索引
- 在stdio传输上启动MCP服务器
- 已准备好接受客户的查询
注:
- 服务器将阻止并等待MCP协议消息。使用Ctrl+C停止。
- 确保Ollama在您的主机上运行(而不是在Docker中)
- 容器通过以下方式连接到主机Ollama
host.docker.internal:11434
运行测试客户端(Python)
测试客户端直接使用Python(而不是Docker)运行:
首先,安装依赖项:
pip install -r requirements.txt然后运行测试客户端:
python -m test_mcp_client.client_example注: 在启动测试客户端之前,请确保MCP服务器(Docker容器)正在运行。
这表明:
- 列出可用工具
- 呼叫
search_web网络搜索工具 - 呼叫
query_documentsRAG查询工具
预期产量:
Available tools:
- search_web: Perform a web search and return sourced answers...
- query_documents: Answer questions using RAG workflow...
Example 1: Web Search
Query: 'What is Current weather in Jacksonville, FL?'
Result: [Search results...]
Example 2: RAG Query
Query: 'Tell me about Reasoning-oriented Reinforcement Learning in simple terms'
Result: [Answer based on your documents...]可用工具
search_web
执行网络搜索并返回源答案。
参数:
query(str):搜索查询
例子:
result = await client.call_tool("search_web", {"query": "What is Python?"})query_documents
使用RAG工作流和数据目录中的文档回答问题。
参数:
query(str):要回答的问题
例子:
result = await client.call_tool("query_documents", {"query": "Tell me about DeepSeek"})项目结构
.
├── mcp_app/ # MCP application package
│ ├── __init__.py
│ ├── main.py # MCP server with tool definitions
│ └── document_workflow.py # RAG workflow implementation
├── test_mcp_client/ # Test client package
│ ├── __init__.py
│ └── client_example.py # Example MCP client
├── requirements.txt # Python dependencies
├── Dockerfile # Docker configuration
├── .env.example # Environment variables template
├── README.md # This file
└── data/ # Directory for documents (create this)发展
建立开发环境
# Install dependencies
pip install -r requirements.txt
# Run linter
ruff check .
# Format code
black .
# Type checking
mypy .运行测试
pytest建筑
RAG工作流程
RAG工作流程包括三个步骤:
- 工艺文件:从目录加载文档并为其建立索引
- 查找上下文:检索查询的相关文档部分
- 产生答案:使用检索到的上下文合成响应
MCP服务器
服务器使用FastMCP进行工具注册,并使用stdio传输进行通信。
故障排除
Ollama连接问题
- 确保Ollama在主机上运行:
ollama serve - Docker容器使用
http://host.docker.internal:11434连接到主机Ollama - 验证连接:
curl http://localhost:11434/api/tags(来自主持人)
文档索引问题
- 确保文件存在于
data/目录 - 检查中的目录路径
.env是正确的 - 索引是在服务器启动时创建的
链接API问题
- 验证您的API密钥是否设置在
.env - 检查您的Linkup API配额和限制
许可证
MIT许可证
贡献
欢迎投稿!请随时提交拉取请求。
