readResource method

Future<ReadResourceResult> readResource(
  1. String uri, [
  2. Map<String, dynamic>? params
])

Read a resource by URI through its registered handler — the same lookup resources/read uses (exact URI, then RFC 6570 templates that have a handler). A template registered without a handler has nothing to read. params reach the handler as the request params would.

Implementation

Future<ReadResourceResult> readResource(
  String uri, [
  Map<String, dynamic>? params,
]) async {
  for (final entry in _resources.entries) {
    if (entry.key == uri ||
        _uriMatches(uri, entry.key, _templateResources[entry.key])) {
      final handler = _resourceHandlers[entry.key];
      if (handler == null) break;
      return _asResult(
        await handler(uri, params ?? {}),
        ReadResourceResult.fromJson,
      );
    }
  }
  throw McpError(
    'Resource not found: $uri',
    code: ErrorCode.resourceNotFound,
  );
}