forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_mcp_server_page.dart
More file actions
293 lines (268 loc) · 10.3 KB
/
Copy pathadd_mcp_server_page.dart
File metadata and controls
293 lines (268 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:omi/backend/http/api/apps.dart';
import 'package:omi/backend/schema/app.dart';
import 'package:omi/pages/apps/app_detail/app_detail.dart';
import 'package:omi/utils/l10n_extensions.dart';
import 'package:omi/utils/other/temp.dart';
class AddMcpServerPage extends StatefulWidget {
const AddMcpServerPage({super.key});
@override
State<AddMcpServerPage> createState() => _AddMcpServerPageState();
}
class _AddMcpServerPageState extends State<AddMcpServerPage> {
final _nameController = TextEditingController();
final _urlController = TextEditingController();
final _descriptionController = TextEditingController();
final _formKey = GlobalKey<FormState>();
bool _isLoading = false;
bool _isPolling = false;
String? _appId;
Timer? _pollTimer;
@override
void dispose() {
_nameController.dispose();
_urlController.dispose();
_descriptionController.dispose();
_pollTimer?.cancel();
super.dispose();
}
Future<void> _connect() async {
if (!_formKey.currentState!.validate()) return;
setState(() => _isLoading = true);
final result = await addMcpServer(
_nameController.text.trim(),
_urlController.text.trim(),
description: _descriptionController.text.trim(),
);
if (!mounted) return;
setState(() => _isLoading = false);
if (result == null) {
_showError(context.l10n.mcpConnectionFailed);
return;
}
if (result.containsKey('error')) {
_showError(result['error'] as String);
return;
}
_appId = result['app_id'] as String?;
final requiresOauth = result['requires_oauth'] as bool? ?? false;
if (requiresOauth) {
final authUrl = result['auth_url'] as String?;
if (authUrl != null) {
await _openAuthInAppBrowser(authUrl);
} else {
_showError(context.l10n.mcpConnectionFailed);
}
} else {
final toolsCount = result['tools_count'] as int? ?? 0;
if (mounted) {
try {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(context.l10n.mcpServerConnected(toolsCount)), backgroundColor: Colors.green),
);
} catch (_) {}
_navigateToAppDetail(_appId!);
}
}
}
Future<void> _openAuthInAppBrowser(String authUrl) async {
final uri = Uri.parse(authUrl);
try {
await launchUrl(uri, mode: LaunchMode.inAppBrowserView);
} on PlatformException {
try {
await launchUrl(uri, mode: LaunchMode.externalApplication);
} on PlatformException {
if (mounted) _showError(context.l10n.mcpConnectionFailed);
return;
}
}
// Start polling for OAuth completion
_startPollingForCompletion();
}
void _startPollingForCompletion() {
if (_appId == null) return;
setState(() => _isPolling = true);
int attempts = 0;
const maxAttempts = 100; // ~5 minutes at 3s intervals
_pollTimer = Timer.periodic(const Duration(seconds: 3), (timer) async {
attempts++;
if (attempts > maxAttempts || !mounted) {
timer.cancel();
if (mounted) {
setState(() => _isPolling = false);
_showError(context.l10n.mcpConnectionFailed);
}
return;
}
final appData = await getAppDetailsServer(_appId!);
if (appData == null) return;
final status = appData['status'] as String?;
if (status == 'approved') {
timer.cancel();
final chatTools = appData['chat_tools'] as List?;
final toolsCount = chatTools?.length ?? 0;
if (mounted) {
setState(() => _isPolling = false);
try {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(context.l10n.mcpServerConnected(toolsCount)), backgroundColor: Colors.green),
);
final app = App.fromJson(appData);
Navigator.pop(context);
routeToPage(context, AppDetailPage(app: app));
} catch (_) {}
}
}
});
}
Future<void> _navigateToAppDetail(String appId) async {
final appData = await getAppDetailsServer(appId);
if (!mounted) return;
if (appData != null) {
final app = App.fromJson(appData);
Navigator.pop(context);
routeToPage(context, AppDetailPage(app: app));
} else {
Navigator.pop(context, true);
}
}
void _showError(String message) {
if (!mounted) return;
try {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(message), backgroundColor: Colors.red));
} catch (_) {
// Widget may be deactivated during async navigation
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).colorScheme.surface,
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.surface,
title: Text(context.l10n.addMcpServer, style: const TextStyle(fontSize: 18)),
leading: IconButton(icon: const Icon(Icons.arrow_back), onPressed: () => Navigator.pop(context)),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
context.l10n.connectExternalAiTools,
style: Theme.of(context).textTheme.bodyLarge?.copyWith(color: Colors.white.withValues(alpha: 0.7)),
),
const SizedBox(height: 32),
TextFormField(
controller: _nameController,
decoration: InputDecoration(
labelText: context.l10n.appName,
hintText: 'e.g. Mixpanel Analytics',
border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(color: Colors.white.withValues(alpha: 0.3)),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: const BorderSide(color: Colors.white, width: 1.5),
),
),
validator: (value) {
if (value == null || value.trim().isEmpty) {
return context.l10n.appName;
}
return null;
},
),
const SizedBox(height: 16),
TextFormField(
controller: _descriptionController,
decoration: InputDecoration(
labelText: context.l10n.descriptionOptional,
border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(color: Colors.white.withValues(alpha: 0.3)),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: const BorderSide(color: Colors.white, width: 1.5),
),
),
maxLines: 2,
),
const SizedBox(height: 16),
TextFormField(
controller: _urlController,
decoration: InputDecoration(
labelText: context.l10n.mcpServerUrl,
hintText: 'https://mcp.example.com/sse',
border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(color: Colors.white.withValues(alpha: 0.3)),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: const BorderSide(color: Colors.white, width: 1.5),
),
),
keyboardType: TextInputType.url,
autocorrect: false,
validator: (value) {
if (value == null || value.trim().isEmpty) {
return context.l10n.mcpServerUrl;
}
final uri = Uri.tryParse(value.trim());
if (uri == null || !uri.hasScheme || !uri.host.contains('.')) {
return context.l10n.mcpServerUrl;
}
return null;
},
),
const SizedBox(height: 32),
SizedBox(
width: double.infinity,
height: 52,
child: ElevatedButton(
onPressed: (_isLoading || _isPolling) ? null : _connect,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.white,
foregroundColor: Colors.black,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
),
child: (_isLoading || _isPolling)
? Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(strokeWidth: 2, color: Colors.black),
),
if (_isPolling) ...[
const SizedBox(width: 12),
Text(
context.l10n.authorizingMcpServer,
style: const TextStyle(fontSize: 14, color: Colors.black),
),
],
],
)
: Text(context.l10n.connect, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600)),
),
),
],
),
),
),
);
}
}