Skip to content

Commit 63fa00b

Browse files
authored
Add runtime errors resource and tool to clear errors. (#94)
This ended up being a fair bit more complicated than I wanted, as it also refactors things and moves us towards a direction where we can support multiple connected apps (which was a natural extension of creating the resource URI, to include the debug session ID in it). Functionally, this: - Starts collecting errors as soon as a debug session is connected, and creates a runtime errors resource for each session. - Allows us to fire change notifications for when new errors come in. - Also added a `clear_runtime_errors` tool as well as an argument to the `hotReload` tool to clear the errors prior to the hot reload. - Drops the support for `since` in favor of just clearing the errors. Eventually we might want to support pagination of the resource instead (it is already modeled as many parts), I filed a feature request on the MCP repo for this, but it likely won't land for some time if at all.
1 parent 66c01cc commit 63fa00b

File tree

9 files changed

+424
-163
lines changed

9 files changed

+424
-163
lines changed

pkgs/dart_mcp/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
`ResourceListChangedNotification`s and `ResourceUpdatedNotification`s. The
1919
delay can be modified by overriding
2020
`ResourcesSupport.resourceUpdateThrottleDelay`.
21+
- Only send notifications if the peer is still connected. Fixes issues where
22+
notifications are delayed due to throttling and the client has since closed.
2123
- **Breaking**: Fixed paginated result subtypes to use `nextCursor` instead of
2224
`cursor` as the key for the next cursor.
2325
- **Breaking**: Change the `ProgressNotification.progress` and

pkgs/dart_mcp/lib/src/shared.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ base class MCPBase {
7070

7171
/// Sends a notification to the peer.
7272
void sendNotification(String method, [Notification? notification]) =>
73-
_peer.sendNotification(method, notification);
73+
_peer.isClosed ? null : _peer.sendNotification(method, notification);
7474

7575
/// Notifies the peer of progress towards completing some request.
7676
void notifyProgress(ProgressNotification notification) =>

pkgs/dart_tooling_mcp_server/lib/src/mixins/analyzer.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'package:language_server_protocol/protocol_generated.dart' as lsp;
1313
import 'package:meta/meta.dart';
1414

1515
import '../lsp/wire_format.dart';
16+
import '../utils/constants.dart';
1617

1718
/// Mix this in to any MCPServer to add support for analyzing Dart projects.
1819
///
@@ -245,7 +246,7 @@ base mixin DartAnalyzerSupport
245246
for (var entry in diagnostics.entries) {
246247
for (var diagnostic in entry.value) {
247248
final diagnosticJson = diagnostic.toJson();
248-
diagnosticJson['uri'] = entry.key.toString();
249+
diagnosticJson[ParameterNames.uri] = entry.key.toString();
249250
messages.add(TextContent(text: jsonEncode(diagnosticJson)));
250251
}
251252
}
@@ -263,7 +264,7 @@ base mixin DartAnalyzerSupport
263264
final errorResult = await _ensurePrerequisites(request);
264265
if (errorResult != null) return errorResult;
265266

266-
final query = request.arguments!['query'] as String;
267+
final query = request.arguments![ParameterNames.query] as String;
267268
final result = await _lspConnection.sendRequest(
268269
lsp.Method.workspace_symbol.toString(),
269270
lsp.WorkspaceSymbolParams(query: query).toJson(),
@@ -306,7 +307,7 @@ base mixin DartAnalyzerSupport
306307
);
307308
diagnostics[diagnosticParams.uri] = diagnosticParams.diagnostics;
308309
log(LoggingLevel.debug, {
309-
'uri': diagnosticParams.uri,
310+
ParameterNames.uri: diagnosticParams.uri,
310311
'diagnostics':
311312
diagnosticParams.diagnostics.map((d) => d.toJson()).toList(),
312313
});
@@ -369,14 +370,14 @@ base mixin DartAnalyzerSupport
369370
description: 'Look up a symbol or symbols in all workspaces by name.',
370371
inputSchema: Schema.object(
371372
properties: {
372-
'query': Schema.string(
373+
ParameterNames.query: Schema.string(
373374
description:
374375
'Queries are matched based on a case-insensitive partial name '
375376
'match, and do not support complex pattern matching, regexes, '
376377
'or scoped lookups.',
377378
),
378379
},
379-
required: ['query'],
380+
required: [ParameterNames.query],
380381
),
381382
annotations: ToolAnnotations(title: 'Project search', readOnlyHint: true),
382383
);

0 commit comments

Comments
 (0)