postBatch method
Posts a JSON-RPC batch through the active MCP session.
Implementation
Future<List<McpJsonMap>?> postBatch(
List<McpJsonMap> messages, {
bool streamable = true,
bool includeSession = true,
String? protocolVersion,
Map<String, String> headers = const <String, String>{},
}) async {
_validateJsonRpcBatchRequestIds(messages);
final effectiveProtocolVersion = _validatedMcpProtocolVersion(
protocolVersion ?? this.protocolVersion,
'protocolVersion',
);
if (effectiveProtocolVersion == latestProtocolVersion) {
throw const McpStreamableProtocolException(
'MCP 2026 HTTP does not support JSON-RPC batches',
);
}
final response = await _postPayload(
messages,
streamable: streamable,
includeSession: includeSession,
protocolVersion: effectiveProtocolVersion,
extraHeaders: headers,
);
if (response == null) {
return null;
}
if (response is! List) {
throw FormatException('JSON-RPC batch response must be an array');
}
return [
for (final item in response)
_jsonMapFrom(item, label: 'JSON-RPC batch response item'),
];
}