Spanner using MCP

Connect your IDE to Spanner using Toolbox.

Model Context Protocol (MCP) is an open protocol for connecting Large Language Models (LLMs) to data sources like Spanner. This guide covers how to use MCP Toolbox for Databases to expose your developer assistant tools to a Spanner instance:

Before you begin

  1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

  2. Make sure that billing is enabled for your Google Cloud project.

Set up the database

  1. Enable the Spanner API in the Google Cloud project.

  2. Create or select a Spanner instance and database.

  3. Configure the required roles and permissions to complete this task. You will need Cloud Spanner Database User role (roles/spanner.databaseUser) or equivalent IAM permissions to connect to the instance.

  4. Configured Application Default Credentials (ADC) for your environment.

Install MCP Toolbox

  1. Download the latest version of Toolbox as a binary. Select the correct binary corresponding to your OS and CPU architecture. You are required to use Toolbox version V0.6.0+:

    curl -O https://storage.googleapis.com/genai-toolbox/v0.7.0/linux/amd64/toolbox
    curl -O https://storage.googleapis.com/genai-toolbox/v0.7.0/darwin/arm64/toolbox
    curl -O https://storage.googleapis.com/genai-toolbox/v0.7.0/darwin/amd64/toolbox
    curl -O https://storage.googleapis.com/genai-toolbox/v0.7.0/windows/amd64/toolbox
  2. Make the binary executable:

    chmod +x toolbox
    
  3. Verify the installation:

    ./toolbox --version
    

Configure your MCP Client

  1. Install Claude Code.

  2. Create a .mcp.json file in your project root if it doesn’t exist.

  3. Add the following configuration, replace the environment variables with your values, and save:

    Spanner with googlesql dialect

    {
      "mcpServers": {
        "spanner": {
          "command": "./PATH/TO/toolbox",
          "args": ["--prebuilt","spanner","--stdio"],
          "env": {
            "SPANNER_PROJECT": "",
            "SPANNER_INSTANCE": "",
            "SPANNER_DATABASE": ""
          }
        }
      }
    }
    

    Spanner with postgresql dialect

    {
      "mcpServers": {
        "spanner": {
          "command": "./PATH/TO/toolbox",
          "args": ["--prebuilt","spanner-postgres","--stdio"],
          "env": {
            "SPANNER_PROJECT": "",
            "SPANNER_INSTANCE": "",
            "SPANNER_DATABASE": ""          }
        }
      }
    }
    
  4. Restart Claude code to apply the new configuration.

  1. Open Claude desktop and navigate to Settings.

  2. Under the Developer tab, tap Edit Config to open the configuration file.

  3. Add the following configuration, replace the environment variables with your values, and save:

    Spanner with googlesql dialect

    {
      "mcpServers": {
        "spanner": {
          "command": "./PATH/TO/toolbox",
          "args": ["--prebuilt","spanner","--stdio"],
          "env": {
            "SPANNER_PROJECT": "",
            "SPANNER_INSTANCE": "",
            "SPANNER_DATABASE": ""
          }
        }
      }
    }
    

    Spanner with postgresql dialect

    {
      "mcpServers": {
        "spanner": {
          "command": "./PATH/TO/toolbox",
          "args": ["--prebuilt","spanner-postgres","--stdio"],
          "env": {
            "SPANNER_PROJECT": "",
            "SPANNER_INSTANCE": "",
            "SPANNER_DATABASE": ""
          }
        }
      }
    }
    
  4. Restart Claude desktop.

  5. From the new chat screen, you should see a hammer (MCP) icon appear with the new MCP server available.

  1. Open the Cline extension in VS Code and tap the MCP Servers icon.

  2. Tap Configure MCP Servers to open the configuration file.

  3. Add the following configuration, replace the environment variables with your values, and save:

    Spanner with googlesql dialect

    {
      "mcpServers": {
        "spanner": {
          "command": "./PATH/TO/toolbox",
          "args": ["--prebuilt","spanner","--stdio"],
          "env": {
            "SPANNER_PROJECT": "",
            "SPANNER_INSTANCE": "",
            "SPANNER_DATABASE": ""
          }
        }
      }
    }
    

    Spanner with postgresql dialect

    {
      "mcpServers": {
        "spanner": {
          "command": "./PATH/TO/toolbox",
          "args": ["--prebuilt","spanner-postgres","--stdio"],
          "env": {
            "SPANNER_PROJECT": "",
            "SPANNER_INSTANCE": "",
            "SPANNER_DATABASE": ""
          }
        }
      }
    }
    
  4. You should see a green active status after the server is successfully connected.

  1. Create a .cursor directory in your project root if it doesn’t exist.

  2. Create a .cursor/mcp.json file if it doesn’t exist and open it.

  3. Add the following configuration, replace the environment variables with your values, and save:

    Spanner with googlesql dialect

    {
      "mcpServers": {
        "spanner": {
          "command": "./PATH/TO/toolbox",
          "args": ["--prebuilt","spanner","--stdio"],
          "env": {
            "SPANNER_PROJECT": "",
            "SPANNER_INSTANCE": "",
            "SPANNER_DATABASE": ""
          }
        }
      }
    }
    

    Spanner with postgresql dialect

    {
      "mcpServers": {
        "spanner": {
          "command": "./PATH/TO/toolbox",
          "args": ["--prebuilt","spanner-postgres","--stdio"],
          "env": {
            "SPANNER_PROJECT": "",
            "SPANNER_INSTANCE": "",
            "SPANNER_DATABASE": ""
          }
        }
      }
    }
    
  4. Cursor and navigate to Settings > Cursor Settings > MCP. You should see a green active status after the server is successfully connected.

  1. Open VS Code and create a .vscode directory in your project root if it doesn’t exist.

  2. Create a .vscode/mcp.json file if it doesn’t exist and open it.

  3. Add the following configuration, replace the environment variables with your values, and save:

    Spanner with googlesql dialect

    {
      "mcpServers": {
        "spanner": {
          "command": "./PATH/TO/toolbox",
          "args": ["--prebuilt","spanner","--stdio"],
          "env": {
            "SPANNER_PROJECT": "",
            "SPANNER_INSTANCE": "",
            "SPANNER_DATABASE": ""
          }
        }
      }
    }
    

    Spanner with postgresql dialect

    {
      "mcpServers": {
        "spanner": {
          "command": "./PATH/TO/toolbox",
          "args": ["--prebuilt","spanner-postgres","--stdio"],
          "env": {
            "SPANNER_PROJECT": "",
            "SPANNER_INSTANCE": "",
            "SPANNER_DATABASE": ""
          }
        }
      }
    }
    
  1. Open Windsurf and navigate to the Cascade assistant.

  2. Tap on the hammer (MCP) icon, then Configure to open the configuration file.

  3. Add the following configuration, replace the environment variables with your values, and save:

    Spanner with googlesql dialect

    {
      "mcpServers": {
        "spanner": {
          "command": "./PATH/TO/toolbox",
          "args": ["--prebuilt","spanner","--stdio"],
          "env": {
            "SPANNER_PROJECT": "",
            "SPANNER_INSTANCE": "",
            "SPANNER_DATABASE": ""
          }
        }
      }
    }
    

    Spanner with postgresql dialect

    {
      "mcpServers": {
        "spanner": {
          "command": "./PATH/TO/toolbox",
          "args": ["--prebuilt","spanner-postgres","--stdio"],
          "env": {
            "SPANNER_PROJECT": "",
            "SPANNER_INSTANCE": "",
            "SPANNER_DATABASE": ""
          }
        }
      }
    }
    

Use Tools

Your AI tool is now connected to Spanner using MCP. Try asking your AI assistant to list tables, create a table, or define and execute other SQL statements.

The following tools are available to the LLM:

  1. list_tables: lists tables and descriptions
  2. execute_sql: execute DML SQL statement
  3. execute_sql_dql: execute DQL SQL statement

Note

Prebuilt tools are pre-1.0, so expect some tool changes between versions. LLMs will adapt to the tools available, so this shouldn’t affect most users.