Open7
genai-toolboxでClaude CodeとCloudSQLを繋いでみる / 附: Cloud Runへのデプロイ
Goal
genai-toolbox
をmcpサーバーとして使い、Claude CodeとCloudSQLを繋ぐ。
0. CloudSQLの作成/readonlyユーザーの作成
省略します。
toolkit
のダウンロード
1. 下記に従って、toolkit
のダウンロード、yamlファイルの作成をおこなう。
筆者はMacBook Air M3なのでOSにはdarwin/arm64
を用いる。
$export OS="darwin/arm64" # one of linux/amd64, darwin/arm64, darwin/amd64, or windows/amd64
$curl -O https://storage.googleapis.com/genai-toolbox/v0.10.0/$OS/toolbox
実行権限を追加。
$chmod +x toolbox
2. yamlファイルを作成
source
およびtools
を定義する。
source
sources:
my-cloud-sql-mysql-source:
kind: cloud-sql-mysql
project: <google-project>
region: <region>
instance: <instance-name>
database: <database>
user: <username>
password: <password>
tools
MySQL向けには2種類。
- mysql-execute-sql
- mysql-sql
前者は任意のSQL、後者はSQLを指定。
tools:
list-tables:
kind: mysql-sql
source: my-cloud-sql-mysql-source
description: データベース内のテーブル一覧を表示します。
statement: SHOW TABLES;
toolsets:
my-toolset:
- list-tables
実行
$./toolbox --tools-file "tools.yaml"
以下のようにserverが起動する。
./toolbox --tools-file "./tools.yaml"
2025-07-28T18:15:47.111358+09:00 INFO "Initialized 1 sources."
2025-07-28T18:15:47.111422+09:00 INFO "Initialized 0 authServices."
2025-07-28T18:15:47.111433+09:00 INFO "Initialized 1 tools."
2025-07-28T18:15:47.111443+09:00 INFO "Initialized 2 toolsets."
2025-07-28T18:15:47.111515+09:00 INFO "Server ready to serve!"
2025-07-28T18:16:36.678757+09:00 INFO
テスト
$npx @modelcontextprotocol/inspector
- transport:
Streamable HTTP
- url:
http://127.0.0.1:5000/mcp
- Proxy Session Tokenにターミナルのtoken
をセット。
List Toolでツールが表示され、実行してテスト。
Claude Codeとの繋ぎこみ
$claude mcp add --transport http cloudsql-toolkit http://127.0.0.1:5000/mcp --scope=user
scopeは適宜変えてください。
これで実行できるようになった。
Claude Desktopとの繋ぎこみ
"cloudsql": {
"command": "/Users/hosaka/.volta/bin/npx",
"args": [
"-y",
"supergateway",
"--streamableHttp",
"http://<your toolkit>/mcp"
]
}
動的SQL文
動的にパラメータを与えたい場合は、以下のようにする。
tools:
search_flights_by_number:
kind: mysql-sql
source: my-mysql-instance
statement: |
SELECT * FROM flights
WHERE airline = ?
AND flight_number = ?
LIMIT 10
description: |
Use this tool to get information for a specific flight.
Takes an airline code and flight number and returns info on the flight.
Do NOT use this tool with a flight id. Do NOT guess an airline code or flight number.
A airline code is a code for an airline service consisting of two-character
airline designator and followed by flight number, which is 1 to 4 digit number.
For example, if given CY 0123, the airline is "CY", and flight_number is "123".
Another example for this is DL 1234, the airline is "DL", and flight_number is "1234".
If the tool returns more than one option choose the date closes to today.
Example:
{{
"airline": "CY",
"flight_number": "888",
}}
Example:
{{
"airline": "DL",
"flight_number": "1234",
}}
parameters:
- name: airline
type: string
description: Airline unique 2 letter identifier
- name: flight_number
type: string
description: 1 to 4 digit number
Cloud Runへのデプロイ
Cloud RunにデプロイしてRemote MCP化する。
下記にドキュメントがある。
0. サービスアカウント作成
ドキュメントに従ってサービスアカウント作成。
$gcloud iam service-accounts create toolbox-identity
roleを追加。yaml構成ファイルはsecretとして扱うため。
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:toolbox-identity@$PROJECT_ID.iam.gserviceaccount.com \
--role roles/secretmanager.secretAccessor
これに加えて、Cloud SQL クライアント
を付与した。
1. Secret作成
$gcloud secrets create tools --data-file=tools.yaml
2. デプロイ
$gcloud run deploy toolbox \
--image us-central1-docker.pkg.dev/database-toolbox/toolbox/toolbox:latest \
--service-account toolbox-identity \
--region us-central1 \
--set-secrets "/app/tools.yaml=tools:latest" \
--args="--tools-file=/app/tools.yaml","--address=0.0.0.0","--port=8080"
この時、--allow-unauthenticated
を付けていないので保護付きになる。
3. Cloud Run Proxy
ドキュメントからは外れるが、アクセスにはCloud Run Proxyを使った。
$gcloud run service proxy toolbox
これでhttp://127.0.0.1:8080
がCloud Runインスタンスにproxyされるのでアクセスできるようになる。
これで保護付きリモートMCPサーバーの完成。
関連記事