Files
cutdex/lib/integrations/mcp/mcp_endpoint_controller.dart
lingjhf 06f1c26681
Gitea CI / Assemble and validate CI artifacts (push) Successful in 1m47s
Gitea CI / Check code and tests (push) Successful in 7m15s
Gitea CI / Build Android APK (push) Successful in 4m6s
Gitea CI / Build Windows x64 app (push) Successful in 3m34s
Gitea CI / Audit and package Windows x64 (push) Successful in 1m29s
Gitea CI / Gitea CI passed (push) Successful in 1s
Gitea Release / Prepare draft Release (push) Failing after 8s
Gitea Release / Upload Android to draft Release (push) Skipped
Gitea Release / Upload Windows to draft Release (push) Skipped
Gitea Release / Verify and publish Release (push) Skipped
Move editor UI into the app and release verified Core dependencies (#14)
2026-10-11 19:59:40 +00:00

53 lines
1.2 KiB
Dart

import 'dart:async';
import 'package:cutdex_core/mcp.dart';
import 'local_mcp_bridge.dart';
/// Serializes endpoint replacement so late cleanup cannot close its successor.
class EditorMcpEndpoint {
Future<void> _tail = Future.value();
LocalMcpBridge? _current;
bool _closed = false;
Future<T> _run<T>(Future<T> Function() action) {
final result = _tail.then((_) => action());
_tail = result.then<void>((_) {}, onError: (Object _, StackTrace _) {});
return result;
}
Future<LocalMcpBridge> open(
CutdexMcpServer server, {
required int port,
required String token,
}) => _run(() async {
if (_closed) throw StateError('MCP endpoint owner is closed');
await _current?.close();
_current = null;
return _current = await LocalMcpBridge.start(
server,
port: port,
token: token,
);
});
Future<void> release(LocalMcpBridge bridge) => _run(() async {
if (!identical(_current, bridge)) return;
await bridge.close();
_current = null;
});
Future<void> close() {
_closed = true;
return _run(() async {
await _current?.close();
_current = null;
});
}
Future<void> stop() => _run(() async {
await _current?.close();
_current = null;
});
}