flutter_mcp_ui_runtime 0.2.2
flutter_mcp_ui_runtime: ^0.2.2 copied to clipboard
Runtime for building dynamic Flutter UIs from JSON with lifecycle management, state handling, and MCP protocol support.
Flutter MCP UI Runtime Examples #
This directory contains demonstration applications showcasing the Flutter MCP UI Runtime capabilities.
Overview #
The Flutter MCP UI Runtime enables dynamic UI rendering from MCP (Model Context Protocol) server definitions. This example demonstrates:
- MCP Server (
demo_mcp_server/
) - Serves UI definitions and handles real-time data - Flutter Client (
demo_mcp_client/
) - Connects to MCP server and renders dynamic UIs - Runtime Demo (
demo_mcp_runtime/
) - Standalone runtime feature demonstrations
Quick Start #
Option 1: Using the Launch Script (Recommended) #
cd demo_mcp_client
./run_demo.sh
This script will:
- Start the MCP server in the background
- Launch the Flutter client
- Handle cleanup on exit
Option 2: Manual Launch #
-
Start the MCP Server:
cd demo_mcp_server dart run bin/server.dart
-
Run the Flutter Client (in a new terminal):
cd demo_mcp_client flutter run
Architecture #
MCP Server (demo_mcp_server
) #
The server demonstrates:
- UI Resource Management: Serves dashboard, settings, and charts UIs
- Real-time Streaming: Sends live data updates via MCP notifications
- Tool Execution: Handles client tool calls for UI interactions
- State Persistence: Maintains UI state across tool calls
Key features:
- Stdio-based MCP communication
- JSON-RPC 2.0 protocol implementation
- Dynamic UI definition generation
- Real-time data simulation
Flutter Client (demo_mcp_client
) #
The client showcases:
- MCP Connection: Automatic server discovery and connection
- Dynamic UI Rendering: Renders UIs from server definitions
- State Synchronization: Maintains UI state with server updates
- Tool Integration: Executes server-side tools for actions
- Fallback Mode: Demo mode when server is unavailable
Key components:
MCPClientWrapper
: Handles MCP protocol communicationMCPUIRuntimeHelper
: Renders dynamic UIs from definitions- State management with real-time updates
- Notification handling for streaming data
Features Demonstrated #
1. Dashboard UI #
- Live Metrics: Real-time CPU, memory, network statistics
- Interactive Counter: Server-persisted counter state
- Data Streaming: Toggle real-time data updates
- Activity Feed: Recent system activities
2. Settings UI #
- Form Controls: Dropdowns, switches, sliders
- State Binding: Two-way data binding
- Conditional Rendering: Dynamic UI based on state
- Tool Execution: Save settings to server
3. Charts UI #
- Data Visualization: Chart rendering placeholder
- Dynamic Data: Generate chart data on demand
- Empty States: Conditional content display
Advanced Features #
Real-time Streaming #
// Server sends notifications
sendNotification('stream/data', {
'cpu': cpuUsage,
'memory': memoryUsage,
'timestamp': DateTime.now()
});
// Client receives updates
_mcpClient.notifications.listen((notification) {
if (notification.method == 'stream/data') {
// Update UI with streaming data
}
});
Tool Execution #
// Client calls server tool
await _mcpClient.callTool('increment_counter', {});
// Server handles tool
case 'increment_counter':
_uiStates['dashboard']['counter']++;
sendNotification('state/update', {...});
State Management #
// UI definition with state binding
{
'type': 'text',
'properties': {
'content': {'binding': '"Count: " + state.counter'}
}
}
// Actions update state
{
'type': 'state.setValue',
'path': 'counter',
'value': 0
}
Development Guide #
Adding New UIs #
-
Server Side - Add UI definition in
server.dart
:case 'ui://newui': return getNewUI();
-
Define UI Structure:
Map<String, dynamic> getNewUI() { return { 'mcpRuntime': { 'version': '1.0', 'runtime': {...}, 'ui': {...} } }; }
-
Client Auto-Discovery: The client will automatically discover and list the new UI
Adding New Tools #
-
Register Tool in server's
tools/list
:{ 'name': 'new_tool', 'description': 'Tool description', 'inputSchema': {...} }
-
Implement Handler:
case 'new_tool': return handleNewTool(args);
Custom Notifications #
-
Send from Server:
sendNotification('custom/event', {'data': value});
-
Handle in Client:
case 'custom/event': _handleCustomEvent(notification.params);
Testing #
Unit Tests #
cd demo_mcp_client
flutter test
Integration Testing #
- Start server and client
- Verify connection status
- Test UI interactions
- Validate tool execution
- Check streaming updates
Troubleshooting #
Common Issues #
-
Connection Failed
- Ensure server is running
- Check Dart is in PATH
- Verify server path in client
-
UI Not Rendering
- Check UI definition format
- Validate JSON structure
- Review console for errors
-
Tools Not Working
- Verify tool registration
- Check parameter schemas
- Review server logs
Debug Mode #
Enable verbose logging:
// In MCPClientWrapper
print('MCP Request: $request');
print('MCP Response: $response');
Resources #
License #
See the main project LICENSE file.