Skip to content
loredotlinkPublic

About

Controlled path for running SQL that centralizes authentication and avoids distributing database secrets

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

dbproxy

Teams often need to let internal agents, tools, or workflows inspect production data without giving each caller direct database network access, long-lived credentials, or broad infrastructure permissions. That usually turns simple read or maintenance tasks into a choice between unsafe shared access and slow manual handoffs through engineers who already have the right environment configured.

This service solves that by putting a narrow MCP interface in front of PostgreSQL: clients authenticate through Google OAuth, the service connects to a fixed database with AWS IAM authentication, and the only exposed capability is execute_sql. The result is a controlled path for running SQL that centralizes authentication, avoids distributing database secrets, and makes it practical to give approved users or tools database access without opening up the database itself.

Google OAuth Setup

  1. In Google Cloud Console, create a Web OAuth client.
  2. Add an authorized redirect URI matching your FastMCP server callback.
  3. For the default local setup, use http://localhost:8000/auth/callback.
  4. If you set BASE_URL, use ${BASE_URL}/auth/callback instead.

The redirect URI must match exactly. http://localhost:8000/auth/callback and http://127.0.0.1:8000/auth/callback are different values, so use the same host everywhere: in Google OAuth settings, BASE_URL, and the MCP client's server URL.

Environment Variables

Create a local .env file or export these in your shell. The server loads them through Pydantic settings, and it also accepts the legacy OAUTH_CLIENT_ID / OAUTH_CLIENT_SECRET names:

GOOGLE_CLIENT_ID=your-google-client-id.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=your-google-client-secret
DATABASE_HOST=mydb.123456789012.us-east-1.rds.amazonaws.com
DATABASE_PORT=5432
DATABASE_NAME=app_db
DATABASE_USER=app_user
BASE_URL=http://localhost:8000
HOST=localhost
HOST_NAME=localhost
PORT=8000
OAUTH_STORAGE_PATH=~/.dbproxy/oauth-proxy

The server always requests the fixed Google scopes openid, email, and profile. FastMCP's OAuth proxy state is stored on the local filesystem at OAUTH_STORAGE_PATH. If you omit it, the server defaults to ~/.dbproxy/oauth-proxy. DATABASE_HOST must be the real RDS or Aurora PostgreSQL endpoint hostname. DATABASE_PORT defaults to 5432. DATABASE_NAME selects the fixed database the tool connects to for every request. DATABASE_USER is the PostgreSQL user that has been configured for IAM authentication. The server uses AWS IAM database authentication for each connection. It generates an auth token with boto3.Session() so credentials come from the implicit default AWS configuration and credential chain. The AWS region must be inferable from DATABASE_HOST. Do not use a custom Route53 alias for DATABASE_HOST; IAM auth tokens must be generated for the actual RDS endpoint hostname. If you omit BASE_URL, the server builds it from HOST_NAME and PORT. HOST controls which interface the HTTP server binds to.

Run

uv run fastmcp run

If you prefer not to rely on .env autoloading, uv run --env-file .env fastmcp run still works too.

This uses the checked-in fastmcp.json to auto-discover the server from the repository root and serves the same HTTP MCP endpoint using the same environment variables.

The MCP endpoint is served at http://localhost:8000/mcp by default, and the Google OAuth callback is handled at http://localhost:8000/auth/callback.

If you prefer 127.0.0.1, set either BASE_URL=http://127.0.0.1:8000 or HOST_NAME=127.0.0.1, and point your MCP client at http://127.0.0.1:8000 so the protected resource URL matches exactly.

Smoke Test With The FastMCP Client

If a third-party MCP client is failing, verify the server with FastMCP's own OAuth-aware client first:

import asyncio

from fastmcp import Client


async def main() -> None:
    async with Client("http://localhost:8000/mcp", auth="oauth") as client:
        result = await client.call_tool(
            "execute_sql",
            {"sql": "select current_database() as database, current_user as user"},
        )
        print(result)


asyncio.run(main())

On first connect this should open a browser, complete Google sign-in, and then return the query result.

For statements that return rows, execute_sql returns a payload like this:

{
  "status": "SELECT 1",
  "rowcount": 1,
  "columns": ["database", "user"],
  "rows": [
    {
      "database": "app_db",
      "user": "app_user"
    }
  ]
}

For statements that do not return rows, it returns the PostgreSQL status message and affected row count.

Troubleshooting

If your server logs look like this repeatedly:

  • GET /.well-known/oauth-protected-resource/mcp 200
  • POST /mcp 401 Unauthorized
  • no requests to /register, /authorize, or /token

then the FastMCP server is up, but the client is not completing the OAuth flow. The most common causes are:

  • The client has stale cached OAuth tokens for this server. Clear the MCP client's stored tokens for this server and reconnect.
  • The client does not support browser-based OAuth for remote HTTP MCP servers.
  • The client is connecting to localhost while the server and Google OAuth app are configured for 127.0.0.1, or vice versa.

This project was validated against FastMCP's OAuth client flow: the server advertises protected-resource metadata, dynamic client registration, and authorization-server metadata correctly, and a compatible client should proceed to /register and then /authorize on first connection.

Included Tools

  • execute_sql: executes the provided SQL string against the configured PostgreSQL database

About

Controlled path for running SQL that centralizes authentication and avoids distributing database secrets

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages