Skip to main content
Glama

MySQL MCP Server

run_sql_query

Execute read-only SQL SELECT queries to retrieve data from a MySQL database via the MySQL MCP Server, with results formatted in JSON for simplified integration and analysis.

Instructions

执行只读 SQL 查询(仅限 SELECT 语句)

Input Schema

NameRequiredDescriptionDefault
queryYes要执行的 SQL SELECT 查询语句

Input Schema (JSON Schema)

{ "properties": { "query": { "description": "要执行的 SQL SELECT 查询语句", "type": "string" } }, "required": [ "query" ], "type": "object" }

Implementation Reference

  • The main handler function that validates the SQL query (ensures it's a SELECT), executes it using the MySQL pool, and returns the results as JSON-formatted text content.
    private async handleReadQuery(request: any, transactionId: string) { if (!isValidSqlQueryArgs(request.params.arguments)) { throw new McpError( ErrorCode.InvalidParams, 'SQL 查询参数无效。' ); } const query = request.params.arguments.query; if (!isReadOnlyQuery(query)) { throw new McpError( ErrorCode.InvalidParams, 'run_sql_query 工具仅允许 SELECT 查询。' ); } console.error(`[${transactionId}] 执行 SELECT 查询: ${query}`); try { const [rows] = await this.pool.query(query); console.error(`[${transactionId}] 查询执行成功`); return { content: [ { type: 'text', text: JSON.stringify(rows, null, 2), }, ], }; } catch (error) { console.error(`[${transactionId}] 查询出错:`, error); if (error instanceof Error) { return { content: [ { type: 'text', text: `MySQL 错误: ${error.message}`, }, ], isError: true, }; } throw error; } }
  • The input schema definition for the run_sql_query tool, specifying that it takes a 'query' string parameter.
    name: 'run_sql_query', description: '执行只读 SQL 查询(仅限 SELECT 语句)', inputSchema: { type: 'object', properties: { query: { type: 'string', description: '要执行的 SQL SELECT 查询语句', }, }, required: ['query'], }, },
  • src/index.ts:100-187 (registration)
    Registration of the run_sql_query tool in the ListTools response, including its name, description, and schema.
    this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [ { name: 'run_sql_query', description: '执行只读 SQL 查询(仅限 SELECT 语句)', inputSchema: { type: 'object', properties: { query: { type: 'string', description: '要执行的 SQL SELECT 查询语句', }, }, required: ['query'], }, }, { name: 'create_table', description: '在 MySQL 数据库中创建新表', inputSchema: { type: 'object', properties: { query: { type: 'string', description: '要执行的 SQL CREATE TABLE 语句', }, }, required: ['query'], }, }, { name: 'insert_data', description: '向 MySQL 数据库表插入数据', inputSchema: { type: 'object', properties: { query: { type: 'string', description: '要执行的 SQL INSERT INTO 语句', }, }, required: ['query'], }, }, { name: 'update_data', description: '更新 MySQL 数据库表中的数据', inputSchema: { type: 'object', properties: { query: { type: 'string', description: '要执行的 SQL UPDATE 语句', }, }, required: ['query'], }, }, { name: 'delete_data', description: '从 MySQL 数据库表中删除数据', inputSchema: { type: 'object', properties: { query: { type: 'string', description: '要执行的 SQL DELETE FROM 语句', }, }, required: ['query'], }, }, { name: 'execute_sql', description: '执行任意非 SELECT 的 SQL 语句(如 ALTER TABLE、DROP 等)', inputSchema: { type: 'object', properties: { query: { type: 'string', description: '要执行的 SQL 语句', }, }, required: ['query'], }, }, ], }));
  • src/index.ts:190-214 (registration)
    Dispatcher/registration for tool calls, routing 'run_sql_query' to the handleReadQuery method.
    this.server.setRequestHandler(CallToolRequestSchema, async (request) => { const transactionId = generateTransactionId(); console.error(`[${transactionId}] 正在处理请求: ${request.params.name}`); // 根据工具类型分发处理 switch (request.params.name) { case 'run_sql_query': return this.handleReadQuery(request, transactionId); case 'create_table': return this.handleCreateTable(request, transactionId); case 'insert_data': return this.handleInsertData(request, transactionId); case 'update_data': return this.handleUpdateData(request, transactionId); case 'delete_data': return this.handleDeleteData(request, transactionId); case 'execute_sql': return this.handleExecuteSql(request, transactionId); default: throw new McpError( ErrorCode.MethodNotFound, `未知工具: ${request.params.name}` ); } });
  • Helper function to check if a SQL query is a read-only SELECT statement.
    const isReadOnlyQuery = (query: string): boolean => query.trim().toLowerCase().startsWith('select');

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/alittleyellowkevin/Mysql-MCP'

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