使用说明
MCP服务器-人工智能知识库集成
一种模型上下文协议(MCP)服务器实现,使AI模型能够通过智能函数调用和内容匹配算法动态访问和查询知识库。
🚀 概述
该项目演示了如何构建MCP服务器并将其与AI模型(Google Gemini和OpenAI)集成,以实现动态知识检索。它具有一个智能问题匹配系统,该系统使用基于正则表达式的文本提取和单词重叠评分来从结构化知识库中识别最相关的信息。
📁 项目结构
MCP-Server/
├── server.py # Main MCP server implementation
├── kb.json # Knowledge base data file
├── GeminiAI-integration/ # Google Gemini AI integration
│ ├── client.py # Gemini MCP client with intelligent matching
│ └── control_flow.md # Detailed architecture documentation
├── function-calling/ # Direct function calling examples
│ ├── function_calling.py # Gemini function calling demo
│ └── tools.py # Utility functions
├── server-scripts/ # Alternative server implementations
└── requirements.txt # Python dependencies
🛠️ 主要特点
1. 智能问题匹配
- 基于正则表达式的知识库问题提取
- 内容相关性的单词重叠评分算法
- 支持标点符号的文本规范化
- 调试输出以匹配透明度
2. 多AI模型支持
- 谷歌双子座:支持异步的高级函数调用
- OpenAI GPT:兼容的集成模式
- 跨模型的一致MCP协议实现
3. 动态工具发现
- 自动MCP工具注册和发现
- 工具参数的JSON模式验证
- 实时知识库查询
4. 流程管理
- 跨平台服务器进程处理
- 带信号处理的优雅关机
- 资源清理和内存管理
- Windows特定进程组支持
🔧 安装
先决条件
- Python 3.8+
- UV包管理器(或pip)
- Gemini集成的Google API密钥
设置
- 克隆存储库:
git clone https://github.com/PacifistaPx0/MCP-Server.git
cd MCP-Server
- 安装依赖项:
# Using UV (recommended)
uv pip install -r requirements.txt
# Or using pip
pip install -r requirements.txt
- 设置环境变量:
# Create .env file
echo "GOOGLE_API_KEY=your_api_key_here" > .env
- 安装其他软件包:
# For Gemini integration
uv pip install google-generativeai
# For MCP support
uv pip install mcp
🚀 用法
运行MCP服务器
# Start the MCP server
python server.py
Gemini集成(推荐)
# Run Gemini client with intelligent matching
cd GeminiAI-integration
python client.py
示例查询:
Query: "How can I submit a report on expenses?"
Output:
✓ Executing function: get_knowledge_base
✓ Most relevant question (Q4): How do I submit an expense report?
DEBUG Q1 Score: 0, Matching: set()
DEBUG Q2 Score: 0, Matching: set()
DEBUG Q3 Score: 0, Matching: set()
DEBUG Q4 Score: 3, Matching: {'report', 'expense', 'submit'}
DEBUG Q5 Score: 1, Matching: {'report'}
Response: Expense reports should be submitted through the company's expense management system...
直接函数调用
# Test direct function calling with Gemini
cd function-calling
python function_calling.py
🏗️ 建筑
核心组件
-
MCP服务器(
server.py)- 处理MCP协议通信
- 提供知识库内容
- 管理工具注册和发现
-
智能匹配算法
- 使用正则表达式模式提取问题
- 通过删除标点符号来规范文本
- 计算单词重叠分数
- 返回最相关的内容部分
-
多模型客户端
- 并发操作的异步/等待模式
- 用于成本监控的令牌使用跟踪
- 错误处理和资源清理
数据流
graph TD
A[User Query] --> B[Gemini AI]
B --> C{Function Call?}
C -->|Yes| D[MCP Server]
D --> E[Knowledge Base Query]
E --> F[Question Matching Algorithm]
F --> G[Return Relevant Content]
G --> H[Enhanced Query to Gemini]
H --> I[Final Response]
C -->|No| J[Direct Response]
📊 问题匹配算法
智能匹配系统使用复杂的评分方法:
def find_matching_question(kb_text, user_query):
# 1. Extract all questions from knowledge base
# 2. Normalize text (remove punctuation, lowercase)
# 3. Calculate word overlap scores
# 4. Return highest scoring match
评分示例:
- 问:“我如何提交费用报告?”
- Q4得分:3(匹配:“报告”、“费用”、“提交”)
- Q5得分:1(匹配:“报告”)
- 结果:Q4被选为最相关
🔧 配置
环境变量
# Required
GOOGLE_API_KEY=your_gemini_api_key
# Optional
OPENAI_API_KEY=your_openai_api_key # For OpenAI integration
知识库格式
这 kb.json 文件使用结构化问答格式:
[
{
"question": "What is our company's vacation policy?",
"answer": "Full-time employees are entitled to 20 paid vacation days..."
}
]
🧪 测试
运行单个组件
# Test MCP server standalone
python server.py
# Test Gemini integration
cd GeminiAI-integration && python client.py
# Test function calling
cd function-calling && python function_calling.py
# Test question matching algorithm
python test.py
示例测试用例
- 休假政策查询:“我们公司的假期政策是什么?”
- 费用报告:“我如何提交费用报告?”
- 远程工作:“我们的远程工作政策是什么?”
- 软件许可证:“如何申请新软件?”
- 安全事件:“如何报告安全问题?”
📈 性能特征
- 初始化:约2-3秒(服务器启动)
- 查询处理:~1-2秒(包括AI呼叫)
- 内存使用:~50-100MB(取决于知识库大小)
- 代币效率:优化提示将API成本降低约30%
🔍 故障排除
常见问题
-
“没有属性'configure'”错误
# Solution: Use correct import import google.generativeai as genai # Not: from google import genai -
服务器连接超时
# Check if server process is running ps aux | grep server.py -
API关键问题
# Verify environment variable echo $GOOGLE_API_KEY
🤝 贡献
- 分叉存储库
- 创建要素分支(
git checkout -b feature/amazing-feature) - 提交更改(
git commit -m 'Add amazing feature') - 推送到分支(
git push origin feature/amazing-feature) - 打开拉取请求
📄 许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
🙏 致谢
- 模型上下文协议(MCP):Anthropic的MCP框架
- 谷歌双子座AI:具有函数调用的高级语言模型
- OpenAI:GPT模型和API标准
- UV包装管理器:快速Python包管理
📚 文档
- 控制流文件 -详细的架构指南
- MCP协议文件 -MCP官方规格
- Google Gemini API -Gemini集成指南
