code_context provides full MCP (Model Context Protocol) support for AI agents.
A ready-to-use MCP server is included. Add to ~/.cursor/mcp.json:
{
"mcpServers": {
"code_context": {
"type": "stdio",
"command": "dart",
"args": ["run", "/path/to/code_context/bin/mcp_server.dart"]
}
}
}Restart Cursor, then ask Claude to use the tools:
- "Use dart_status to check the index"
- "Use dart_index_flutter to index the Flutter SDK"
- "Use dart_query to find references to MyClass"
| Tool | Description |
|---|---|
dart_query |
Query codebase with DSL (refs, def, find, grep, etc.) |
dart_index_flutter |
Index Flutter SDK packages (~1 min, one-time) |
dart_index_deps |
Index pub dependencies from pubspec.lock |
dart_refresh |
Refresh project index and reload dependencies |
dart_status |
Show index status (files, symbols, packages loaded) |
Add CodeContextSupport to your own MCP server:
import 'package:code_context/code_context_mcp.dart';
import 'package:dart_mcp/server.dart';
base class MyServer extends MCPServer
with LoggingSupport, ToolsSupport, RootsTrackingSupport, CodeContextSupport {
// Your server implementation
}The mixin automatically:
- Registers
DartBinding()for Dart project auto-detection - Indexes project roots on first query
- Caches indexes for fast subsequent queries
- Watches for file changes and updates incrementally
- Loads pre-computed SDK/package indexes
- Watches package_config.json and notifies when deps change
Execute any DSL query:
dart_query("refs AuthService.login")
dart_query("find Auth* kind:class")
dart_query("grep TODO -l")
dart_query("find String kind:class lang:Dart")
Returns formatted text or JSON depending on query type.
Shows current index state:
{
"files": 85,
"symbols": 1234,
"references": 5678,
"packages": ["my_app", "my_core"],
"externalLoaded": ["flutter-3.32.0", "collection-1.18.0"],
"sdkLoaded": "3.7.1"
}Pre-indexes Flutter SDK for cross-package queries. Run once per Flutter version:
dart_index_flutter("/path/to/flutter")
Takes ~1-2 minutes. Enables queries like hierarchy MyWidget showing Flutter types.
Indexes all dependencies from pubspec.lock:
dart_index_deps("/path/to/project")
Run after adding new dependencies or setting up a new project.
Force refresh the index:
dart_refresh() # Normal refresh
dart_refresh(fullReindex: true) # Full re-index from scratch
Use when:
- pubspec.yaml or pubspec.lock changes
- Major refactoring
- Index seems stale
The MCP server uses auto-detection to select the appropriate language binding:
- On initialization, bindings are registered (currently
DartBinding) - When a project root is set,
CodeContext.open()auto-detects the language - The correct binding is used based on project files (
pubspec.yaml-> Dart)
Future language bindings (TypeScript, Python) will work the same way - just register them at startup and auto-detection handles the rest.