I have a custom AI Agent with tools that you can read about in this post. I started with a few basic tools (web, search and weather) but was thinking about a command line tool from the start and the possibility to even build its own tools. So when the AI Agent felt mature, i decided to explore the idea further.
Windows Command Tool
The first step was to add a Windows command line tool that the AI Agent can invoke. With it, the AI can trigger all command line tools that exist in Windows 11. Essentially, it can control most parts of the computer and do operations on the OS, file system and network.
This also includes running dotnet commands with PowerShell to build and run .NET programs.
The command tool is implemented in the manager app and specified to the AI Agent in the system prompt that the manager app sends to it.
Since tasks like creating and running a program are done in multiple steps, the command tool is invoked with a JSON structure that has a collection of commands and arguments. They are executed by the manager app which then returns the console output for each command to the AI Agent.
If Needed, Build a New Tool
I then added to the system prompt that if the AI Agent needs a simple .NET 8 program to complete, or simplify, its current task, it can build and run it with the command tool.
Task that Triggers the AI Agent to Create and Use a new Tool
The AI Agent uses the most straightforward method of solving the task. So it may use PowerShell without creating a program first if that is possible. Like “Give me 10 unique Guids”. The following task however triggers the AI Agent to create a .net 8 program and run it, to solve the task, since it can not do it internally, via web search or scripting.
Task Prompt Sent to the AI Agent
Use the Runge-Kutta 4th order method to solve the following differential equation: dy/dx = x + y, with initial condition y(0) = 1, and find the value of y(1) with a step size of 0.1.
First Response From the AI Agent
The AI Agent sends a collection of commands to create a .Net 8 program, insert code into it and run it.
{
"toolInstruction": {
"commands": [
{
"command": "dotnet",
"arguments": "new console -n RungeKuttaSolver -f net8.0",
"result": ""
},
{
"command": "powershell.exe",
"arguments": "-NoProfile -ExecutionPolicy Bypass -Command \"Set-Content -Path RungeKuttaSolver\\Program.cs -Value @'\nusing System;\n\nclass Program\n{\n static double f(double x, double y)\n {\n return x + y;\n }\n\n static double RungeKutta4(double x0, double y0, double x, double h)\n {\n int n = (int)((x - x0) / h);\n double k1, k2, k3, k4;\n\n double y = y0;\n for (int i = 0; i < n; i++)\n {\n k1 = h * f(x0, y);\n k2 = h * f(x0 + 0.5 * h, y + 0.5 * k1);\n k3 = h * f(x0 + 0.5 * h, y + 0.5 * k2);\n k4 = h * f(x0 + h, y + k3);\n\n y += (k1 + 2 * k2 + 2 * k3 + k4) / 6;\n\n x0 += h;\n }\n\n return y;\n }\n\n static void Main()\n {\n double x0 = 0;\n double y0 = 1;\n double x = 1;\n double h = 0.1;\n\n double result = RungeKutta4(x0, y0, x, h);\n Console.WriteLine(\\\"The value of y at x = 1 is: \\\", result);\n }\n}\n'@\"",
"result": ""
},
{
"command": "dotnet",
"arguments": "run --project RungeKuttaSolver",
"result": ""
}
]
}
}
Result Sent Back to the AI Agent
The program was created and executed. But it did not output a result. So it has a bug that the AI Agent has to manage. The result for each command in the “result” property.
{
"commands": [
{
"command": "dotnet",
"arguments": "new console -n RungeKuttaSolver -f net8.0",
"result": "The template \u0022Console App\u0022 was created successfully.\r\n\r\nProcessing post-creation actions...\r\nRestoring C:\\Projects\\AIAgent\\genTools\\RungeKuttaSolver\\RungeKuttaSolver.csproj:\r\n Determining projects to restore...\r\n Restored C:\\Projects\\AIAgent\\genTools\\RungeKuttaSolver\\RungeKuttaSolver.csproj (in 106 ms).\r\nRestore succeeded.\r\n\r\n\r\n"
},
{
"command": "powershell.exe",
"arguments": "-NoProfile -ExecutionPolicy Bypass -Command \u0022Set-Content -Path RungeKuttaSolver\\Program.cs -Value @\u0027\nusing System;\n\nclass Program\n{\n static double f(double x, double y)\n {\n return x \u002B y;\n }\n\n static double RungeKutta4(double x0, double y0, double x, double h)\n {\n int n = (int)((x - x0) / h);\n double k1, k2, k3, k4;\n\n double y = y0;\n for (int i = 0; i \u003C n; i\u002B\u002B)\n {\n k1 = h * f(x0, y);\n k2 = h * f(x0 \u002B 0.5 * h, y \u002B 0.5 * k1);\n k3 = h * f(x0 \u002B 0.5 * h, y \u002B 0.5 * k2);\n k4 = h * f(x0 \u002B h, y \u002B k3);\n\n y \u002B= (k1 \u002B 2 * k2 \u002B 2 * k3 \u002B k4) / 6;\n\n x0 \u002B= h;\n }\n\n return y;\n }\n\n static void Main()\n {\n double x0 = 0;\n double y0 = 1;\n double x = 1;\n double h = 0.1;\n\n double result = RungeKutta4(x0, y0, x, h);\n Console.WriteLine(\\\u0022The value of y at x = 1 is: \\\u0022, result);\n }\n}\n\u0027@\u0022",
"result": ""
},
{
"command": "dotnet",
"arguments": "run --project RungeKuttaSolver",
"result": "The value of y at x = 1 is: \r\n"
}
]
}
Second Response From the AI Agent
The AI Agent noticed that the program did not return a valid result and updates the code. Then executes the program again.
{
"toolInstruction": {
"commands": [
{
"command": "powershell.exe",
"arguments": "-NoProfile -ExecutionPolicy Bypass -Command \"Set-Content -Path RungeKuttaSolver\\Program.cs -Value @'\nusing System;\n\nclass Program\n{\n static double f(double x, double y)\n {\n return x + y;\n }\n\n static double RungeKutta4(double x0, double y0, double x, double h)\n {\n int n = (int)((x - x0) / h);\n double k1, k2, k3, k4;\n\n double y = y0;\n for (int i = 0; i < n; i++)\n {\n k1 = h * f(x0, y);\n k2 = h * f(x0 + 0.5 * h, y + 0.5 * k1);\n k3 = h * f(x0 + 0.5 * h, y + 0.5 * k2);\n k4 = h * f(x0 + h, y + k3);\n\n y += (k1 + 2 * k2 + 2 * k3 + k4) / 6;\n\n x0 += h;\n }\n\n return y;\n }\n\n static void Main()\n {\n double x0 = 0;\n double y0 = 1;\n double x = 1;\n double h = 0.1;\n\n double result = RungeKutta4(x0, y0, x, h);\n Console.WriteLine(\\\"The value of y at x = 1 is: {0}\\\", result);\n }\n}\n'@\"",
"result": ""
},
{
"command": "dotnet",
"arguments": "run --project RungeKuttaSolver",
"result": ""
}
]
}
}
Result Sent Back to the AI Agent
This time the program works and outputs a valid response. The result is sent to the AI Agent. I have removed the code that is visible above.
{
"commands": [
{
"command": "powershell.exe",
"arguments": "-NoProfile -ExecutionPolicy Bypass -Command \u0022Set-Content -Path RungeKuttaSolver\\Program.cs -Value CODE REMOVED TO SHORTEN EXAMPLE",
"result": ""
},
{
"command": "dotnet",
"arguments": "run --project RungeKuttaSolver",
"result": "The value of y at x = 1 is: 3,436559488270332\r\n"
}
]
}
Third Response From the AI Agent
The AI Agent is content with the output of the program and returns a task result.
{
"taskResult" :"Using the Runge-Kutta 4th order method to solve the differential equation dy/dx = x + y with the initial condition y(0) = 1 and a step size of 0.1, the value of y at x = 1 is approximately 3.4366."
}
Add Tool to Toolbox
If the AI Agent has built a tool I want to add to its permanent toolbox, I simply add it to the system prompt so the AI Agent knows of it when a new task starts. Otherwise I remove it from disk after the current task is done. The adding to the system prompt could be automated, but I want to review it for now.
Functionality Accessible to the Command Tool
Instructions to the console could be a range of other things like opening and modifying files, running network commands like tracert and more.
The AI Agent has the flexibility to choose the target shell where the commands will be executed, as long as it exists on the Windows 11 computer that the AI Agent manager app with tools runs on.
This includes:
PowerShell scripting offers advanced scripting capabilities, access to .NET libraries, and the ability to manage system processes and configurations.
WMI provides a way to interact with and manage Windows system components. It can be used to query system information, manage services, and automate administrative tasks.
The .NET CLI (dotnet) allows for creating, building, and running .NET applications directly from the command line. It supports various operations, including project management, NuGet package management, and application deployment.
Git provides version control capabilities, enabling the management of repositories, tracking changes, collaborating with others, and automating workflows using tools like Git Bash or Windows Terminal.
Using the schtasks command in CMD or PowerShell allows for the creation, management, and automation of scheduled tasks.
Windows CLI offers a suite of networking tools like ping, tracert, ipconfig, netstat, and nslookup.
Commands like robocopy for robust file copying, xcopy for extended file copy, and dir for directory listing provide options for managing files and directories.
The Windows Package Manager (winget) facilitates searching, installing, updating, and managing software packages from the command line, similar to package managers on Linux.
Tools like reg for registry editing, gpupdate for updating group policy, and sfc (System File Checker) for repairing system files provide deep system management capabilities.
WSL allows for running a full Linux environment directly on Windows. This feature enables the execution of Linux commands, running scripts, and using Linux-based development tools seamlessly alongside Windows applications.
AI Agent Autonomy
The AI Agent has full autonomy in deciding which tools to invoke in which order and how many times. Combined with its own knowledge and functionality. If it can answer without tools, it does that. If it needs the predefined tools, it uses them and if it needs new ones, it builds and uses them.