|
| 1 | +--- |
| 2 | +name: flutter-app-runtime |
| 3 | +description: Interacts with running Dart and Flutter applications via the Dart MCP server to enable hot reload, hot restart, widget inspection, and error fetching. Use when modifying UI widgets, debugging errors, inspecting running apps, or proactively hot reloading whenever changes are made to Flutter UI widgets. |
| 4 | +metadata: |
| 5 | + model: models/gemini-3.1-pro-preview |
| 6 | + last_modified: Fri, 07 Aug 2026 21:16:50 GMT |
| 7 | +--- |
| 8 | +# Managing Dart and Flutter Applications via MCP |
| 9 | + |
| 10 | +## Contents |
| 11 | +- [Core Capabilities](#core-capabilities) |
| 12 | +- [Workflow: Application Discovery and Connection](#workflow-application-discovery-and-connection) |
| 13 | +- [Workflow: UI Modification and Hot Reloading](#workflow-ui-modification-and-hot-reloading) |
| 14 | +- [Workflow: Runtime Debugging and Error Resolution](#workflow-runtime-debugging-and-error-resolution) |
| 15 | +- [Workflow: UI Interaction via Flutter Driver](#workflow-ui-interaction-via-flutter-driver) |
| 16 | +- [Examples](#examples) |
| 17 | + |
| 18 | +## Core Capabilities |
| 19 | + |
| 20 | +The Dart and Flutter MCP server manages the underlying connection to the application's VM service via the Dart Tooling Daemon (`dtd`). Assume the server is already configured and active. |
| 21 | + |
| 22 | +- **Hot Reload:** Proactively push visual feedback for UI widget updates and simple logic changes. |
| 23 | +- **Hot Restart:** Reset the app state for deep non-widget changes or clean resets. |
| 24 | +- **Widget Inspection:** Query tree structures and identify UI hierarchy names. |
| 25 | +- **Error Fetching:** Retrieve live runtime exceptions and trace pointers live. |
| 26 | +- **Package Management:** Search pub.dev and manage `pubspec.yaml` dependencies. |
| 27 | + |
| 28 | +## Workflow: Application Discovery and Connection |
| 29 | + |
| 30 | +Execute this workflow before attempting runtime operations. |
| 31 | + |
| 32 | +- [ ] 1. Use the `dtd` tool (with `listDtdUris` and `connect`, or `listConnectedApps`) to discover active application instances in the current workspace. |
| 33 | +- [ ] 2. Extract the `appUri` for the target application. |
| 34 | +- [ ] 3. Verify the connection using `dtd` or `vm_service` to ensure the `appUri` matches the project's workspace. Re-sync via `dtd` if necessary. |
| 35 | +- [ ] 4. **Conditional:** If no app is running, inform the user immediately, but **do not stop**. Proceed with static code edits and file modifications. |
| 36 | + |
| 37 | +## Workflow: UI Modification and Hot Reloading |
| 38 | + |
| 39 | +Whenever modifying Flutter UI widgets or Dart code, follow this feedback loop to maintain visual consistency. |
| 40 | + |
| 41 | +- [ ] 1. Implement the requested code changes in the target `.dart` files. |
| 42 | +- [ ] 2. **Conditional:** |
| 43 | + - If editing Flutter UI widgets or simple methods, trigger `hot_reload` across all targeted instances immediately. |
| 44 | + - If modifying foundational logic, state initialization, or `main()`, trigger `hot_restart` to reset state across all devices. |
| 45 | +- [ ] 3. Run `get_runtime_errors` to verify the reload/restart did not introduce new exceptions. |
| 46 | +- [ ] 4. If errors exist, review the stack trace, apply a fix, and repeat step 2. |
| 47 | + |
| 48 | +## Workflow: Runtime Debugging and Error Resolution |
| 49 | + |
| 50 | +When tasked with fixing layout issues (e.g., RenderFlex overflow) or runtime exceptions, use this diagnostic loop. |
| 51 | + |
| 52 | +- [ ] 1. Execute `get_runtime_errors` to fetch live exceptions from the running application. |
| 53 | +- [ ] 2. Identify the failing widget or logic block from the stack trace. |
| 54 | +- [ ] 3. Execute `widget_inspector` to query the live UI tree structure and understand the layout constraints causing the issue. |
| 55 | +- [ ] 4. Apply the code fix to the relevant Dart file. |
| 56 | +- [ ] 5. Trigger `hot_reload`. |
| 57 | +- [ ] 6. Execute `get_runtime_errors` again to confirm the error is resolved. |
| 58 | + |
| 59 | +## Workflow: UI Interaction via Flutter Driver |
| 60 | + |
| 61 | +To drive a running Flutter app (take screenshots, tap buttons, enter text), the app must be instrumented with `flutter_driver`. |
| 62 | + |
| 63 | +- [ ] 1. Verify `flutter_driver` is in `pubspec.yaml`. If not, run `flutter pub add "flutter_driver:{sdk: flutter}"`. |
| 64 | +- [ ] 2. Ensure the app's `main()` function conditionally enables the driver extension: |
| 65 | + ```dart |
| 66 | + import 'package:flutter_driver/driver_extension.dart'; |
| 67 | + |
| 68 | + void main() { |
| 69 | + if (const bool.fromEnvironment('ENABLE_FLUTTER_DRIVER')) { |
| 70 | + enableFlutterDriverExtension(); |
| 71 | + } |
| 72 | + runApp(const MyApp()); |
| 73 | + } |
| 74 | + ``` |
| 75 | +- [ ] 3. Instruct the user to launch the app with: `flutter run -d <device-id> --dart-define=ENABLE_FLUTTER_DRIVER=true`. |
| 76 | +- [ ] 4. Use the `dtd` tool to discover the app and `flutter_driver_command` to drive its UI. |
| 77 | +- [ ] 5. **Conditional (Web Targets):** If the target is a web build, do not use `flutter_driver` finder-based commands (screenshots/taps). Instead, rely on a browser-driving MCP. Ensure the user runs `flutter run -d web-server` so DTD connects properly to the driven browser. |
| 78 | + |
| 79 | +## Examples |
| 80 | + |
| 81 | +### Scenario: Adding a Package and Scaffolding UI |
| 82 | + |
| 83 | +**Input:** "Add a line chart to map user scores over time." |
| 84 | + |
| 85 | +**Execution Steps:** |
| 86 | +1. Execute `pub_dev_search` with query "line chart". |
| 87 | +2. Identify `fl_chart` as the optimal package. |
| 88 | +3. Add dependency to `pubspec.yaml`. |
| 89 | +4. Generate the widget code using `fl_chart` boilerplate. |
| 90 | +5. Insert the widget into the target UI file. |
| 91 | +6. Trigger `hot_reload`. |
| 92 | +7. Run `get_runtime_errors` to ensure no missing constraints or syntax errors exist. |
| 93 | + |
| 94 | +### Scenario: Proactive Hot Reloading |
| 95 | + |
| 96 | +**Input:** "Change the primary button color to blue and increase the padding to 16." |
| 97 | + |
| 98 | +**Execution Steps:** |
| 99 | +1. Locate the target `ElevatedButton` or `Container` in the Dart code. |
| 100 | +2. Update `style: ElevatedButton.styleFrom(backgroundColor: Colors.blue)` and `padding: EdgeInsets.all(16)`. |
| 101 | +3. Save the file. |
| 102 | +4. Immediately execute `hot_reload` using the active `appUri`. |
| 103 | +5. Confirm successful reload via tool output. |
0 commit comments