|
| 1 | +--- |
| 2 | +title: Dart and Flutter MCP server |
| 3 | +description: > |
| 4 | + Learn about the Dart and Flutter MCP server tool that |
| 5 | + exposes Dart and Flutter tools to compatible |
| 6 | + AI-assistant clients and agents. |
| 7 | +--- |
| 8 | + |
| 9 | +This guide discusses the Dart and Flutter MCP server. |
| 10 | + |
| 11 | +:::note experimental |
| 12 | +The Dart and Flutter MCP server is experimental and likely to evolve quickly. |
| 13 | +The following instructions require Dart 3.9 or later. |
| 14 | +::: |
| 15 | + |
| 16 | +## Overview |
| 17 | + |
| 18 | +The [Dart and Flutter MCP server][] |
| 19 | +exposes Dart and Flutter development tool actions to |
| 20 | +compatible AI-assistant clients. MCP (model context protocol) |
| 21 | +is a protocol that enables communication between development tools |
| 22 | +and AI assistants, allowing the assistants to understand the |
| 23 | +context of the code and perform actions on behalf of the developer. |
| 24 | + |
| 25 | +The Dart and Flutter MCP server can work with any MCP client that |
| 26 | +supports standard I/O (stdio) as the transport medium. |
| 27 | +To access all the features of the Dart and Flutter MCP server, |
| 28 | +an MCP client must support [Tools][] and [Resources][]. |
| 29 | +For the best development experience with the Dart and Flutter MCP server, |
| 30 | +an MCP client should also support [Roots][]. |
| 31 | + |
| 32 | +If you are using a client that claims it |
| 33 | +supports roots but doesn't actually set them, |
| 34 | +pass `--force-roots-fallback` to enable tools for managing the roots. |
| 35 | + |
| 36 | +The Dart and Flutter MCP server provides a growing list of tools that |
| 37 | +grant AI assistants deep insights into your project. |
| 38 | +Here is an overview of a few things it can do: |
| 39 | + |
| 40 | +* Analyze and fix errors in your project's code. |
| 41 | +* Resolve symbols to elements to ensure their existence and |
| 42 | + fetch documentation and signature information for them. |
| 43 | +* Introspect and interact with your running application. |
| 44 | +* Search the [pub.dev site]({{site.pub}}) for the best package for a use case. |
| 45 | +* Manage package dependencies in your `pubspec.yaml` file. |
| 46 | +* Run tests and analyze the results. |
| 47 | +* Format code with the same formatter and config as |
| 48 | + [`dart format`][] and the Dart analysis server. |
| 49 | + |
| 50 | +[Tools]: https://modelcontextprotocol.io/docs/concepts/tools |
| 51 | +[Resources]: https://modelcontextprotocol.io/docs/concepts/resources |
| 52 | +[Roots]: https://modelcontextprotocol.io/docs/concepts/roots |
| 53 | +[Dart and Flutter MCP server]: https://github.com/dart-lang/ai/tree/main/pkgs/dart_mcp_server |
| 54 | +[`dart format`]: {{site.dart-site}}/tools/dart-format |
| 55 | + |
| 56 | +## Set up your MCP client |
| 57 | + |
| 58 | +Run the server with the `dart mcp-server` command, |
| 59 | +which must be configured in your preferred client. |
| 60 | + |
| 61 | +This section provides instructions for setting up the |
| 62 | +Dart and Flutter MCP server with popular tools like Firebase Studio, |
| 63 | +Gemini CLI, Gemini Code Assist, Cursor, and GitHub Copilot. |
| 64 | + |
| 65 | +### Gemini CLI |
| 66 | + |
| 67 | +To configure the [Gemini CLI][] to use the Dart and Flutter MCP server, |
| 68 | +add a Dart entry to the `mcpServers` section of the Gemini config. |
| 69 | + |
| 70 | +- To enable the server for all projects on your device, |
| 71 | + edit the `~/.gemini/settings.json` file in your home directory. |
| 72 | +- To enable the server for a specific project, |
| 73 | + edit the `.gemini/settings.json` file in the project's root directory. |
| 74 | + |
| 75 | +```json title=".gemini/settings.json" |
| 76 | +{ |
| 77 | + "mcpServers": { |
| 78 | + "dart": { |
| 79 | + "command": "dart", |
| 80 | + "args": [ |
| 81 | + "mcp-server" |
| 82 | + ] |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +For more information, check out the official Gemini CLI |
| 89 | +documentation for [setting up MCP servers][]. |
| 90 | + |
| 91 | +[Gemini CLI]: https://geminicli.com/docs/ |
| 92 | +[setting up MCP servers]: https://geminicli.com/docs/tools/mcp-server/#how-to-set-up-your-mcp-server |
| 93 | + |
| 94 | +### Firebase Studio |
| 95 | + |
| 96 | +If you wish to use the Dart and Flutter MCP Server in [Firebase Studio][], |
| 97 | +an agentic cloud-based development environment that helps |
| 98 | +you build and ship production-quality full-stack AI apps, |
| 99 | +follow these steps: |
| 100 | + |
| 101 | +1. In your Firebase Studio app project, create a |
| 102 | + `.idx/mcp.json` file in your project if it |
| 103 | + doesn't exist already and add the following |
| 104 | + Dart and Flutter MCP Server configuration to the file: |
| 105 | + |
| 106 | + ```json title=".idx/mcp.json" |
| 107 | + { |
| 108 | + "mcpServers": { |
| 109 | + "dart": { |
| 110 | + "command": "dart", |
| 111 | + "args": [ |
| 112 | + "mcp-server" |
| 113 | + ] |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + ``` |
| 118 | + |
| 119 | +1. Ensure your environment is running Dart SDK 3.9/Flutter 3.35 or later. |
| 120 | +1. Rebuild your workspace to complete the setup. |
| 121 | + * Open the Command Palette (**Shift+Ctrl+P**). |
| 122 | + * Enter **Firebase Studio: Rebuild Environment**. |
| 123 | + |
| 124 | +For more information about MCP server configuration in |
| 125 | +Firebase Studio, see [Customize your Firebase Studio workspace][]. |
| 126 | + |
| 127 | +[Firebase Studio]: https://firebase.studio/ |
| 128 | +[Customize your Firebase Studio workspace]: https://firebase.google.com/docs/studio/customize-workspace |
| 129 | + |
| 130 | +### Gemini Code Assist in VS Code |
| 131 | + |
| 132 | +[Gemini Code Assist][]'s [Agent mode][] integrates the |
| 133 | +Gemini CLI to provide a powerful AI agent directly in your IDE. |
| 134 | +If you haven't set up Gemini Code Assist or its agent mode yet, |
| 135 | +follow its [Before you begin instructions][gca-setup] to get started. |
| 136 | + |
| 137 | +To configure Gemini Code Assist to use the Dart and Flutter MCP server, |
| 138 | +follow the instructions to [configure the Gemini CLI][]. |
| 139 | + |
| 140 | +You can verify the MCP server has been configured |
| 141 | +properly by typing `/mcp` in the chat window in Agent mode. |
| 142 | + |
| 143 | +For more information see the official Gemini Code Assist |
| 144 | +documentation for [using agent mode][]. |
| 145 | + |
| 146 | +[gca-setup]: https://developers.google.com/gemini-code-assist/docs/use-agentic-chat-pair-programmer#before-you-begin |
| 147 | +[Gemini Code Assist]: https://codeassist.google/ |
| 148 | +[Agent mode]: https://developers.google.com/gemini-code-assist/docs/use-agentic-chat-pair-programmer |
| 149 | +[configure the Gemini CLI]: #gemini-cli |
| 150 | +[using agent mode]: https://developers.google.com/gemini-code-assist/docs/use-agentic-chat-pair-programmer#before-you-begin |
| 151 | + |
| 152 | +### GitHub Copilot in VS Code |
| 153 | + |
| 154 | +:::note |
| 155 | +Support for the Dart and Flutter MCP server in VS Code requires |
| 156 | +v3.116 or later of the [Dart Code extension][]. |
| 157 | +::: |
| 158 | + |
| 159 | +By default, the Dart extension uses the |
| 160 | +[VS Code MCP API][] to register the Dart and Flutter MCP server, as well |
| 161 | +as a tool to provide the URI for the active Dart Tooling Daemon. |
| 162 | + |
| 163 | +Explicitly enable or disable the Dart and Flutter MCP server by |
| 164 | +configuring the `dart.mcpServer` setting in your VS Code settings. |
| 165 | + |
| 166 | +To change this globally, update your user settings: |
| 167 | + |
| 168 | +1. In VS Code, click **View > Command Palette** and then |
| 169 | + search for **Preferences: Open User Settings (JSON)**. |
| 170 | + |
| 171 | +1. Add the following setting: |
| 172 | + |
| 173 | + ```json |
| 174 | + "dart.mcpServer": true |
| 175 | + ``` |
| 176 | + |
| 177 | +If you'd like this setting to apply only to a specific workspace, |
| 178 | +add the entry to your workspace settings: |
| 179 | + |
| 180 | +1. In VS Code, click **View > Command Palette** and then |
| 181 | + search for **Preferences: Open Workspace Settings (JSON)**. |
| 182 | + |
| 183 | +1. Add the following setting: |
| 184 | + |
| 185 | + ```json |
| 186 | + "dart.mcpServer": true |
| 187 | + ``` |
| 188 | + |
| 189 | +For more information, see the official VS Code |
| 190 | +documentation for [enabling MCP support][]. |
| 191 | + |
| 192 | +[Dart Code extension]: https://marketplace.visualstudio.com/items?itemName=Dart-Code.dart-code |
| 193 | +[VS Code MCP API]: https://code.visualstudio.com/api/extension-guides/mcp |
| 194 | +[enabling MCP support]: https://code.visualstudio.com/docs/copilot/chat/mcp-servers#_enable-mcp-support-in-vs-code |
| 195 | + |
| 196 | +### Cursor |
| 197 | + |
| 198 | +The easiest way to configure the Dart and Flutter MCP server with |
| 199 | +Cursor is by clicking the **Add to Cursor** button: |
| 200 | + |
| 201 | +[](cursor://anysphere.cursor-deeplink/mcp/install?name=dart&config=eyJ0eXBlIjoic3RkaW8iLCJjb21tYW5kIjoiZGFydCBtY3Atc2VydmVyIC0tZXhwZXJpbWVudGFsLW1jcC1zZXJ2ZXIgLS1mb3JjZS1yb290cy1mYWxsYmFjayJ9){:.light-mode-visible} |
| 202 | +[](cursor://anysphere.cursor-deeplink/mcp/install?name=dart&config=eyJ0eXBlIjoic3RkaW8iLCJjb21tYW5kIjoiZGFydCBtY3Atc2VydmVyIC0tZXhwZXJpbWVudGFsLW1jcC1zZXJ2ZXIgLS1mb3JjZS1yb290cy1mYWxsYmFjayJ9){:.dark-mode-visible} |
| 203 | + |
| 204 | +Alternatively, you can configure the server manually: |
| 205 | + |
| 206 | +1. Go to **Cursor > Settings > Cursor Settings > Tools & Integrations**. |
| 207 | +1. Click **Add Custom MCP** or **New MCP Server** |
| 208 | + depending on whether you already have other MCP servers configured. |
| 209 | +1. Edit the `.cursor/mcp.json` file in your local project |
| 210 | + (configuration will only apply to this project) or |
| 211 | + edit the global `~/.cursor/mcp.json` file in your home directory |
| 212 | + (configuration will apply for all projects) to |
| 213 | + configure the Dart and Flutter MCP server: |
| 214 | + |
| 215 | + ```json title=".cursor/mcp.json" |
| 216 | + { |
| 217 | + "mcpServers": { |
| 218 | + "dart": { |
| 219 | + "command": "dart", |
| 220 | + "args": [ |
| 221 | + "mcp-server", |
| 222 | + "--experimental-mcp-server", |
| 223 | + "--force-roots-fallback" |
| 224 | + ] |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | + ``` |
| 229 | + |
| 230 | +For more information, see the official Cursor |
| 231 | +documentation for [installing MCP servers][]. |
| 232 | + |
| 233 | +[installing MCP servers]: https://docs.cursor.com/context/model-context-protocol#installing-mcp-servers |
| 234 | + |
| 235 | +## Use your MCP client |
| 236 | + |
| 237 | +Once you've set up the Dart and Flutter MCP server with a client, |
| 238 | +the Dart and Flutter MCP server enables the client to not only reason |
| 239 | +about your project's context but also to take action with tools. |
| 240 | + |
| 241 | +The [Large Language Model (LLM)][LLM] decides which tools to use and when, |
| 242 | +so you can focus on describing your goal in natural language. |
| 243 | +Let's see this in action with a couple of examples using |
| 244 | +GitHub Copilot's Agent mode in VS Code. |
| 245 | + |
| 246 | +[LLM]: https://developers.google.com/machine-learning/resources/intro-llms |
| 247 | + |
| 248 | +### Fix a runtime layout error in a Flutter app |
| 249 | + |
| 250 | +We've all been there: you build a beautiful UI, run the app, |
| 251 | +and are greeted by the infamous yellow-and-black stripes of |
| 252 | +a RenderFlex overflow error. |
| 253 | +Instead of manually debugging the widget tree, you can now |
| 254 | +ask your AI assistant for help with a prompt similar to the following: |
| 255 | + |
| 256 | +> Check for and fix static and runtime analysis issues. |
| 257 | +> Check for and fix any layout issues. |
| 258 | + |
| 259 | +Behind the scenes, the AI agent uses the Dart and Flutter MCP server's tools to: |
| 260 | + |
| 261 | +* See the error: It uses a tool to get the current runtime errors |
| 262 | + from the running application. |
| 263 | +* Inspect the UI: It accesses the Flutter widget tree to understand |
| 264 | + the layout that is causing the overflow. |
| 265 | +* Apply a fix: Armed with this context, it applies a fix and |
| 266 | + checks once more for any remaining errors. |
| 267 | + |
| 268 | +You can then keep or undo the code changes. |
| 269 | + |
| 270 | +### Add new functionality with package search |
| 271 | + |
| 272 | +Imagine you need to add a chart to your app. |
| 273 | +Which package should you use? How do you add it and write the boilerplate? |
| 274 | +The Dart and Flutter MCP server can streamline this entire process with |
| 275 | +a prompt similar to the following: |
| 276 | + |
| 277 | +> Find a suitable package to add a line chart that |
| 278 | +> maps the number of button presses over time. |
| 279 | + |
| 280 | +The AI agent now acts as a true assistant: |
| 281 | + |
| 282 | +* Find the right tool: It uses the `pub_dev_search` tool to |
| 283 | + find popular and highly-rated charting libraries. |
| 284 | +* Manage dependencies: After you confirm its choice, |
| 285 | + such as [`package:fl_chart`][], |
| 286 | + it uses a tool to add the package as a dependency. |
| 287 | +* Generate the code: It generates the new widget code, |
| 288 | + complete with boilerplate for a line chart that it places in the UI. |
| 289 | + It even self-corrects syntax errors introduced during the process. |
| 290 | + You can customize further from there. |
| 291 | + |
| 292 | +What used to be a multi-step process of research, |
| 293 | +reading documentation, editing `pubspec.yaml`, and |
| 294 | +writing the appropriate code in your app, |
| 295 | +is now a single request. |
| 296 | + |
| 297 | +[`package:fl_chart`]: {{site.pub-pkg}}/fl_chart |
0 commit comments