Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Server
✓ import { Server } from '@modelcontextprotocol/sdk/server/index.js'
✗ import { Server } from '@modelcontextprotocol/sdk'
The SDK exports Server from a specific subpath. Direct import from root may not work.
mssql
✓ import sql from 'mssql'
✗ const sql = require('mssql')
Package is ESM-only. CommonJS require will fail unless using dynamic import.
dotenv
✓ import 'dotenv/config'
✗ import dotenv from 'dotenv'; dotenv.config()
For ESM, use import 'dotenv/config' to load .env automatically. The object export is also available.
Sets up an MCP server for MSSQL, connecting to the database and registering tools for executing queries, listing tables, and describing schemas.
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import sql from 'mssql';
import 'dotenv/config';
async function main() {
const server = new Server({
name: 'mcp-mssql-server',
version: '1.0.2'
}, {
capabilities: { tools: {} }
});
const pool = await sql.connect({
server: process.env.DB_SERVER,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE_NAME,
options: { encrypt: true, trustServerCertificate: true }
});
server.setRequestHandler('tools/list', async () => ({
tools: [
{
name: 'execute_sql_query',
description: 'Execute a SQL query',
inputSchema: {
type: 'object',
properties: {
query: { type: 'string' },
parameters: {
type: 'array',
items: {
type: 'object',
properties: {
name: { type: 'string' },
type: { type: 'string' },
value: {}
}
}
}
},
required: ['query']
}
},
{
name: 'list_tables',
description: 'List all tables',
inputSchema: { type: 'object', properties: {} }
},
{
name: 'describe_table',
description: 'Describe a table',
inputSchema: {
type: 'object',
properties: {
table_name: { type: 'string' }
},
required: ['table_name']
}
}
]
}));
server.setRequestHandler('tools/call', async (request) => {
const { name, arguments: args } = request.params;
if (name === 'execute_sql_query') {
const result = await pool.request().query(args.query);
return { content: [{ type: 'text', text: JSON.stringify(result.recordset) }] };
} else if (name === 'list_tables') {
const result = await pool.request().query("SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'");
return { content: [{ type: 'text', text: JSON.stringify(result.recordset) }] };
} else if (name === 'describe_table') {
const result = await pool.request()
.input('table_name', sql.NVarChar, args.table_name)
.query("SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @table_name");
return { content: [{ type: 'text', text: JSON.stringify(result.recordset) }] };
}
throw new Error(`Unknown tool: ${name}`);
});
const transport = new StdioServerTransport();
await server.connect(transport);
}
main().catch(console.error);
Errors
Common errors & fixes
TypeError: Cannot find module '@modelcontextprotocol/sdk'
Missing dependency or incorrect import path when using MCP SDK.
fixRun 'npm install @modelcontextprotocol/sdk' and ensure import path is correct: '@modelcontextprotocol/sdk/server/index.js'
ConnectionError: Failed to connect to SERVER:1433 - Could not connect (sequence)
MSSQL server is not reachable or credentials are wrong.
fixCheck DB_SERVER, DB_USER, DB_PASSWORD, and ensure firewall allows port 1433. Also check options.encrypt and options.trustServerCertificate.
ReferenceError: require is not defined
The package is ESM-only; using CommonJS require() in an ESM context.
fixUse dynamic import() or switch to import syntax in an ESM file.
RequestError: Incorrect syntax near '@userId'
SQL parameter name mismatch or missing parameter declaration.
fixEnsure parameters in the request match exactly with SQL variable names. Use mssql's input() method correctly.
Audit
Dependencies
@modelcontextprotocol/sdkrequiredRequired to implement the MCP protocol and server transport.
mssqlrequiredMicrosoft SQL Server client for Node.js. Used to connect and query the database.
expressoptionalUsed for HTTP transport mode to serve the MCP server over HTTP.
dotenvrequiredLoads environment variables from .env file for configuration.
winstonrequiredLogging framework used for structured logging to files and console.