Querying Kafka with SQL from an AI agent
DEV Community

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_sql. It takes one SQL query, the same query the SQL Query page runs, and returns the answer. The counting happens in DuckDB, 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

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 Code:

claude mcp add --transport http alginte http://localhost:8888/mcp

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.

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 topic:

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

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 records. 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

  • Querying a Kafka topic with SQL, from the console: the SQL Query page, a topic joined with a file and with another topic.
  • Querying a Kafka Streams state store with SQL: a running app's state, read and joined with topics.
  • run_sql is the MCP tool for SQL. The MCP docs describe its parameters and its answer.
Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.