SQLite MCP服务器
用于SQLite数据库操作的模型上下文协议(MCP)服务器,使用FastMCP构建。此服务器允许LLM代理读取、创建、更新和删除SQLite数据库中的数据。
特性
- 数据库管理:打开/关闭SQLite数据库
- CRUD操作:创建表、插入、读取、更新和删除记录
- 查询执行:执行原始SQL SELECT查询
- 架构检查:列出表和查看表架构
- 类型安全:完整的类型提示和错误处理
安装
先决条件
- Python 3.8或更高版本
- 点
设置
- 克隆或导航到项目目录:
cd sqlite-mcp- 创建虚拟环境(推荐):
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- 安装依赖项:
pip install -r requirements.txt快速开始
运行服务器
# Using the npm script
npm start
# Or directly with Python
python -m sqlite_mcp.server
# Or with uvicorn (if using HTTP transport)
uvicorn sqlite_mcp.server:mcp --reload可用工具
1. open_database
打开或创建SQLite数据库文件。
参数:
path(string):SQLite数据库文件的路径
例子:
{
"path": "/path/to/my_database.db"
}2. close_database
关闭当前数据库连接。
例子:
{}3. execute_query
执行SELECT查询并返回结果。
参数:
query(字符串):SQL SELECT查询parameters(数组,可选):查询已准备语句的参数
例子:
{
"query": "SELECT * FROM users WHERE age > ?",
"parameters": [18]
}4. create_table
在数据库中创建新表。
参数:
table(string):表名schema(字符串):列定义
例子:
{
"table": "users",
"schema": "id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE, age INTEGER"
}5. 插入
在表格中插入一行。
参数:
table(string):表名data(对象):列名和值
例子:
{
"table": "users",
"data": {
"name": "John Doe",
"email": "john@example.com",
"age": 30
}
}6. 更新
更新表中的行。
参数:
table(string):表名data(对象):列名和新值where(string):WHERE子句条件where_params(数组,可选):WHERE子句的参数
例子:
{
"table": "users",
"data": {
"age": 31
},
"where": "id = ?",
"where_params": [1]
}7. 删除
从表中删除行。
参数:
table(string):表名where(string):WHERE子句条件where_params(数组,可选):WHERE子句的参数
例子:
{
"table": "users",
"where": "id = ?",
"where_params": [1]
}8. list_tables
列出数据库中的所有表。
例子:
{}退货:
{
"tables": ["users", "products", "orders"]
}9. get_table_schema
获取表的架构(列、类型、约束)。
参数:
table(string):表名
例子:
{
"table": "users"
}退货:
{
"columns": [
{
"cid": 0,
"name": "id",
"type": "INTEGER",
"notnull": 0,
"dflt_value": null,
"pk": 1
},
{
"cid": 1,
"name": "name",
"type": "TEXT",
"notnull": 1,
"dflt_value": null,
"pk": 0
}
]
}使用示例
示例1:创建数据库和表
# Open database
call open_database with path="/tmp/myapp.db"
# Create a users table
call create_table with table="users" schema="id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE, age INTEGER"
# List tables
call list_tables with no parameters示例2:插入和查询数据
# Insert a user
call insert with table="users" data={"name": "Alice Johnson", "email": "alice@example.com", "age": 28}
# Query users
call execute_query with query="SELECT * FROM users WHERE age >= ?" parameters=[25]示例3:更新记录
# Update user's age
call update with table="users" data={"age": 29} where="name = ?" where_params=["Alice Johnson"]
# Verify update
call execute_query with query="SELECT * FROM users WHERE name = ?" parameters=["Alice Johnson"]示例4:删除记录
# Delete a user
call delete with table="users" where="id = ?" where_params=[1]
# List remaining users
call execute_query with query="SELECT * FROM users"与LLM代理集成
此MCP服务器旨在与LLM代理一起使用。如果配置正确,代理可以:
- 创建数据库和表
- 插入、更新和删除记录
- 查询数据
- 检查数据库架构
代理提示示例
You have access to a SQLite database through MCP tools.
Create a simple task management database with the following requirements:
1. Create a "tasks" table with columns: id (PRIMARY KEY), title, description, status, and created_at
2. Insert 3 sample tasks
3. Query all tasks with status='pending'
4. Update the first task's status to 'completed'错误处理
所有工具都包括全面的错误处理。常见错误:
- “没有打开的数据库”:呼叫
open_database第一 - “表创建失败”:检查架构参数中的SQL语法
- “查询执行失败”:验证SQL查询语法和参数
- “插入/更新/删除失败”:检查表名、列名和数据类型
项目结构
sqlite-mcp/
├── sqlite_mcp/
│ ├── __init__.py # Package initialization
│ ├── server.py # FastMCP server with tool definitions
│ └── db.py # SQLite database operations
├── requirements.txt # Python dependencies
├── package.json # Project metadata
└── README.md # This file配置
要将此服务器与Claude或其他MCP客户端一起使用,请将其添加到配置文件中:
适用于克劳德桌面
编辑 ~/.config/Claude/claude_desktop_config.json:
{
"mcpServers": {
"sqlite-mcp": {
"command": "python",
"args": ["-m", "sqlite_mcp.server"],
"cwd": "/path/to/sqlite-mcp"
}
}
}业绩说明
- SQLite适用于单用户和小型团队应用程序
- 对于并发访问,考虑使用连接池
- 大型查询可能受益于适当的索引
- 使用事务实现数据一致性(如果需要,可以添加)
安全考虑
⚠️ 重要:此服务器直接执行SQL查询。当使用不受信任的输入时:
- 始终使用参数化查询(
parameters工具中的字段) - 在发送到服务器之前验证输入数据
- 限制数据库文件权限
- 不要暴露数据库文件中的敏感数据
故障排除
服务器无法启动
- 检查Python版本(3.8+)
- 验证已安装的所有依赖项:
pip install -r requirements.txt - 如果使用HTTP传输,请检查端口冲突
找不到数据库文件
- 确保目录路径存在
- 检查文件权限
- 对数据库文件使用绝对路径
查询错误
- 验证表名和列名是否完全匹配
- 使用正确的SQL语法
- 检查数据类型是否与列定义匹配
发展
要修改服务器,请执行以下操作:
- 编辑
sqlite_mcp/server.py添加新工具 - 编辑
sqlite_mcp/db.py修改数据库操作 - 重新启动服务器以应用更改
许可证
麻省理工学院
贡献
请随时提交问题和增强请求!
