TsgcWSAPIServer_MCP › Methods › CreateTask
Turns the current tools/call into a background task of the MCP tasks extension.
function CreateTask(const aSession: TsgcAI_MCP_Session; const aResponse: TsgcAI_MCP_Response): TsgcAI_MCP_Task;
| Name | Type | Description |
|---|---|---|
aSession | TsgcAI_MCP_Session | Session of the current request, as received in OnMCPRequestTool. |
aResponse | TsgcAI_MCP_Response | Response of the current request. When a task is created, the response is sent as a task handle (resultType task). |
The new TsgcAI_MCP_Task, or nil when no task can be created: MCPOptions.Tasks.Enabled is False, the client is a legacy client, the request did not declare the io.modelcontextprotocol/tasks extension, the request is not tools/call, the tool has TaskSupport aimcptsForbidden, or MCPOptions.Tasks.MaxTasks has been reached.
Call it inside OnMCPRequestTool. When it returns a task, start the work in a background thread, exit the handler and finish the task later with Complete or Fail. The task object is owned by the server, do not free it. When it returns nil, fill the response synchronously as usual. See Tasks.
oTask := MCPServer.CreateTask(aSession, aResponse);
if Assigned(oTask) then
StartBackgroundJob(oTask) // calls oTask.Complete(...) when done
else
aResponse.Result.Content.AddText(RunJobNow);