# TsgcAIAgent

Name: AI Agent

- Product: sgcAI (https://www.esegece.com/products/sgcai/)
- Family: Chat & agents
- Page: https://www.esegece.com/products/sgcai/components/ai-agent/

Runs the tool loop of a TsgcAIChat on every provider: offers local tools, methods added with AddMethod and the tools of MCP clients, executes the calls, asks approval for tools that change data, and stops on the answer, MaxSteps, the token limit, Cancel or an error.

## Members

- Key properties: `Chat`, `Tools`, `MCPClients`, `Options`, `Running`, `StopReason`, `StepCount`, `Version`
- Key methods: `Run`, `Cancel`, `Tools.Add`, `Tools.AddMethod`
- Key events: `OnStep`, `OnToolCall`, `OnToolApproval`, `OnUsage`

## Usage

```pascal
uses
  sgcAI, sgcAI_Chat, sgcAI_Agent;

procedure TForm1.AskAgent;
var
  oTool: TsgcAIAgentTool;
  vAnswer: string;
begin
  sgcAIChat1.Provider := aicpAnthropic;
  sgcAIChat1.ChatOptions.ApiKey := 'sk-ant-...';
  sgcAIChat1.ChatOptions.Model := 'claude-opus-5-5';

  sgcAIAgent1.Chat := sgcAIChat1;
  // optional: the tools of an MCP server are offered too
  sgcAIAgent1.MCPClients.Add(sgcWSAPIClient_MCP1);
  oTool := sgcAIAgent1.Tools.Add('get_weather', 'Current weather of a city');
  oTool.AddParam('city', aatpString, 'City name');
  oTool.ReadOnly := True;
  oTool.OnExecute := DoGetWeather;
  sgcAIAgent1.Options.MaxSteps := 8;
  sgcAIAgent1.OnToolApproval := DoToolApproval;

  vAnswer := sgcAIAgent1.Run('Do I need an umbrella in Paris today?');
  Memo1.Lines.Add(vAnswer);
end;

procedure TForm1.DoGetWeather(Sender: TObject; const aArguments: string;
  var aResult: string);
begin
  // aArguments is a JSON object, for example {"city":"Paris"}
  aResult := '{"city":"Paris","celsius":18,"sky":"rain"}';
end;

procedure TForm1.DoToolApproval(Sender: TObject; const aToolName,
  aArguments: string; var aApproved: Boolean);
begin
  // tools that are not ReadOnly ask before they run
  aApproved := MessageDlg('Run ' + aToolName + ' ' + aArguments + '?',
    mtConfirmation, [mbYes, mbNo], 0) = mrYes;
end;
```

---
Generated by tools/llms_components.py from products/sgcai/catalog.json.
