This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Read-only MCP (Model Context Protocol) server for Microsoft SQL Server. Exposes SQL Server databases to AI agents via the MCP SDK over stdio transport. All queries are validated to be read-only (SELECT/WITH/SHOW/DESCRIBE/EXPLAIN only).
npm run build # TypeScript compile (tsc) → dist/
npm run dev # Run with tsx (no build needed)
npm start # Run compiled output (dist/index.js)
npm run lint # ESLint on src/**/*.ts
npm test # Jest (no tests exist yet)ESM project — uses "type": "module" with .js extensions in imports. TypeScript targets ES2022 with ESNext modules.
src/index.ts — Contains the SqlServerMCPServer class and main(). Uses dynamic imports for all dependencies. Reads config from environment variables, validates with Zod, creates the MCP Server with stdio transport, and registers ListToolsRequest / CallToolRequest handlers. Connection to SQL Server is deferred until first tool use (not at startup).
All tools extend BaseTool (src/tools/base.ts) which provides:
executeQuery()— validates viaQueryValidator, sanitizes, adds TOP row limit, runs queryexecuteSafeQuery()— wrapsexecuteQuerywith lazy connection and error handling
Each tool implements four abstract methods: getName(), getDescription(), getInputSchema(), execute(). Tools are registered in a Map<string, BaseTool> keyed by tool name.
To add a new tool: create a file in src/tools/, extend BaseTool, export from src/tools/index.ts, and add the class to the toolClasses array in src/index.ts:initializeTools().
- QueryValidator (
src/security.ts) — Whitelist of allowed statement prefixes, blacklist of forbidden keywords (INSERT/UPDATE/DELETE/DROP/EXEC etc.), SQL injection pattern detection, query sanitization, automatic TOP clause injection - ParameterValidator (
src/validation.ts) — Zod schemas for identifiers (schema/table/column/database names), bracket-escaping viaescapeIdentifier(), reserved word rejection - Error hierarchy (
src/errors.ts) —MCPErrorbase with typed subclasses (ConnectionError,SecurityError,ValidationError,QueryError,TimeoutError,PermissionError).ErrorHandlermaps SQL Server error codes to these types with user-facing suggestions.
src/types.ts — ConnectionConfigSchema (Zod), ConnectionConfig, and interfaces for all SQL Server metadata shapes (TableInfo, ColumnInfo, ForeignKeyInfo, ViewInfo, DatabaseInfo, ServerInfo, QueryResult, TableStats).
Required: SQLSERVER_HOST, SQLSERVER_USER, SQLSERVER_PASSWORD
Optional: SQLSERVER_DATABASE, SQLSERVER_PORT (1433), SQLSERVER_ENCRYPT (true), SQLSERVER_TRUST_CERT (true), SQLSERVER_CONNECTION_TIMEOUT (30000), SQLSERVER_REQUEST_TIMEOUT (60000), SQLSERVER_MAX_ROWS (1000)
- Strict TypeScript (
noUnusedLocals,noUnusedParameters,exactOptionalPropertyTypes,noImplicitReturns) - Tool names use snake_case (
list_tables,execute_query) - SQL queries in tools use
INFORMATION_SCHEMAviews for metadata - Identifiers are bracket-escaped (
[name]) before interpolation into SQL