TsgcWSAPIServer_MCP › Methods › CreateTask

CreateTask Method

Turns the current tools/call into a background task of the MCP tasks extension.

Syntax

function CreateTask(const aSession: TsgcAI_MCP_Session; const aResponse: TsgcAI_MCP_Response): TsgcAI_MCP_Task;

Parameters

NameTypeDescription
aSessionTsgcAI_MCP_SessionSession of the current request, as received in OnMCPRequestTool.
aResponseTsgcAI_MCP_ResponseResponse of the current request. When a task is created, the response is sent as a task handle (resultType task).

Return Value

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.

Remarks

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.

Example

oTask := MCPServer.CreateTask(aSession, aResponse);
if Assigned(oTask) then
  StartBackgroundJob(oTask) // calls oTask.Complete(...) when done
else
  aResponse.Result.Content.AddText(RunJobNow);

Back to Methods