Skip to main content
Glama

query

Execute SQL queries on MySQL databases directly using prepared statements. Manage data retrieval and enhance AI capabilities with structured database access.

Instructions

Execute an SQL query on the connected database.

Input Schema

NameRequiredDescriptionDefault
paramsNoParameters for prepared statements
sqlYesSQL query to execute

Input Schema (JSON Schema)

{ "properties": { "params": { "description": "Parameters for prepared statements", "items": { "type": "string" }, "type": "array" }, "sql": { "description": "SQL query to execute", "type": "string" } }, "required": [ "sql" ], "type": "object" }

Implementation Reference

  • The handler for the 'query' tool. It extracts SQL and parameters from the request, calls executeQuery to run the query (with validation), and returns the results as JSON text content or an error.
    case "query": { try { const sql = request.params.arguments?.sql as string; const params = (request.params.arguments?.params as any[]) || []; // Execute query with validation const rows = await executeQuery(sql, params); return { content: [{ type: "text", text: JSON.stringify(rows, null, 2) }], isError: false }; } catch (error) { return { content: [ { type: "text", text: error instanceof Error ? error.message : "Unknown error occurred" } ], isError: true }; } }
  • The input schema for the 'query' tool, defining the expected parameters: sql (required string) and optional params (array of strings).
    inputSchema: { type: "object", properties: { sql: { type: "string", description: "SQL query to execute" }, params: { type: "array", description: "Parameters for prepared statements", items: { type: "string" } } }, required: ["sql"] }
  • src/index.ts:187-202 (registration)
    The registration of the 'query' tool in the ListTools response, including name, description, and input schema.
    { name: "query", description: "Execute an SQL query on the connected database.", inputSchema: { type: "object", properties: { sql: { type: "string", description: "SQL query to execute" }, params: { type: "array", description: "Parameters for prepared statements", items: { type: "string" } } }, required: ["sql"] } },
  • Helper function executeQuery that performs the actual database query execution, including read-only mode checks using getQueryType and isWriteOperation.
    async function executeQuery(sql: string, params: any[] = []): Promise<any> { const conn = await getConnection(); // Check if in readonly mode and validate query type if (connectionConfig.readonly) { const queryType = getQueryType(sql); if (isWriteOperation(queryType)) { throw new Error( "Server is in read-only mode. Write operations are not allowed." ); } } // Execute the query const [rows] = await conn.query(sql, params); return rows; }

Other Tools

Related Tools

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/sussa3007/mysql-mcp'

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