Skip to main content
Glama
Darkstar326

MCP MySQL Server

by Darkstar326

mysql_connect

Establish a connection to MySQL databases by providing host, user, and password parameters. Use this tool to initiate secure database interactions for query execution and management operations.

Instructions

Connect to a MySQL database with provided connection parameters

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
hostYesMySQL server hostname or IP address
portNoMySQL server port (default: 3306)
userYesDatabase username
passwordYesDatabase password
databaseNoDatabase name (optional)
sslNoUse SSL connection (default: false)

Implementation Reference

  • The main handler function for the mysql_connect tool. It validates input using ConfigSchema, closes any existing connection pool, creates a new MySQL pool using createConnection, tests it, and returns a success message.
    private async handleConnect(args: any) {
      try {
        const config = ConfigSchema.parse(args);
        this.config = config;
        
        // Close existing connection if any
        if (this.pool) {
          await this.pool.end();
        }
    
        this.pool = await this.createConnection(config);
        
        return {
          content: [
            {
              type: "text",
              text: `Successfully connected to MySQL server at ${config.host}:${config.port}${config.database ? ` (database: ${config.database})` : ""}`,
            },
          ],
        };
      } catch (error) {
        throw new Error(`Connection failed: ${error instanceof Error ? error.message : String(error)}`);
      }
    }
  • Zod schema used to validate the input parameters for mysql_connect tool.
    const ConfigSchema = z.object({
      host: z.string(),
      port: z.number().optional().default(3306),
      user: z.string(),
      password: z.string(),
      database: z.string().optional(),
      ssl: z.boolean().optional().default(false),
      connectionLimit: z.number().optional().default(10),
    });
  • src/index.ts:101-135 (registration)
    Registers the mysql_connect tool in the ListTools response, providing name, description, and JSON inputSchema matching the ConfigSchema.
      name: "mysql_connect",
      description: "Connect to a MySQL database with provided connection parameters",
      inputSchema: {
        type: "object",
        properties: {
          host: {
            type: "string",
            description: "MySQL server hostname or IP address",
          },
          port: {
            type: "number",
            description: "MySQL server port (default: 3306)",
            default: 3306,
          },
          user: {
            type: "string",
            description: "Database username",
          },
          password: {
            type: "string",
            description: "Database password",
          },
          database: {
            type: "string",
            description: "Database name (optional)",
          },
          ssl: {
            type: "boolean",
            description: "Use SSL connection (default: false)",
            default: false,
          },
        },
        required: ["host", "user", "password"],
      },
    },
  • Helper method called by handleConnect to create the mysql2/promise Pool with config options, optionally enable SSL, and test the connection with ping.
    private async createConnection(config: Config): Promise<mysql.Pool> {
      try {
        const poolConfig: mysql.PoolOptions = {
          host: config.host,
          port: config.port,
          user: config.user,
          password: config.password,
          database: config.database,
          connectionLimit: config.connectionLimit,
          multipleStatements: false,
        };
    
        if (config.ssl) {
          poolConfig.ssl = {};
        }
    
        this.pool = mysql.createPool(poolConfig);
    
        // Test the connection
        const connection = await this.pool.getConnection();
        await connection.ping();
        connection.release();
    
        return this.pool;
      } catch (error) {
        throw new Error(`Failed to connect to MySQL: ${error instanceof Error ? error.message : String(error)}`);
      }
    }

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

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