MSSQL Model Context Protocol Server

1.2.2 · active · verified Tue Apr 21

The `mssql-mcp-server` package provides a Model Context Protocol (MCP) server specifically designed for Microsoft SQL Server, enabling comprehensive database schema exploration and modernization planning. Currently at version 1.2.2, this server facilitates detailed analysis of database structures, including tables, views, stored procedures, triggers, and functions. It leverages the `tedious` library for pure JavaScript connectivity, supporting crucial features like Windows Authentication (NTLM), which necessitates explicit domain credentials through environment variables for secure, non-interactive background processes. Key differentiators include its extensive suite of 29 analytical tools, capabilities for extracting full SQL source code from stored procedures for business logic analysis, and a focus on assisting with modernization efforts for applications like Classic ASP to modern .NET/Angular architectures. While a specific release cadence isn't detailed, the project appears actively maintained with recent updates focused on enhancing core analysis capabilities.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically initialize and start the `mssql-mcp-server` using environment variables for SQL Server connection details, including NTLM authentication. It then shows a hypothetical HTTP POST request to the running server to execute the `test_connection` tool, simulating how an external client or AI agent would interact with the server's API.

import { MssqlMcpServer } from 'mssql-mcp-server';
import * as dotenv from 'dotenv'; // For local development, install with `npm install dotenv`

dotenv.config(); // Load environment variables from .env file

// Configure SQL Server connection details using environment variables
const SQL_SERVER_HOST = process.env.SQL_SERVER_HOST ?? 'localhost';
const SQL_SERVER_PORT = parseInt(process.env.SQL_SERVER_PORT ?? '1433', 10);
const SQL_SERVER_USER = process.env.SQL_SERVER_USER ?? ''; // For NTLM Windows Authentication
const SQL_SERVER_PASSWORD = process.env.SQL_SERVER_PASSWORD ?? ''; // For NTLM
const SQL_SERVER_DOMAIN = process.env.SQL_SERVER_DOMAIN ?? ''; // For NTLM
const SQL_SERVER_DATABASE = process.env.SQL_SERVER_DATABASE ?? 'master';
const SERVER_PORT = parseInt(process.env.MCP_SERVER_PORT ?? '3000', 10); // Port for the MCP server itself

async function startMcpServer() {
  if (!SQL_SERVER_USER || !SQL_SERVER_PASSWORD || !SQL_SERVER_DOMAIN) {
    console.warn("WARNING: NTLM credentials (SQL_SERVER_USER, SQL_SERVER_PASSWORD, SQL_SERVER_DOMAIN) are not fully set. Windows Authentication might fail.");
  }

  try {
    // Instantiate the MCP server with a default connection
    const server = new MssqlMcpServer({
      connections: {
        default: {
          server: SQL_SERVER_HOST,
          port: SQL_SERVER_PORT,
          userName: SQL_SERVER_USER,
          password: SQL_SERVER_PASSWORD,
          domain: SQL_SERVER_DOMAIN,
          options: {
            database: SQL_SERVER_DATABASE,
            encrypt: true, // Recommended for Azure SQL DB and modern instances
            trustServerCertificate: true // Set to false in production if using trusted certificates
          }
        }
      },
      serverPort: SERVER_PORT
    });

    await server.start();
    console.log(`MSSQL MCP Server started successfully on port ${SERVER_PORT}`);

    // --- Simulate an external client (e.g., an AI agent) calling a tool ---
    console.log('\n--- Simulating API call to test_connection tool ---');
    const toolCallResponse = await fetch(`http://localhost:${SERVER_PORT}/api/tool`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        toolName: 'test_connection',
        toolArgs: { connectionName: 'default' }
      })
    });

    const result = await toolCallResponse.json();
    console.log('Result from test_connection:', result);

  } catch (error) {
    console.error('Failed to start MSSQL MCP Server or call tool:', error);
    process.exit(1);
  }
}

startMcpServer();

view raw JSON →