redshift_query
Execute SQL queries on Amazon Redshift databases to retrieve data in JSON format for analysis and reporting purposes.
Instructions
Execute a SQL query on Redshift and return results as JSON.
Args:
sql: The SQL query to execute
Returns:
JSON string of the query results or error message
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sql | Yes |
Implementation Reference
- redshift_mcp_server.py:60-75 (handler)The main handler function for the 'redshift_query' tool. It executes the provided SQL query on a Redshift (or local Postgres) database using pandas.read_sql and returns the results as a formatted JSON string. Includes error handling.def redshift_query(sql: str) -> str: """ Execute a SQL query on Redshift and return results as JSON. Args: sql: The SQL query to execute Returns: JSON string of the query results or error message """ try: with get_connection() as conn: df = pd.read_sql(sql, conn) return df.to_json(orient="records", indent=2) except Exception as e: return f"Error executing query: {str(e)}"
- redshift_mcp_server.py:32-56 (helper)Supporting helper function used by redshift_query to establish a database connection, supporting both Redshift and local Postgres for testing.def get_connection(): """Create a connection to Redshift or local Postgres.""" try: # If host is localhost and port is 5432, assume local Postgres for testing if REDSHIFT_HOST == "localhost" and REDSHIFT_PORT == 5432: import psycopg2 return psycopg2.connect( host=REDSHIFT_HOST, port=REDSHIFT_PORT, database=REDSHIFT_DATABASE, user=REDSHIFT_USER, password=REDSHIFT_PASSWORD ) else: return redshift_connector.connect( host=REDSHIFT_HOST, port=REDSHIFT_PORT, database=REDSHIFT_DATABASE, user=REDSHIFT_USER, password=REDSHIFT_PASSWORD ) except Exception as e: logger.error(f"Connection error: {e}") raise
- redshift_mcp_server.py:60-60 (registration)The @mcp.tool() decorator registers the redshift_query function as an MCP tool.def redshift_query(sql: str) -> str: