MSSQL MCP Server

MSSQL MCP Server

Enables read-only exploration and querying of Microsoft SQL Server databases through a standardized interface.

Category
访问服务器

README

MSSQL MCP Server (Read-Only)

A Model Context Protocol (MCP) server that provides read-only access to Microsoft SQL Server databases. This server enables safe database exploration and querying through a standardized interface.

Features

  • 🔒 Read-only operations - All queries are validated to prevent data modification
  • 🔐 Secure by default - Credentials read only from environment variables
  • 📊 Schema exploration - Discover tables, views, procedures, and functions
  • 🔍 Query execution - Execute SELECT queries with automatic safety limits
  • 📝 Stored procedure inspection - View procedure definitions and parameters
  • 🛡️ Input validation - Strict validation of identifiers and query patterns

Installation

npm install

Configuration

Create a .env file with your database credentials:

DB_SERVER=your-server-address
DB_DATABASE=your-database-name
DB_USER=your-username
DB_PASSWORD=your-password
DB_PORT=1433
DB_ENCRYPT=true
DB_TRUST_SERVER_CERTIFICATE=false
DB_CONNECTION_TIMEOUT=30000
DB_REQUEST_TIMEOUT=30000

Usage

Run the server:

npm start
# or
node server.ts

The server communicates via stdio and follows the MCP protocol.

Available Tools

1. connect_database

Purpose: Establish a connection to a Microsoft SQL Server database.

Description: Connects to the database using credentials from environment variables only. This ensures security by preventing credential injection through user input.

Parameters: None (uses environment variables)

Returns: Connection status message

Example:

{
  "content": [{
    "type": "text",
    "text": "✅ Connected to SQL Server: server-name (DB: database-name)"
  }]
}

2. execute_query

Purpose: Execute read-only SQL queries (SELECT statements or CTEs).

Description:

  • Only allows SELECT and WITH (CTE) queries
  • Blocks DDL, DML, and execution keywords
  • Automatically applies TOP(limit) to plain SELECT queries if not present
  • Supports parameterized queries for safe user input
  • Blocks comments and semicolons to prevent injection

Parameters:

  • query (string, required): SQL query starting with SELECT or WITH
  • parameters (object, optional): Key-value pairs for parameterized queries
  • limit (number, optional, default: 200): Maximum rows returned (1-10000)

Returns: Query results with metadata (execution time, row count, etc.)

Example:

{
  "query": "SELECT * FROM clients WHERE id = @id",
  "parameters": { "id": 123 },
  "limit": 100
}

3. get_schema

Purpose: Retrieve database schema information (tables, views, procedures, functions).

Description: Lists database objects filtered by type and optionally by schema name. All identifiers are validated to prevent SQL injection.

Parameters:

  • objectType (enum, optional, default: "tables"): One of: "tables", "views", "procedures", "functions", "all"
  • schemaName (string, optional): Filter by schema name (alphanumeric and underscore only)

Returns: Array of schema objects with their metadata

Example:

{
  "objectType": "all",
  "schemaName": "dbo"
}

4. describe_table

Purpose: Get detailed structure information for a specific table.

Description: Returns column information including data types, nullability, defaults, and ordinal positions. Schema and table names are validated.

Parameters:

  • tableName (string, required): Name of the table
  • schemaName (string, optional, default: "dbo"): Schema name

Returns: Array of column definitions

Example:

{
  "tableName": "clients",
  "schemaName": "dbo"
}

5. connection_status

Purpose: Check the current database connection status and configuration.

Description: Returns detailed connection information including server, database, port, connection pool stats, and security mode.

Parameters: None

Returns: Connection status object with:

  • connected: Boolean indicating connection state
  • server: Server address
  • database: Database name
  • port: Port number
  • connectionTime: ISO timestamp of connection
  • security: Security mode information
  • poolInfo: Connection pool statistics

6. disconnect_database

Purpose: Close the current database connection.

Description: Safely closes the connection pool and cleans up resources.

Parameters: None

Returns: Success message


7. get_table_data

Purpose: Read rows from a table with optional filtering, pagination, and sorting.

Description:

  • Validates table and schema names
  • Supports WHERE clauses with parameterized values
  • Supports ORDER BY with validation
  • Implements OFFSET/FETCH for pagination
  • Maximum 10,000 rows per request

Parameters:

  • tableName (string, required): Table name (alphanumeric and underscore only)
  • schemaName (string, optional, default: "dbo"): Schema name
  • limit (number, optional, default: 100): Maximum rows (1-10000)
  • offset (number, optional, default: 0): Rows to skip
  • whereClause (string, optional): WHERE clause without the WHERE keyword
  • orderBy (string, optional): ORDER BY clause without the ORDER BY keyword
  • parameters (object, optional): Parameters for WHERE clause

Returns: Table data with metadata (row count, execution time, etc.)

Example:

{
  "tableName": "clients",
  "schemaName": "dbo",
  "limit": 50,
  "offset": 0,
  "whereClause": "age > @minAge",
  "orderBy": "name ASC",
  "parameters": { "minAge": 18 }
}

8. list_procedures

Purpose: List stored procedures in a specific schema.

Description: Returns all stored procedures with their creation and modification dates. Schema name is validated.

Parameters:

  • schemaName (string, optional, default: "dbo"): Schema name to filter

Returns: Array of procedure information

Example:

{
  "schemaName": "dbo"
}

9. describe_procedure

Purpose: Get detailed parameter information for a stored procedure.

Description: Returns procedure parameters including data types, lengths, precision, scale, output flags, and default values. All identifiers are validated.

Parameters:

  • procedureName (string, required): Name of the procedure
  • schemaName (string, optional, default: "dbo"): Schema name

Returns: Array of parameter definitions

Example:

{
  "procedureName": "GetClientInfo",
  "schemaName": "dbo"
}

10. get_procedure_definition

Purpose: Retrieve the T-SQL source code of a stored procedure.

Description: Returns the full procedure definition. Requires VIEW DEFINITION permission on the database. All identifiers are validated.

Parameters:

  • procedureName (string, required): Name of the procedure
  • schemaName (string, optional, default: "dbo"): Schema name

Returns: Procedure definition text or error message if not found/no permission

Example:

{
  "procedureName": "GetClientInfo",
  "schemaName": "dbo"
}

11. list_databases

Purpose: List all databases on the connected SQL Server instance.

Description: Returns database information including IDs, creation dates, collation, state, access mode, read-only status, and recovery model. Read-only operation.

Parameters: None

Returns: Array of database information


Security Features

Query Validation

  • Only SELECT and WITH (CTE) queries allowed
  • Blocks INSERT, UPDATE, DELETE, DROP, ALTER, CREATE, TRUNCATE, EXEC, etc.
  • Blocks comments (--, /* */)
  • Blocks semicolons to prevent multi-statement execution
  • Automatic TOP limit for plain SELECT queries

Input Validation

  • Table names: Only alphanumeric and underscore
  • Schema names: Only alphanumeric and underscore
  • Parameterized queries: Use parameters for user input
  • ORDER BY validation: Only column names and ASC/DESC

Credential Security

  • Credentials read only from environment variables
  • No credential input through tool parameters
  • Recommended: Use read-only database user with db_datareader role

Resources

connection-info

A resource that provides current connection information in JSON format.

URI: mssql://connection/info

Content: JSON object with connection status and configuration

Error Handling

All tools return structured error responses with descriptive messages. Common errors include:

  • Connection not established
  • Invalid query syntax
  • Permission denied
  • Invalid identifier format
  • Query validation failures

Best Practices

  1. Always use parameterized queries when including user input
  2. Use appropriate limits to avoid large result sets
  3. Check connection status before executing queries
  4. Use schema names explicitly to avoid ambiguity
  5. Disconnect when done to free resources

Limitations

  • Read-only operations only
  • Maximum 10,000 rows per query
  • Single-statement queries only (no semicolons)
  • No comments allowed in queries
  • Requires VIEW DEFINITION permission for procedure definitions

Version

Current version: 1.0.0

Contributing

This is a read-only MCP server designed for safe database exploration. Contributions should maintain the security-first approach and read-only nature of the server.

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选