create_table
Create new tables in MySQL databases by executing SQL CREATE TABLE statements to structure and organize data storage.
Instructions
在 MySQL 数据库中创建新表
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | 要执行的 SQL CREATE TABLE 语句 |
Implementation Reference
- src/index.ts:267-317 (handler)The main handler function for the 'create_table' tool. It validates the input arguments, checks if the query is a CREATE TABLE statement, executes the query on the MySQL pool, and returns the result or error response.private async handleCreateTable(request: any, transactionId: string) { if (!isValidSqlQueryArgs(request.params.arguments)) { throw new McpError( ErrorCode.InvalidParams, 'SQL 查询参数无效。' ); } const query = request.params.arguments.query; if (!isCreateTableQuery(query)) { throw new McpError( ErrorCode.InvalidParams, 'create_table 工具仅允许 CREATE TABLE 查询。' ); } console.error(`[${transactionId}] 执行 CREATE TABLE 查询: ${query}`); try { const [result] = await this.pool.query(query); console.error(`[${transactionId}] 表创建成功`); return { content: [ { type: 'text', text: JSON.stringify({ success: true, message: '表创建成功', result }, null, 2), }, ], }; } catch (error) { console.error(`[${transactionId}] 查询出错:`, error); if (error instanceof Error) { return { content: [ { type: 'text', text: `MySQL 错误: ${error.message}`, }, ], isError: true, }; } throw error; } }
- src/index.ts:119-129 (schema)Input schema definition for the 'create_table' tool, specifying the 'query' parameter as a required string for the CREATE TABLE SQL statement.inputSchema: { type: 'object', properties: { query: { type: 'string', description: '要执行的 SQL CREATE TABLE 语句', }, }, required: ['query'], }, },
- src/index.ts:117-129 (registration)Registration of the 'create_table' tool in the ListTools response, including name, description, and input schema.name: 'create_table', description: '在 MySQL 数据库中创建新表', inputSchema: { type: 'object', properties: { query: { type: 'string', description: '要执行的 SQL CREATE TABLE 语句', }, }, required: ['query'], }, },
- src/index.ts:36-38 (helper)Helper function to validate if the SQL query is a CREATE TABLE statement, used in the handler.const isCreateTableQuery = (query: string): boolean => query.trim().toLowerCase().startsWith('create table');
- src/index.ts:198-199 (registration)Dispatch/handling registration in the CallToolRequestSchema switch statement for the 'create_table' tool.case 'create_table': return this.handleCreateTable(request, transactionId);