DEV Community

Cover image for Building an MCP SQL Tool That Lets LLMs Query Live Databases with Wanaku
Otavio Rodolfo Piske
Otavio Rodolfo Piske

Posted on

Building an MCP SQL Tool That Lets LLMs Query Live Databases with Wanaku

AI assistants are great at reasoning, but they have a fundamental blind spot: their answers come from training data that's frozen in time. Ask an AI "what laptops do you have under $1000?" and you'll get a plausible-sounding answer — just not one that reflects what's actually in your inventory right now.

What if the AI could just... check?

That's the problem Wanaku's upcoming sql-tool service template solves. Coming in version 0.2.0, it connects AI assistants to live relational databases through the Model Context Protocol (MCP), letting them query real data instead of guessing from stale knowledge.

Wait, What's Wanaku? What's MCP?

Quick context if you're new here:

  • MCP (Model Context Protocol) is an open protocol that lets AI assistants use external tools, access resources, and interact with services in a standardized way. Think of it as a universal API layer for AI — instead of every AI client needing custom integrations, they can all speak MCP.

  • Wanaku is an open-source MCP router and capability management platform. It acts as the glue between AI clients (like Claude Desktop, Codex, or custom LLM apps) and backend services. You expose capabilities as MCP-compatible tools, and Wanaku handles routing, authentication, and service discovery.

The upcoming 0.2.0 release (PR #1358 pending merge) introduces service templates — pre-packaged capability patterns you can instantiate with just a CLI command. The sql-tool template is the first of these, and it's designed to bridge the gap between AI assistants and relational databases.

Real-Time SQL Querying vs. Vector RAG

Here's a scenario every enterprise developer has seen: you ask an AI assistant about operational data in your system, and it confidently spits out an outdated or completely fabricated metric.

The classic workaround is RAG (Retrieval-Augmented Generation) — you pre-index your data, convert it into vector embeddings, and store it in a vector database for the AI to search. That works beautifully for text documents, wikis, and static datasets.

The RAG Limitation

What about live operational data? Your shifting inventory, current customer orders, or real-time application metrics change too fast for vector syncs. You don't want to pre-index that data — you want the LLM to query it on-demand via SQL.## The Solution: Exposing SQL Queries as MCP Tools

The sql-tool template packages everything a developer needs to securely expose a live SQL database query as an executable MCP tool:

  • An Apache Camel route that executes safe queries against your target data store.
  • An MCP tool definition that compatible AI clients can dynamically discover and call.
  • Pre-packaged runtime dependencies (including JDBC drivers, SQL components, and JSON marshalling).
  • Support for parameterizing AI inputs into dynamic SQL queries via Camel Simple expressions.

You provide the database connection details and the baseline query syntax; the Wanaku gateway handles the rest.


Step-by-Step Guide: Connecting an LLM to PostgreSQL

Here is a preview of how to instantiate the SQL template once 0.2.0 ships. In this example, we have a PostgreSQL database containing a live product catalog, and we want our AI assistant to accurately answer: "What laptops do you have under $800?"

Step 1: Deploy the PostgreSQL Database Container

Spin up a local PostgreSQL instance using Podman (Docker commands work identically here):

podman run --rm --name wanaku-postgres \
  -p 5432:5432 \
  -e POSTGRES_PASSWORD=wanaku \
  postgres
Enter fullscreen mode Exit fullscreen mode

Load your schema and data:

podman exec -i wanaku-postgres psql -U postgres -d postgres < sql-tool-demo.sql
Enter fullscreen mode Exit fullscreen mode

You can download the demo SQL script from the Wanaku website.

Step 2: Instantiate the Wanaku SQL Tool Template

Now, expose your live database to the MCP network by mapping the AI's input to a dynamic SQL query:

wanaku service template instantiate \
  --name sql-tool \
  --property forage.jdbc.username=postgres \
  --property forage.jdbc.password=wanaku \
  --property sql.query='SELECT name, price FROM products WHERE price < ${body} ORDER BY price' \
  --service-name product-catalog \
  --service-system product-catalog
Enter fullscreen mode Exit fullscreen mode

Notice the ${body} placeholder in the query? That's a Camel Simple expression. At runtime, it gets replaced with whatever input the AI sends. So when a user asks "what laptops are under $800?", the AI calls the tool with 800 as input, and the query becomes:

SELECT name, price FROM products WHERE price < 800 ORDER BY price
Enter fullscreen mode Exit fullscreen mode

This is the key insight: the SQL template doesn't run static queries — it accepts dynamic input from the AI, making it a true interactive tool.

Step 3: Verify the Catalog Deployment

Ensure your new service is correctly registered in the local capability catalog:

wanaku service catalog list
Enter fullscreen mode Exit fullscreen mode

You can also confirm your live data store connectivity entries using:

wanaku data-store list --plain
Enter fullscreen mode Exit fullscreen mode

Step 4: Querying Live Data via Your AI Client

Point any MCP-compatible AI client (such as Claude Desktop or Continue.dev) at your local Wanaku router gateway. The LLM will automatically discover the product-catalog schema tool capability and query the database directly.

Output showing the results from the SQL tool

No hallucinations. No stale data lakes. Just direct, deterministic SQL results parsed instantly by the LLM.

How It Works Under the Hood

When an AI assistant invokes the generated database tool, the orchestration pipeline executes across these layers:

  1. Tool Invocation: The AI client sends an MCP payload containing the user's filtered variables.
  2. Routing: Wanaku maps the request to the underlying Apache Camel SQL endpoint.
  3. Parameter Injection: The ${body} runtime expression safely binds the AI's variables to the SQL execution context.
  4. Execution: The statement runs natively against your PostgreSQL deployment.
  5. Serialization: The rows are marshalled into a structured JSON string payload.
  6. Response Generation: The LLM receives the raw JSON and translates it into a natural language response for the user.

The template wires all of this automatically — you just provide the query and connection details.

Parameterizing AI Prompts with Dynamic SQL

Since the sql-tool template is built on the Apache Camel SQL component, you can use any expression the component supports.

Simple value substitution:

SELECT * FROM products WHERE category_id = ${body}
Enter fullscreen mode Exit fullscreen mode

Named parameters with headers:

SELECT * FROM products WHERE price < :#maxPrice AND category_id = :#category
Enter fullscreen mode Exit fullscreen mode

For more advanced patterns, refer to the Camel SQL documentation.

Why This Matters

This demo is small — three tables, ten products — but the pattern scales to real-world use cases:

  • Customer support bots: query order history, account status, or ticket details in real time
  • Internal tools: let AI assistants search employee directories, project databases, or asset inventories
  • Analytics and reporting: expose reporting queries as MCP tools so AI can pull fresh metrics on demand
  • Developer productivity: connect AI code assistants to your test databases for context-aware suggestions

The point isn't the query. It's that the AI is no longer guessing — it's checking.

When Can You Try It?

The sql-tool template is part of the upcoming Wanaku 0.2.0 release, currently pending the merge of PR #1358. Once it ships:

  1. Install Wanaku
  2. Follow the setup steps above to instantiate the template
  3. Connect your MCP-compatible AI client to the Wanaku router

If you want to try it before the official release, you can build Wanaku from the PR branch or use one of the early-builds.

Get Involved

Wanaku is open source, and we'd love your input:

The sql-tool template is just the beginning — we're building a library of reusable capability patterns that make it trivial to connect AI assistants to real systems. If you're excited about that vision, come build with us.

Top comments (0)