TTMSMCPClient is the host side of the Model Context Protocol: it holds a connection to a language model and a collection of connections to MCP servers, and routes the tool calls between them. You pick a service and a model, add one or more servers — a local process over STDIO, or a remote endpoint over SSE or streamable HTTP — and call Execute with a question. The client discovers each server's tools, advertises them to the model, executes the ones the model asks for against the server that owns them, and returns the finished answer on a single event. It also ships a ready-made settings dialog for editing servers and API keys, and a tools dialog for inspecting what a connection exposes.
procedure TForm1.FormCreate(Sender: TObject);
var
Server: TTMSMCPClientServerItem;
begin
{ MCPClient is a TTMSMCPClient dropped on the form. }
MCPClient.Service := aiOpenAI;
MCPClient.APIKeys.OpenAI := GetEnvironmentVariable('OPENAI_API_KEY');
MCPClient.Settings.OpenAIModel := 'gpt-4o-mini';
MCPClient.OnExecuted := DoExecuted;
{ Let every discovered tool run without asking first. }
MCPClient.ToolCallMode := tcmAllow;
Server := MCPClient.Servers.Add;
Server.DisplayName := 'Reports';
Server.TransportType := ttSTDIO;
Server.Command := 'my-mcp-server.exe';
Server.Args.Add('--root');
Server.Args.Add('C:\Reports');
Server.OnGetToolsList := DoServerToolsReady;
{ Start returns before the handshake finishes, so leave the button
disabled until the server has answered tools/list. }
AskButton.Enabled := False;
Server.Start;
end;
procedure TForm1.DoServerToolsReady(Sender: TObject);
begin
{ MCPClient.Tools now holds the tools the model may call. }
AskButton.Enabled := True;
end;
procedure TForm1.AskButtonClick(Sender: TObject);
begin
MCPClient.Execute('Summarise the newest report in the reports folder.');
end;
procedure TForm1.DoExecuted(Sender: TObject; AResponse: string;
AHttpStatusCode: Integer; AHttpResult: string);
begin
if AResponse <> '' then
ShowAnswer(AResponse)
else
ShowAnswer(Format('The request failed (HTTP %d): %s',
[AHttpStatusCode, AHttpResult]));
end;
The server collection, the three client transports and their platforms, JSON configuration files, tool discovery and routing, approving tool calls, elicitation, and logging.