âš¡ TL;DR
Integrate a Model Context Protocol (MCP) server into your Flutter/Dart workflow. Step-by-step setup with Claude Code and real-world use cases.
Model Context Protocol (MCP) lets AI tools like Claude Code plug into your local development environment. In this guide, I’ll walk you through building a small MCP server in Dart and wiring it into Claude Code, so it can read your project structure, run tests, and even generate code that matches your team’s patterns.
I’ve been using MCP with Flutter for 3 months now. It’s transformed how I write boilerplate, refactor widgets, and debug platform-specific issues. Here’s how to set it up.
What is MCP?
MCP is an open protocol that standardizes how AI applications connect to data sources and tools. Think of it as a USB-C port for AI: instead of every AI tool building custom integrations, they all plug into MCP servers that expose your data safely.
For Flutter/Dart, an MCP server can:
- Read your
pubspec.yamlto understand dependencies - Analyze your
lib/directory structure - Run
flutter analyzeand report issues - Generate code that follows your existing patterns
- Execute
dart testand report failures
Prerequisites
- Flutter SDK installed
- Claude Code CLI installed (
npm install -g @anthropic-ai/claude-code) - Basic Dart/Flutter knowledge
Step 1: Create a Dart MCP Server
Create a new Dart package for your MCP server:
dart create -t package mcp_flutter_server
cd mcp_flutter_serverAdd the MCP SDK dependency (mcp_dart is the community Dart SDK port of the official TypeScript MCP SDK — check pub.dev for the current version):
# pubspec.yaml
dependencies:
mcp_dart: ^2.4.0
path: ^1.9.0Note: the code below is a reference implementation I’ve adapted from the SDK’s docs. The
mcp_dartAPI is evolving quickly between minor versions, so if a name doesn’t compile against your installed version, check the package’s getting-started guide on pub.dev — the concepts (server, tools, schemas, stdio transport) are stable even when method signatures shift.
Step 2: Implement the MCP Server
Create bin/mcp_server.dart:
import 'package:mcp_dart/mcp_dart.dart';
import 'dart:io';
import 'dart:convert';
import 'package:path/path.dart' as path;
void main() async {
final server = McpServer(
const Implementation(name: 'flutter-mcp-server', version: '1.0.0'),
);
// Tool: List Flutter project structure
server.addTool(
name: 'getProjectStructure',
description: 'Get the structure of a Flutter project',
inputSchema: ToolInputSchema(
type: 'object',
properties: {
'projectPath': {
'type': 'string',
'description': 'Path to the Flutter project',
},
},
required: ['projectPath'],
),
callback: (args, extra) async {
final projectPath = args['projectPath'] as String;
final structure = <String, dynamic>{};
// Read pubspec.yaml
final pubspecFile = File(path.join(projectPath, 'pubspec.yaml'));
if (await pubspecFile.exists()) {
structure['pubspec'] = await pubspecFile.readAsString();
}
// List lib directory
final libDir = Directory(path.join(projectPath, 'lib'));
if (await libDir.exists()) {
final files = <String>[];
await for (final entity in libDir.list(recursive: true)) {
if (entity is File) {
files.add(path.relative(entity.path, from: projectPath));
}
}
structure['libFiles'] = files;
}
return CallToolResult(
content: [TextContent(text: jsonEncode(structure))],
);
},
);
// Tool: Run Flutter analyze
server.addTool(
name: 'runFlutterAnalyze',
description: 'Run flutter analyze on a project',
inputSchema: ToolInputSchema(
type: 'object',
properties: {
'projectPath': {
'type': 'string',
'description': 'Path to the Flutter project',
},
},
required: ['projectPath'],
),
callback: (args, extra) async {
final projectPath = args['projectPath'] as String;
final result = await Process.run(
'flutter',
['analyze'],
workingDirectory: projectPath,
runInShell: true,
);
return CallToolResult(
content: [
TextContent(text: 'Exit code: ${result.exitCode}'),
TextContent(text: result.stdout.toString()),
if (result.stderr.isNotEmpty) TextContent(text: result.stderr.toString()),
],
isError: result.exitCode != 0,
);
},
);
// Tool: Run Dart tests
server.addTool(
name: 'runDartTest',
description: 'Run dart test on a specific test file',
inputSchema: ToolInputSchema(
type: 'object',
properties: {
'projectPath': {'type': 'string'},
'testFile': {'type': 'string'},
},
required: ['projectPath', 'testFile'],
),
callback: (args, extra) async {
final projectPath = args['projectPath'] as String;
final testFile = args['testFile'] as String;
final result = await Process.run(
'dart',
['test', testFile],
workingDirectory: projectPath,
);
return CallToolResult(
content: [
TextContent(text: result.stdout.toString()),
if (result.stderr.isNotEmpty) TextContent(text: result.stderr.toString()),
],
isError: result.exitCode != 0,
);
},
);
// Start the server over stdio
final transport = StdioServerTransport();
await server.connect(transport);
}Step 3: Configure Claude Code to Use Your MCP Server
The easiest way is the claude mcp add command:
claude mcp add flutter-dart -- dart /absolute/path/to/mcp_flutter_server/bin/mcp_server.dartBy default this registers the server in local scope (private to you, for the current project, stored in ~/.claude.json). To share it with your team, use project scope, which writes a .mcp.json file at the project root that you can commit to git:
// .mcp.json (project root)
{
"mcpServers": {
"flutter-dart": {
"command": "dart",
"args": ["/absolute/path/to/mcp_flutter_server/bin/mcp_server.dart"]
}
}
}Use claude mcp add --scope user flutter-dart -- ... if you want the server available across all your projects.
Step 4: Use the MCP Server in Claude Code
Now when you start Claude Code in a Flutter project, it can use your MCP tools:
cd /path/to/your/flutter/project
claudeInside Claude Code, ask:
Use the MCP server to analyze my Flutter project structure and tell me:
1. What dependencies I have
2. What files exist in lib/
3. Any analyzer errorsClaude Code will call getProjectStructure and runFlutterAnalyze through your MCP server and report back.
Step 5: Advanced Usage - Auto-Generate Boilerplate
Add a tool to generate BLoC boilerplate:
server.addTool(
name: 'generateBloc',
description: 'Generate BLoC pattern files for a feature',
inputSchema: ToolInputSchema(
type: 'object',
properties: {
'featureName': {'type': 'string'},
'projectPath': {'type': 'string'},
},
required: ['featureName', 'projectPath'],
),
callback: (args, extra) async {
final featureName = args['featureName'] as String;
final projectPath = args['projectPath'] as String;
final featurePath = path.join(projectPath, 'lib', 'features', featureName);
// Create directories
await Directory(path.join(featurePath, 'bloc')).create(recursive: true);
await Directory(path.join(featurePath, 'ui')).create(recursive: true);
// Generate BLoC file
final blocFile = File(path.join(featurePath, 'bloc', '${featureName}_bloc.dart'));
await blocFile.writeAsString('''
import 'package:flutter_bloc/flutter_bloc.dart';
abstract class ${featureName}Event {}
abstract class ${featureName}State {}
class ${featureName}Initial extends ${featureName}State {}
class ${featureName}Bloc extends Bloc<${featureName}Event, ${featureName}State> {
${featureName}Bloc() : super(${featureName}Initial()) {
on<${featureName}Event>((event, emit) {
// TODO: implement
});
}
}
''');
return CallToolResult(
content: [TextContent(text: 'Generated BLoC for $featureName')],
);
},
);Real-World Use Cases
1. Onboarding New Team Members
Ask Claude: "Explain the BLoC pattern used in the auth feature"The MCP server reads lib/features/auth/bloc/ and Claude explains the existing implementation.
2. Pre-Commit Hook Validation
Ask Claude: "Run flutter analyze and fix any issues"MCP runs analysis, Claude suggests fixes, you review before committing.
3. Documentation Generation
Ask Claude: "Generate API documentation for the UserRepository class"MCP reads the file, Claude generates markdown docs.
Security Considerations
- File System Access: MCP servers can read/write files. Only run servers from trusted sources.
- Command Execution: The
runFlutterAnalyzetool executes shell commands. Validate inputs to prevent injection. - Authentication: For web-based MCP servers, add authentication headers.
Troubleshooting
MCP Server Not Starting
Check logs:
dart /path/to/mcp_server.dart 2>&1Common issues:
- Dart not in PATH
- Missing
mcp_dartdependency (rundart pub get) - A version mismatch between the code and your installed
mcp_dart(the SDK evolves fast — see the note in Step 2)
Claude Code Not Detecting MCP
Verify the server is registered:
claude mcp listIf it’s missing, re-add it with claude mcp add (Step 3), and ensure the command and args match your Dart executable and server path. You can also test the server in isolation by speaking raw MCP over stdio — or simply running the script and checking it doesn’t crash:
dart /path/to/mcp_server.dart 2>&1Conclusion
Adding an MCP server to your Flutter/Dart tooling bridges AI assistance with your local dev environment. Instead of copying code snippets into chat windows, Claude Code can now understand your project structure, run analysis, and generate code that fits your existing architecture.
The ecosystem is young (MCP was announced in late 2024), but tooling is maturing fast. For Flutter developers, this is a game-changer for productivity.
Related Posts
- Adding an MCP Server to Your React Native Tooling - A Practical Guide
- Claude Code vs GitHub Copilot Agent vs Cursor: 3 Months of Real Work
- All about SOLID Principles in Flutter: Examples and Tips
- Antigravity vs Claude Code vs Codex: Honest 2026 Test
FAQ
What is an MCP server for Flutter development?
An MCP (Model Context Protocol) server is a small Dart program that exposes your Flutter project’s data and tools to AI assistants like Claude Code over a standardized protocol. It can read pubspec.yaml and your lib directory, run flutter analyze, execute dart test, and generate code matching your project’s patterns, so the assistant works from real context instead of guesses.
How do I connect an MCP server to Claude Code?
Run claude mcp add with a server name followed by the command that starts your server, such as dart plus the path to your server script. By default this registers in local scope for the current project. To share the setup with a team, use project scope, which writes a .mcp.json at the project root you can commit to git.
Is it safe to run an MCP server on my machine?
It depends on the server’s source. MCP servers can read and write files on your disk and execute shell commands, so only run servers from sources you trust, and review any tool that runs commands like flutter analyze for input validation to prevent injection. For team or web-based deployments, add authentication before exposing the server beyond your local machine.
What can Claude Code do with a Flutter MCP server that it cannot do otherwise?
Without MCP, Claude Code only sees files you paste or that its built-in tools read. With an MCP server, it can call purpose-built tools: verify your dependency list from pubspec.yaml, run flutter analyze and report real analyzer output, execute specific dart test files, and generate BLoC or screen boilerplate that matches your existing directory structure and conventions.

Written by Mihir Pipermitwala
Senior Full Stack Consultant & Mobile Engineering Specialist building user-focused applications in healthcare with Flutter, Dart and React Native. I write about what I actually build and test — every tutorial here comes from real project work.
More about Mihir →