TTMSMCPServer turns a Delphi application into a Model Context Protocol server: a JSON-RPC 2.0 endpoint that advertises what the application can do and lets an AI agent call it. The server owns three collections — tools it can execute, resources it can read, and prompts it can supply — and handles the rest of the protocol around them: capability negotiation across four protocol versions, per-session state, change notifications, pagination, sampling, elicitation, long-running tasks, logging, completions, and an extensions framework.
procedure TForm1.FormCreate(Sender: TObject);
begin
Server := TTMSMCPServer.Create(Self);
Server.ServerName := 'InventoryServer';
Server.ServerVersion := '1.0.0';
Server.ServerDescription := 'Read-only access to the warehouse inventory';
Server.Tools.RegisterTool('stock_level',
'Returns the number of units on hand for a product',
function(const Args: array of TValue): TValue
begin
Result := StockLevel(Args[0].AsString);
end,
ptInteger);
{ With no Transport assigned the server uses STDIO. }
Server.Start;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Server.Stop;
end;
// The anonymous method of the Delphi API is an interface in C++Builder.
class TStockLevelMethod : public TCppInterfacedObject<TTMSMCPMethod>
{
public:
TValue __fastcall Invoke(const TValue *Args, const NativeInt Args_High)
{
return TValue::From<int>(StockLevel(TValue(Args[0]).AsString()));
}
};
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Server = new TTMSMCPServer(this);
Server->ServerName = "InventoryServer";
Server->ServerVersion = "1.0.0";
Server->ServerDescription = "Read-only access to the warehouse inventory";
Server->Tools->RegisterTool("stock_level",
"Returns the number of units on hand for a product",
_di_TTMSMCPMethod(new TStockLevelMethod()),
TTMSMCPToolPropertyType::ptInteger);
// With no Transport assigned the server uses STDIO.
Server->Start();
}
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
Server->Stop();
}