AI Agent

TsgcAIAgent makes your Delphi 7 to 13 application agent-ready. Give it a TsgcAIChat and your tools, call Run, and it sends the prompt with the tools, runs every tool call the model asks for, sends the results back and returns the final answer.

TsgcAIAgent

The tool loop of an AI model, done for you. Local tools, methods of your own objects and the tools of your MCP servers are offered to the model, every call is checked, approved when needed and executed, and the loop stops on the answer, a step limit, the token limit, a cancel or an error.

Component class

TsgcAIAgent

Purpose

Tool calling agent for every LLM provider

Family

Chat & agents

Platforms

Windows, macOS, Linux, iOS and Android

Editions. TsgcAIAgent ships in sgcAI, in sgcWebSockets Enterprise and in All-Access, the same editions as TsgcAIChat. New in 2026.11.0. sgcAI is self-contained: it includes the sgcWebSockets Core runtime the component is built on. Available for Delphi 7 to Delphi 13 and C++ Builder.

Let the model use your tools

Assign the chat, add a tool with its parameters and handler, call Run. Tools that change data ask for approval first.

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;

Key properties & methods

The members you reach for most often.

Run and Cancel

Run(aPrompt) sends the prompt with the tools, executes the calls of every step and returns the final answer. Cancel stops a run before the next model request or tool. Running, StepCount and StopReason tell how it ended: aasrCompleted, aasrMaxSteps, aasrCancelled, aasrError or aasrMaxTokens. aasrMaxTokens means that an answer was cut by the token limit, and the tool calls of a cut answer are not run because their arguments may be incomplete.

Tools

Tools.Add(Name, Description) returns a tool. Describe its arguments with AddParam (string, integer, number, boolean, array or object) or with a raw JSON Schema in InputSchema, and answer it in OnExecute. Tools.AddMethod turns a public method of an object into a tool on Delphi 2010 and later. A tool without a handler reaches OnToolCall.

MCP tools

Add TsgcWSAPIClient_MCP clients to MCPClients. With Options.ImportMCPTools the agent initializes each client, lists its tools page by page and calls them through CallTool. A tool blocked by MCP tool pinning is never offered, and a local tool with the same name wins.

Approval

A tool marked ReadOnly runs at once. Any other tool asks OnToolApproval first. A denied or unknown tool goes back to the model as an error text, so the conversation history always stays valid. Assign a TsgcAIGuardrails to the chat to allow, deny or require approval by tool name.

Options

Options.MaxSteps (10 by default) caps the model requests of one run. The tools of the last allowed request still run and their results are stored, so a later Run continues with real results. Options.ImportMCPTools switches the MCP import on or off.

Events

OnStep reports every step: model text, tool call, tool result, denied tool and final answer. OnUsage reports the tokens of every model request of the run. Assign a TsgcAIUsageMeter to the chat to price them.

Every provider

The agent works with every provider of TsgcAIChat: OpenAI, Anthropic, Gemini, DeepSeek, Ollama, Grok and Mistral, each in its own tool calling format. With chat fallbacks, a run continues on the next provider when one fails.

Specifications & references

Authoritative sources for the services and protocols this component uses.

Keep exploring

Online HelpFull API reference and usage guide for the sgcAI components.
All sgcAI ComponentsBrowse the feature matrix of every sgcAI component.
Download Free TrialTry it with your own API keys.
PricingSingle, Team and Site licenses, full source code.
Best value: All-AccessEvery eSeGeCe product, Premium Support included, from €1,059/year.
See All-Access pricing

Ready to Get Started?

Download the free trial and let a language model call the functions of your Delphi application.