MCP MySQL Server

0.2.0 · active · verified Wed Apr 22

The @enemyrr/mcp-mysql-server package provides a Model Context Protocol (MCP) server designed to enable AI models, such as those integrated into Cursor IDE or Claude Desktop, to interact with MySQL databases. It offers a standardized interface for AI agents to perform common database operations like connecting, querying, executing DML statements, and managing schema. Currently at version 0.2.0, the package is in early development, implying potentially frequent updates and API changes before a stable 1.0 release. Its key differentiator is simplifying MySQL integration for AI agents through the MCP abstraction, allowing models to operate on databases without needing to handle direct driver interactions or complex connection pooling. Configuration is flexible, supporting database URLs, individual credentials via `.env` files, or direct arguments to the `connect_db` tool.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates connecting to a MySQL database using environment variables, executing a simple SELECT query, performing an INSERT statement, and listing database tables via the `use_mcp_tool` abstraction.

// This code assumes you are running within an environment that provides `use_mcp_tool`,
// such as Cursor IDE or a compatible AI agent framework.
// Ensure your .env file in the workspace root has DATABASE_URL=mysql://user:password@host:3306/database
// or individual DB_HOST, DB_USER, etc.

async function runDbOperations() {
  console.log("Attempting to connect to MySQL...");
  // 1. Connect to the database using environment variables from the workspace
  const connectResult = await use_mcp_tool({
    server_name: "mysql",
    tool_name: "connect_db",
    arguments: {
      workspace: process.env.MCP_WORKSPACE_PATH ?? "./" // Uses .env from current directory
    }
  });

  if (connectResult.status === 'success') {
    console.log("Successfully connected to MySQL database.");

    // 2. Execute a simple SELECT query
    const selectQueryResult = await use_mcp_tool({
      server_name: "mysql",
      tool_name: "query",
      arguments: {
        sql: "SELECT 1 + 1 AS solution, CURRENT_USER() AS user;",
        params: []
      }
    });

    if (selectQueryResult.status === 'success') {
      console.log("Select Query result:", selectQueryResult.data);
    } else {
      console.error("Select Query failed:", selectQueryResult.error);
    }

    // 3. Example: Execute an INSERT statement (requires a 'test_table' or similar)
    //    Ensure a table exists, e.g., CREATE TABLE test_table (id INT AUTO_INCREMENT PRIMARY KEY, message VARCHAR(255));
    const insertQueryResult = await use_mcp_tool({
      server_name: "mysql",
      tool_name: "execute",
      arguments: {
        sql: "INSERT INTO test_table (message) VALUES (?)",
        params: ["Hello from MCP MySQL Server!"]
      }
    });

    if (insertQueryResult.status === 'success') {
      console.log("Insert Query successful. Rows affected:", insertQueryResult.data.rowsAffected);
    } else {
      console.error("Insert Query failed:", insertQueryResult.error);
    }

    // 4. List all tables in the connected database
    const listTablesResult = await use_mcp_tool({
      server_name: "mysql",
      tool_name: "list_tables"
    });

    if (listTablesResult.status === 'success') {
      console.log("Tables in database:", listTablesResult.data);
    } else {
      console.error("List tables failed:", listTablesResult.error);
    }

  } else {
    console.error("Failed to connect to MySQL database:", connectResult.error);
  }
}

// In a real AI agent context, this function would be called implicitly or explicitly by the agent.
runDbOperations();

view raw JSON →