Skip to main content
Glama

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
NameRequiredDescriptionDefault
queryYesThe SQL query to execute. IMPORTANT: When referencing objects, use bracketed, schema-qualified names (e.g., SELECT * FROM [dbo].[Users]).

Implementation Reference

  • 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 };
      }
    };
  • 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"]
      }
    },
  • 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';

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/hendrickcastro/MCPQL'

If you have feedback or need assistance with the MCP directory API, please join our Discord server