query_mssql
Execute read-only MSSQL queries to retrieve data from databases. Returns query results with column information, row counts, and preview data for analysis.
Instructions
Run a read-only MSSQL query (SELECT/WITH). Returns columns, rowCount, and sliced rows for preview.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sql | Yes | Read-only SQL beginning with SELECT or WITH. Parameters as @name. | |
| params | No | Key-value parameters for @name placeholders in SQL. | |
| limit | No | Maximum rows to return (client-side slice). | |
| maxChars | No | Max characters in serialized preview. |
Implementation Reference
- src/tools/queryMssql.ts:46-63 (handler)The handler function for the 'query_mssql' tool. It executes a read-only SQL query using the queryReadOnly helper, slices the rows to the specified limit, truncates large JSON previews, and returns a structured success response with columns, row count, and preview rows, or an error response.async ({ sql, params, limit, maxChars }) => { try { const { rows, columns } = await queryReadOnly(sql, params); const sliced = rows.slice(0, limit); const payload = { columns, rowCount: rows.length, rows: sliced }; let text = JSON.stringify(payload); if (text.length > maxChars) { text = text.slice(0, maxChars) + "... (truncated)"; } return createSuccessResponse({ columns, rowCount: rows.length, rows: sliced, }); } catch (e: any) { return createErrorResponse(`MSSQL query failed: ${e?.message || e}`); } },
- src/tools/queryMssql.ts:12-36 (schema)Zod schemas defining the input parameters for the query_mssql tool: sql (string), params (optional record), limit (number 1-500 default 100), maxChars (number 1000-200000 default 12000).const sqlSchema = z .string() .min(1) .describe( "Read-only SQL beginning with SELECT or WITH. Parameters as @name.", ); const paramsSchema = z .record(z.any()) .optional() .describe("Key-value parameters for @name placeholders in SQL."); const limitSchema = z .number() .int() .min(1) .max(500) .default(100) .describe("Maximum rows to return (client-side slice)."); const maxCharsSchema = z .number() .int() .min(1000) .max(200000) .default(12000) .describe("Max characters in serialized preview.");
- src/index.ts:76-76 (registration)Top-level registration of the QueryMssql tool instance on the MCP server in the main index file.new QueryMssql().register(server);
- src/common/db/mssql.ts:62-80 (helper)Core helper function that validates the SQL is read-only SELECT/WITH, connects to the MSSQL connection pool, binds parameters, executes the query, and returns rows and column names.export async function queryReadOnly( sql: string, params?: QueryParams, ): Promise<{ rows: any[]; columns: string[] }> { if (!isReadOnlySelect(sql)) { throw new Error("Only read-only SELECT/WITH queries are allowed"); } const pool = await getPool(); const request = pool.request(); if (params && typeof params === "object") { for (const [k, v] of Object.entries(params)) { request.input(k, mapJsToSqlType(v), v); } } const result = await request.query<any>(sql); const rows: any[] = (result.recordset as any[]) || []; const columns: string[] = rows[0] ? Object.keys(rows[0]) : []; return { rows, columns }; }