Supabase MCP agent

Using the Supabase MCP server to create an Agent that can create projects, database schemas, edge functions, and more:

1"""🔑 Supabase MCP Agent - Showcase Supabase MCP Capabilities
2
3This example demonstrates how to use the Supabase MCP server to create projects, database schemas, edge functions, and more.
4
5Setup:
61. Install Python dependencies: `uv pip install kern-ai mcp-sdk`
72. Create a Supabase Access Token: https://supabase.com/dashboard/account/tokens and set it as the SUPABASE_ACCESS_TOKEN environment variable.
8"""
9
10import asyncio
11import os
12from textwrap import dedent
13
14from kern.agent import Agent
15from kern.models.openai import OpenAIResponses
16from kern.tools.mcp import MCPTools
17from kern.tools.reasoning import ReasoningTools
18from kern.utils.log import log_error, log_exception, log_info
19
20
21async def run_agent(task: str) -> None:
22 token = os.getenv("SUPABASE_ACCESS_TOKEN")
23 if not token:
24 log_error("SUPABASE_ACCESS_TOKEN environment variable not set.")
25 return
26
27 npx_cmd = "npx.cmd" if os.name == "nt" else "npx"
28
29 try:
30 async with MCPTools(
31 f"{npx_cmd} -y @supabase/mcp-server-supabase@latest --access-token={token}"
32 ) as mcp:
33 instructions = dedent(f"""
34 You are an expert Supabase MCP architect. Given the project description:
35 {task}
36
37 Automatically perform the following steps :
38 1. Plan the entire database schema based on the project description.
39 2. Call `list_organizations` and select the first organization in the response.
40 3. Use `get_cost(type='project')` to estimate project creation cost and mention the cost in your response.
41 4. Create a new Supabase project with `create_project`, passing the confirmed cost ID.
42 5. Poll project status with `get_project` until the status is `ACTIVE_HEALTHY`.
43 6. Analyze the project requirements and propose a complete, normalized SQL schema (tables, columns, data types, indexes, constraints, triggers, and functions) as DDL statements.
44 7. Apply the schema using `apply_migration`, naming the migration `initial_schema`.
45 8. Validate the deployed schema via `list_tables` and `list_extensions`.
46 8. Deploy a simple health-check edge function with `deploy_edge_function`.
47 9. Retrieve and print the project URL (`get_project_url`) and anon key (`get_anon_key`).
48 """)
49 agent = Agent(
50 model=OpenAIResponses(id="gpt-5.2"),
51 instructions=instructions,
52 tools=[mcp, ReasoningTools(add_instructions=True)],
53 markdown=True,
54 )
55
56 log_info(f"Running Supabase project agent for: {task}")
57 await agent.aprint_response(
58 message=task,
59 stream=True,
60 show_full_reasoning=True,
61 )
62 except Exception as e:
63 log_exception(f"Unexpected error: {e}")
64
65
66if __name__ == "__main__":
67 demo_description = (
68 "Develop a cloud-based SaaS platform with AI-powered task suggestions, calendar syncing, predictive prioritization, "
69 "team collaboration, and project analytics."
70 )
71 asyncio.run(run_agent(demo_description))
72
73
74# Example prompts to try:
75"""
76A SaaS tool that helps businesses automate document processing using AI. Users can upload invoices, contracts, or PDFs and get structured data, smart summaries, and red flag alerts for compliance or anomalies. Ideal for legal teams, accountants, and enterprise back offices.
77
78An AI-enhanced SaaS platform for streamlining the recruitment process. Features include automated candidate screening using NLP, AI interview scheduling, bias detection in job descriptions, and pipeline analytics. Designed for fast-growing startups and mid-sized HR teams.
79
80An internal SaaS tool for HR departments to monitor employee wellbeing. Combines weekly mood check-ins, anonymous feedback, and AI-driven burnout detection models. Integrates with Slack and HR systems to support a healthier workplace culture.
81"""