WorkingDirectory icon
Queries icon
Query icon
OutputValues icon
Log icon
McpToolTrigger icon

Ephemeral SQL Sandbox and Query Profiler for AI Agents

Test SQL queries in an ephemeral DuckDB sandbox via Kestra MCP. Let AI agents verify syntax, row output, and query plans safely before touching production.

Categories
AIData

When AI coding agents (Claude Code, Cursor, Windsurf) assist developers with database queries, backend migrations, or analytics models, they frequently output untested SQL. LLMs can misjudge join conditions, create unintended Cartesian products, or hallucinate non-existent SQL functions. Granting agents live credentials to development or production databases poses serious data privacy and security risks, while preventing them from testing leaves developers to debug broken SQL manually.

This blueprint exposes an ephemeral SQL sandbox and profiler tool over Kestra's native Model Context Protocol (MCP) server. When an agent proposes an analytical query, it invokes test_sql_in_sandbox. Kestra spawns an isolated DuckDB database inside a temporary working directory, seeds baseline customer and order dimensional schemas, captures the EXPLAIN execution plan, and executes the query. The verified row count, query plan, and result preview are returned directly to the agent over MCP, allowing the model to self-correct before code is committed.

How it works

  1. An AI agent discovers test_sql_in_sandbox exposed on Kestra's built-in MCP server via mcp_sql_sandbox (io.kestra.plugin.core.trigger.McpToolTrigger).
  2. sandbox_workspace (io.kestra.plugin.core.flow.WorkingDirectory) creates an isolated filesystem boundary for the execution.
  3. initialize_sandbox_schema (io.kestra.plugin.jdbc.duckdb.Queries) provisions an in-memory DuckDB database (sandbox.duckdb) and populates baseline tables (customers, orders).
  4. explain_query_plan (io.kestra.plugin.jdbc.duckdb.Query) runs EXPLAIN on the agent's proposed SQL to capture scan types and join algorithms when capture_explain_plan is enabled.
  5. execute_sandbox_query (io.kestra.plugin.jdbc.duckdb.Query) runs the query, enforcing execution within the sandbox.
  6. format_agent_response (io.kestra.plugin.core.output.OutputValues) extracts row counts, applies safety slicing using max_rows, and outputs structured JSON.
  7. outputs returns the execution status, row count, query plan, and result records back to the agent in the MCP tool response.

What you get

  • A safe, read-only SQL evaluation environment for coding agents with zero risk of mutating real databases.
  • Automated detection of syntax errors, broken group-by clauses, and invalid aggregations before pull requests are opened.
  • Transparent query plan inspection allowing agents to verify index usage and join efficiency.
  • Zero infrastructure footprint: DuckDB runs embedded inside Kestra without requiring external database containers.

Who it's for

  • Platform Engineers building governed MCP tool catalogs for AI coding agents.
  • Data Engineers looking to let AI assistants test SQL transformations against sample schemas.
  • Software Developers using Cursor or Claude Code who want fast SQL verification without local database setups.

Why orchestrate this with Kestra

Kestra provides native MCP server support, declarative task isolation, and embedded database plugins out of the box. Instead of building custom Python web servers with MCP wrappers and managing ephemeral SQLite or DuckDB lifecycle hooks manually, Kestra handles execution tracking, input validation, working directory cleanup, and tool discovery in a single declarative YAML file.

Pitfalls

  • Dialect Differences: DuckDB implements standard ANSI SQL and PostgreSQL-compatible syntax. If your production warehouse uses proprietary functions (e.g., Snowflake-specific or Oracle-specific built-ins), ensure the agent targets standard ANSI SQL or loads custom mock scalar functions into the initialization step.
  • Result Set Size: Returning millions of rows over MCP can exhaust an LLM's context window. The blueprint enforces max_rows slicing (default 25) to protect agent memory limits while returning total row_count.

Prerequisites

  • A running Kestra 2.0+ instance with the default MCP server enabled.
  • An MCP-compatible client (Claude Desktop, Claude Code, Cursor, or Windsurf).

Secrets

  • No secrets required. The sandbox runs entirely in-memory with embedded DuckDB.

Quick start

  1. Import this blueprint into your Kestra instance.
  2. Connect your MCP client to Kestra's MCP endpoint (e.g., http://localhost:8080/api/v1/main/mcp/default).
  3. In your coding assistant (Claude Code or Cursor), ask: "Run a query in the SQL sandbox to find top customers by total order spend."
  4. The assistant invokes test_sql_in_sandbox and receives the verified row output and execution plan.

How to extend

  • Swap initialize_sandbox_schema to ingest real sanitized schema DDL or Parquet files from an S3 bucket or namespace files.
  • Add custom DuckDB extensions (e.g., httpfs, spatial, or json) to support testing complex analytical functions.
  • Add a threshold assertion that fails the task if execution time exceeds a latency budget, flagging unoptimized queries back to the agent.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.