使用说明
@nam088/mcp数据库服务器
MCP(模型上下文协议)服务器,用于多种数据库类型,采用模块化架构设计,易于扩展。
📖 阅读越南语: README.vi.md
特性
服务器提供以下工具:
- 怎么翻译:执行SELECT查询并返回结果
- execute_sql:执行任何SQL命令(INSERT、UPDATE、DELETE、CREATE等)
- list_tables:列出数据库中的所有表
- describe_table:获取有关表架构的详细信息
建筑
该项目分为单独的模块:
src/
├── index.ts # Entry point, initializes adapter based on DB_TYPE
├── server.ts # MCP server implementation
└── adapters/
├── base.ts # Base adapter interface
├── postgres.ts # PostgreSQL adapter implementation
├── mysql.ts # MySQL adapter implementation
├── sqlite.ts # SQLite adapter implementation
├── redis.ts # Redis adapter implementation
├── mongo.ts # MongoDB adapter implementation
└── ldap.ts # LDAP adapter implementation
数据库适配器
每个数据库都有自己的适配器来实现 DatabaseAdapter 接口:
PostgresAdapter:PostgreSQL支持13个SQL工具MySQLAdapter:支持MySQL/MariaDB和13个SQL工具SQLiteAdapter:SQLite支持13个SQL工具(使用better-squarite3)RedisAdapter:支持16个Redis工具的RedisMongoAdapter:15个MongoDB工具支持MongoDBLDAPAdapter:使用6个LDAP工具支持LDAP
安装
- 安装依赖项:
npm install
- 构建项目:
npm run build
配置
环境变量
-
DB_TYPE:数据库类型(默认值:postgres)postgres或postgresql:PostgreSQLmysql或mysql2:MySQL/MariaDBsqlite:SQLiteredis:Redismongodb或mongo:MongoDBldap:LDAP
-
READ_ONLY_MODE:只读模式(默认:true-更安全)true或未设置:仅允许读取,阻止所有写入操作(默认)false或0:允许读取和写入(必须明确设置)
-
POSTGRES_CONNECTION_STRING:PostgreSQL的连接字符串 -
MYSQL_CONNECTION_STRING或MYSQL_URL:MySQL的连接字符串 -
SQLITE_CONNECTION_STRING或SQLITE_URL:SQLite的连接字符串(文件路径) -
REDIS_CONNECTION_STRING或REDIS_URL:Redis的连接字符串 -
MONGODB_CONNECTION_STRING或MONGODB_URL:MongoDB的连接字符串 -
LDAP_CONNECTION_STRING或LDAP_URL:LDAP的连接字符串 -
LDAP_BIND_DN:绑定LDAP身份验证的DN(可选) -
LDAP_BIND_PASSWORD:绑定LDAP身份验证密码(可选) -
DATABASE_URL:连接字符串(PostgreSQL、MySQL或SQLite的回退)
示例:
# PostgreSQL with read-only mode (default, no need to set READ_ONLY_MODE)
export DB_TYPE="postgres"
export POSTGRES_CONNECTION_STRING="postgresql://user:password@localhost:5432/mydb"
# READ_ONLY_MODE defaults to true
# Redis with write access (must be set explicitly)
export DB_TYPE="redis"
export REDIS_CONNECTION_STRING="redis://localhost:6379"
export READ_ONLY_MODE="false"
# MySQL with read-only mode (default)
export DB_TYPE="mysql"
export MYSQL_CONNECTION_STRING="mysql://user:password@localhost:3306/mydb"
# READ_ONLY_MODE defaults to true
# SQLite with read-only mode (default)
export DB_TYPE="sqlite"
export SQLITE_CONNECTION_STRING="sqlite://./database.sqlite"
# READ_ONLY_MODE defaults to true
# MongoDB with read-only mode (default)
export DB_TYPE="mongodb"
export MONGODB_CONNECTION_STRING="mongodb://localhost:27017/mydb"
# READ_ONLY_MODE defaults to true
用法
使用npx(推荐)
发布后,您可以直接使用以下命令运行服务器 npx 不安装:
npx @nam088/mcp-database-server
地方发展
在本地运行服务器:
npm start
或者使用watch在开发模式下运行:
npm run dev
MCP客户端配置
将服务器添加到MCP客户端配置中(例如,Claude Desktop)。您可以使用 npx (推荐)或 node 一条本地路径。
使用npx(推荐)
最简单的方法是使用 npx,它将自动下载并运行该包:
{
"mcpServers": {
"postgres-readonly": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "postgres",
"POSTGRES_CONNECTION_STRING": "postgresql://user:password@localhost:5432/mydb"
}
}
}
}
备注:The -y 如果尚未存在,标志会自动接受包安装。
使用本地安装
如果您更喜欢在本地安装或使用特定路径:
{
"mcpServers": {
"postgres-readonly": {
"command": "node",
"args": ["/path/to/database-server/dist/index.js"],
"env": {
"DB_TYPE": "postgres",
"POSTGRES_CONNECTION_STRING": "postgresql://user:password@localhost:5432/mydb"
}
}
}
}
PostgreSQL
带有npx的只读模式(默认)
无需设置 READ_ONLY_MODE 因为这是默认设置:
{
"mcpServers": {
"postgres-readonly": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "postgres",
"POSTGRES_CONNECTION_STRING": "postgresql://user:password@localhost:5432/mydb"
}
}
}
}
npx读写模式
备注:您必须明确设置 READ_ONLY_MODE 到 "false" 以允许写入操作。
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "postgres",
"POSTGRES_CONNECTION_STRING": "postgresql://user:password@localhost:5432/mydb",
"READ_ONLY_MODE": "false"
}
}
}
}
使用npx的DATABASE_URL
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "postgres",
"DATABASE_URL": "postgresql://user:password@localhost:5432/mydb"
}
}
}
}
瑞迪斯
带有npx的只读模式
{
"mcpServers": {
"redis-readonly": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "redis",
"REDIS_CONNECTION_STRING": "redis://localhost:6379",
"READ_ONLY_MODE": "true"
}
}
}
}
npx读写模式
备注:您必须明确设置 READ_ONLY_MODE 到 "false" 以允许写入操作。
{
"mcpServers": {
"redis": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "redis",
"REDIS_CONNECTION_STRING": "redis://localhost:6379",
"READ_ONLY_MODE": "false"
}
}
}
}
使用REDIS_URL和npx密码
{
"mcpServers": {
"redis": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "redis",
"REDIS_URL": "redis://:password@localhost:6379/0"
}
}
}
}
使用npx的SSL/TLS Redis
{
"mcpServers": {
"redis": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "redis",
"REDIS_CONNECTION_STRING": "rediss://user:password@redis.example.com:6380"
}
}
}
}
MongoDB
带有npx的只读模式
{
"mcpServers": {
"mongodb-readonly": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "mongodb",
"MONGODB_CONNECTION_STRING": "mongodb://localhost:27017/mydb",
"READ_ONLY_MODE": "true"
}
}
}
}
npx读写模式
备注:您必须明确设置 READ_ONLY_MODE 到 "false" 以允许写入操作。
{
"mcpServers": {
"mongodb": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "mongodb",
"MONGODB_CONNECTION_STRING": "mongodb://localhost:27017/mydb",
"READ_ONLY_MODE": "false"
}
}
}
}
使用npx进行身份验证的MongoDB
{
"mcpServers": {
"mongodb": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "mongodb",
"MONGODB_CONNECTION_STRING": "mongodb://username:password@localhost:27017/mydb?authSource=admin"
}
}
}
}
使用npx设置副本的MongoDB
{
"mcpServers": {
"mongodb": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "mongodb",
"MONGODB_CONNECTION_STRING": "mongodb://host1:27017,host2:27017,host3:27017/mydb?replicaSet=myReplicaSet"
}
}
}
}
使用npx的MongoDB Atlas(云)
{
"mcpServers": {
"mongodb-atlas": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "mongodb",
"MONGODB_CONNECTION_STRING": "mongodb+srv://username:password@cluster.mongodb.net/mydb?retryWrites=true&w=majority"
}
}
}
}
MySQL
带有npx的只读模式(默认)
无需设置 READ_ONLY_MODE 因为这是默认设置:
{
"mcpServers": {
"mysql-readonly": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "mysql",
"MYSQL_CONNECTION_STRING": "mysql://user:password@localhost:3306/mydb"
}
}
}
}
npx读写模式
备注:您必须明确设置 READ_ONLY_MODE 到 "false" 以允许写入操作。
{
"mcpServers": {
"mysql": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "mysql",
"MYSQL_CONNECTION_STRING": "mysql://user:password@localhost:3306/mydb",
"READ_ONLY_MODE": "false"
}
}
}
}
MySQL使用npx的DATABASE_URL
{
"mcpServers": {
"mysql": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "mysql",
"DATABASE_URL": "mysql://user:password@localhost:3306/mydb"
}
}
}
}
SQLite
带有npx的只读模式(默认)
无需设置 READ_ONLY_MODE 因为这是默认设置:
{
"mcpServers": {
"sqlite-readonly": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "sqlite",
"SQLITE_CONNECTION_STRING": "sqlite://./database.sqlite"
}
}
}
}
npx读写模式
备注:您必须明确设置 READ_ONLY_MODE 到 "false" 以允许写入操作。
{
"mcpServers": {
"sqlite": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "sqlite",
"SQLITE_CONNECTION_STRING": "sqlite://./database.sqlite",
"READ_ONLY_MODE": "false"
}
}
}
}
使用npx的具有绝对路径的SQLite
{
"mcpServers": {
"sqlite": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "sqlite",
"SQLITE_CONNECTION_STRING": "/path/to/database.sqlite"
}
}
}
}
轻量级目录访问协议
备注:该项目使用 ldapts 而不是 ldapjs (已退役),以确保可持续性和更好的支持。
带有npx的只读模式(默认)
无需设置 READ_ONLY_MODE 因为这是默认设置:
{
"mcpServers": {
"ldap-readonly": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "ldap",
"LDAP_CONNECTION_STRING": "ldap://localhost:389"
}
}
}
}
npx读写模式
备注:您必须明确设置 READ_ONLY_MODE 到 "false" 以允许写入操作。
{
"mcpServers": {
"ldap": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "ldap",
"LDAP_CONNECTION_STRING": "ldap://localhost:389",
"READ_ONLY_MODE": "false"
}
}
}
}
使用npx进行身份验证的LDAP
{
"mcpServers": {
"ldap": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "ldap",
"LDAP_CONNECTION_STRING": "ldap://localhost:389",
"LDAP_BIND_DN": "cn=admin,dc=example,dc=com",
"LDAP_BIND_PASSWORD": "password123"
}
}
}
}
使用npx的LDAP和LDAPS(SSL/TLS)
{
"mcpServers": {
"ldap": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "ldap",
"LDAP_CONNECTION_STRING": "ldaps://ldap.example.com:636"
}
}
}
}
使用npx的Active Directory(Microsoft)
{
"mcpServers": {
"ldap-ad": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "ldap",
"LDAP_CONNECTION_STRING": "ldap://ad.example.com:389",
"LDAP_BIND_DN": "CN=Service Account,CN=Users,DC=example,DC=com",
"LDAP_BIND_PASSWORD": "password123"
}
}
}
}
使用npx配置多个数据库
您可以在同一MCP客户端中配置多个数据库:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "postgres",
"POSTGRES_CONNECTION_STRING": "postgresql://user:password@localhost:5432/mydb",
"READ_ONLY_MODE": "true"
}
},
"redis": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "redis",
"REDIS_CONNECTION_STRING": "redis://localhost:6379",
"READ_ONLY_MODE": "false"
}
},
"mysql": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "mysql",
"MYSQL_CONNECTION_STRING": "mysql://user:password@localhost:3306/mydb"
}
},
"sqlite": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "sqlite",
"SQLITE_CONNECTION_STRING": "sqlite://./database.sqlite"
}
},
"mongodb": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "mongodb",
"MONGODB_CONNECTION_STRING": "mongodb://localhost:27017/mydb"
}
}
}
}
只读模式
⚠️ 默认情况下,服务器以只读模式运行 保护数据免受意外删除或修改。当 READ_ONLY_MODE 是 true 如果未设置(默认),服务器将阻止所有写入操作:
PostgreSQL、MySQL、SQLite:
- ✅ 允许:
query(选择) - ❌ 此 路 不通:
execute_sql(插入、更新、删除、创建等)
Redis:
- ✅ 允许:
redis_get,redis_keys,redis_exists,redis_ttl,redis_type,redis_dbsize,redis_info,redis_hget,redis_hgetall,redis_lrange,redis_smembers,redis_zrange - ❌ 此 路 不通:
redis_set,redis_del,redis_expire,redis_hset
MongoDB:
- ✅ 允许:
mongo_find,mongo_find_one,mongo_count,mongo_aggregate,mongo_list_collections,mongo_get_collection_stats,mongo_get_indexes,mongo_get_database_stats - ❌ 此 路 不通:
mongo_insert_one,mongo_insert_many,mongo_update_one,mongo_update_many,mongo_delete_one,mongo_delete_many,mongo_create_index
LDAP:
- ✅ 允许:
ldap_search,ldap_authenticate,ldap_compare - ❌ 此 路 不通:
ldap_add,ldap_modify,ldap_delete
当尝试在只读模式下执行写入操作时,服务器将返回错误:
Error: Server is running in read-only mode. Write operations are disabled.
添加新的数据库适配器
要添加对新数据库的支持,请执行以下操作:
- 在中创建新的适配器文件
src/adapters/(例如。,mysql.ts) - 实施
DatabaseAdapter接口来自base.ts - 在中添加新案例
src/index.ts初始化适配器
例子:
// src/adapters/mysql.ts
import { DatabaseAdapter, QueryResult, ExecuteResult, TableSchema } from "./base.js";
export class MySQLAdapter implements DatabaseAdapter {
// Implement methods from DatabaseAdapter
async query(sql: string): Promise<QueryResult> { ... }
async execute(sql: string, params?: any[]): Promise<ExecuteResult> { ... }
async listTables(schema?: string): Promise<string[]> { ... }
async describeTable(table: string, schema?: string): Promise<TableSchema> { ... }
}
然后添加到 src/index.ts:
case "mysql":
return new MySQLAdapter(connectionString);
工具
怎么翻译
执行SELECT查询:
{
"name": "query",
"arguments": {
"sql": "SELECT * FROM users LIMIT 10"
}
}
execute_sql
执行SQL命令:
{
"name": "execute_sql",
"arguments": {
"sql": "INSERT INTO users (name, email) VALUES ('John', 'john@example.com')"
}
}
list_tables
列表表格:
{
"name": "list_tables",
"arguments": {
"schema": "public"
}
}
describe_table
描述表架构:
{
"name": "describe_table",
"arguments": {
"table": "users",
"schema": "public"
}
}
许可证
麻省理工学院
