Files
cutdex/lib/integrations/mcp/local_mcp_bridge.dart
T
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

99 lines
2.8 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:cutdex_core/mcp.dart';
/// Private loopback transport for the stdio launcher. This is not an MCP HTTP
/// endpoint. The first line authenticates; remaining bytes are MCP stdio frames.
class LocalMcpBridge {
LocalMcpBridge._(this._listener, this._server, this._token) {
_listener.listen(_accept);
}
final ServerSocket _listener;
final CutdexMcpServer _server;
final String _token;
final _sockets = <Socket>{};
final _sessions = <Future<void>>{};
bool _closed = false;
int get port => _listener.port;
static Future<LocalMcpBridge> start(
CutdexMcpServer server, {
required int port,
required String token,
}) async {
if (port < 0 ||
port > 65535 ||
token.length < 32 ||
token.length > 1024 ||
token.contains('\n') ||
token.contains('\r')) {
throw ArgumentError(
'MCP requires a valid port and a 32-1024 character token',
);
}
final socket = await ServerSocket.bind(InternetAddress.loopbackIPv4, port);
return LocalMcpBridge._(socket, server, token);
}
void _accept(Socket socket) {
if (_closed || _sockets.length >= 32) {
socket.destroy();
return;
}
_sockets.add(socket);
final task = _serve(socket);
_sessions.add(task);
unawaited(
task.whenComplete(() {
_sessions.remove(task);
}),
);
}
Future<void> _serve(Socket socket) async {
final input = StreamIterator<List<int>>(socket);
try {
final bytes = <int>[];
final deadline = DateTime.now().add(const Duration(seconds: 5));
while (await input.moveNext().timeout(
deadline.difference(DateTime.now()),
)) {
final chunk = input.current;
final newline = chunk.indexOf(10);
final count = newline < 0 ? chunk.length : newline;
if (bytes.length + count > 4096) return;
bytes.addAll(chunk.take(count));
if (newline < 0) continue;
if (utf8.decode(bytes) != _token) return;
Stream<List<int>> frames() async* {
if (newline + 1 < chunk.length) yield chunk.sublist(newline + 1);
while (await input.moveNext()) {
yield input.current;
}
}
await serveMcpStdio(_server, input: frames(), output: socket);
return;
}
} catch (_) {
// Transport failures end this connection, not the project or other clients.
} finally {
socket.destroy();
await input.cancel();
_sockets.remove(socket);
}
}
Future<void> close() async {
if (_closed) return;
_closed = true;
await _listener.close();
for (final socket in _sockets.toList()) {
socket.destroy();
}
await Future.wait(_sessions.toList());
}
}