query
Execute SQL statements directly on Microsoft SQL Server databases through a Model Context Protocol server designed for AI assistant integration.
Instructions
Query the database with a SQL statement
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | The SQL query to execute |
Implementation Reference
- src/tools/query.ts:20-24 (handler)The handler function for the "query" MCP tool. It executes the provided SQL query using the database helper, handles empty results, and formats the output using toResult.async query({ query }: { query: string }) { const results = await database.query(query); if (!results.length) return this.toResult("No results found for the query."); return this.toResult(JSON.stringify(results)); }
- src/tools/query.ts:13-13 (schema)Input schema definition for the "query" tool using Zod, specifying a string parameter for the SQL query.{ query: z.string().describe("The SQL query to execute") },
- src/tools/query.ts:10-15 (registration)Registration of the "query" tool on the MCP server, including name, description, input schema, and binding to the handler method.server.tool( "query", "Query the database with a SQL statement", { query: z.string().describe("The SQL query to execute") }, tools.query.bind(tools) );
- src/utils/database.ts:57-70 (helper)Database query helper function called by the tool handler to execute SQL queries against the MSSQL database.export async function query(query: string, interpolations: any = null): Promise<mssql.IRecordSet<any>> { let connection = null; try { connection = await connect(); const request = connection.request(); if (interpolations) { Object.entries(interpolations).forEach(([key, value]) => request.input(key, value)); } const { recordset } = await request.query(query); return recordset; } finally { await connection?.close(); } }
- src/tools/base.ts:9-11 (helper)Helper method from BaseTools used by the handler to format tool results as MCP CallToolResult.protected toResult(content: string): CallToolResult { return { content: [{ type: "text", text: content }] }; }