Database MCP Server

Database MCP Server

Enables AI assistants to safely query and explore SQL Server and PostgreSQL databases with read-only access, supporting schema discovery, relationship exploration, and query execution.

Category
访问服务器

README

Database MCP Server

A secure, read-only Model Context Protocol (MCP) server that enables AI assistants (Claude Code, Cursor, etc.) to safely query and explore SQL Server and PostgreSQL databases.

What is this?

This MCP server acts as a bridge between AI assistants and your databases. It provides safe, read-only access so AI can help you understand your database schema, query data, and discover relationships — all without risking data modification.

Features

  • Read-Only by Design — Only SELECT queries allowed, preventing accidental data changes
  • Multi-Database — Supports both SQL Server and PostgreSQL
  • Multiple Profiles — Connect to multiple databases simultaneously (local, staging, production)
  • Relationship Discovery — Automatically discover foreign key relationships between tables
  • Safety Features — Automatic row limiting, query validation, cross-database blocking
  • PostgreSQL Extras — EXPLAIN plans, materialized views, extensions, enum types

Quick Start

Prerequisites

  • Node.js 18+
  • SQL Server and/or PostgreSQL database
  • Claude Code or Cursor IDE

Installation

cd sqlserver-mcp
npm install
npm run build

Add to Your Project

Add a .mcp.json file to the root of any project where you want database access:

PostgreSQL:

{
  "mcpServers": {
    "database": {
      "command": "node",
      "args": ["/absolute/path/to/sqlserver-mcp/build/index.js"],
      "env": {
        "SQLSERVER_CONNECTIONS": "{\"mydb\":{\"databaseType\":\"postgresql\",\"connectionString\":\"postgresql://user:pass@localhost:5432/dbname\"}}"
      }
    }
  }
}

SQL Server:

{
  "mcpServers": {
    "database": {
      "command": "node",
      "args": ["/absolute/path/to/sqlserver-mcp/build/index.js"],
      "env": {
        "SQLSERVER_CONNECTIONS": "{\"mydb\":{\"server\":\"localhost\",\"database\":\"MyDB\",\"user\":\"sa\",\"password\":\"yourpassword\",\"options\":{\"encrypt\":false,\"trustServerCertificate\":true}}}"
      }
    }
  }
}

Multiple databases:

{
  "mcpServers": {
    "database": {
      "command": "node",
      "args": ["/absolute/path/to/sqlserver-mcp/build/index.js"],
      "env": {
        "SQLSERVER_CONNECTIONS": "{\"pg_local\":{\"databaseType\":\"postgresql\",\"connectionString\":\"postgresql://user:pass@localhost:5432/appdb\"},\"sql_prod\":{\"server\":\"prod.server.com\",\"database\":\"ProdDB\",\"user\":\"readonly\",\"password\":\"pass\",\"options\":{\"encrypt\":true}}}"
      }
    }
  }
}

The profile name (e.g., mydb, pg_local) is what gets passed as the profile parameter to every tool call.

Alternative: Config File

Instead of inline JSON, you can use a config file:

{
  "mcpServers": {
    "database": {
      "command": "node",
      "args": ["/absolute/path/to/sqlserver-mcp/build/index.js"],
      "env": {
        "SQLSERVER_CONFIG_FILE": "/path/to/config.json"
      }
    }
  }
}

Where config.json contains:

{
  "local_pg": {
    "databaseType": "postgresql",
    "connectionString": "postgresql://user:pass@localhost:5432/mydb"
  },
  "local_sql": {
    "server": "localhost",
    "database": "MyDB",
    "user": "sa",
    "password": "yourpassword",
    "options": {
      "encrypt": false,
      "trustServerCertificate": true
    }
  }
}

Restart your IDE after adding or changing .mcp.json.

Available Tools

Tool Database Description
list-schemas Both List schemas with owner info and table counts
list-tables Both List tables with row counts and type info
describe-table Both Column details: types, nullability, PKs, defaults, identity
get-relationships Both Foreign key relationships (outgoing and incoming)
get-indexes Both Index details: type, columns, uniqueness, filters
run-select-query Both Execute read-only SELECT queries with parameters
explain-query Both Get estimated execution plan for a query
estimate-cost Both Estimate query cost and row counts
list-materialized-views PostgreSQL List materialized views with size and status
list-extensions PostgreSQL List installed and available extensions
list-enums PostgreSQL List user-defined enum types with values

Usage Examples

Ask your AI assistant:

  • "List all tables in the mydb database"
  • "Describe the Users table in mydb"
  • "Show me the relationships for the Orders table"
  • "Run this query on mydb: SELECT * FROM users WHERE active = true"
  • "Explain this query: SELECT u., o.total FROM users u JOIN orders o ON u.id = o.user_id"*
  • "What extensions are installed on mydb?"

Connection Profile Options

SQL Server

{
  "server": "hostname",
  "database": "database_name",
  "user": "username",
  "password": "password",
  "port": 1433,
  "options": {
    "encrypt": true,
    "trustServerCertificate": false,
    "applicationIntent": "ReadOnly",
    "requestTimeout": 30000,
    "connectionTimeout": 15000
  }
}

PostgreSQL (structured)

{
  "databaseType": "postgresql",
  "server": "hostname",
  "database": "database_name",
  "user": "username",
  "password": "password",
  "port": 5432,
  "pgOptions": {
    "ssl": true,
    "statement_timeout": 30000,
    "application_name": "mcp-server"
  }
}

PostgreSQL (connection string)

{
  "databaseType": "postgresql",
  "connectionString": "postgresql://user:pass@host:5432/dbname?sslmode=require"
}

When using connectionString, the server, database, user, and password fields are still required but can be set to placeholder values — the connection string takes precedence.

Security

  • Read-only enforcement — INSERT, UPDATE, DELETE, DROP, CREATE, ALTER, EXEC, MERGE, GRANT, REVOKE are all blocked
  • Automatic row limiting — Queries limited to 1000 rows by default (max 10,000)
  • Cross-database blocking — Three-part names (database.schema.table) are rejected
  • Parameter validation — Only alphanumeric parameter names allowed
  • Error sanitization — Credentials, IPs, and file paths are masked in error output

Best practice: Create a read-only database user for the MCP server.

-- PostgreSQL
CREATE USER mcp_readonly WITH PASSWORD 'secure_password';
GRANT CONNECT ON DATABASE mydb TO mcp_readonly;
GRANT USAGE ON SCHEMA public TO mcp_readonly;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO mcp_readonly;

-- SQL Server
CREATE LOGIN mcp_readonly WITH PASSWORD = 'secure_password';
CREATE USER mcp_readonly FOR LOGIN mcp_readonly;
EXEC sp_addrolemember 'db_datareader', 'mcp_readonly';

Development

npm run build            # Compile TypeScript
npm run dev              # Watch mode
npm test                 # Run all tests
npm run test:coverage    # Coverage report

Troubleshooting

"Unknown connection profile" — The profile name in your tool call doesn't match what's in the config. Check spelling.

"connect ECONNREFUSED" — The database isn't running or the host/port is wrong. Verify the database is accessible.

"Cannot find module" — Run npm run build first. Check the path in .mcp.json is absolute and correct.

License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选