Skip to content

Commit 1b77335

Browse files
committed
mcp-agent-for-postgres
1 parent 33fd712 commit 1b77335

3 files changed

Lines changed: 140 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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`)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from . import agent
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
from dotenv import load_dotenv
18+
from google.adk.models.gemma_llm import Gemma
19+
from google.genai.types import GenerateContentConfig
20+
21+
from google.adk.agents.llm_agent import LlmAgent
22+
from google.adk.tools.mcp_tool import StdioConnectionParams
23+
from google.adk.tools.mcp_tool.mcp_toolset import MCPToolset
24+
from mcp import StdioServerParameters
25+
26+
load_dotenv()
27+
28+
POSTGRES_CONNECTION_STRING = os.getenv("POSTGRES_CONNECTION_STRING")
29+
30+
root_agent = LlmAgent(
31+
model="gemini-2.0-flash",
32+
name="postgres_agent",
33+
instruction=(
34+
"You are a PostgreSQL database assistant. "
35+
"Use the provided tools to query, manage, and interact with "
36+
"the PostgreSQL database. Ask clarifying questions when unsure."
37+
),
38+
39+
tools=[
40+
MCPToolset(
41+
connection_params=StdioConnectionParams(
42+
server_params=StdioServerParameters(
43+
command='uvx',
44+
args=[
45+
'postgres-mcp',
46+
'--access-mode=unrestricted'
47+
],
48+
env={
49+
"DATABASE_URI": POSTGRES_CONNECTION_STRING
50+
}
51+
),
52+
timeout=60,
53+
),
54+
)
55+
],
56+
generate_content_config=GenerateContentConfig(
57+
temperature=1.0,
58+
top_p=0.95,
59+
)
60+
)

0 commit comments

Comments
 (0)