Skip to main content
Glama
alittleyellowkevin

MySQL MCP Server

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
NameRequiredDescriptionDefault
queryYes要执行的 SQL CREATE TABLE 语句

Implementation Reference

  • 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;
      }
    }
  • 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'],
      },
    },
  • 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);

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