Skip to content

Commit 0a85ddf

Browse files
authored
Parse output from flutter run with --machine (#301)
1 parent a29274b commit 0a85ddf

File tree

4 files changed

+304
-83
lines changed

4 files changed

+304
-83
lines changed

pkgs/dart_mcp_server/CHANGELOG.md

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,74 @@
11
# 0.1.1 (Dart SDK 3.10.0) - WIP
22

3-
* Change tools that accept multiple roots to not return immediately on the first
3+
- Change tools that accept multiple roots to not return immediately on the first
44
failure.
5-
* Add failure reason field to analytics events so we can know why tool calls are
5+
- Add failure reason field to analytics events so we can know why tool calls are
66
failing.
7-
* Add a flutter_driver command for executing flutter driver commands on a device.
8-
* Allow for multiple package arguments to `pub add` and `pub remove`.
9-
* Require dart_mcp version 0.3.1.
10-
* Add support for the flutter_driver screenshot command.
11-
* Change the widget tree to the full version instead of the summary. The summary
7+
- Add a flutter_driver command for executing flutter driver commands on a device.
8+
- Allow for multiple package arguments to `pub add` and `pub remove`.
9+
- Require dart_mcp version 0.3.1.
10+
- Add support for the flutter_driver screenshot command.
11+
- Change the widget tree to the full version instead of the summary. The summary
1212
tends to hide nested text widgets which makes it difficult to find widgets
1313
based on their text values.
14-
* Add an `--exclude-tool` command line flag to exclude tools by name.
15-
* Add the abillity to limit the output of `analyze_files` to a set of paths.
16-
* Stop reporting non-zero exit codes from command line tools as tool errors.
17-
* Add descriptions for pub tools, add support for `pub deps` and `pub outdated`.
18-
* Fix a bug in hot_reload ([#290](https://github.com/dart-lang/ai/issues/290)).
19-
* Add the `list_devices`, `launch_app`, `get_app_logs`, and `list_running_apps`
14+
- Add an `--exclude-tool` command line flag to exclude tools by name.
15+
- Add the abillity to limit the output of `analyze_files` to a set of paths.
16+
- Stop reporting non-zero exit codes from command line tools as tool errors.
17+
- Add descriptions for pub tools, add support for `pub deps` and `pub outdated`.
18+
- Fix a bug in hot_reload ([#290](https://github.com/dart-lang/ai/issues/290)).
19+
- Add the `list_devices`, `launch_app`, `get_app_logs`, and `list_running_apps`
2020
tools for running Flutter apps.
21-
* Add the `hot_restart` tool for restarting running Flutter apps.
22-
* Add extra log output to failed launches, and allow AI to specify the maxLines
21+
- Add the `hot_restart` tool for restarting running Flutter apps.
22+
- Add extra log output to failed launches, and allow AI to specify the maxLines
2323
of log output.
24+
- Convert `launch_app` to use `--machine` output to capture the DTD URI.
2425

2526
# 0.1.0 (Dart SDK 3.9.0)
2627

27-
* Add documentation/homepage/repository links to pub results.
28-
* Handle relative paths under roots without trailing slashes.
29-
* Fix executable paths for dart/flutter on windows.
30-
* Pass the provided root instead of the resolved root for project type detection.
31-
* Be more flexible about roots by comparing canonicalized paths.
32-
* Create the working dir if it doesn't exist.
33-
* Add the --platform and --empty arguments to the flutter create tool.
34-
* Invoke dart/flutter in a more robust way.
35-
* Remove qualifiedNames from the pub dev api search.
36-
* Flutter/Dart create tool.
37-
* Limit the tokens returned by the runtime errors tool/resource.
38-
* Add RootsFallbackSupport mixin.
39-
* Fix error handling around stream listeners.
40-
* Add a 'pub-dev-search' mcp tool.
41-
* Drop pubspec-parse, use yaml instead.
42-
* Handle failing to listen to vm service streams during startup.
43-
* Add tool for enabling/disabling the widget selector.
44-
* Add a tool to get the active cursor location.
45-
* Add hover tool support.
46-
* Add a test command and project detection.
47-
* Add signature_help tool.
48-
* Add runtime errors resource and tool to clear errors.
49-
* Require roots for all CLI tools.
50-
* Require roots to be set for analyzer tools.
51-
* Add debug logs for when DTD sees Editor.getDebugSessions get registered.
52-
* Add tool annotations to tools.
53-
* Implement a tool to resolve workspace symbols based on a query.
54-
* Add a dart pub tool.
55-
* Update analyze tool to use LSP, simplify tool.
56-
* Add tool for getting the selected widget.
57-
* Handle missing roots capability better.
58-
* Add `get_widget_tree` tool.
59-
* Add a tool for getting runtime errors.
60-
* Add Dart CLI tool support.
61-
* Add a hot reload tool.
62-
* Add basic analysis support.
63-
* Add the beginnings of a Dart tooling MCP server.
64-
* Instruct clients to prefer MCP tools over running tools in the shell.
65-
* Reduce output size of `run_tests` tool to save on input tokens.
66-
* Add `--log-file` argument to log all protocol traffic to a file.
67-
* Improve error text for failed DTD connections as well as the tool description.
68-
* Add support for injecting an `Analytics` instance to track usage.
69-
* Listen to the new DTD `ConnectedApp` service instead of the `Editor.DebugSessions`
28+
- Add documentation/homepage/repository links to pub results.
29+
- Handle relative paths under roots without trailing slashes.
30+
- Fix executable paths for dart/flutter on windows.
31+
- Pass the provided root instead of the resolved root for project type detection.
32+
- Be more flexible about roots by comparing canonicalized paths.
33+
- Create the working dir if it doesn't exist.
34+
- Add the --platform and --empty arguments to the flutter create tool.
35+
- Invoke dart/flutter in a more robust way.
36+
- Remove qualifiedNames from the pub dev api search.
37+
- Flutter/Dart create tool.
38+
- Limit the tokens returned by the runtime errors tool/resource.
39+
- Add RootsFallbackSupport mixin.
40+
- Fix error handling around stream listeners.
41+
- Add a 'pub-dev-search' mcp tool.
42+
- Drop pubspec-parse, use yaml instead.
43+
- Handle failing to listen to vm service streams during startup.
44+
- Add tool for enabling/disabling the widget selector.
45+
- Add a tool to get the active cursor location.
46+
- Add hover tool support.
47+
- Add a test command and project detection.
48+
- Add signature_help tool.
49+
- Add runtime errors resource and tool to clear errors.
50+
- Require roots for all CLI tools.
51+
- Require roots to be set for analyzer tools.
52+
- Add debug logs for when DTD sees Editor.getDebugSessions get registered.
53+
- Add tool annotations to tools.
54+
- Implement a tool to resolve workspace symbols based on a query.
55+
- Add a dart pub tool.
56+
- Update analyze tool to use LSP, simplify tool.
57+
- Add tool for getting the selected widget.
58+
- Handle missing roots capability better.
59+
- Add `get_widget_tree` tool.
60+
- Add a tool for getting runtime errors.
61+
- Add Dart CLI tool support.
62+
- Add a hot reload tool.
63+
- Add basic analysis support.
64+
- Add the beginnings of a Dart tooling MCP server.
65+
- Instruct clients to prefer MCP tools over running tools in the shell.
66+
- Reduce output size of `run_tests` tool to save on input tokens.
67+
- Add `--log-file` argument to log all protocol traffic to a file.
68+
- Improve error text for failed DTD connections as well as the tool description.
69+
- Add support for injecting an `Analytics` instance to track usage.
70+
- Listen to the new DTD `ConnectedApp` service instead of the `Editor.DebugSessions`
7071
service, when available.
71-
* Screenshot tool disabled until
72+
- Screenshot tool disabled until
7273
https://github.com/flutter/flutter/issues/170357 is resolved.
73-
* Add `arg_parser.dart` public library with minimal deps to be used by the dart tool.
74+
- Add `arg_parser.dart` public library with minimal deps to be used by the dart tool.

pkgs/dart_mcp_server/lib/src/mixins/flutter_launcher.dart

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ base mixin FlutterLauncherSupport
9797
sdk.flutterExecutablePath,
9898
'run',
9999
'--print-dtd',
100+
'--machine',
100101
'--device-id',
101102
device,
102103
if (target != null) '--target',
@@ -116,16 +117,29 @@ base mixin FlutterLauncherSupport
116117

117118
late StreamSubscription stdoutSubscription;
118119
late StreamSubscription stderrSubscription;
119-
final dtdUriRegex = RegExp(
120-
r'The Dart Tooling Daemon is available at: (ws://.+:\d+/\S+=)',
121-
);
122120

123121
void checkForDtdUri(String line) {
124-
final match = dtdUriRegex.firstMatch(line);
125-
if (match != null && !completer.isCompleted) {
126-
final dtdUri = Uri.parse(match.group(1)!);
127-
log(LoggingLevel.debug, 'Found DTD URI: $dtdUri');
128-
completer.complete((dtdUri: dtdUri, pid: process!.pid));
122+
line = line.trim();
123+
// Check for --machine output first.
124+
if (line.startsWith('[') && line.endsWith(']')) {
125+
// Looking for:
126+
// [{"event":"app.dtd","params":{"appId":"cd6c66eb-35e9-4ac1-96df-727540138346","uri":"ws://127.0.0.1:59548/3OpAaPw9i34="}}]
127+
try {
128+
final json =
129+
jsonDecode(line.substring(1, line.length - 1))
130+
as Map<String, Object?>;
131+
if (json['event'] == 'app.dtd' && json['params'] != null) {
132+
final params = json['params'] as Map<String, Object?>;
133+
if (params['uri'] != null) {
134+
final dtdUri = Uri.parse(params['uri'] as String);
135+
log(LoggingLevel.debug, 'Found machine DTD URI: $dtdUri');
136+
completer.complete((dtdUri: dtdUri, pid: process!.pid));
137+
}
138+
}
139+
} on FormatException {
140+
// Ignore failures to parse the JSON or the URI.
141+
log(LoggingLevel.debug, 'Failed to parse $line for the DTD URI.');
142+
}
129143
}
130144
}
131145

@@ -213,7 +227,7 @@ base mixin FlutterLauncherSupport
213227
} catch (e, s) {
214228
log(LoggingLevel.error, 'Error launching Flutter application: $e\n$s');
215229
if (process != null) {
216-
process.kill();
230+
processManager.killPid(process.pid);
217231
// The exitCode handler will perform the rest of the cleanup.
218232
}
219233
return CallToolResult(

pkgs/dart_mcp_server/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ dependencies:
4242
dev_dependencies:
4343
analyzer: ^7.5.2
4444
dart_flutter_team_lints: ^3.2.1
45+
fake_async: ^1.3.3
4546
pub_semver: ^2.2.0
4647
test: ^1.25.15
4748
test_descriptor: ^2.0.2

0 commit comments

Comments
 (0)