mcp_execute_query
Execute raw SQL queries to retrieve data from SQL Server databases using schema-qualified object references for reliable results.
Instructions
Execute a raw SQL query and return the results
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | The SQL query to execute. IMPORTANT: When referencing objects, use bracketed, schema-qualified names (e.g., SELECT * FROM [dbo].[Users]). |
Implementation Reference
- src/tools/dataOperations.ts:71-90 (handler)The core handler function that performs permission validation using validateQueryPermission, executes the provided SQL query against the database pool, and returns the recordset or an error.export const mcp_execute_query = async (args: { query: string }): Promise<ToolResult<any[]>> => { const { query } = args; console.log('Executing mcp_execute_query with query:', query); // Security validation for SQL query execution const permission = validateQueryPermission(query); if (!permission.allowed) { return { success: false, error: permission.message || 'Query execution not allowed' }; } try { const pool = getPool(); const result = await pool.request().query(query); return { success: true, data: result.recordset }; } catch (error: any) { // Only log the error message, not the full stack trace for cleaner output console.error(`Error in mcp_execute_query: ${error.message}`); return { success: false, error: error.message }; } };
- src/tools.ts:102-116 (schema)Tool schema definition in the MCP_MSQL_TOOLS array, specifying the name, description, and inputSchema requiring a 'query' string parameter.{ name: "mcp_execute_query", description: "Execute a raw SQL query and return the results", inputSchema: { type: "object", properties: { query: { type: "string", description: "The SQL query to execute. IMPORTANT: When referencing objects, use bracketed, schema-qualified names (e.g., SELECT * FROM [dbo].[Users])." } }, required: ["query"] } },
- src/tools/index.ts:30-33 (registration)Re-exports the mcp_execute_query handler from dataOperations.js as part of the tools index module.export { mcp_execute_procedure, // Execute stored procedure mcp_execute_query // Execute SQL query } from './dataOperations.js';