Skip to main content
Glama
SQL Server MCP

SQL Server MCP

by bpamiri

pymssql-mcp

An MCP (Model Context Protocol) server for Microsoft SQL Server databases. Enables AI assistants like Claude to interact with SQL Server through a standardized protocol.

PyPI version Python 3.10+ License: Apache-2.0

Features

  • Natural Language Queries: Ask Claude about your data in plain English

  • Schema Discovery: Explore tables, views, columns, and relationships

  • CRUD Operations: Read, insert, update, and delete rows safely

  • Stored Procedures: Execute stored procedures with parameters

  • Multi-Database Support: List and switch between databases

  • Data Export: Export query results to JSON or CSV files

  • Transaction Support: Begin, commit, and rollback transactions

  • Knowledge Persistence: Claude remembers what it learns about your database

  • Safety Controls: Read-only mode, command blocking, row limits, schema restrictions

  • Connection Watchdog: Automatic recovery from hung connections

  • OAuth Integration: Deploy as a Claude.ai Custom Connector with SSO

Related MCP server: MSSQL MCP Server

Documentation

Guide

Description

What is MCP?

Understanding MCP and pymssql-mcp

Installation

Complete installation guide

Quickstart

Get running in 10 minutes

Configuration

All configuration options

Tools Reference

Detailed tool documentation

Usage Examples

Common usage patterns

OAuth Setup

Claude.ai integration with SSO

Quick Start

1. Install

pip install pymssql-mcp

2. Configure Claude Desktop

Edit your Claude Desktop config file:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json Windows: %APPDATA%\Claude\claude_desktop_config.json

{
  "mcpServers": {
    "mssql": {
      "command": "pymssql-mcp",
      "env": {
        "MSSQL_HOST": "your-server.example.com",
        "MSSQL_USER": "your-username",
        "MSSQL_PASSWORD": "your-password",
        "MSSQL_DATABASE": "your-database",
        "MSSQL_READ_ONLY": "true"
      }
    }
  }
}

3. Restart Claude Desktop

Quit and reopen Claude Desktop. You'll see a hammer icon indicating tools are available.

4. Start Chatting

Ask Claude about your database:

"What tables are available?"

"Describe the Customers table"

"Show me the top 10 orders by total amount"

"How many customers do we have in each state?"

Available Tools

Connection & Database

Tool

Description

connect

Connect to the database

disconnect

Close all connections

list_databases

List available databases

switch_database

Switch database context

Queries & Schema

Tool

Description

execute_query

Run a SELECT query

validate_query

Check if a query is safe

list_tables

List tables and views

describe_table

Get column information

CRUD Operations

Tool

Description

read_rows

Read rows by ID or filter

insert_row

Insert a new row

update_row

Update an existing row

delete_row

Delete a row

Stored Procedures

Tool

Description

list_stored_procs

List available procedures

describe_stored_proc

Get procedure parameters

call_stored_proc

Execute a procedure

Export & Transactions

Tool

Description

export_to_json

Export results to JSON

export_to_csv

Export results to CSV

begin_transaction

Start a transaction

commit_transaction

Commit changes

rollback_transaction

Rollback changes

Knowledge Persistence

Tool

Description

save_knowledge

Save learned information

get_all_knowledge

Retrieve all knowledge

search_knowledge

Search saved knowledge

Configuration

Required Variables

Variable

Description

MSSQL_HOST

SQL Server hostname

MSSQL_USER

Database username

MSSQL_PASSWORD

Database password

MSSQL_DATABASE

Database name

Safety Settings

Variable

Default

Description

MSSQL_READ_ONLY

false

Block all write operations

MSSQL_MAX_ROWS

1000

Maximum rows per query

MSSQL_BLOCKED_COMMANDS

DROP,TRUNCATE,...

Commands to block

MSSQL_ALLOWED_SCHEMAS

(all)

Restrict to specific schemas

MSSQL_BLOCKED_DATABASES

(none)

Hide specific databases

See Configuration Reference for all options.

Deployment Modes

Local (Default)

Run as a local process with Claude Desktop:

pymssql-mcp

HTTP/SSE Server

Run as a shared HTTP server for multiple users:

pymssql-mcp --http --host 0.0.0.0 --port 8080

Streamable HTTP (Claude.ai Integration)

Run with OAuth authentication for Claude.ai:

pymssql-mcp --streamable-http --host 0.0.0.0 --port 8080

See OAuth Setup for complete integration instructions.

Development

# Clone repository
git clone https://github.com/bpamiri/pymssql-mcp.git
cd pymssql-mcp

# Install with dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Lint and format
ruff check .
ruff format .

# Type check
mypy src/

Security

pymssql-mcp includes multiple safety features:

  • Read-only mode: Prevent all write operations

  • Command blocking: Block dangerous SQL commands (DROP, TRUNCATE, etc.)

  • Schema restrictions: Limit access to specific schemas

  • Database blocklist: Hide sensitive databases

  • Row limits: Cap query results to prevent memory issues

  • Query validation: Analyze queries before execution

  • Parameterized queries: Prevent SQL injection

See SECURITY.md for security policy and best practices.

Contributing

Contributions are welcome! See CONTRIBUTING.md for guidelines.

License

Apache-2.0. See LICENSE for details.

Available Tools

18 tools
call_stored_procB

Execute a stored procedure.

Args:
    procedure: Procedure name, optionally with schema (e.g., 'dbo.sp_GetUser' or 'sp_GetUser')
    params: Input parameter values as dictionary (parameter names without @)

Returns:
    Dictionary with:
    - procedure: Full procedure name
    - result_sets: List of result sets (each is a list of row dictionaries)
    - status: 'success' or error

Inputs: procedure (required), params. Returns: object

connectA

Establish connection to the SQL Server database.

Uses configuration from environment variables:
- MSSQL_HOST: Server hostname or IP
- MSSQL_USER: Username
- MSSQL_PASSWORD: Password
- MSSQL_DATABASE: Database name
- MSSQL_PORT: Port (default: 1433)

Returns:
    Connection status and details including host, database, and timestamp.

Inputs: no named parameters. Returns: object

delete_rowA

Delete a row by primary key.

Args:
    table: Table name (can include schema: 'dbo.Users' or 'Users')
    id: Primary key value of the row to delete

Returns:
    Dictionary with:
    - status: 'deleted' or error
    - table: Full table name
    - id: The deleted row's ID
    - rows_affected: Number of rows deleted (should be 1)

Inputs: table (required), id (required). Returns: object

describe_stored_procB

Get parameter information for a stored procedure.

Args:
    procedure: Procedure name, optionally with schema (e.g., 'dbo.sp_GetUser' or 'sp_GetUser')

Returns:
    Dictionary with:
    - procedure: Full procedure name (schema.name)
    - parameters: List of parameter info (name, type, direction, etc.)

Inputs: procedure (required). Returns: object

describe_tableA

Get detailed column information for a table.

Retrieves column definitions, primary keys, foreign keys, and indexes.

Args:
    table: Table name, optionally with schema (e.g., 'dbo.Users' or 'Users').
           Defaults to 'dbo' schema if not specified.

Returns:
    Dictionary with:
    - table: Full table name (schema.table)
    - columns: List of column info (name, type, nullable, etc.)
    - primary_key: List of primary key column names
    - foreign_keys: List of foreign key relationships
    - indexes: List of index info

Inputs: table (required). Returns: object

disconnectA

Close all connections to the SQL Server database.

Returns:
    Disconnection status and count of closed connections.

Inputs: no named parameters. Returns: object

execute_queryA

Execute a read-only SQL query and return results.

Only SELECT statements are allowed. The query will have a row limit applied
automatically if not specified.

Args:
    query: SQL SELECT statement to execute
    max_rows: Maximum rows to return (overrides default, capped by MSSQL_MAX_ROWS)

Returns:
    Dictionary with:
    - query: The original query
    - executed_query: The query that was actually executed (may include TOP)
    - columns: List of column names
    - rows: List of row dictionaries
    - row_count: Number of rows returned
    - max_rows: The effective row limit applied

Inputs: query (required), max_rows. Returns: object

export_to_csvA

Export query results to a CSV file.

Args:
    query: SQL SELECT query to execute
    filename: Output filename (relative or absolute path)
    delimiter: Field delimiter (default: comma)

Returns:
    Dictionary with:
    - status: 'success' or error
    - path: Absolute path to created file
    - row_count: Number of rows exported
    - file_size: Size of created file in bytes

Inputs: query (required), filename (required), delimiter. Returns: object

export_to_jsonA

Export query results to a JSON file.

Args:
    query: SQL SELECT query to execute
    filename: Output filename (relative or absolute path)

Returns:
    Dictionary with:
    - status: 'success' or error
    - path: Absolute path to created file
    - row_count: Number of rows exported
    - file_size: Size of created file in bytes

Inputs: query (required), filename (required). Returns: object

insert_rowB

Insert a new row into a table.

Args:
    table: Table name (can include schema: 'dbo.Users' or 'Users')
    data: Dictionary of column names and values to insert

Returns:
    Dictionary with:
    - status: 'success' or error
    - table: Full table name
    - inserted: The inserted row (including generated identity columns)

Inputs: table (required), data (required). Returns: object

list_connectionsB

List all active database connections.

Returns:
    List of active connections with their details (name, host, database,
    connection time, and active status).

Inputs: no named parameters. Returns: object

list_databasesA

List all available databases on the SQL Server.

Queries sys.databases to discover accessible databases. System databases
(master, tempdb, model, msdb) are excluded by default. Databases in the
blocklist (MSSQL_BLOCKED_DATABASES) are always excluded.

Args:
    include_system: If True, include system databases in the list

Returns:
    Dictionary with:
    - databases: List of available database names
    - current_database: The currently active database
    - count: Number of databases returned
    - blocked_count: Number of databases hidden due to blocklist

Inputs: include_system. Returns: object

list_stored_procsA

List available stored procedures in the database.

Args:
    schema: Filter by schema name (e.g., 'dbo')
    pattern: Filter by name pattern using SQL LIKE syntax (e.g., 'sp_%', '%User%')

Returns:
    Dictionary with:
    - procedures: List of procedure info (schema, name, created, modified)
    - count: Number of procedures found

Inputs: schema, pattern. Returns: object

list_tablesA

List all tables and views in the database.

Args:
    schema: Filter by schema name (e.g., 'dbo'). If not specified, returns all schemas.
    include_views: Include views in results (default: True)
    pattern: Filter by name pattern using SQL LIKE syntax (e.g., 'Cust%', '%Order%')

Returns:
    Dictionary with:
    - tables: List of table/view info (schema, name, type)
    - count: Number of results

Inputs: schema, include_views, pattern. Returns: object

read_rowsA

Read rows from a table by primary key or filter.

Provide one of: id (single row), ids (multiple rows), or filter (WHERE clause).

Args:
    table: Table name (can include schema: 'dbo.Users' or 'Users')
    id: Single primary key value (for composite keys, use filter)
    ids: List of primary key values
    filter: WHERE clause without 'WHERE' keyword (e.g., "status = 'active'")
    columns: List of columns to return (default: all columns)
    max_rows: Maximum rows to return

Returns:
    Dictionary with:
    - table: Full table name
    - rows: List of row dictionaries
    - count: Number of rows returned

Inputs: table (required), id, ids, filter, +2 more. Returns: object

switch_databaseA

Switch the active database context.

Changes the current database using the USE statement. The database must
exist, be online, and not be in the blocklist (MSSQL_BLOCKED_DATABASES).

Args:
    database_name: Name of the database to switch to

Returns:
    Dictionary with:
    - status: "switched" on success, "error" on failure
    - database: The new active database name
    - previous_database: The previously active database
    - error: Error message if switch failed

Inputs: database_name (required). Returns: object

update_rowA

Update an existing row by primary key.

Args:
    table: Table name (can include schema: 'dbo.Users' or 'Users')
    id: Primary key value of the row to update
    data: Dictionary of column names and new values

Returns:
    Dictionary with:
    - status: 'success' or error
    - table: Full table name
    - updated: The updated row

Inputs: table (required), id (required), data (required). Returns: object

validate_queryA

Check if a query is safe to execute without running it.

Validates the query against:
- Statement type (SELECT, INSERT, UPDATE, DELETE, DDL, EXEC)
- Blocked commands list
- Read-only mode compliance
- Potential issues (missing WHERE clause, unbounded SELECT)

Args:
    query: SQL statement to validate

Returns:
    Dictionary with:
    - query: The original query
    - valid: Whether the query is valid
    - statement_type: Type of SQL statement
    - warnings: List of warning messages
    - suggestions: List of suggested improvements
    - error: Error message if invalid

Inputs: query (required). Returns: object

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    A4.2/5.0

    Scored across 18 tools

    Disambiguation5/5

    Every tool has a clearly distinct purpose with no ambiguity. For example, call_stored_proc is for executing procedures while describe_stored_proc is for inspecting them, and read_rows is for reading data while execute_query is for running SELECT queries. The tools cover different aspects of SQL Server interaction without overlap.

    Naming Consistency5/5

    All tools follow a consistent verb_noun naming pattern throughout. Examples include describe_table, list_tables, insert_row, update_row, and export_to_csv. The naming is predictable and follows the same convention across all 18 tools.

    Tool Count4/5

    With 18 tools, the count is slightly high but reasonable for a comprehensive SQL Server interface. The tools cover connection management, CRUD operations, stored procedures, metadata inspection, and data export, which justifies the number. It's well-scoped but could potentially be streamlined.

    Completeness5/5

    The tool surface provides complete coverage for SQL Server interaction. It includes connection management (connect, disconnect, list_connections), database operations (list_databases, switch_database), table operations (full CRUD with insert_row, read_rows, update_row, delete_row), stored procedure handling, metadata inspection, query execution, validation, and data export. No obvious gaps exist for the domain.

    Maintenance

    ActivityInactive
    ResponsivenessOutdated data

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      D
      maintenance
      Enables AI assistants to securely interact with Microsoft SQL Server databases to query data, inspect schemas, and retrieve metadata with read-only operations by default and optional write capabilities.
      1
      MIT
    • A
      license
      Not graded
      quality
      D
      maintenance
      Enables AI assistants to interact with Microsoft SQL Server databases through a standardized interface. Supports executing SQL queries, browsing database schemas, and viewing table data with flexible authentication options for both local and Azure SQL databases.
      5
      MIT
    • A
      license
      A
      quality
      D
      maintenance
      Enables AI models to interact with MS SQL Server databases through a standardized interface. Supports executing SQL queries with parameters, listing tables, and describing table schemas.
      3
      MIT
    • A
      license
      Not graded
      quality
      B
      maintenance
      Enables AI assistants to interact with Microsoft SQL Server databases via introspection and query tools. Supports single or multiple databases with read-only mode by default and an optional write capability.
      442 npm
      5
      MIT