Skip to main content
Glama

execute_sql

Run SQL queries directly on the Postgres MCP server to interact with and manage databases efficiently during development, testing, and deployment.

Instructions

Execute any SQL query

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sqlNoSQL to runall

Implementation Reference

  • The main handler function for the 'execute_sql' tool. It executes the provided SQL query using the sql_driver determined by the current access mode, returns formatted results or an error message.
    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))
  • Registers the execute_sql tool dynamically based on the access mode (unrestricted or restricted/read-only), adding it to the MCP server with an appropriate description.
    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")

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/crystaldba/postgres-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server