Skip to content

Commit 45a2168

Browse files
Vrajesh-Babucopybara-github
authored andcommitted
chore: Adds a new sample agent that demonstrates how to integrate PostgreSQL databases using the Model Context Protocol (MCP)
## What's Added - **PostgreSQL MCP Agent** ([mcp_postgres_agent/agent.py](cci:7://file:///Users/admin/git%20repos/adk-python/contributing/samples/mcp_postgres_agent/agent.py:0:0-0:0)): A fully functional agent that connects to PostgreSQL databases via the `postgres-mcp` MCP server - **Comprehensive README** ([mcp_postgres_agent/README.md](cci:7://file:///Users/admin/git%20repos/adk-python/contributing/samples/mcp_postgres_agent/README.md:0:0-0:0)): Documentation with setup instructions, configuration details, and example queries - **Environment Configuration**: Support for secure credential management via `.env` files ## Key Features - **MCP Integration**: Demonstrates proper use of `MCPToolset` with `StdioConnectionParams` - **Zero Installation**: Uses `uvx` to run the MCP server without manual installation - **Secure Credentials**: Database connection strings passed via environment variables - **Production-Ready**: Uses Gemini 2.0 Flash with unrestricted access mode for full database operations ## Technical Details - **Model**: Gemini 2.0 Flash - **MCP Server**: `postgres-mcp` (via `uvx`) - **Connection**: StdioConnectionParams with 60-second timeout - **Environment Variable**: Maps `POSTGRES_CONNECTION_STRING` to `DATABASE_URI` ## Testing The agent has been tested with: - PostgreSQL database connections (local and remote) - Schema inspection queries - Data querying operations - Table listing and management ## Example Queries Users can interact with the agent using natural language queries like: - "What tables are in the database?" - "Show me the schema for the users table" - "Query the first 10 rows from the products table" This sample serves as a reference implementation for developers looking to integrate PostgreSQL databases with ADK agents using MCP. COPYBARA_INTEGRATE_REVIEW=#3182 from Vrajesh-Babu:postgres-mcp f3b3846 PiperOrigin-RevId: 822865931
1 parent 9cf7ab1 commit 45a2168

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed
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). **Warning**: Using unrestricted mode in a production environment can pose significant security risks. It is recommended to use a more restrictive access mode or configure database user permissions appropriately for production use.
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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.agents.llm_agent import LlmAgent
19+
from google.adk.tools.mcp_tool import StdioConnectionParams
20+
from google.adk.tools.mcp_tool.mcp_toolset import MCPToolset
21+
from google.genai.types import GenerateContentConfig
22+
from mcp import StdioServerParameters
23+
24+
load_dotenv()
25+
26+
POSTGRES_CONNECTION_STRING = os.getenv("POSTGRES_CONNECTION_STRING")
27+
if not POSTGRES_CONNECTION_STRING:
28+
raise ValueError(
29+
"POSTGRES_CONNECTION_STRING environment variable not set. "
30+
"Please create a .env file with this variable."
31+
)
32+
33+
root_agent = LlmAgent(
34+
model="gemini-2.0-flash",
35+
name="postgres_agent",
36+
instruction=(
37+
"You are a PostgreSQL database assistant. "
38+
"Use the provided tools to query, manage, and interact with "
39+
"the PostgreSQL database. Ask clarifying questions when unsure."
40+
),
41+
tools=[
42+
MCPToolset(
43+
connection_params=StdioConnectionParams(
44+
server_params=StdioServerParameters(
45+
command="uvx",
46+
args=["postgres-mcp", "--access-mode=unrestricted"],
47+
env={"DATABASE_URI": POSTGRES_CONNECTION_STRING},
48+
),
49+
timeout=60,
50+
),
51+
)
52+
],
53+
generate_content_config=GenerateContentConfig(
54+
temperature=0.2,
55+
top_p=0.95,
56+
),
57+
)

0 commit comments

Comments
 (0)