callTool method
Calls one MCP tool and returns its JSON-like result payload.
Implementation
Future<Object?> callTool({
required McpConnectionParams connectionParams,
required String toolName,
required Map<String, dynamic> args,
Map<String, String>? headers,
}) async {
final String key = _connectionKey(connectionParams);
final McpToolExecutor? executor = _executorsByUrl[key]?[toolName];
if (executor != null) {
final Object? result = executor(args, headers: headers);
if (result is Future<Object?>) {
return result;
}
if (result is Future) {
return await result;
}
return result;
}
if (connectionParams is StreamableHTTPConnectionParams &&
!_remoteClient.isRemoteCapable(connectionParams)) {
throw ArgumentError('MCP tool `$toolName` is not registered.');
}
await _ensureInitialized(connectionParams, headers: headers);
if (!_hasCapability(connectionParams, 'tools')) {
throw StateError(
'MCP server `${_connectionLabel(connectionParams)}` does not expose `tools` capability.',
);
}
final Object? result = await _call(
connectionParams: connectionParams,
method: 'tools/call',
params: <String, Object?>{'name': toolName, 'arguments': args},
headers: headers,
);
if (result is Map) {
return _asStringObjectMap(result);
}
return result;
}