Tools from a Git Repository¶
An agent can call a Python function that lives in a Git repository instead of in an installed app.
Nautobot clones the repository, this app imports the tools in it, and each one becomes an
AI Tool record of kind git.
Nautobot uses the same mechanism for Git-sourced Jobs, with the same directory convention, the same private import, and the same trust boundary.
WARNING: Code in the repository runs inside Nautobot, with the permissions of the process that runs it. Only give this content type to a repository you control. A user who may add a Git repository can already add a Job the same way. This content type has the same trust as a Job.
The repository layout¶
my-tools-repo/
├── __init__.py # required, and empty is fine
└── ai_tools.py # or an ai_tools/ package with its own __init__.py
The root __init__.py is required. Without it, Python does not treat the clone as a package and
finds nothing in it.
Declare a tool¶
# ai_tools.py
from nautobot_ai_models.tools import register_ai_tool
@register_ai_tool(writable=False)
def site_device_count(site: str) -> str:
"""Count the devices at one site. Send the site name."""
from nautobot.dcim.models import Device
return f"{Device.objects.filter(location__name=site).count()} devices at {site}"
The name comes from the function. The description comes from the docstring, and the model reads it to decide whether to call the tool. The argument schema comes from the type hints.
Pass writable=True when a call to the function changes something. There is no default.
Get the user¶
To show only the objects that the user can view, pass takes_user=True. Add a keyword-only
parameter user:
@register_ai_tool(writable=False, takes_user=True)
def site_device_count(site: str, *, user) -> str:
"""Count the devices at one site that you can view. Send the site name."""
from nautobot.dcim.models import Device
devices = Device.objects.restrict(user, "view").filter(location__name=site)
return f"{devices.count()} devices at {site}"
The tool gets the user of the agent build. The model does not see user and cannot send it. See
AI Tool.
Add the repository¶
- Go to Extensibility > Git Repositories and add a repository.
- Tick AI tools under Provides.
- Save. Nautobot clones the repository and runs the sync.
Each sync reports what it did in the Job Result, under the grouping ai tools.
What a sync does¶
| The repository | The record |
|---|---|
| Declares a new tool | A record arrives, enabled or disabled as new_tools_enabled says |
| Reworded a description | The digest changes, and the record is disabled if disable_on_definition_change is on |
| Dropped a tool | The record is reported and kept |
| Stopped providing AI tools | Every record from it is disabled and kept |
| Is deleted | Its records go with it |
A sync never deletes a record. Its name can still be on an approved call, and an agent that is bound to the record protects it. You cannot delete a repository whose tools are in use.
Two things worth knowing¶
Every process imports the code it needs, when it needs it. Each process has its own registry.
The worker that ran the sync holds the tools of the repository, and a different worker holds
nothing. So the agent builder imports the repository on the first miss, from the repository that the
record names. A record names a repository and the fixed ai_tools module in it. It never names an
import path.
Turning a tool on is a separate decision. Set new_tools_enabled to False, and a synced tool
arrives disabled. A person then reads what the repository declares, sets writable from what the
code does, and enables the tool. The setting exists for that review.