MySQL MCP Server

repository·main·Indexed 41 days ago

https://github.com/f4ww4z/mcp-mysql-server

A Model Context Protocol (MCP) server version 0.1.0 that enables AI models to interact with MySQL databases. It provides tools for connecting to databases, executing SELECT queries via `query`, performing data modifications via `execute`, and inspecting schemas using `list_tables` and `describe_table`.

Tokens
2.2K
Snippets
13
Records
14
Agent score
1%

What's inside mcp-mysql-server

  1. Install @f4ww4z/mcp-mysql-server

    main

    You can install the MySQL MCP server using Smithery for automatic Claude Desktop configuration, or via manual installation using npx.

    ### Installing via Smithery
    ```bash
    npx -y @smithery/cli install @f4ww4z/mcp-mysql-server --client claude

    Manual Installation

    npx @f4ww4z/mcp-mysql-server
  2. Run evaluations for the MySQL MCP server

    main

    You can run evaluations using mcp-eval. You can load environment variables by prefixing the command. This uses the index.ts file from the source to run tests against the server.

    OPENAI_API_KEY=your-key  npx mcp-eval src/evals/evals.ts src/index.ts
  3. Configure @f4ww4z/mcp-mysql-server for Codex

    main

    To use this server with Codex, add the configuration to your config.toml file.

    • macOS: ~/.codex/config.toml
    • Windows: %USERPROFILE%\.codex\config.toml
    [mcp_servers.mcp-mysql-server]
    command = "npx"
    args = [
      "-y",
      "@f4ww4z/mcp-mysql-server",
      "mysql://user:password@127.0.0.1:3306/database"
    ]
  4. Configure @f4ww4z/mcp-mysql-server for MCP clients

    main

    The server can be configured using either a connection string passed as an argument or via environment variables.

    Option 1: Connection String (Recommended) Pass the connection URI directly in the args array.

    Option 2: Environment Variables Set the following environment variables in your MCP settings configuration:

    • MYSQL_HOST
    • MYSQL_USER
    • MYSQL_PASSWORD
    • MYSQL_DATABASE
    // Option 1: Connection String
    {
      "mcpServers": {
        "mysql": {
          "command": "npx",
          "args": ["-y", "@f4ww4z/mcp-mysql-server", "mysql://user:password@localhost:port/database"]
        }
      }
    }
    
    // Option 2: Environment Variables
    {
      "mcpServers": {
        "mysql": {
          "command": "npx",
          "args": ["-y", "@f4ww4z/mcp-mysql-server"],
          "env": {
            "MYSQL_HOST": "your_host",
            "MYSQL_USER": "your_user",
            "MYSQL_PASSWORD": "your_password",
            "MYSQL_DATABASE": "your_database"
          }
        }
      }
    }
  5. Configure the MySQL MCP Server connection

    main

    The MySQL MCP server can be configured using a MySQL connection URL passed as a command-line argument or via specific environment variables. If no configuration is provided, the server will exit with an error.

    Using a Connection URL

    Pass a mysql:// URL as the first command-line argument: mysql://user:password@host:port/database

    Using Environment Variables

    If no command-line argument is provided, the server falls back to these environment variables:

    • MYSQL_HOST
    • MYSQL_USER
    • MYSQL_PASSWORD
    • MYSQL_DATABASE
    • MYSQL_PORT (defaults to 3306 if not set)

    Using the connect_db tool

    You can also change or establish a connection dynamically during a session using the connect_db tool, which accepts either a url or individual connection parameters (host, user, password, database, port).

    # Example using command line argument
    node index.js mysql://root:secret@localhost:3306/my_database
    
    # Example using environment variables
    export MYSQL_HOST=localhost
    export MYSQL_USER=root
    export MYSQL_PASSWORD=secret
    export MYSQL_DATABASE=my_database
    node index.js
  6. Tool: connect_db

    main

    Establishes a connection to the MySQL database using the provided credentials.

    use_mcp_tool({
      server_name: "mysql",
      tool_name: "connect_db",
      arguments: {
        host: "localhost",
        user: "your_user",
        password: "your_password",
        database: "your_database"
      }
    });
  7. Tool: execute

    main

    Executes data-modifying queries such as INSERT, UPDATE, or DELETE. Supports prepared statement parameters.

    use_mcp_tool({
      server_name: "mysql",
      tool_name: "execute",
      arguments: {
        sql: "INSERT INTO users (name, email) VALUES (?, ?)",
        params: ["John Doe", "john@example.com"]
      }
    });
  8. Use `list_tables` and `describe_table` for schema inspection

    main

    To explore the database schema, use these two tools:

    1. list_tables: Returns a JSON array of all table names in the current database.
    2. describe_table: Returns the column structure (field names, types, etc.) for a specific table.

    describe_table Arguments:

    • table (string, required): The name of the table to inspect.
    // Example: List all tables
    {
      "name": "list_tables",
      "arguments": {}
    }
    
    // Example: Describe the 'users' table
    {
      "name": "describe_table",
      "arguments": {
        "table": "users"
      }
    }
  9. Use the `execute` tool for data modifications

    main

    The execute tool is used for queries that modify data, such as INSERT, UPDATE, or DELETE. It will throw an error if you attempt to use a SELECT statement with this tool.

    Arguments:

    • sql (string, required): The SQL query.
    • params (array, optional): An array of values to be used as query parameters.
    {
      "name": "execute",
      "arguments": {
        "sql": "UPDATE users SET email = ? WHERE id = ?",
        "params": ["newemail@example.com", 1]
      }
    }