create_table
Execute SQL CREATE TABLE queries to establish new tables in a MySQL database, enabling structured data storage and management for efficient operations.
Instructions
Creates a new table in the MySQL database.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | The SQL CREATE TABLE query to execute. |
Implementation Reference
- src/index.ts:258-308 (handler)The handler function that validates the input, checks if it's a CREATE TABLE query, executes it on the MySQL database, and returns a formatted response with success or error details.private async handleCreateTable(request: any, transactionId: string) { if (!isValidSqlQueryArgs(request.params.arguments)) { throw new McpError( ErrorCode.InvalidParams, 'Invalid SQL query arguments.' ); } const query = request.params.arguments.query; if (!isCreateTableQuery(query)) { throw new McpError( ErrorCode.InvalidParams, 'Only CREATE TABLE queries are allowed with create_table tool.' ); } console.error(`[${transactionId}] Executing CREATE TABLE query: ${query}`); try { const [result] = await this.pool.query(query); console.error(`[${transactionId}] Table created successfully`); return { content: [ { type: 'text', text: JSON.stringify({ success: true, message: 'Table created successfully', result }, null, 2), }, ], }; } catch (error) { console.error(`[${transactionId}] Query error:`, error); if (error instanceof Error) { return { content: [ { type: 'text', text: `MySQL error: ${error.message}`, }, ], isError: true, }; } throw error; } }
- src/index.ts:109-121 (schema)The input schema for the create_table tool, defining the expected 'query' parameter as a string for the CREATE TABLE SQL statement.name: 'create_table', description: 'Creates a new table in the MySQL database.', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'The SQL CREATE TABLE query to execute.', }, }, required: ['query'], }, },
- src/index.ts:189-190 (registration)The switch case that registers and routes calls to the create_table tool to its handler function.case 'create_table': return this.handleCreateTable(request, transactionId);
- src/index.ts:34-35 (helper)Helper function used by the handler to validate that the provided query starts with 'CREATE TABLE'.const isCreateTableQuery = (query: string): boolean => query.trim().toLowerCase().startsWith('create table');
- src/index.ts:24-27 (helper)Helper function used by the handler to validate that the arguments contain a 'query' string.const isValidSqlQueryArgs = (args: any): args is SqlQueryArgs => typeof args === 'object' && args !== null && typeof args.query === 'string';