- MCP Host Client Server SSE
MCP Host Client Server SSE
Content
MCP Host Client Server SSE
基于MCP(Model Context Protocol)协议实现的自然语言SQL数据库查询系统,支持通过大语言模型将自然语言转换为SQL查询。本项目展示了如何使用MCP协议构建服务器-客户端架构,并集成OpenAI实现智能查询功能。
功能特点
- 🚀 基于MCP协议的服务器-客户端架构
- 📝 支持自然语言转SQL查询
- 🔄 使用SSE(Server-Sent Events)实现实时通信
- 📊 SQLite数据库集成
- 🤖 OpenAI/DeepSeek接口集成
- 📝 完整的日志记录系统
- ⚡ 异步操作支持
系统架构
graph TD
A[用户] -->|自然语言问题| B[MCP Host]
B -->|API调用| C[OpenAI]
C -->|SQL查询| B
B -->|MCP请求| D[MCP Server]
D -->|查询| E[SQLite DB]
E -->|结果| D
D -->|SSE响应| B
B -->|自然语言解释| A
快速开始
前置要求
- Python 3.8+
- pip包管理器
- OpenAI API密钥
安装步骤
- 克隆仓库:
git clone https://github.com/HanShan006/MCP_Host_Client_Server_SSE.git
cd MCP_Host_Client_Server_SSE
- 安装依赖:
pip install openai mcp-framework starlette uvicorn
- 配置API密钥: 创建config.ini文件并添加以下内容:
[secrets]
API_KEY=your_api_key_here
- 创建示例数据库:
python create_database.py
运行服务
- 启动MCP服务器:
python mcp_server.py
- 运行客户端应用:
python mcp_host.py
数据库结构
users表
| 字段 | 类型 | 说明 |
|---|---|---|
| id | INTEGER | 用户ID(主键) |
| name | TEXT | 用户名 |
| age | INTEGER | 年龄 |
| TEXT | 邮箱地址 |
orders表
| 字段 | 类型 | 说明 |
|---|---|---|
| id | INTEGER | 订单ID(主键) |
| user_id | INTEGER | 用户ID(外键) |
| product_name | TEXT | 商品名称 |
| price | REAL | 价格 |
| order_date | TEXT | 订单日期 |
示例查询
系统支持多种自然语言查询,例如:
- "查询所有用户的信息"
- "显示张三的所有订单"
- "统计每个用户的消费总额"
- "查询订单金额超过5000元的用户信息"
项目结构
MCP_Host_Client_Server_SSE/
├── mcp_server.py # MCP服务器实现
├── mcp_client.py # MCP客户端实现
├── mcp_host.py # 主机程序(集成OpenAI)
├── create_database.py # 数据库初始化脚本
├── database.db # SQLite数据库文件
├── config.ini # 配置文件
└── README.md # 项目文档
技术栈
- Python 3.8+
- MCP Framework
- SQLite
- OpenAI API
- Starlette
- Server-Sent Events (SSE)
- Async/Await
版本历史
v1.0.0 (2025-05-07)
- ✨ 初始版本发布
- 🎉 实现基础MCP服务器功能
- 🎉 完成客户端通信模块
- 🎉 集成OpenAI自然语言处理
- 🎉 添加SQLite数据库支持
- 📝 完善项目文档
贡献者
- 程序员寒山 (@HanShan006)
开源协议
本项目采用 MIT 协议开源,详情请参见 LICENSE 文件。
Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
Serper MCP ServerA Serper MCP Server
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
ChatWiseThe second fastest AI chatbot™
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
DeepChatYour AI Partner on Desktop
CursorThe AI Code Editor
Amap Maps高德地图官方 MCP Server
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
Playwright McpPlaywright MCP server
WindsurfThe new purpose-built IDE to harness magic
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题;
Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
Tavily Mcp