# SQL Runner

The SQL Runner Tool (CansoSQLRunnerTool) is a tool in Canso toolkit that enables AI agents to execute SQL queries against databases. It delegates the execution of the SQL queries to the [Task Server](https://docs.canso.ai/ai-agents/concepts/task-server), where the queries are executed, and results are returned to the AI Agent.

### Usage

The constructor for the CansoSQLRunnerTool has following parameters:

| Parameter     | Description          | Example            |
| ------------- | -------------------- | ------------------ |
| `db_host`     | Database hostname    | `"db.example.com"` |
| `db_port`     | Database port        | `"5432"`           |
| `db_username` | Database username    | `"db_user"`        |
| `db_password` | Database password    | `"password123"`    |
| `db_name`     | Target database name | `"my_database"`    |

The input parameters for the tool, provided at runtime by the workflow or the agent, include:

| Parameter | Description          | Example                 |
| --------- | -------------------- | ----------------------- |
| `query`   | Query to be executed | `"select * from table"` |

The following code snippet illustrates how the CansoSQLRunnerTool can be integrated with your AI Agent.

```python
from gru.tools import CansoSQLRunnerTool
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import ToolNode

sql_tool = CansoSQLRunnerTool(
    db_host="db.example.com",
    db_port="5432",
    db_username="db_user",
    db_password="password123",
    db_name="my_database"
)

tools = [sql_tool]
tool_node = ToolNode(tools)

model = ChatOpenAI(model="gpt-4o", temperature=0,  max_tokens=None, timeout=None, max_retries=2,)
model = model.bind_tools(tools)

```

## Tool Tips

* Go to [Toolkit](https://docs.canso.ai/ai-agents/toolkit) ⬅️
* See [Kubernetes Job Tool](https://docs.canso.ai/ai-agents/toolkit/broken-reference) ➡️
* Learn about [Task Server](https://docs.canso.ai/ai-agents/concepts/task-server) ➡️
* Explore [Examples](https://github.com/Yugen-ai/gru/blob/main/gru_docs/ai-agents/examples.md) ➡️
