Skip to content
Merged
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
45 changes: 44 additions & 1 deletion src/io/flutter/run/daemon/FlutterApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.google.common.base.Stopwatch;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.process.ProcessAdapter;
Expand All @@ -27,13 +28,17 @@
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.EventDispatcher;
import com.intellij.util.concurrency.AppExecutorUtil;
import com.jetbrains.lang.dart.ide.toolingDaemon.DartToolingDaemonService;
import de.roderick.weberknecht.WebSocketException;
import io.flutter.FlutterMessages;
import io.flutter.FlutterUtils;
import io.flutter.ObservatoryConnector;
import io.flutter.bazel.Workspace;
import io.flutter.bazel.WorkspaceCache;
import io.flutter.dart.DtdUtils;
import io.flutter.dart.FlutterDartAnalysisServer;
import io.flutter.logging.FlutterConsoleLogManager;
import io.flutter.logging.PluginLogger;
import io.flutter.run.FlutterDebugProcess;
import io.flutter.run.FlutterDevice;
import io.flutter.run.FlutterLaunchMode;
Expand Down Expand Up @@ -677,10 +682,13 @@ public void dispose() {
* Listens for events while running or debugging an app.
*/
class FlutterAppDaemonEventListener implements DaemonEvent.Listener {
private static final @NotNull Logger LOG = Logger.getInstance(FlutterAppDaemonEventListener.class);
private static final @NotNull Logger LOG = PluginLogger.createLogger(FlutterAppDaemonEventListener.class);

private final @NotNull FlutterApp app;
private final @NotNull ProgressHelper progress;
private String appVmServiceUri;
private final DtdUtils dtdUtils = new DtdUtils();


FlutterAppDaemonEventListener(@NotNull FlutterApp app, @NotNull Project project) {
this.app = app;
Expand Down Expand Up @@ -738,6 +746,12 @@ public void onAppStarting(DaemonEvent.AppStarting event) {
@Override
public void onAppDebugPort(@NotNull DaemonEvent.AppDebugPort debugInfo) {
app.setWsUrl(debugInfo.wsUri);
this.appVmServiceUri = debugInfo.wsUri;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we get any sort of identifier that we can use as a map key that we can look up when we get the app stopped event for this particular app?

While the MCP server today only supports one connected app, we do want to have it support multiple in the future, so ideally the IDE plugins wouldn't be assuming just one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IJ can register multiple apps to DTD. There's an app name that's a unique identifier, or the VM service URI is unique too.

This is an example of the params we send with two apps running at the same time:

2025-08-08 09:28:04 io.flutter.run.daemon.FlutterAppDaemonEventListener [INFO   ] Successful request ConnectedApp.registerVmService to DTD with params: {"uri":"ws://127.0.0.1:50489/m1aDg3zyHq4=/ws","name":"752c8ff1-86dd-4c91-abab-825747059057"}  
2025-08-08 09:29:33 io.flutter.run.daemon.FlutterAppDaemonEventListener [INFO   ] Successful request ConnectedApp.registerVmService to DTD with params: {"uri":"ws://127.0.0.1:51041/iUbSRQCLBfg=/ws","name":"65328744-2368-46bf-9e23-70066b219342"}  

The unregisterVmService takes the URI as a param also, so I imagine you would use that as the key?


final JsonObject params = new JsonObject();
params.addProperty("uri", debugInfo.wsUri);
params.addProperty("name", debugInfo.appId);
sendDtdRequest("ConnectedApp.registerVmService", params);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how we want to test this (And I'd love to get your thoughts), but in anticipation, maybe we should introduce some constants somewhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for now I can add these methods as constants. Maybe a separate file that links to DTD documentation would be helpful.

I haven't thought much about testing; I suppose one part would be testing dtdService.sendRequest in the Dart plugin. And ultimately integration testing in the Dart plugin.


// Print the conneciton info to the console.
final ConsoleView console = app.getConsole();
Expand Down Expand Up @@ -804,7 +818,36 @@ public void onAppStopped(@NotNull DaemonEvent.AppStopped stopped) {
if (stopped.error != null && app.getConsole() != null) {
app.getConsole().print("Finished with error: " + stopped.error + "\n", ConsoleViewContentType.ERROR_OUTPUT);
}

final JsonObject params = new JsonObject();
params.addProperty("uri", appVmServiceUri);
sendDtdRequest("ConnectedApp.unregisterVmService", params);

progress.cancel();
app.getProcessHandler().destroyProcess();
}

private void sendDtdRequest(@NotNull String requestName, @NotNull JsonObject params) {
try {
final DartToolingDaemonService dtdService = dtdUtils.readyDtdService(app.getProject()).get();
if (dtdService == null) {
return;
}
// This removes secret from params when we print out after receiving response.
JsonObject initialParams = params.deepCopy();
dtdService.sendRequest(requestName, params, true, object -> {
final JsonObject result = object.getAsJsonObject("result");
final JsonPrimitive type = result != null ? result.getAsJsonPrimitive("type") : null;
if (type != null && "Success".equals(type.getAsString())) {
LOG.info("Successful request " + requestName + " to DTD with params: " + initialParams);
} else {
LOG.warn("Failed request " + requestName + "to DTD with params: " + initialParams);
LOG.warn("Result: " + result);
}
});
}
catch (InterruptedException | java.util.concurrent.ExecutionException | WebSocketException e) {
LOG.error("Exception while sending DTD request", e);
}
}
}