load method
FR-APP-ONLINE-001~004 — Raw JSON form used internally by
loadOnline and by legacy callers.
resources lets a caller pass a resource list it has already fetched
(e.g. for MCP Serving bundle-document detection) so the server is listed
only once; when null the list is fetched here.
Implementation
Future<Map<String, dynamic>> load(
Client client, {
List<Resource>? resources,
}) async {
final List<Resource> resolved;
if (resources != null) {
resolved = resources;
} else {
try {
resolved = await client.listResources();
} catch (e, st) {
throw _wrapLoad('listResources failed', e, st);
}
}
_logger.debug('Resources listed', {'count': resolved.length});
final appUri = _pickAppUri(resolved);
if (appUri == null) {
throw ResourceNotFoundException('No UI resources found');
}
_logger.info('Loading application', {'uri': appUri});
final ReadResourceResult resource;
try {
resource = await client.readResource(appUri);
} catch (e, st) {
throw _wrapLoad('readResource failed', e, st);
}
if (resource.contents.isEmpty) {
throw DefinitionParseException(appUri);
}
final text = resource.contents.first.text;
if (text == null) {
throw DefinitionParseException(appUri);
}
try {
final decoded = jsonDecode(text);
if (decoded is! Map<String, dynamic>) {
throw DefinitionParseException(appUri);
}
return decoded;
} on DefinitionParseException {
rethrow;
} catch (e) {
throw DefinitionParseException(appUri, cause: e);
}
}