tidb_query
Execute SQL queries on TiDB databases through the MCP server, enabling SELECT, INSERT, UPDATE, and DELETE operations for efficient data management.
Instructions
Execute SQL queries against TiDB
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sql | Yes | The SQL query to execute |
Implementation Reference
- src/index.ts:90-133 (handler)Registers and implements the handler for CallToolRequestSchema, specifically handling 'tidb_query' by executing SQL queries on TiDB with permission checks for INSERT/UPDATE/DELETE.this.server.setRequestHandler(CallToolRequestSchema, async (request) => { if (request.params.name !== 'tidb_query') { throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${request.params.name}`); } if (!request.params.arguments || typeof request.params.arguments.sql !== 'string') { throw new McpError(ErrorCode.InvalidParams, 'SQL query is required and must be a string'); } try { const sql = request.params.arguments.sql.trim().toLowerCase(); const sqlType = sql.split(' ')[0]; // 检查操作权限 if (sqlType === 'insert' && process.env.ALLOW_INSERT_OPERATION !== 'true') { throw new McpError(ErrorCode.InvalidRequest, 'INSERT operations are not allowed'); } if (sqlType === 'update' && process.env.ALLOW_UPDATE_OPERATION !== 'true') { throw new McpError(ErrorCode.InvalidRequest, 'UPDATE operations are not allowed'); } if (sqlType === 'delete' && process.env.ALLOW_DELETE_OPERATION !== 'true') { throw new McpError(ErrorCode.InvalidRequest, 'DELETE operations are not allowed'); } const [rows] = await this.pool.query(sql); return { content: [{ type: 'text', text: JSON.stringify(rows, null, 2) }] }; } catch (error) { if (error instanceof Error) { return { content: [{ type: 'text', text: `TiDB query error: ${error.message}` }], isError: true }; } throw error; } });
- src/index.ts:76-85 (schema)Defines the input schema for the tidb_query tool, requiring a 'sql' string property.inputSchema: { type: 'object', properties: { sql: { type: 'string', description: 'The SQL query to execute' } }, required: ['sql'] }
- src/index.ts:73-88 (registration)Registers the 'tidb_query' tool in the ListToolsRequestSchema response, including name, description, and input schema.{ name: 'tidb_query', description: 'Execute SQL queries against TiDB', inputSchema: { type: 'object', properties: { sql: { type: 'string', description: 'The SQL query to execute' } }, required: ['sql'] } } ] }));