Skip to main content

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.com below 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's MCP_PATH); the bare origin answers 404.

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:

ScopeWhen to useStored in
--scope local (default)One-off / per-project~/.claude.json (your home)
--scope userWant it in every project on your machine~/.claude.json
--scope projectWant 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 a WWW-Authenticate header) means your credential is missing, expired or revoked. Claude Code re-runs the sign-in; that is the fix.
  • 5xx (500/503, no WWW-Authenticate) means the gateway itself could not check your credential — typically its state database is briefly unavailable (503 with code state_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:

  1. describe_schema on prod / billing
  2. write a query against invoices joining events
  3. call run_query
  4. 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" adds analyze: true for reads).
  • Table health — "how big is orders and 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_write on that database. Ask for a dry run first ("how many rows would this update touch?"): the agent passes dry_run: true, the gateway runs the statement, reports rows_affected and 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.

SymptomLikely cause
401 on every call, login loopsBrowser callback got blocked, or your session was revoked. /mcp → Authenticate again.
forbidden on a query that used to workYou 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_queryQuery was slow. Ask the agent to explain first, or narrow it. Operator-set; not bypassable.
truncated: trueResult was clipped. Add a tighter WHERE/LIMIT, or aggregate.
Gateway tools missing from /mcpThe 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 401Gateway-side failure (often its state DB). Not a sign-in problem — retry; tell the operator if it persists.
TLS / cert error instead of 401Corporate 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​