Skip to content

handle relative paths under roots without trailing slashes #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/dart_mcp_server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ jobs:
channel: ${{ matrix.flutterSdk }}
cache: true
cache-key: "flutter-:os:-:channel:-:version:-:arch:-:hash:"
# Exposes the DART_SDK environment variable to all other actions.
# https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#setting-an-environment-variable
- run: echo "DART_SDK=$(which dart | xargs dirname | xargs dirname)" >> "$GITHUB_ENV"

- name: fetch counter app deps
working-directory: pkgs/dart_mcp_server/test_fixtures/counter_app
Expand Down
20 changes: 12 additions & 8 deletions pkgs/dart_mcp_server/lib/src/utils/cli_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'dart:async';
import 'package:collection/collection.dart';
import 'package:dart_mcp/server.dart';
import 'package:file/file.dart';
import 'package:path/path.dart' as p;
import 'package:process/process.dart';
import 'package:yaml/yaml.dart';

Expand Down Expand Up @@ -164,7 +163,7 @@ Future<CallToolResult> runCommandInRoot(
}

final root = knownRoots.firstWhereOrNull(
(root) => _isUnderRoot(root, rootUriString),
(root) => _isUnderRoot(root, rootUriString, fileSystem),
);
if (root == null) {
return CallToolResult(
Expand Down Expand Up @@ -201,7 +200,9 @@ Future<CallToolResult> runCommandInRoot(
final paths =
(rootConfig?[ParameterNames.paths] as List?)?.cast<String>() ??
defaultPaths;
final invalidPaths = paths.where((path) => !_isUnderRoot(root, path));
final invalidPaths = paths.where(
(path) => !_isUnderRoot(root, path, fileSystem),
);
if (invalidPaths.isNotEmpty) {
return CallToolResult(
content: [
Expand Down Expand Up @@ -268,8 +269,10 @@ Future<String> defaultCommandForRoot(
/// Returns whether [uri] is under or exactly equal to [root].
///
/// Relative uris will always be under [root] unless they escape it with `../`.
bool _isUnderRoot(Root root, String uri) {
final rootUri = Uri.parse(root.uri);
bool _isUnderRoot(Root root, String uri, FileSystem fileSystem) {
// This normalizes the URI to ensure it is treated as a directory (for example
// ensures it ends with a trailing slash).
final rootUri = fileSystem.directory(Uri.parse(root.uri)).uri;
final resolvedUri = rootUri.resolve(uri);
// We don't care about queries or fragments, but the scheme/authority must
// match.
Expand All @@ -279,10 +282,11 @@ bool _isUnderRoot(Root root, String uri) {
}
// Canonicalizing the paths handles any `../` segments and also deals with
// trailing slashes versus no trailing slashes.
final canonicalRootPath = p.canonicalize(rootUri.path);
final canonicalUriPath = p.canonicalize(resolvedUri.path);

final canonicalRootPath = fileSystem.path.canonicalize(rootUri.path);
final canonicalUriPath = fileSystem.path.canonicalize(resolvedUri.path);
return canonicalRootPath == canonicalUriPath ||
canonicalUriPath.startsWith(canonicalRootPath);
fileSystem.path.isWithin(canonicalRootPath, canonicalUriPath);
}

/// The schema for the `roots` parameter for any tool that accepts it.
Expand Down
91 changes: 52 additions & 39 deletions pkgs/dart_mcp_server/test/utils/cli_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,45 +139,58 @@ void main() {
});

test('with paths inside of known roots', () async {
final paths = ['file:///foo/', 'file:///foo', './', '.'];
final result = await runCommandInRoots(
CallToolRequest(
name: 'foo',
arguments: {
ParameterNames.roots: [
{ParameterNames.root: 'file:///foo', ParameterNames.paths: paths},
{
ParameterNames.root: 'file:///foo/',
ParameterNames.paths: paths,
},
],
},
),
commandForRoot: (_, _, _) => 'fake',
commandDescription: '',
processManager: processManager,
knownRoots: [Root(uri: 'file:///foo/')],
fileSystem: fileSystem,
sdk: Sdk(),
);
expect(
result.isError,
isNot(true),
reason: result.content.map((c) => (c as TextContent).text).join('\n'),
);
expect(
processManager.commandsRan,
unorderedEquals([
equalsCommand((
command: ['fake', ...paths],
workingDirectory: '/foo/',
)),
equalsCommand((
command: ['fake', ...paths],
workingDirectory: '/foo',
)),
]),
);
// Check with registered roots that do and do not have trailing slashes.
for (final knownRoot in ['file:///foo', 'file:///foo/']) {
processManager.reset();
final paths = [
'file:///foo/',
'file:///foo',
'./',
'.',
'lib/foo.dart',
];
final result = await runCommandInRoots(
CallToolRequest(
name: 'foo',
arguments: {
ParameterNames.roots: [
{
ParameterNames.root: 'file:///foo',
ParameterNames.paths: paths,
},
{
ParameterNames.root: 'file:///foo/',
ParameterNames.paths: paths,
},
],
},
),
commandForRoot: (_, _, _) => 'fake',
commandDescription: '',
processManager: processManager,
knownRoots: [Root(uri: knownRoot)],
fileSystem: fileSystem,
sdk: Sdk(),
);
expect(
result.isError,
isNot(true),
reason: result.content.map((c) => (c as TextContent).text).join('\n'),
);
expect(
processManager.commandsRan,
unorderedEquals([
equalsCommand((
command: ['fake', ...paths],
workingDirectory: '/foo/',
)),
equalsCommand((
command: ['fake', ...paths],
workingDirectory: '/foo',
)),
]),
);
}
});
});

Expand Down