query_mssql
Execute read-only MSSQL queries to retrieve database information through your LINE bot. Run SELECT statements with parameter support and get structured results with column data and row counts.
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 |
|---|---|---|---|
| limit | No | Maximum rows to return (client-side slice). | |
| maxChars | No | Max characters in serialized preview. | |
| params | No | Key-value parameters for @name placeholders in SQL. | |
| sql | Yes | Read-only SQL beginning with SELECT or WITH. Parameters as @name. |
Implementation Reference
- src/tools/queryMssql.ts:46-63 (handler)Executes the read-only SQL query using queryReadOnly helper, applies client-side row slicing up to limit and truncates JSON preview if over maxChars, returns structured response with columns, rowCount, and sliced rows, or 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 input schemas for the tool: sql (required SELECT/WITH string with @param placeholders), optional params (object), limit (1-500 rows default 100), maxChars (1000-200k chars for preview 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/tools/queryMssql.ts:37-64 (registration)Registers the 'query_mssql' tool on the MCP server using server.tool(), providing name, description, Zod schema for inputs, and the async handler function.server.tool( "query_mssql", "Run a read-only MSSQL query (SELECT/WITH). Returns columns, rowCount, and sliced rows for preview.", { sql: sqlSchema, params: paramsSchema, limit: limitSchema, maxChars: maxCharsSchema, }, 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/index.ts:76-76 (registration)Top-level instantiation and registration of QueryMssql class on the main MCP server instance in the entrypoint.new QueryMssql().register(server);
- src/common/db/mssql.ts:62-80 (helper)Core helper function that validates SQL is read-only SELECT/WITH, connects to MSSQL connection pool, binds parameters safely, executes query, extracts rows and columns.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 }; }