create_table
Use this tool to execute SQL CREATE TABLE statements in a MySQL database via the MCP server, enabling structured table creation and management.
Instructions
在 MySQL 数据库中创建新表
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | 要执行的 SQL CREATE TABLE 语句 |
Input Schema (JSON Schema)
{
"properties": {
"query": {
"description": "要执行的 SQL CREATE TABLE 语句",
"type": "string"
}
},
"required": [
"query"
],
"type": "object"
}
Implementation Reference
- src/index.ts:267-317 (handler)The handler function that validates the input arguments, checks if it's a CREATE TABLE query, executes the SQL query on the MySQL connection pool, 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, '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-128 (schema)The input schema definition for the create_table tool, specifying an object with a required 'query' string property for the CREATE TABLE SQL statement.inputSchema: { type: 'object', properties: { query: { type: 'string', description: '要执行的 SQL CREATE TABLE 语句', }, }, required: ['query'], },
- src/index.ts:116-129 (registration)The registration of the create_table tool in the tools list returned by ListToolsRequestHandler, 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-37 (helper)Helper function used to validate that the provided query is a CREATE TABLE statement by checking if it starts with 'create table' after trimming and lowercasing.const isCreateTableQuery = (query: string): boolean => query.trim().toLowerCase().startsWith('create table');