Skip to main content
Glama
nilsir

MCP Server MySQL

by nilsir

connect

Establish a connection to a MySQL database using provided credentials or environment variables, enabling database operations through the MCP Server MySQL.

Instructions

Connect to a MySQL database. If not called explicitly, will use environment variables for connection.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
hostNoDatabase host
portNoDatabase port
userNoDatabase user
passwordNoDatabase password
databaseNoDatabase name

Implementation Reference

  • The handler function for the 'connect' tool. It creates a MySQL connection pool using the provided parameters or falls back to environment variables, closes any existing pool, tests the new connection by acquiring and releasing it, and returns a success message with connection details.
    async ({ host, port, user, password, database }) => {
      const config = {
        host: host || process.env.MYSQL_HOST || "localhost",
        port: port || parseInt(process.env.MYSQL_PORT || "3306", 10),
        user: user || process.env.MYSQL_USER || "root",
        password: password || process.env.MYSQL_PASSWORD || "",
        database: database,
        waitForConnections: true,
        connectionLimit: 10,
        queueLimit: 0,
      };
    
      // Close existing pool if any
      if (pool) {
        await pool.end();
      }
    
      pool = mysql.createPool(config);
    
      // Test connection
      const connection = await pool.getConnection();
      connection.release();
    
      const output = {
        success: true,
        host: config.host,
        port: config.port,
        database: config.database || null,
      };
    
      return {
        content: [
          {
            type: "text" as const,
            text: `Successfully connected to MySQL server at ${config.host}:${config.port}${config.database ? ` (database: ${config.database})` : ""}`,
          },
        ],
        structuredContent: output,
      };
    }
  • Zod schema defining the optional input parameters for the 'connect' tool: host, port, user, password, and database.
    {
      host: z.string().optional().describe("Database host"),
      port: z.number().optional().describe("Database port"),
      user: z.string().optional().describe("Database user"),
      password: z.string().optional().describe("Database password"),
      database: z.string().optional().describe("Database name"),
    },
  • src/index.ts:89-140 (registration)
    Registration of the 'connect' tool on the MCP server using server.tool(), specifying the tool name, description, input schema, and handler function.
    // Tool: connect
    server.tool(
      "connect",
      "Connect to a MySQL database. If not called explicitly, will use environment variables for connection.",
      {
        host: z.string().optional().describe("Database host"),
        port: z.number().optional().describe("Database port"),
        user: z.string().optional().describe("Database user"),
        password: z.string().optional().describe("Database password"),
        database: z.string().optional().describe("Database name"),
      },
      async ({ host, port, user, password, database }) => {
        const config = {
          host: host || process.env.MYSQL_HOST || "localhost",
          port: port || parseInt(process.env.MYSQL_PORT || "3306", 10),
          user: user || process.env.MYSQL_USER || "root",
          password: password || process.env.MYSQL_PASSWORD || "",
          database: database,
          waitForConnections: true,
          connectionLimit: 10,
          queueLimit: 0,
        };
    
        // Close existing pool if any
        if (pool) {
          await pool.end();
        }
    
        pool = mysql.createPool(config);
    
        // Test connection
        const connection = await pool.getConnection();
        connection.release();
    
        const output = {
          success: true,
          host: config.host,
          port: config.port,
          database: config.database || null,
        };
    
        return {
          content: [
            {
              type: "text" as const,
              text: `Successfully connected to MySQL server at ${config.host}:${config.port}${config.database ? ` (database: ${config.database})` : ""}`,
            },
          ],
          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