Skip to main content
Glama

MseeP.ai 安全评估徽章

MCP 数据库服务器

用于连接和使用各种数据库系统的模型上下文协议 (MCP) 实现。

支持的数据库

  • SQLite

  • PostgreSQL

  • 微软 SQL 服务器

  • MongoDB

安装

npm install -g mcp-dbs

用法

MCP 数据库服务器可以以两种模式使用:

SSE 模式(默认)

默认情况下,服务器在端口 3001 上以 SSE(服务器发送事件)模式运行:

npx mcp-dbs

这将启动一个 HTTP 服务器,其 SSE 端点位于http://localhost:3001/mcp

自定义端口

您可以使用--port选项指定自定义端口:

npx mcp-dbs --port 8080

STDIO模式

对于通过标准输入/输出进行通信的工具,您可以使用--stdio选项:

npx mcp-dbs --stdio

Claude 桌面集成

您可以将 mcp-dbs 与 Claude Desktop 集成,只需将其添加到您的 Claude 配置文件中即可。

配置步骤

  1. 打开或创建您的 Claude Desktop 配置文件

  2. 将 mcp-dbs 配置添加到mcpServers部分:

{ "mcpServers": { "mcp-dbs": { "command": "node", "args": [ "/path/to/your/mcp-dbs/dist/cli.js", "--stdio" ], "env": { "MCP_MONGODB_URI": "mongodb://localhost:27017", "MCP_MONGODB_DATABASE": "your-database-name" } } } }

用您自己的数据库连接详细信息替换环境变量。

笔记

  • command应该是node

  • args中,提供 mcp-dbs 安装中 cli.js 文件的绝对路径

  • 为您的数据库类型配置适当的环境变量(请参阅下面的环境变量部分)

  • 您可以对任何受支持的数据库(SQLite、PostgreSQL、SQL Server 或 MongoDB)使用环境变量

与 Claude 一起使用

配置完成后,Claude 将能够使用下文所述的 MCP 工具访问您的数据库。您可以要求 Claude 执行以下操作:

  • 连接到数据库

  • 执行查询并获取结果

  • 探索数据库架构

  • 使用表格和数据

工具

  • connect-database :连接到数据库

  • disconnect-database :断开与数据库的连接

  • 执行查询:执行查询并返回结果

  • 执行更新:执行查询但不返回结果

资源

  • 数据库模式:获取完整的数据库模式

  • table-schema :获取特定表的架构

  • 表列表:获取所有表的列表

使用环境变量进行配置

您可以使用环境变量配置数据库连接:

SQLite

# Set these environment variables before connecting export MCP_SQLITE_FILENAME="path/to/database.db" export MCP_SQLITE_CREATE_IF_NOT_EXISTS="true"

PostgreSQL

# Set these environment variables before connecting export MCP_POSTGRES_HOST="your-postgres-host" export MCP_POSTGRES_PORT="5432" export MCP_POSTGRES_DATABASE="your-database-name" export MCP_POSTGRES_USER="your-username" export MCP_POSTGRES_PASSWORD="your-password" export MCP_POSTGRES_SSL="false"

SQL 服务器

# Set these environment variables before connecting export MCP_MSSQL_SERVER="your-server-address" export MCP_MSSQL_PORT="1433" export MCP_MSSQL_DATABASE="your-database-name" export MCP_MSSQL_USER="your-username" export MCP_MSSQL_PASSWORD="your-password" export MCP_MSSQL_ENCRYPT="true" export MCP_MSSQL_TRUST_SERVER_CERTIFICATE="true"

MongoDB

# Set these environment variables before connecting export MCP_MONGODB_URI="mongodb://localhost:27017" export MCP_MONGODB_DATABASE="your-database-name" export MCP_MONGODB_MAX_POOL_SIZE="10" export MCP_MONGODB_USE_UNIFIED_TOPOLOGY="true"

这些环境变量将优先于传递给 connect-database 工具的任何配置。

MCP 工具

该服务器公开以下 MCP 工具:

连接数据库

连接到数据库。

参数:

  • connectionId :连接的唯一标识符

  • type :数据库类型( sqlitepostgresmssqlmongodb

SQLite 示例:

{ "connectionId": "my-sqlite-db", "type": "sqlite" }

PostgreSQL 示例:

{ "connectionId": "my-postgres-db", "type": "postgres" }

SQL Server 示例:

{ "connectionId": "my-mssql-db", "type": "mssql" }

MongoDB 示例:

{ "connectionId": "my-mongodb-db", "type": "mongodb" }

断开数据库

断开与数据库的连接。

参数:

  • connectionId :要断开的连接 ID

执行查询

执行返回结果的查询。

参数:

  • connectionId :连接 ID

  • query :SQL 查询或 MongoDB 聚合管道(作为 JSON 字符串)

  • params :(可选)查询参数数组。对于 MongoDB,第一个参数是集合名称。

SQL 示例:

{ "connectionId": "my-postgres-db", "query": "SELECT * FROM users WHERE age > $1", "params": [21] }

MongoDB 示例:

{ "connectionId": "my-mongodb-db", "query": "[{\"$match\": {\"age\": {\"$gt\": 21}}}, {\"$sort\": {\"name\": 1}}]", "params": ["users"] }

MongoDB 的示例(带有嵌入式集合的新格式):

{ "connectionId": "my-mongodb-db", "query": "{\"collection\": \"users\", \"pipeline\": [{\"$match\": {\"age\": {\"$gt\": 21}}}, {\"$sort\": {\"name\": 1}}]}" }

MongoDB 示例(shell 语法):

{ "connectionId": "my-mongodb-db", "query": "db.getCollection('users').find({\"age\": {\"$gt\": 21}})" }

MongoDB 的示例(直接集合引用 shell 语法):

{ "connectionId": "my-mongodb-db", "query": "db.users.find({\"age\": {\"$gt\": 21}})" }

MongoDB 示例(原始命令):

{ "connectionId": "my-mongodb-db", "query": "{\"find\": \"users\", \"filter\": {\"age\": {\"$gt\": 21}}}" }

执行更新

执行不返回结果的查询(INSERT、UPDATE、DELETE)。

参数:

  • connectionId :连接 ID

  • query :SQL 查询或 MongoDB 命令(作为 JSON 字符串)

  • params :(可选)查询参数数组。对于 MongoDB,第一个参数是集合名称。

SQL 示例:

{ "connectionId": "my-postgres-db", "query": "INSERT INTO users (name, age) VALUES ($1, $2)", "params": ["John Doe", 30] }

MongoDB 示例:

{ "connectionId": "my-mongodb-db", "query": "{\"insertOne\": {\"name\": \"John Doe\", \"age\": 30}}", "params": ["users"] }

MongoDB 的示例(带有嵌入式集合的新格式):

{ "connectionId": "my-mongodb-db", "query": "{\"collection\": \"users\", \"operation\": {\"insertOne\": {\"name\": \"John Doe\", \"age\": 30}}}" }

MongoDB 示例(shell 语法):

{ "connectionId": "my-mongodb-db", "query": "db.getCollection('users').insertOne({\"name\": \"John Doe\", \"age\": 30})" }

MongoDB 的示例(直接集合引用 shell 语法):

{ "connectionId": "my-mongodb-db", "query": "db.users.insertOne({\"name\": \"John Doe\", \"age\": 30})" }

MongoDB 示例(原始命令):

{ "connectionId": "my-mongodb-db", "query": "{\"insert\": \"users\", \"documents\": [{\"name\": \"John Doe\", \"age\": 30}]}" }

MCP 资源

服务器公开以下 MCP 资源:

数据库架构

URI: database://{connectionId}/schema

返回有关数据库的架构信息,包括所有表及其列。

表模式

URI: database://{connectionId}/tables/{tableName}

返回有关特定表的架构信息,包括其列。

表格列表

URI: database://{connectionId}/tables

返回数据库中所有表的列表。

发展

测试

运行测试:

npm test

支持该项目

如果您发现这个项目有帮助,请考虑给我买杯咖啡!

扫描上方二维码或点击此处支持该项目的发展。

执照

麻省理工学院

-
security - not tested
A
license - permissive license
-
quality - not tested

Related MCP Servers

  • A
    security
    A
    license
    A
    quality
    A powerful Model Context Protocol (MCP) tool for exploring and managing different types of databases including PostgreSQL, MySQL, and Firestore.
    Last updated -
    9
    5
    MIT License
  • -
    security
    F
    license
    -
    quality
    A Model Context Protocol server that provides tools for connecting to and interacting with various database systems (SQLite, PostgreSQL, MySQL/MariaDB, SQL Server) through a unified interface.
    Last updated -
    3
  • A
    security
    F
    license
    A
    quality
    An all-in-one Model Context Protocol (MCP) server that connects your coding AI to numerous databases, data warehouses, data pipelines, and cloud services, streamlining development workflow through seamless integrations.
    Last updated -
    3
    • Apple
    • Linux
  • -
    security
    A
    license
    -
    quality
    An implementation of the Model Context Protocol (MCP) server that enables multiple clients to connect simultaneously and handles basic context management and messaging with an extendable architecture.
    Last updated -
    MIT License

View all related MCP servers

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/cuongtl1992/mcp-dbs'

If you have feedback or need assistance with the MCP directory API, please join our Discord server