Skip to main content
Glama
kevinbin

MCP MySQL Server

by kevinbin

query

Run SELECT queries on MySQL databases using SQL statements and optional parameters via the MCP MySQL Server, enabling data retrieval and interaction with database systems.

Instructions

Execute a SELECT query

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
paramsNoQuery parameters (optional)
sqlYesSQL SELECT query

Implementation Reference

  • The main handler function for the 'query' tool that validates the SQL is a SELECT statement, executes the query using the private executeQuery method, 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) }] }; }
  • src/index.ts:375-395 (registration)
    Registration of the 'query' tool in the ListTools response, defining its name, description, and input schema.
    { name: 'query', description: 'Execute a SELECT query', inputSchema: { type: 'object', properties: { sql: { type: 'string', description: 'SQL SELECT query', }, params: { type: 'array', items: { type: ['string', 'number', 'boolean', 'null'], }, description: 'Query parameters (optional)', }, }, required: ['sql'], }, },
  • TypeScript interfaces defining the input arguments (QueryArgs: sql and optional params) and output structure (QueryResult: text content) for the query tool.
    interface QueryResult { content: Array<{ type: 'text'; text: string; }>; } interface QueryArgs { sql: string; params?: Array<string | number | boolean | null>; }
  • Core helper method that performs the actual database query execution after ensuring connection, handling MySQL pool query and errors.
    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); } }
  • src/index.ts:521-522 (registration)
    Dispatch case in the CallToolRequest handler that routes 'query' tool calls to the handleQuery method.
    case 'query': return await this.handleQuery(request.params.arguments as unknown as QueryArgs);

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

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