SQL Tool querys with parameters connected to AI Agent

Describe the problem/error/question

I am making an Agent that can answer questions by querying my database. For security reasons I have only given it access to stored procedures and I have added them as tools to the agent, describing how they work. It works perfectly when I hardcode the query parameters in the tools but I want the AI to fill them in on its own by talking to the user.

All three SQL tools are Execute Query and I need to add the Query to the tool.
Very simply put it just runs EXEC dbo.GetValueByDate @Date =
They all use different queries though so @Date is not a parameters in the other two, they might have 2 or 3 other ones. Like EXEC dbo.GetCoursesByStudentID @StudentId= or like in the picture:

How do I get the input parameters to drag into the query?
Do I always need to have the AI ask about date, even though it doesn’t matter for the other two queries but just to keep it as a parameter in the input?
Do I need another node in between or do I have to use three different AI agents for each query?

Please share your workflow

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 1.120.4
  • Database (default: SQLite): Microsoft SQL Server
  • n8n EXECUTIONS_PROCESS setting (default: own, main): ?
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Windows

This sounds like help with a homework assignment :wink:

Whilst form the AI Agent you can call a node like Microsoft SQL query directly, you do run into the problem of how to specify the parameters and how to tell the AI Agent how to fill in those parameters.

The relevant documentation is AI Tools in n8n. Here it mentions that there are a number of sub-nodes you can call from the AI Agent. The one I think you may want to try is the Call n8n Workflow Tool. This will allow you to do a few things:

  1. Specify some input parameters (like a queryDate and queryTime)
  2. Specify descriptions about each parameter (so it’s a hint to the AI agent on what these should be filled, their format with and whether they are mandatory or optional).
  3. A description of what this workflow does so the agent knows when to call it.

So if I start with just one of your SQL Query nodes, it might look something like this:

:backhand_index_pointing_up: Some things you may need to pay attention to:

  1. The Workflow Inputs I’ve set to be Defined automatically by the model read more here.
  2. Look at your System prompt and make it clear to your model when it should call a particular tool and what it is called.

Then as for the sub-workflow, you’ll need to create another workflow called query_by_date_or_time and it may look somethig like this:

You may even want to add some error-handline logic here so if the workflow is called and the mandatory fields are missing or have the wrong format, you can have the workflow return a useful error message (back to the model, so it can fix its mistake or ask in the chat for more information).

:warning: Please mark this as the solution if you feel your question has been answered :smiling_cat_with_heart_eyes:

1 Like

Thank you!

This did solve my problem!

I had tried the $FromAi() but could not get the keyword to work, even with a lot of prompting but I think the error was that MSQL tool didn’t even have that option.

I am only confused why the SQL query tool doesn’t have the Defined by AI options as it would have saved me the steps of creating 5 new workflows :smiley:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.