DEV Community

Tejaswa Hinduja
Tejaswa Hinduja

Posted on

How to Build an agent using coral

How I Built an AI Agent That Queries GitHub, Calendar, and Sleep Data With One SQL Interface

A practical guide to Coral — the local-first SQL runtime that gives your agents a single query layer over any API


I spent the better part of a week building an agent that could answer questions like "why am I not productive lately?" The idea was simple: pull data from GitHub, Google Calendar, and sleep tracking, cross-reference everything, and get an AI answer.

The hard part turned out to be the data plumbing. Every source has its own auth, its own pagination, its own quirks. By the time I had GitHub and Calendar talking to the same script, I had written more glue code than actual agent logic.

Then I found Coral.

This post walks through what Coral is, why it matters for agent development, and how to build a working AI agent with it from scratch.


The problem with how agents access data today

Most agent workflows look like this:

Agent → Tool A → API A
Agent → Tool B → API B
Agent → Tool C → API C
Enter fullscreen mode Exit fullscreen mode

Each tool call is isolated. The agent has to reason about three separate schemas, handle three different auth flows, and then try to mentally join the results. This creates:

  • Too many LLM round-trips — each tool call is its own context window exchange
  • Repeated glue code — pagination, retry logic, rate limiting, per source
  • Poor cross-source reasoning — the LLM sees results one at a time, not as a unified dataset
  • High token costs — raw API responses are verbose JSON, not trimmed result sets

The benchmark from the Coral team actually quantified this: across 82 real-world tasks, Claude was 20% more accurate and 2x more cost efficient using Coral versus direct provider MCPs. For complex multi-hop queries (the kind coding agents actually do), accuracy jumped 31% and cost dropped 3.4x.

The reason is architectural. Coral gives agents one SQL interface over everything.


What is Coral?

Coral is a local-first SQL runtime that sits between your agent and your data sources. You write SQL, Coral translates it into API calls (handling auth, pagination, and retries), and returns clean tabular rows.

Your Agent → SQL → Coral → GitHub API
                         → Google Calendar API  
                         → Local JSONL files
                   ↓
              One result set
Enter fullscreen mode Exit fullscreen mode

The key properties:

Local-first. Everything runs on your machine. Credentials are stored locally and never leave. Coral is a read layer — it makes API calls on your behalf but doesn't send your data anywhere.

Sources as SQL schemas. When you connect GitHub, it appears as a github schema. You query github.commits, github.issues, github.pulls just like database tables. Same pattern for every source.

Joins across sources. Because every source looks like a SQL table, you can JOIN across them. Coral executes the join locally after fetching each side. That single query replaces three separate tool calls and the LLM reasoning needed to combine them.

MCP server built-in. Coral ships with an MCP server so you can wire it directly into Claude Code, Cursor, or any MCP-compatible agent with one command.


Installing Coral

macOS:

brew install withcoral/tap/coral
Enter fullscreen mode Exit fullscreen mode

Linux:

curl -fsSL https://withcoral.com/install.sh | sh
Enter fullscreen mode Exit fullscreen mode

Windows (10/11 x86_64):
Download coral-x86_64-pc-windows-msvc.zip from the latest GitHub release, extract it, and add coral.exe to your PATH.

Verify the install:

coral --version
Enter fullscreen mode Exit fullscreen mode

Connecting your first source

Coral ships with bundled sources for GitHub, Google Calendar, Linear, Sentry, Datadog, Stripe, Slack, and more. See everything available:

coral source discover
Enter fullscreen mode Exit fullscreen mode

Let's connect GitHub. You'll need a Personal Access Token with repo and read:user scopes:

coral source add --interactive github
# Coral will prompt: Enter your GitHub PAT
Enter fullscreen mode Exit fullscreen mode

Once connected, verify it works:

coral sql "SELECT schema_name, table_name FROM coral.tables WHERE schema_name = 'github'"
Enter fullscreen mode Exit fullscreen mode

You should see rows for github.commits, github.issues, github.pulls, github.user, and others.

Try a real query:

coral sql "
  SELECT CAST(commit__author__date AS DATE) AS date, COUNT(*) AS commits
  FROM github.commits
  WHERE owner = 'your-username'
    AND commit__author__date >= NOW() - INTERVAL '7 days'
  GROUP BY 1
  ORDER BY 1 DESC
"
Enter fullscreen mode Exit fullscreen mode

Coral flattens nested JSON with __ as the separator. So commit.author.date in the GitHub API response becomes commit__author__date in your SQL. Run SELECT column_name FROM coral.columns WHERE schema_name = 'github' AND table_name = 'commits' to inspect the exact column names for any table.


Building the agent

We'll build a Node.js agent that answers natural language questions about your GitHub activity. Ask it "what have I been shipping this week?" and it queries your real commit data and returns an AI-generated summary.

Project setup

mkdir my-coral-agent && cd my-coral-agent
npm init -y
npm install @google/genai dotenv
Enter fullscreen mode Exit fullscreen mode

Create a .env file:

GEMINI_API_KEY=your_key_here
GITHUB_USERNAME=your_github_username
Enter fullscreen mode Exit fullscreen mode

The agent — agent.ts

The full agent is around 80 lines. Here's how it works:

Step 1: Query Coral for real data

import { execFileSync } from 'child_process'

function coralQuery<T>(sql: string): T[] {
  const stdout = execFileSync('coral', ['sql', '--format', 'json', sql.trim()], {
    encoding: 'utf-8',
    timeout: 30_000,
    stdio: ['pipe', 'pipe', 'ignore'],  // suppress Coral's stderr
  })
  return JSON.parse(stdout) as T[]
}
Enter fullscreen mode Exit fullscreen mode

We call Coral's CLI and ask for JSON output. execFileSync blocks until Coral finishes — Coral handles all the GitHub API calls, pagination, and auth internally.

Step 2: Gather the data

function getGithubActivity(username: string, days: number) {
  // Commits per day
  const commits = coralQuery<{ date: string; repos: string; count: number }>(`
    SELECT
      CAST(commit__author__date AS DATE)   AS date,
      ARRAY_AGG(DISTINCT repo)             AS repos,
      COUNT(*)                             AS count
    FROM github.commits
    WHERE owner = '${username}'
      AND commit__author__date >= NOW() - INTERVAL '${days} days'
    GROUP BY 1
    ORDER BY 1 DESC
  `)

  // Open PRs
  const prs = coralQuery<{ title: string; state: string; created_at: string }>(`
    SELECT title, state, created_at
    FROM github.pulls
    WHERE owner = '${username}'
      AND created_at >= NOW() - INTERVAL '${days} days'
    ORDER BY created_at DESC
    LIMIT 20
  `)

  return { commits, prs }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Ask Gemini

import { GoogleGenAI } from '@google/genai'

async function ask(question: string, data: object): Promise<string> {
  const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY! })

  const response = await ai.models.generateContent({
    model: 'gemini-2.5-flash',
    config: {
      systemInstruction: `You are a developer productivity assistant. 
        You have access to real GitHub data. Be direct and specific. 
        Reference actual repos and dates from the data.`,
    },
    contents: `
      Developer question: "${question}"

      GitHub data (last 7 days):
      ${JSON.stringify(data, null, 2)}

      Answer the question using this data.
    `,
  })

  return response.text ?? ''
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Wire it together

import dotenv from 'dotenv'
dotenv.config()

async function main() {
  const question = process.argv.slice(2).join(' ')
  if (!question) {
    console.log('Usage: npx tsx agent.ts "what have I been working on?"')
    process.exit(1)
  }

  const username = process.env.GITHUB_USERNAME!
  console.log('Querying GitHub via Coral...')

  let data: object
  try {
    data = getGithubActivity(username, 7)
  } catch (err) {
    console.error('Coral query failed:', err)
    process.exit(1)
  }

  console.log('Analyzing...\n')
  const answer = await ask(question, data)
  console.log(answer)
}

main()
Enter fullscreen mode Exit fullscreen mode

Run it

npx tsx agent.ts "what have I been shipping this week?"
npx tsx agent.ts "which repo am I spending the most time on?"
npx tsx agent.ts "have I been less active than usual?"
Enter fullscreen mode Exit fullscreen mode

The agent queries your live GitHub data, hands it to Gemini, and returns a natural language answer grounded in actual commit history.


Going further: cross-source queries

The real power of Coral is joins across sources. Add a second source:

coral source add --interactive google_calendar
Enter fullscreen mode Exit fullscreen mode

Now you can ask questions that span both:

-- Commits on days with heavy meeting load
SELECT
  CAST(c.commit__author__date AS DATE) AS date,
  COUNT(DISTINCT c.sha)                AS commits,
  COUNT(DISTINCT e.id)                 AS meetings
FROM github.commits c
LEFT JOIN google_calendar.events e
  ON CAST(c.commit__author__date AS DATE) = CAST(e.start_date_time AS DATE)
WHERE c.owner = 'your-username'
  AND c.commit__author__date >= NOW() - INTERVAL '30 days'
GROUP BY 1
ORDER BY 1 DESC
Enter fullscreen mode Exit fullscreen mode

This is a query that would normally require two separate API calls, result storage, and manual joining in your agent code. With Coral, it's one SQL statement.


Using Coral over MCP

If you're using Claude Code, Cursor, or any MCP-compatible agent, you can expose Coral as an MCP server — no custom code required:

# Claude Code
claude mcp add --scope user coral -- coral mcp-stdio
Enter fullscreen mode Exit fullscreen mode

After this, your agent can query all connected sources directly using natural language. Ask it to "show me open GitHub issues assigned to me" and it will write the SQL and run it through Coral automatically.

You can also install Coral's skills to teach your agent the discovery-first workflow:

npx skills add withcoral/skills
Enter fullscreen mode Exit fullscreen mode

This gives your agent knowledge of coral.tables, coral.columns, and the metadata tables it needs to explore schemas before writing queries.


What I actually built

The agent I described at the start — the one that cross-references GitHub, Calendar, and sleep data to answer "why am I not productive?" — uses exactly this pattern.

The behavioral metrics engine computes:

  • Sleep decline vs baseline
  • Late-night coding session frequency
  • Meeting load increase as a percentage over your own baseline
  • Context switching (repos per day, live from GitHub commits)
  • Interruption count trends

All of it feeds into Gemini as a structured prompt. The result looks like this in the terminal:

  BURNOUT RISK  ─────────────────────────────────

  ▘▀▀▀▀▀▀▀▀▀▀▀▝
  ▌  75/100    ▐
  ▌    HIGH    ▐
  ▗▄▄▄███████▄▖

  ────────────────────────────────────────────────

  TL;DR
  ────────────────────────────────────────
  Sleep dropped 38% while meetings tripled — you're
  compensating with late-night coding and it's not working.

  KEY FINDINGS
  ────────────────────────────────────────
  ◆ Average sleep fell from 8.1h to 5.0h over 30 days
  ◆ Meetings increased from 1.5/day to 5.2/day (+180%)
  ◆ 6 late-night coding sessions in the last 7 days vs 0 in baseline
  ◆ Context switching across 4 repos/day vs 1 repo/day baseline
  ◆ Productivity score: 36/100 (down from 79 baseline)
Enter fullscreen mode Exit fullscreen mode

The full source is on GitHub. The key file is src/coral/sources/github.ts which shows exactly how to wrap Coral queries in a TypeScript function and handle the column naming conventions.


Summary

Coral is one of those tools that removes a whole category of boilerplate. Instead of writing per-source auth, pagination, and retry logic, you write SQL. Instead of your agent reasoning about three separate tool outputs, it reasons about one result set.

The setup is:

# 1. Install
brew install withcoral/tap/coral   # or curl on Linux, zip on Windows

# 2. Connect a source  
coral source add --interactive github

# 3. Query
coral sql "SELECT repo, COUNT(*) FROM github.commits WHERE owner = 'you' GROUP BY 1"

# 4. Use in your agent
execFileSync('coral', ['sql', '--format', 'json', query])
Enter fullscreen mode Exit fullscreen mode

That's really it. The complexity sits inside Coral, and your agent code stays clean.


If you build something with Coral, I'd like to see it. The xetroc source is at github.com/TejaswaHinduja/xetroc if you want a reference implementation to work from.

Top comments (0)