callToolAsTask method

Future<Task> callToolAsTask(
  1. String name,
  2. Map<String, dynamic> toolArguments, {
  3. int? ttl,
  4. Map<String, dynamic>? meta,
  5. RequestOptions? options,
})

Call name as a 2025-11-25 task: the answer is the task, the result comes from taskResult. Only where the server accepts tool tasks and the tool allows them (tools/list is read when the tool is unknown).

Implementation

Future<Task> callToolAsTask(String name, Map<String, dynamic> toolArguments,
    {int? ttl, Map<String, dynamic>? meta, RequestOptions? options}) async {
  if (_statelessMode) {
    throw McpError('In 2026-07-28 the server decides which calls become tasks; use callTool');
  }
  if (!_serverToolTasks) {
    throw McpError('The server does not accept task-augmented tool calls');
  }
  if (!_toolTaskSupport.containsKey(name)) await listTools();
  final support = _toolTaskSupport[name] ?? 'forbidden';
  if (support == 'forbidden') throw McpError('Tool "$name" does not run as a task');
  final response = await _sendRequest('tools/call', _withMeta({
    'name': name,
    'arguments': Map<String, dynamic>.from(toolArguments),
    'task': {if (ttl != null) 'ttl': ttl},
  }, meta), options);
  final created = Map<String, dynamic>.from(response as Map);
  if (created['task'] is! Map) throw McpError('The server did not answer with a task');
  return taskOf(created);
}