subscribe method
FR-RES-001~003
Implementation
Future<void> subscribe({
required Client client,
required MCPUIRuntime runtime,
required String uri,
String? binding,
String? ownerKey,
}) async {
_logger.debug('Subscribing resource',
{'uri': uri, 'binding': binding, 'ownerKey': ownerKey});
try {
await client.subscribeResource(uri);
} catch (e, st) {
_logger.logError('subscribeResource failed', e, st, {'uri': uri});
throw ResourceSubscriptionException(uri, cause: e);
}
if (ownerKey != null) {
_active.putIfAbsent(ownerKey, () => <String>{}).add(uri);
}
if (binding != null) {
runtime.registerResourceSubscription(uri, binding);
_logger.debug('Registered binding',
{'uri': uri, 'binding': binding});
}
// Initial read (FR-RES-003) — failures are logged but not propagated.
try {
final resource = await client.readResource(uri);
if (resource.contents.isEmpty) return;
final text = resource.contents.first.text;
if (text == null) return;
final decoded = jsonDecode(text);
if (decoded is Map<String, dynamic>) {
decoded.forEach((key, value) {
runtime.stateManager.set(key, value);
});
}
} catch (e) {
_logger.warn('Initial resource read failed', {'uri': uri}, e);
}
}