tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Agentic AI > MCP Protocol > MCP Server for Database Access

MCP Server for Database Access

Author: Venkata Sudhakar

Putting database access logic inside an ADK agent creates tight coupling - every agent that needs data must duplicate the connection and query code. A better pattern is to encapsulate all database access inside an MCP server. The server owns the connection pool, enforces parameterised queries to prevent SQL injection, and exposes clean tool interfaces. Any ADK agent connects to this server and gets database capabilities without knowing any SQL internals.

FastMCP makes it straightforward to wrap SQLite or PostgreSQL queries as MCP tools. Each tool accepts typed parameters, runs a safe parameterised query, and returns structured results. The MCP server runs as a subprocess started by MCPToolset, so it shares the same process lifetime as the agent session.

The below example shows a ShopMax India MCP server exposing order database queries to an ADK agent.


Now connect an ADK agent to query the database via MCP,


It gives the following output,

Q: What orders do we have in Mumbai?
A: ShopMax India has 1 order from Mumbai:
- ORD-001: Priya ordered a Laptop for Rs 45,000. Status: Delivered.

Q: Give me a revenue summary by order status.
A: Here is the revenue summary:
- Delivered: 3 orders, total Rs 98,000
- Shipped: 1 order, total Rs 35,000
- Processing: 1 order, total Rs 45,000

The MCP server enforces parameterised queries throughout, making SQL injection impossible regardless of what the agent passes. In production, replace the in-memory SQLite database with a PostgreSQL connection using psycopg2 or asyncpg. Use a connection pool (e.g. psycopg2.pool.ThreadedConnectionPool) so the MCP server can handle concurrent tool calls from multiple agent sessions. Add read-only database credentials to the MCP server so agents cannot accidentally mutate data through the query tools.


 
  


  
bl  br