execute_sql
Execute SQL queries on PostgreSQL databases to retrieve, modify, or analyze data directly through the MCP server interface.
Instructions
Execute any SQL query
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "execute_sql"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sql | No | SQL to run | all |
Implementation Reference
- src/postgres_mcp/server.py:390-402 (handler)The implementation of the `execute_sql` tool which executes SQL against the database driver.
async def execute_sql( sql: str = Field(description="SQL to run", default="all"), ) -> ResponseType: """Executes a SQL query against the database.""" try: sql_driver = await get_sql_driver() rows = await sql_driver.execute_query(sql) # type: ignore if rows is None: return format_text_response("No results") return format_text_response(list([r.cells for r in rows])) except Exception as e: logger.error(f"Error executing query: {e}") return format_error_response(str(e)) - src/postgres_mcp/server.py:550-553 (registration)Registration of the `execute_sql` tool within the MCP server, with different descriptions depending on the access mode.
if current_access_mode == AccessMode.UNRESTRICTED: mcp.add_tool(execute_sql, description="Execute any SQL query") else: mcp.add_tool(execute_sql, description="Execute a read-only SQL query")