Tidy MCP deadline guard for analyzer
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
file-length / file length <= 250 lines (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run

Guard moves to request_guard.dart (file-length gate); README notes
the token reload, deadline and timeouts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWXgrX1QxVjqMiz6KFs
This commit is contained in:
Krzysztof Rudnicki 2026-11-16 08:06:05 +01:00
parent e885465f55
commit b350b3e8c1
4 changed files with 49 additions and 38 deletions

View File

@ -0,0 +1,40 @@
import 'dart:async';
import 'dart:io';
/// Answers 503 here because the library can only say 403 for a refusal; it
/// then fails to write its own 403 ("Header already sent", swallowed by the
/// zone guard in `TodoMcpServer.start`) and the request stays refused.
bool answerUnavailable(HttpRequest request) {
request.response
..statusCode = HttpStatus.serviceUnavailable
..write('Service Unavailable')
..close().ignore();
return false;
}
/// Closes [request]'s response with 503 once [deadline] passes, unless it was
/// answered first. Safe on any state: a response already closed or a dead
/// socket is ignored.
void guardDeadline(HttpRequest request, Duration deadline) {
if (request.method == 'GET') return;
final response = request.response;
final timer = Timer(deadline, () async {
try {
response
..statusCode = HttpStatus.serviceUnavailable
..write('Request timed out');
} on Object {
// Headers already sent: just end the stream.
}
try {
await response.close();
} on Object {
// The peer is gone; the socket is released either way.
}
});
unawaited(
response.done
.then<void>((_) {}, onError: (_) {})
.whenComplete(timer.cancel),
);
}

View File

@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:io';
import 'package:mcp_dart/mcp_dart.dart';
import 'package:todo/mcp/request_guard.dart';
import 'package:todo/mcp/task_service.dart';
import 'package:todo/mcp/token_file.dart';
@ -59,13 +60,13 @@ class TodoMcpServer {
port: port,
authenticator: (request) {
if (request is! HttpRequest) return false;
_guardDeadline(request, requestDeadline);
guardDeadline(request, requestDeadline);
return switch (store.verdict(
request.headers.value(HttpHeaders.authorizationHeader),
)) {
TokenVerdict.accepted => true,
TokenVerdict.rejected => false,
TokenVerdict.unavailable => _answerUnavailable(request),
TokenVerdict.unavailable => answerUnavailable(request),
};
},
allowedHosts: const {'localhost', '127.0.0.1', '::1', _publicHost},
@ -86,40 +87,6 @@ class TodoMcpServer {
Future<void> close() => _http.stop();
}
/// Answers 503 here because the library can only say 403 for a refusal; it
/// then fails to write its own 403 ("Header already sent", swallowed by the
/// zone guard in [TodoMcpServer.start]) and the request stays refused.
bool _answerUnavailable(HttpRequest request) {
request.response
..statusCode = HttpStatus.serviceUnavailable
..write('Service Unavailable')
..close().ignore();
return false;
}
/// Closes [request]'s response with 503 once [deadline] passes, unless it was
/// answered first. Safe on any state: a response already closed or a dead
/// socket is ignored.
void _guardDeadline(HttpRequest request, Duration deadline) {
if (request.method == 'GET') return;
final response = request.response;
final timer = Timer(deadline, () async {
try {
response
..statusCode = HttpStatus.serviceUnavailable
..write('Request timed out');
} on StateError {
// Headers already sent: just end the stream.
}
try {
await response.close();
} on Object {
// The peer is gone; the socket is released either way.
}
});
response.done.then<void>((_) {}, onError: (_) {}).whenComplete(timer.cancel);
}
McpServer _server(TaskService tasks) {
final server = McpServer(
const Implementation(name: 'todo', version: '1.0.0'),

View File

@ -33,4 +33,8 @@ systemctl --user enable --now todo-mcp.service
```
Deploy a new build: `mcp/install.sh` (restarts a running unit).
Revoke a token: delete its line in `tokens`; no restart needed.
Revoke a token: delete its line in `tokens`; no restart needed. The file is
loaded once and re-read when its mtime or size changes; a read error (or a
deleted file) keeps the last good set, and before any good load the answer is
503, not 403. A POST open for 90 s gets 503 and its socket closed; sync HTTP
calls time out after 30 s; the unit sets `LimitNOFILE=65536`.

View File

@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.0.93+1
version: 1.0.94+1
environment:
sdk: ^3.12.2