Hello,
We get an Access Violation whenever a request with Tools is answered by OpenAI with a function call.
How to reproduce
TMS AI Studio demo: Language Models\Weather Lookup
Select OpenAI as the service
Ask: What is the weather for London?
This raises an Access Violation.
Cause
In TMS.MCP.CloudAI.pas, procedure TTMSMCPCloudAI.DoOpenAIRequest, inside the function_call branch: AResponse is created, and after reading the usage object it is released in the finally block with AResponse.Free, leaving a dangling pointer.
At the end of the same procedure the object is released again.
if not lToolReq then
DoExecuted(AResponse, ARequestResult.ResponseCode, ARequestResult.ResultString)
else
begin
if Assigned(AResponse) then
AResponse.Free; // <-- AResponse is already freed here
DecBusy;
end;
Assigned() does not protect anything because the variable still holds the old (already freed) address.
Workaround we are using
Replacing AResponse.Free with FreeAndNil(AResponse) in that finally block fixes it: the second release is then correctly skipped by Assigned().
Best regards,