# querylite-mcp-client
**Repository Path**: itsansui/querylite-mcp-client
## Basic Information
- **Project Name**: querylite-mcp-client
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: MIT
- **Default Branch**: main
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2026-10-20
- **Last Updated**: 2026-10-20
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# querylite-mcp-client
通过官方 Python MCP SDK 调用 `querylite-mcp` 的独立客户端,提供异步 Python 接口和命令行工具。客户端通过标准输入输出连接服务,所有数据库操作都经由 MCP 工具执行。
需要 Python **3.11 或以上版本**、Node.js **22.13.0 或以上版本**。当前服务项目位于同级目录 `../querylite-mcp`。
## 工程结构
```text
querylite-mcp-client/
├── src/querylite_client/
│ ├── __init__.py # 公共接口
│ ├── __main__.py # python -m 入口
│ ├── client.py # MCP 连接、工具发现与数据库操作
│ ├── config.py # TOML 配置校验、路径及启动命令
│ ├── cli.py # 命令行解析、JSON 输入输出及退出码
│ ├── values.py # Python 参数与 MCP 数据类型转换
│ ├── errors.py # 配置、连接、输入和工具异常
│ ├── demo.py # 独立示例表的增删改查演示
│ └── py.typed # 类型信息标记
├── tests/ # 单元测试和真实 MCP 集成测试
├── examples/ # Python 调用、JSON 输入及启动配置
├── docs/ # 工程设计说明
├── .github/workflows/ # 持续集成配置
├── client.toml # 当前目录可直接运行的本地配置
├── pyproject.toml # 依赖、命令行入口与工具配置
├── uv.lock # 可复现依赖锁定
├── README.md
└── LICENSE
```
## 安装和首次运行
先确认服务已安装依赖并完成构建:
```powershell
cd D:\Users\27999\Desktop\ces\querylite-mcp
npm ci
npm run build
```
进入客户端项目:
```powershell
cd D:\Users\27999\Desktop\ces\querylite-mcp-client
uv sync --locked
uv run querylite-client info
uv run querylite-client tools
uv run querylite-client demo
```
`demo` 会创建名称随机的独立示例表,依次新增、查询、更新和删除一条记录,并在正常完成后删除示例表。结果包含每一步的 JSON 返回值。
默认配置显式启用读写和创建功能,数据库为客户端项目自己的 `data/client-demo.sqlite`。如需连接业务数据库,修改 `client.toml` 中的 `database.path`,并按需要设置读写权限。
没有 `uv` 时也可使用标准虚拟环境:
```powershell
python -m venv .venv
.\.venv\Scripts\python.exe -m pip install -e .
.\.venv\Scripts\querylite-client.exe --help
.\.venv\Scripts\querylite-client.exe demo
```
## 配置
当前 `client.toml`:
```toml
[server]
launcher = "node"
project_dir = "../querylite-mcp"
timeout_seconds = 30
[database]
path = "./data/client-demo.sqlite"
allow_write = true
create = true
```
所有配置文件中的相对路径,均以**配置文件所在目录**为基准。因此使用 `--config` 指向绝对路径时,不受命令行工作目录影响。
支持三种启动方式:
| `server.launcher` | 启动方式 | 前置条件 |
| --- | --- | --- |
| `node` | 执行服务项目中的 `dist/cli.js` | 服务已运行 `npm ci` 和 `npm run build` |
| `npx-local` | 通过 `npx --package` 启动本地 `.tgz` 包 | 服务已运行 `npm pack` |
| `npx` | 通过 `npx` 启动 npm 包 | 服务包已发布到 npm |
本地安装包模式:
```powershell
uv run querylite-client --config examples/client.npx-local.toml demo
```
这个模式对应的配置见 [examples/client.npx-local.toml](examples/client.npx-local.toml)。服务尚未发布到 npm 时,使用 `node` 或 `npx-local` 即可。npm 模式示例见 [examples/client.npx.toml](examples/client.npx.toml)。
完整配置项:
| 配置项 | 默认值 | 说明 |
| --- | --- | --- |
| `server.launcher` | `"node"` | `node`、`npx-local` 或 `npx` |
| `server.project_dir` | `"../querylite-mcp"` | 本地服务项目路径,供 `node` 模式使用 |
| `server.archive_path` | 无 | 本地 `.tgz` 文件路径,供 `npx-local` 模式使用 |
| `server.npm_package` | `"querylite-mcp@0.1.0"` | npm 包及版本,供 `npx` 模式使用 |
| `server.node_command` | `"node"` | Node.js 命令或可执行文件路径 |
| `server.npx_command` | `"npx"` | npx 命令或可执行文件路径 |
| `server.timeout_seconds` | `30` | SDK 响应读取超时,大于 0 且不超过 3600 秒 |
| `server.env` | `{}` | 显式传递给子进程的额外环境变量 |
| `database.path` | 必填 | SQLite 文件路径 |
| `database.allow_write` | `false` | 是否开放写入工具 |
| `database.create` | `false` | 是否允许创建数据库,要求同时启用写入 |
配置校验会拒绝未知字段、非法数值和未启用写入的创建操作。支持中文、空格及普通文件路径,不支持内存数据库或 SQLite URI。
每条连接会启动并持有一个 MCP 服务子进程。`async with` 结束时释放连接和子进程;在同一连接里可以连续调用多个工具。命令行的每次调用拥有独立连接。
## 命令行
```powershell
uv run querylite-client --help
uv run python -m querylite_client --help
```
全局参数放在子命令前面:
```powershell
uv run querylite-client --config client.toml info
uv run querylite-client --read-only info
uv run querylite-client --db ./data/other.sqlite --allow-write --create info
```
`--db` 提供的相对路径按命令行当前工作目录解析。`--read-only` 同时关闭写入和自动创建,适用于已经存在的数据库。
| 子命令 | 功能 |
| --- | --- |
| `tools` | 列出 MCP 工具及输入结构 |
| `info` | 查看当前数据库、权限及限制 |
| `tables` | 分页查看表和视图 |
| `describe
` | 查看字段、索引和外键 |
| `query ` | 执行参数化只读查询 |
| `execute ` | 执行单条写入 SQL 或 DDL |
| `insert ` | 新增一条记录 |
| `update ` | 按条件更新记录 |
| `delete ` | 按条件删除记录 |
| `transaction` | 原子执行多条写入 SQL |
| `call ` | 按名称直接调用 MCP 工具 |
| `demo` | 运行独立示例表的完整增删改查演示 |
### 建表、增删改查
```powershell
uv run querylite-client execute "CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE)"
uv run querylite-client insert users --values-file examples/insert-values.json
uv run querylite-client query "SELECT * FROM users WHERE id = ?" --params-file examples/query-params.json
uv run querylite-client update users --values-file examples/update-values.json --where-file examples/where-id.json
uv run querylite-client describe users
uv run querylite-client delete users --where-file examples/where-id.json
```
为避免不同终端对 JSON 引号的转义差异,示例使用 `--values-file`、`--where-file`、`--params-file`。这些文件均为 UTF-8 JSON,允许文件开头包含 UTF-8 BOM。
也支持直接传 JSON 字符串:
```text
--values '{"name":"张三"}'
--where '[{"column":"id","operator":"eq","value":1}]'
--params '[1]'
```
上面的字符串形式需要根据所用终端正确转义。更新和删除要求非空条件数组,多个条件以 `AND` 连接;支持的条件与服务端一致。
### 事务和通用调用
```powershell
uv run querylite-client transaction --statements-file examples/transaction.json
uv run querylite-client call db_info
uv run querylite-client tables --limit 20 --offset 0
```
事务示例以已经创建的 `users` 表为目标。`call` 支持 `--arguments` 或 `--arguments-file`,参数直接传给对应 MCP 工具。
### 输出及退出码
成功结果输出到标准输出,格式为 UTF-8 JSON;服务日志和错误输出到标准错误。
| 退出码 | 含义 |
| --- | --- |
| `0` | 调用成功 |
| `2` | 命令参数、输入或配置错误 |
| `3` | 服务启动、连接、协议或其他交互错误 |
| `4` | MCP 工具报告业务错误 |
| `130` | 用户取消 |
服务端日志可能先于客户端错误写入标准错误;客户端错误本身为最后一行 JSON,例如:
```json
{"error": {"code": "UNKNOWN_TABLE", "message": "表或视图不存在:missing_table"}}
```
## Python 接口
```python
import asyncio
from querylite_client import ToolCallError, connect, load_config
async def main():
config = load_config("client.toml")
async with connect(config) as client:
print(await client.info())
print(await client.list_tools())
await client.execute(
"CREATE TABLE IF NOT EXISTS users("
"id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE)"
)
created = await client.insert(
"users", {"name": "张三", "email": "zhangsan@example.com"}
)
user_id = created["lastInsertRowid"]
where = [{"column": "id", "operator": "eq", "value": user_id}]
print(await client.query("SELECT * FROM users WHERE id = ?", [user_id]))
print(await client.update("users", {"name": "李四"}, where))
print(await client.delete("users", where))
try:
await client.describe_table("missing_table")
except ToolCallError as error:
print(error.tool, error.code, error.message)
asyncio.run(main())
```
可直接运行的演示:
```powershell
uv run python -X utf8 examples/basic_usage.py
```
异步接口与服务工具对应:
| Python 接口 | MCP 工具 |
| --- | --- |
| `client.info()` | `db_info` |
| `client.list_tables()` | `db_list_tables` |
| `client.describe_table()` | `db_describe_table` |
| `client.query()` | `db_query` |
| `client.insert()` | `db_insert` |
| `client.update()` | `db_update` |
| `client.delete()` | `db_delete` |
| `client.execute()` | `db_execute` |
| `client.transaction()` | `db_transaction` |
`client.list_tools()` 负责工具发现,`client.call_tool()` 提供通用调用。
Python `int` 超过 JavaScript 安全整数范围时会自动转为服务端的 `{"integer": "..."}`;超出 SQLite 有符号 64 位整数范围会被拒绝。Python `bytes` 自动转为 `{"base64": "..."}`。布尔值、普通数值、字符串和 `None` 按服务端约定传递。
返回值保持服务端的 JSON 结构:大整数仍是十进制字符串,二进制仍是 Base64 对象,不会将普通数字字符串自动转换为整数。通用 `call_tool()` 直接使用 MCP 参数结构,不执行字段类型转换。
## 错误与超时
配置、输入、连接和工具业务错误分别使用 `ConfigurationError`、`InputError`、`ConnectionError`、`ToolCallError`,共同继承 `QueryLiteClientError`。
客户端不自动重试请求。写入请求发生超时或连接中断时,服务端可能已经提交;应先查询当前数据确认结果,再决定下一步。响应读取超时不能保证中断 SQLite 查询。
查询结果中的 `truncated` 和 `truncationReason` 会保留,调用方应主动处理分页。数据访问限制、SQL 类型限制、事务和级联影响行数的定义由服务端执行。
## 开发与验证
```powershell
uv sync --locked --group dev
uv run ruff check src tests examples
uv run ruff format --check src tests examples
uv run mypy
uv run pytest -q
uv build
```
仅运行单元测试:
```powershell
uv run pytest -m "not integration"
```
运行真实 MCP 集成测试:
```powershell
$env:QUERYLITE_SERVER_PROJECT = "D:/Users/27999/Desktop/ces/querylite-mcp"
uv run pytest -m integration
```
集成测试通过临时数据库连接真实服务,覆盖增删改查、事务回滚、只读约束、错误传递、子进程释放和本地 `npx` 安装包启动。服务未构建或安装包不存在时,相关集成测试会说明原因并跳过;本机交付验证中所有集成测试均实际运行。
持续集成覆盖 Python 3.11 和 3.13 的 Windows、Linux 环境,执行单元测试、静态检查和构建;真实服务集成测试需另外提供服务项目。
构建产物位于 `dist/`,包括可安装的 `.whl` 和源码分发包。详细工程职责见 [docs/architecture.md](docs/architecture.md)。