|
| 1 | +# PostgreSQL MCP Agent |
| 2 | + |
| 3 | +This agent uses the PostgreSQL MCP server to interact with PostgreSQL databases. It demonstrates how to: |
| 4 | +- Connect to a PostgreSQL database using MCP (Model Context Protocol) |
| 5 | +- Use `uvx` to run the MCP server without manual installation |
| 6 | +- Pass database credentials securely via environment variables |
| 7 | + |
| 8 | +## Prerequisites |
| 9 | + |
| 10 | +* **PostgreSQL Database**: You need access to a PostgreSQL database with a connection string |
| 11 | +* **uvx**: The agent uses `uvx` (part of the `uv` package manager) to run the MCP server |
| 12 | + |
| 13 | +## Setup Instructions |
| 14 | + |
| 15 | +### 1. Configure Database Connection |
| 16 | + |
| 17 | +Create a `.env` file in the `mcp_postgres_agent` directory: |
| 18 | + |
| 19 | +```bash |
| 20 | +POSTGRES_CONNECTION_STRING=postgresql://user:password@host:port/database |
| 21 | +``` |
| 22 | + |
| 23 | +Example connection string format: |
| 24 | +``` |
| 25 | +postgresql://username:password@localhost:5432/mydb |
| 26 | +postgresql://postgres.xyz:password@aws-region.pooler.supabase.com:5432/postgres |
| 27 | +``` |
| 28 | + |
| 29 | +### 2. Run the Agent |
| 30 | + |
| 31 | +Start the ADK Web UI from the samples directory: |
| 32 | + |
| 33 | +```bash |
| 34 | +adk web |
| 35 | +``` |
| 36 | + |
| 37 | +The agent will automatically: |
| 38 | +- Load the connection string from the `.env` file |
| 39 | +- Use `uvx` to run the `postgres-mcp` server with unrestricted access mode |
| 40 | +- Connect to your PostgreSQL database |
| 41 | + |
| 42 | +### 3. Example Queries |
| 43 | + |
| 44 | +Once the agent is running, try these queries: |
| 45 | + |
| 46 | +* "What tables are in the database?" |
| 47 | +* "Show me the schema for the users table" |
| 48 | +* "Query the first 10 rows from the products table" |
| 49 | +* "What indexes exist on the orders table?" |
| 50 | +* "Create a new table called test_table with columns id and name" |
| 51 | + |
| 52 | +## Configuration Details |
| 53 | + |
| 54 | +The agent uses: |
| 55 | +- **Model**: Gemini 2.0 Flash |
| 56 | +- **MCP Server**: `postgres-mcp` (via `uvx`) |
| 57 | +- **Access Mode**: Unrestricted (allows read/write operations) |
| 58 | +- **Connection**: StdioConnectionParams with 60-second timeout |
| 59 | +- **Environment Variable**: `DATABASE_URI` (mapped from `POSTGRES_CONNECTION_STRING`) |
| 60 | + |
| 61 | +## Troubleshooting |
| 62 | + |
| 63 | +- Ensure your `POSTGRES_CONNECTION_STRING` is correctly formatted |
| 64 | +- Verify database credentials and network access |
| 65 | +- Check that `uv` is installed (`pip install uv` or `brew install uv`) |
0 commit comments