Skip to main content
Glama

run_sql_query

Execute read-only SQL SELECT queries on a MySQL database using the Model Context Protocol server for data retrieval and analysis.

Instructions

Executes a read-only SQL query (SELECT statements only) against the MySQL database.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesThe SQL SELECT query to execute.

Implementation Reference

  • Implements the run_sql_query tool: validates input, checks for SELECT query, executes via MySQL pool, returns JSON results or error.
    private async handleReadQuery(request: any, transactionId: string) { if (!isValidSqlQueryArgs(request.params.arguments)) { throw new McpError( ErrorCode.InvalidParams, 'Invalid SQL query arguments.' ); } const query = request.params.arguments.query; if (!isReadOnlyQuery(query)) { throw new McpError( ErrorCode.InvalidParams, 'Only SELECT queries are allowed with run_sql_query tool.' ); } console.error(`[${transactionId}] Executing SELECT query: ${query}`); try { const [rows] = await this.pool.query(query); console.error(`[${transactionId}] Query executed successfully`); return { content: [ { type: 'text', text: JSON.stringify(rows, null, 2), }, ], }; } catch (error) { console.error(`[${transactionId}] Query error:`, error); if (error instanceof Error) { return { content: [ { type: 'text', text: `MySQL error: ${error.message}`, }, ], isError: true, }; } throw error; } }
  • src/index.ts:94-107 (registration)
    Registers the run_sql_query tool in the list of available tools, including name, description, and input schema.
    { name: 'run_sql_query', description: 'Executes a read-only SQL query (SELECT statements only) against the MySQL database.', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'The SQL SELECT query to execute.', }, }, required: ['query'], }, },
  • src/index.ts:187-188 (registration)
    Dispatches calls to the run_sql_query tool to the handleReadQuery method.
    case 'run_sql_query': return this.handleReadQuery(request, transactionId);
  • Defines the input schema for the run_sql_query tool: object with required 'query' string.
    inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'The SQL SELECT query to execute.', }, }, required: ['query'], },
  • Helper function to check if a query is a read-only SELECT query.
    const isReadOnlyQuery = (query: string): boolean => query.trim().toLowerCase().startsWith('select');

Other Tools

Related Tools

  • @alittleyellowkevin/Mysql-MCP
  • @alittleyellowkevin/Mysql-MCP
  • @devakone/mysql-query-mcp-server
  • @Malove86/mcp-mysql-server
  • @Atomzzm/mcp-mysql-server
  • @yaoxiaolinglong/mcp-mongodb-mysql-server

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

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