Skip to main content
Glama
irwantocrimson

MySQL MCP Server

get-schema

Retrieve the structure of a MySQL database table to understand its columns, data types, and constraints for development or analysis.

Instructions

Get the schema of a table

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
schemaNameYesThe name of the schema

Implementation Reference

  • The handler function for the 'get-schema' tool. It extracts the schemaName from args, calls the getSchema helper, and returns the result as a JSON-formatted text content block.
    async (args) => {
      const schemaName = args.schemaName;
      const result = await getSchema(schemaName);
      return {
        content: [
          {
            type: "text",
            text: JSON.stringify(result, null, 2),
          }
        ]
      };
    }
  • Zod schema definition for the input parameter 'schemaName' of type string.
    schemaName: z.string().describe("The name of the schema"),
  • src/index.ts:82-101 (registration)
    Registration of the 'get-schema' tool using server.tool, including name, description, input schema, metadata, and handler function.
    server.tool("get-schema", "Get the schema of a table", {
      schemaName: z.string().describe("The name of the schema"),
    }, {
      title: "Get Schema",
      readOnlyHint: true,
      destructiveHint: false,
    },
      async (args) => {
        const schemaName = args.schemaName;
        const result = await getSchema(schemaName);
        return {
          content: [
            {
              type: "text",
              text: JSON.stringify(result, null, 2),
            }
          ]
        };
      }
    );
  • Helper function that connects to the MySQL database using configuration, executes a DESCRIBE query on the specified schemaName (table), and returns the results or error.
    async function getSchema(schemaName: string) {
      // Create the connection to database using config (environment variables or command-line arguments)
      const connection = await mysql.createConnection({
        host: config.host,
        port: config.port,
        user: config.user,
        password: config.password,
        database: config.database,
      });
    
      try {
        const [results] = await connection.query(
          `DESCRIBE ${mysql.escapeId(schemaName)}`
        );
    
        return results;
      } catch (err) {
        console.log(err);
        return err;
      } finally {
        connection.end();
      }
    }
Install Server

Other Tools

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

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