DEV Community

alginte software for alginte

Posted on Originally published at alginte.com on AI-assisted

Querying Kafka with SQL from an AI agent

Give an AI agent a tool that reads Kafka records, and it can look at what a topic holds. That works when the answer is in a few records. A question about totals needs every record in the topic. "Which customer spent the most?" means adding up each customer's orders and ranking the totals. With only a records tool, the agent has to pull every record into its own context and do that work itself. Fifty thousand records would not fit in its context, and a model is not a reliable calculator.

In short

Alginte has a better place for that work. It serves its tools through the Model Context Protocol, and one of them is run_sql1. It takes one SQL query, the same query the SQL Query page runs, and returns the answer. The counting happens in DuckDB2, before anything reaches the agent. The agent gets ten rows instead of 50,000 records.

Starting the playground

The examples here run on the playground. It is Alginte with a one-node Kafka and a schema registry, seeded with sample topics. It needs Docker, and two commands start it:

curl -O https://raw.githubusercontent.com/alginte/community/main/playground/docker-compose.yml
docker compose up -d
Enter fullscreen mode Exit fullscreen mode

docker compose down removes it all, and the next up -d starts fresh.

Connecting an agent

The MCP endpoint is /mcp. With the playground running, one command connects Claude Code3:

claude mcp add --transport http alginte http://localhost:8888/mcp
Enter fullscreen mode Exit fullscreen mode

One question, a few queries

We connected Claude Code to a fresh playground, with no other MCP servers, and asked one question: "Which customers spent the most on the orders-large topic?" The topic holds 50,000 orders.

It answered with a table of the top ten customers. c-1001 came first at 65,141.32, well ahead of c-2417 at 36,660.03.

Claude Code answers the question about the orders-large topic. It called Alginte 6 times. It says it read all 50,000 orders, which are Avro. There are 300 customers and 2,416,071.42 in revenue. It counted spend as quantity times priceEur, because each item has the same price at every quantity. A table ranks the top ten customers. c-1001 is first with 1,424 orders and 65,141.32. c-2417 is second at 36,660.03, and c-6650 third at 29,223.16. The answer calls c-1001 the clear outlier, at about 1.8 times the next customer.

How the agent got there

First the agent asked how big the topic is: 50,000 records on one partition. Then it asked run_sql for the columns with a DESCRIBE over the newest five records, guessing the values were JSON. The answer's note said the values carry the schema registry's framing for Avro, and to read them with value_format := 'Avro'. Its next DESCRIBE used Avro and got the fields.

The question itself was one query over the whole topic4:

SELECT customerId, count(*) AS orders, sum(quantity) AS units,
       round(sum(quantity * priceEur), 2) AS spend
FROM kafka_topic('orders-large', from_offset := 0, max := 50000,
                 value_format := 'Avro')
GROUP BY ALL
ORDER BY spend DESC
LIMIT 10
Enter fullscreen mode Exit fullscreen mode

DuckDB read the 50,000 orders and returned ten rows, in about a second. Two more queries checked the answer. One showed that each item has one price at every quantity, so priceEur is a price per unit. The other counted 50,000 distinct orders from 300 customers.

In its answer, the agent says it read all 50,000 orders. Its queries read them in DuckDB, and not one record reached the agent. It made six calls and read six short answers.

The same tool reaches further than one topic. A query can join a topic with a file, or with the state of a Kafka Streams app, exactly as on the SQL Query page.

When the range is smaller than the topic

kafka_topic() reads a range, by default the last 1,000 records5. On a big topic, a sum over that range is not the topic's total. An agent that misses this reports the wrong numbers. On orders-large, the last 1,000 orders keep c-1001 first, at 986.38 instead of 65,141.32. c-9751 comes second, at 816.73. Over the whole topic, c-2417 is second, at 36,660.03.

In our run, the agent's first DESCRIBE read only the newest five records. Its answer also said the range did not cover the topic, and how to read it all: from_offset := 0, max := 50000. The agent's counting query then used exactly that range, so its totals cover the whole topic.

The tool's description tells the agent to pass such a note on, or to widen the range before it calls a result a total. The SQL Query page shows the same note.

What the agent gets back

run_sql is read-only. It runs in the same locked-down engine as the SQL Query page. An answer carries 100 rows by default and 1,000 at most, so a model's context stays small. When more rows match, the answer says it was cut.

The answer's notes say what a table function left out: a range smaller than the topic, records the format could not read, or a state store read only in part. The agent learns that the answer is incomplete, and why. A query DuckDB refuses comes back with DuckDB's own message, so the agent can fix it and try again.

run_sql is one of the playground's read-only tools. An agent can look at topics, consumer groups and streams, and now ask them a question.

Recap

  • run_sql is one MCP tool: one read-only SQL query over topics, files and state stores.
  • DuckDB counts and joins, and only the answer reaches the agent.
  • An answer that covers only part of a topic says so, and says how to read it all.
  • A refused query comes back as DuckDB's message, so the agent can correct it.
  • The same locked-down engine as the SQL Query page, with a smaller default answer.

Related reading


  1. run_sql is the MCP tool for SQL. The MCP docs describe its parameters and its answer. ↩

  2. DuckDB comes built into Alginte, so there is nothing to install. DuckDB is a trademark of the DuckDB Foundation. ↩

  3. Claude Code is one MCP client. Other AI apps that speak MCP connect to the same endpoint, as the MCP docs show. ↩

  4. kafka_topic() is a table function that reads one topic as rows. The SQL Query docs list its parameters. ↩

  5. A query chooses another range with last := n, or with from_offset and max, as the SQL Query docs show. One kafka_topic() call reads at most 100,000 records. That limit is the alginte.sql.max-records property, in the configuration docs. ↩

Top comments (0)