Skip to main content
Glama
enemyrr

MCP-MySQL Server

by enemyrr

query

Run SELECT queries on MySQL databases using SQL commands and optional parameters, enabling efficient data retrieval through the MCP-MySQL Server interface.

Instructions

Execute a SELECT query

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
paramsNoAn optional array of parameters (as strings) to bind to the SQL query placeholders (e.g., ?).
sqlYesThe SQL SELECT query string to execute.

Implementation Reference

  • The handler function for the 'query' tool. Validates that the SQL is a SELECT query, executes it using executeQuery helper, and returns the results as a JSON-formatted text content.
    private async handleQuery(args: QueryArgs): Promise<QueryResult> { this.validateSqlInput(args.sql, ['SELECT']); const rows = await this.executeQuery(args.sql, args.params || []); return { content: [{ type: 'text', text: JSON.stringify(rows, null, 2) }] }; }
  • Input schema definition for the 'query' tool, specifying the sql string (required) and optional params array.
    inputSchema: { type: 'object', properties: { sql: { type: 'string', description: 'The SQL SELECT query string to execute.', }, params: { type: 'array', items: { type: 'string', description: 'A parameter value (as string) to bind to the query.' }, description: 'An optional array of parameters (as strings) to bind to the SQL query placeholders (e.g., ?).', optional: true, }, }, required: ['sql'], },
  • src/index.ts:421-443 (registration)
    Registration of the 'query' tool in the ListTools response, including name, description, and input schema.
    { name: 'query', description: 'Execute a SELECT query', inputSchema: { type: 'object', properties: { sql: { type: 'string', description: 'The SQL SELECT query string to execute.', }, params: { type: 'array', items: { type: 'string', description: 'A parameter value (as string) to bind to the query.' }, description: 'An optional array of parameters (as strings) to bind to the SQL query placeholders (e.g., ?).', optional: true, }, }, required: ['sql'], }, },
  • Helper method that ensures database connection, executes the SQL query using mysql2 pool.query, handles errors, and returns the result.
    private async executeQuery<T>(sql: string, params: any[] = []): Promise<T> { const pool = await this.ensureConnection(); try { const [result] = await pool.query(sql, params); return result as T; } catch (error) { this.handleDatabaseError(error); } }
  • TypeScript interfaces defining the input QueryArgs and output QueryResult for the query tool.
    interface QueryResult { content: Array<{ type: 'text'; text: string; }>; } interface QueryArgs { sql: string; params?: Array<string | number | boolean | null>; }

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

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