Connecting Claude Code
This is the reference for engineers whose org has already deployed the gateway. Brand new? Run through the 5-minute first-query walkthrough — it covers install → SSO → first real query end-to-end. Come back here for the full surface (scope flags, troubleshooting, pre-configured OAuth, removal).
If you're standing the gateway up, see ../deployment/quickstart.md first.
Every
https://db.internal.acme.combelow is a placeholder. Your platform team gives you the real one — same shape, different domain. The MCP endpoint is the origin plus/mcp(or the operator'sMCP_PATH); the bare origin answers404.
Prerequisites
- Claude Code installed and logged in.
- Your gateway's MCP URL (ask your platform team — looks like
https://db.internal.acme.com/mcp). - A working SSO login at your org's IdP.
Add the gateway as a remote MCP server
The gateway speaks MCP over HTTP. One command:
claude mcp add --transport http db-gateway https://db.internal.acme.com/mcp
That's it. Restart Claude Code (or run /mcp to verify).
Scope flags
The example above uses local scope (just this project, just you). Two other options:
| Scope | When to use | Stored in |
|---|---|---|
--scope local (default) | One-off / per-project | ~/.claude.json (your home) |
--scope user | Want it in every project on your machine | ~/.claude.json |
--scope project | Want every teammate on this repo to get it | .mcp.json in the repo |
Most teams want --scope project so the config lands in .mcp.json and gets reviewed in PR like anything else:
claude mcp add --transport http db-gateway --scope project https://db.internal.acme.com/mcp
The resulting .mcp.json:
{
"mcpServers": {
"db-gateway": {
"type": "http",
"url": "https://db.internal.acme.com/mcp"
}
}
}
Sign in
First time you call a gateway tool, Claude Code will hit a 401 and Claude will tell you to run /mcp. Do that and follow the browser SSO flow.
/mcp
Tokens are stored in your system keychain (macOS) or credentials file, refreshed automatically until they expire. To start over: /mcp → Clear authentication.
Signing out (POST /auth/logout) ends every session for your identity — every machine and every Claude Code instance signed in as you has to run /mcp again.
401 vs 5xx
401(with aWWW-Authenticateheader) means your credential is missing, expired or revoked. Claude Code re-runs the sign-in; that is the fix.5xx(500/503, noWWW-Authenticate) means the gateway itself could not check your credential — typically its state database is briefly unavailable (503with codestate_db_unavailable). Signing in again will not help; retry in a moment. Your session is still valid.
Verify it works
In a Claude Code session, ask the agent to list databases:
What databases can I see through db-gateway?
The agent calls list_servers (each server inlines its databases, with the actions, limits and tools your grants give you there) and shows what your group has access to. whoami answers "who am I logged in as, which groups, until when, with which grants". Empty list = authenticated but no grants — see first-query.md → I see no servers.
For a full first-query walkthrough — including reading your own audit row — see first-query.md.
Daily use
You don't invoke gateway tools directly. You talk to the agent in natural language and it picks the right tool:
Why is invoice 1042 stuck? Check the prod billing DB.
The agent figures out it needs to:
describe_schemaonprod / billing- write a query against
invoicesjoiningevents - call
run_query - surface the result
Every step is logged on the gateway side with your identity.
Beyond queries, ask for:
- Plans — "explain this query" (
explain; "with real timings" addsanalyze: truefor reads). - Table health — "how big is
ordersand when was it last vacuumed?" (table_stats). - Your running queries — "what am I running on prod right now?" (
db_activity), and "cancel that one" (cancel_query). You only ever see and cancel your own. - Writes — only if your group holds
query_writeon that database. Ask for a dry run first ("how many rows would this update touch?"): the agent passesdry_run: true, the gateway runs the statement, reportsrows_affectedand rolls it back. See Headless agents § Write semantics — the rules are the same for humans.
When something requires a reason
Some grants require a reason string (typical for prod). The gateway refuses the call with reason_required; Claude Code has no special prompt for it — the agent reads the error and asks you in chat, then retries the call with reason: "..." set. That reason ends up in the audit log. Saying the reason up front ("…for SUPPORT-4421") saves the round trip.
The agent's question looks something like:
The gateway requires a reason for this query against prod.
What should I tell it? (e.g. "investigating ticket SUPPORT-4421")
Just type the reason. Be specific — future you, on an audit review, will thank present you.
Troubleshooting
Quick reference. The deeper explanations + corporate-proxy / popup-blocker / reason_required cases live in first-query.md → Gotchas.
| Symptom | Likely cause |
|---|---|
401 on every call, login loops | Browser callback got blocked, or your session was revoked. /mcp → Authenticate again. |
forbidden on a query that used to work | You were removed from a group, or the group's grant was changed. Permissions are reviewed in git — check the gateway's config repo. |
timeout on run_query | Query was slow. Ask the agent to explain first, or narrow it. Operator-set; not bypassable. |
truncated: true | Result was clipped. Add a tighter WHERE/LIMIT, or aggregate. |
Gateway tools missing from /mcp | The URL is wrong (most often: missing the /mcp path — the bare origin is a 404), the gateway is down, or your token expired. Check the URL, then /mcp → Reconnect. |
500/503 instead of 401 | Gateway-side failure (often its state DB). Not a sign-in problem — retry; tell the operator if it persists. |
TLS / cert error instead of 401 | Corporate proxy is intercepting. See first-query.md → Corporate proxy / VPN. |
Removing the server
claude mcp remove db-gateway
Pre-configured OAuth (rare)
The gateway serves OAuth Dynamic Client Registration (POST /register) itself, so Claude Code registers on its own and you should not need this. It exists for a deployment whose proxy blocks /register: your platform team then gives you a client-id and a callback port. Then:
claude mcp add --transport http \
--client-id <your-client-id> --client-secret --callback-port 8080 \
db-gateway https://db.internal.acme.com/mcp
--client-secret prompts you to paste the secret with masked input; it's stored in your system keychain, not in any config file.
See also
- First-query walkthrough — 5-minute happy path + the full gotchas list
- Other MCP-aware agents — Cursor, Continue, etc.
- Headless agents — service tokens, run attribution, read/write tiers
- Permissions model — what
forbiddenactually means - MCP tool surface — every tool the gateway exposes