Skip to main content
Glama
nilsir

MCP Server MySQL

by nilsir

create_table

Define new database tables with specific columns, data types, and constraints for structured data storage in MySQL.

Instructions

Create a new table with specified columns

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
tableYesTable name
columnsYesColumn definitions
databaseNoDatabase name (optional)

Implementation Reference

  • The handler function that executes the create_table tool logic: constructs a CREATE TABLE SQL statement from column definitions and executes it.
    async ({ table, columns, database }) => {
      const p = await getPool();
    
      const columnDefs = columns.map((col) => {
        let def = `\`${col.name}\` ${col.type}`;
        if (col.nullable === false) def += " NOT NULL";
        if (col.autoIncrement) def += " AUTO_INCREMENT";
        if (col.default !== undefined) def += ` DEFAULT ${col.default}`;
        if (col.primaryKey) def += " PRIMARY KEY";
        return def;
      });
    
      const tableName = database ? `\`${database}\`.\`${table}\`` : `\`${table}\``;
      const sql = `CREATE TABLE ${tableName} (${columnDefs.join(", ")})`;
    
      await p.execute(sql);
    
      const output = { success: true, table, database: database || null };
    
      return {
        content: [
          {
            type: "text" as const,
            text: `Table ${table} created successfully`,
          },
        ],
        structuredContent: output,
      };
    }
  • Zod schema for column definitions used in the input schema of create_table tool.
    const columnSchema = z.object({
      name: z.string().describe("Column name"),
      type: z.string().describe("Column type (e.g., VARCHAR(255), INT, TEXT)"),
      nullable: z.boolean().optional().describe("Whether column can be null"),
      primaryKey: z.boolean().optional().describe("Whether this is the primary key"),
      autoIncrement: z.boolean().optional().describe("Whether to auto increment"),
      default: z.string().optional().describe("Default value"),
    });
  • src/index.ts:311-348 (registration)
    Registration of the create_table tool using server.tool, including name, description, input schema, and reference to handler.
    server.tool(
      "create_table",
      "Create a new table with specified columns",
      {
        table: z.string().describe("Table name"),
        columns: z.array(columnSchema).describe("Column definitions"),
        database: z.string().optional().describe("Database name (optional)"),
      },
      async ({ table, columns, database }) => {
        const p = await getPool();
    
        const columnDefs = columns.map((col) => {
          let def = `\`${col.name}\` ${col.type}`;
          if (col.nullable === false) def += " NOT NULL";
          if (col.autoIncrement) def += " AUTO_INCREMENT";
          if (col.default !== undefined) def += ` DEFAULT ${col.default}`;
          if (col.primaryKey) def += " PRIMARY KEY";
          return def;
        });
    
        const tableName = database ? `\`${database}\`.\`${table}\`` : `\`${table}\``;
        const sql = `CREATE TABLE ${tableName} (${columnDefs.join(", ")})`;
    
        await p.execute(sql);
    
        const output = { success: true, table, database: database || null };
    
        return {
          content: [
            {
              type: "text" as const,
              text: `Table ${table} created successfully`,
            },
          ],
          structuredContent: output,
        };
      }
    );

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/nilsir/mcp-server-mysql'

If you have feedback or need assistance with the MCP directory API, please join our Discord server