这是一个模型上下文协议(MCP)服务器,它能让 AI 助手与 SurrealDB 数据库进行交互。借助该服务器,AI 助手可以通过标准化接口执行各类数据库操作,为数据处理带来便利。
SurrealDB MCP 服务器通过模型上下文协议,为 AI 助手和 SurrealDB 之间搭建了桥梁,提供了标准化的数据库操作接口。这使得大语言模型(LLMs)能够:
# 直接从 PyPI 运行(发布后)
uvx surreal-mcp
# 或者从 GitHub 运行
uvx --from git+https://github.com/yourusername/surreal-mcp.git surreal-mcp
# 克隆仓库
git clone https://github.com/yourusername/surreal-mcp.git
cd surreal-mcp
# 安装依赖
uv sync
# 运行服务器(多种方式)
uv run surreal-mcp
# 或者
uv run python -m surreal_mcp
# 或者
uv run python main.py
# 克隆仓库
git clone https://github.com/yourusername/surreal-mcp.git
cd surreal-mcp
# 创建虚拟环境
python -m venv venv
source venv/bin/activate # 在 Windows 上:venv\Scripts\activate
# 安装包
pip install -e .
# 运行服务器
surreal-mcp
# 或者
python -m surreal_mcp
服务器需要以下环境变量:
| 属性 | 详情 |
|---|---|
SURREAL_URL |
SurrealDB 连接 URL,例如 ws://localhost:8000/rpc |
SURREAL_USER |
数据库用户名,例如 root |
SURREAL_PASSWORD |
数据库密码,例如 root |
SURREAL_NAMESPACE |
SurrealDB 命名空间,例如 test |
SURREAL_DATABASE |
SurrealDB 数据库,例如 test |
你可以将 .env.example 复制到 .env 并更新为你的值:
cp .env.example .env
# 使用你的数据库凭证编辑 .env
或者手动设置:
export SURREAL_URL="ws://localhost:8000/rpc"
export SURREAL_USER="root"
export SURREAL_PASSWORD="root"
export SURREAL_NAMESPACE="test"
export SURREAL_DATABASE="test"
添加到你的 MCP 客户端设置(例如,Claude Desktop): 使用 uvx(推荐):
{
"mcpServers": {
"surrealdb": {
"command": "uvx",
"args": ["surreal-mcp"],
"env": {
"SURREAL_URL": "ws://localhost:8000/rpc",
"SURREAL_USER": "root",
"SURREAL_PASSWORD": "root",
"SURREAL_NAMESPACE": "test",
"SURREAL_DATABASE": "test"
}
}
}
}
使用本地安装:
{
"mcpServers": {
"surrealdb": {
"command": "uv",
"args": ["run", "surreal-mcp"],
"env": {
"SURREAL_URL": "ws://localhost:8000/rpc",
"SURREAL_USER": "root",
"SURREAL_PASSWORD": "root",
"SURREAL_NAMESPACE": "test",
"SURREAL_DATABASE": "test"
}
}
}
}
执行原始 SurrealQL 查询以进行复杂操作。
-- 示例:带有图遍历的复杂查询
SELECT *, ->purchased->product FROM user WHERE age > 25
从表中检索所有记录或按 ID 检索特定记录。
# 获取所有用户
select("user")
# 获取特定用户
select("user", "john")
创建具有自动生成 ID 的新记录。
create("user", {
"name": "Alice",
"email": "alice@example.com",
"age": 30
})
替换整个记录内容(保留 ID 和时间戳)。
update("user:john", {
"name": "John Smith",
"email": "john.smith@example.com",
"age": 31
})
从数据库中永久删除记录。
delete("user:john")
部分更新特定字段,而不影响其他字段。
merge("user:john", {
"email": "newemail@example.com",
"verified": True
})
对记录应用 JSON Patch 操作(RFC 6902)。
patch("user:john", [
{"op": "replace", "path": "/email", "value": "new@example.com"},
{"op": "add", "path": "/verified", "value": True}
])
使用特定 ID 创建或更新记录。
upsert("settings:global", {
"theme": "dark",
"language": "en"
})
高效批量插入多条记录。
insert("product", [
{"name": "Laptop", "price": 999.99},
{"name": "Mouse", "price": 29.99},
{"name": "Keyboard", "price": 79.99}
])
在记录之间创建图关系。
relate(
"user:john", # from
"purchased", # 关系名称
"product:laptop-123", # to
{"quantity": 1, "date": "2024-01-15"} # 关系数据
)
# 创建用户
user = create("user", {"name": "Alice", "email": "alice@example.com"})
# 更新特定字段
merge(user["id"], {"verified": True, "last_login": "2024-01-01"})
# 带条件查询
results = query("SELECT * FROM user WHERE verified = true ORDER BY created DESC")
# 完成后删除
delete(user["id"])
# 创建实体
user = create("user", {"name": "John"})
product = create("product", {"name": "Laptop", "price": 999})
# 创建关系
relate(user["id"], "purchased", product["id"], {
"quantity": 1,
"total": 999,
"date": "2024-01-15"
})
# 查询关系
purchases = query(f"SELECT * FROM {user['id']}->purchased->product")
# 插入多条记录
products = insert("product", [
{"name": "Laptop", "category": "Electronics", "price": 999},
{"name": "Mouse", "category": "Electronics", "price": 29},
{"name": "Desk", "category": "Furniture", "price": 299}
])
# 批量更新查询
query("UPDATE product SET on_sale = true WHERE category = 'Electronics'")
服务器采用以下技术构建:
项目包含使用 pytest 的全面测试套件。
# 确保 SurrealDB 正在运行
surreal start --user root --pass root
# 运行所有测试
uv run pytest
# 运行并查看覆盖率
uv run pytest --cov=surreal_mcp
# 运行特定测试文件
uv run pytest tests/test_tools.py
# 运行特定测试类或方法
uv run pytest tests/test_tools.py::TestQueryTool
uv run pytest tests/test_tools.py::TestQueryTool::test_query_simple
# 运行并输出详细信息
uv run pytest -v
# 仅运行匹配模式的测试
uv run pytest -k "test_create"
tests/
├── __init__.py
├── conftest.py # 测试夹具和配置
├── test_tools.py # 所有 MCP 工具的测试
└── test_server.py # 服务器配置的测试
测试套件包含常见测试数据的夹具:
clean_db - 确保数据库状态干净sample_user_data - 示例用户数据created_user - 预创建的用户记录created_product - 预创建的产品记录示例测试:
@pytest.mark.asyncio
async def test_create_user(clean_db, sample_user_data):
result = await mcp._tools["create"].func(
table="user",
data=sample_user_data
)
assert result["success"] is True
assert result["data"]["email"] == sample_user_data["email"]
欢迎贡献代码!请随时提交拉取请求。
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)本项目采用 MIT 许可证 - 详情请参阅 LICENSE 文件。