addRoot method

void addRoot(
  1. Root root
)

Add a root to the client's local list. When the client declared rootsListChanged, the server is told with notifications/roots/list_changed (not on 2026-07-28, which has no such notification).

A root is a file:// URI (client/roots) with no .. segments, so a server cannot be pointed outside it.

Implementation

void addRoot(Root root) {
  final uri = Uri.tryParse(root.uri);
  // Dart normalizes dot segments away, so the raw text is checked.
  final traversal = RegExp(r'(^|/)(\.\.|%2[eE]%2[eE])(/|$)').hasMatch(root.uri);
  if (uri == null || uri.scheme != 'file' || traversal) {
    throw ArgumentError.value(root.uri, 'root.uri', 'a root must be a file:// URI without ".." segments');
  }
  if (_roots.any((r) => r.uri == root.uri)) {
    throw McpError('Root with URI "${root.uri}" already exists');
  }
  _roots.add(root);
  _notifyRootsChanged();
}