mysql_query
Execute a read-only SQL query to retrieve data from a MySQL database. Use when fetching data without modifications.
Instructions
Execute a read-only SQL query (e.g., SELECT). Use this for data retrieval.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sql | Yes | The SQL query to execute. |
Implementation Reference
- src/index.ts:24-29 (handler)The async handler function for the 'mysql_query' tool. It receives an object with a 'sql' string, calls db.query(sql), and returns the results as JSON text content.
async ({ sql }) => { const results = await db.query(sql); return { content: [{ type: 'text', text: JSON.stringify(results, null, 2) }], }; } - src/index.ts:18-23 (schema)Input schema definition for the 'mysql_query' tool using Zod. It defines a single required 'sql' parameter of type string.
{ description: 'Execute a read-only SQL query (e.g., SELECT). Use this for data retrieval.', inputSchema: z.object({ sql: z.string().describe('The SQL query to execute.'), }), }, - src/index.ts:16-30 (registration)Registration of the 'mysql_query' tool using server.registerTool() with its name, schema, and handler callback.
server.registerTool( 'mysql_query', { description: 'Execute a read-only SQL query (e.g., SELECT). Use this for data retrieval.', inputSchema: z.object({ sql: z.string().describe('The SQL query to execute.'), }), }, async ({ sql }) => { const results = await db.query(sql); return { content: [{ type: 'text', text: JSON.stringify(results, null, 2) }], }; } ); - src/db.ts:42-44 (helper)The db.query() helper function used by the mysql_query handler. Executes a SQL query on the MySQL connection pool and returns the rows.
export async function query(sql: string, params?: any[]) { const [rows] = await pool.query(sql, params); return rows;