luke2013tw2/aiMcpServer
3.1
If you are the rightful owner of aiMcpServer and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.
The MCP Database Server is an independent service that provides database operations and can be accessed by various AI Agents.
MCP Database Server
獨立的 MCP (Model Context Protocol) Database Server,提供資料庫操作功能,可被不同的 AI Agent 調用。
🚀 功能特色
- 獨立運行:可作為獨立服務運行
- RESTful API:提供 HTTP API 介面
- 資料庫操作:支援 SQLite 資料庫的各種操作
- 多 Agent 支援:可同時服務多個 AI Agent
- 易於整合:提供簡單的 Client SDK
📋 支援的操作
- 取得資料庫結構資訊
- 執行 SQL 查詢
- 取得表格資訊
- 取得範例資料
- 插入、更新、刪除資料
🛠️ 安裝與運行
1. 安裝依賴
# 使用 uv
uv sync
# 或使用 pip
pip install -r requirements.txt
2. 啟動 Server
# 啟動 MCP Server
python -m mcp_server.main
# 或使用 uvicorn
uvicorn mcp_server.main:app --host 0.0.0.0 --port 8001
3. 測試 API
# 檢查服務狀態
curl http://localhost:8001/health
# 取得資料庫結構
curl http://localhost:8001/api/schema
# 執行查詢
curl -X POST http://localhost:8001/api/query \
-H "Content-Type: application/json" \
-d '{"sql": "SELECT * FROM users LIMIT 3"}'
📡 API 端點
健康檢查
GET /health- 檢查服務狀態
資料庫操作
GET /api/schema- 取得資料庫結構POST /api/query- 執行 SQL 查詢GET /api/table/{table_name}- 取得表格資訊GET /api/table/{table_name}/sample- 取得範例資料
🔧 配置
環境變數
創建 .env 文件:
# 服務器配置
MCP_SERVER_HOST=0.0.0.0
MCP_SERVER_PORT=8001
# 資料庫配置
DATABASE_PATH=data/mcp_database.db
# 日誌配置
LOG_LEVEL=INFO
📦 專案結構
mcp_server_project/
├── mcp_server/
│ ├── __init__.py
│ ├── main.py # FastAPI 應用程式
│ ├── database.py # 資料庫操作核心
│ ├── models.py # Pydantic 模型
│ └── client.py # Client SDK
├── data/ # 資料庫文件
├── tests/ # 測試文件
├── pyproject.toml # 專案配置
├── README.md # 專案說明
└── .env.example # 環境變數範例
🔌 Client SDK 使用
Python Client
from mcp_server.client import MCPDatabaseClient
# 創建客戶端
client = MCPDatabaseClient("http://localhost:8001")
# 取得資料庫結構
schema = await client.get_schema()
# 執行查詢
result = await client.execute_query("SELECT * FROM users LIMIT 3")
# 取得表格資訊
table_info = await client.get_table_info("users")
JavaScript/Node.js Client
const MCPClient = require('mcp-database-client');
const client = new MCPClient('http://localhost:8001');
// 執行查詢
const result = await client.executeQuery('SELECT * FROM users LIMIT 3');
🧪 測試
# 運行測試
pytest
# 運行特定測試
pytest tests/test_database.py
# 運行整合測試
pytest tests/test_integration.py
📝 開發
本地開發
# 安裝開發依賴
uv sync --extra dev
# 啟動開發服務器
uvicorn mcp_server.main:app --reload --host 0.0.0.0 --port 8001
程式碼格式化
# 格式化程式碼
black mcp_server/
isort mcp_server/
# 檢查程式碼品質
flake8 mcp_server/
🤝 貢獻
歡迎提交 Issue 和 Pull Request!
📄 授權
MIT License