Skip to content

Commit ec6efa4

Browse files
authored
Fix ping request handling from non-dart clients. (#239)
Fixes #238 - Updates `PingRequest` to wrap `Map<String, Object?>` instead of `Null` to allow empty parameters handling from non-Dart clients.
1 parent 00889b4 commit ec6efa4

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

pkgs/dart_mcp/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.3.3-wip
2+
3+
- Fix `PingRequest` handling when it is sent from a non-Dart client.
4+
15
## 0.3.2
26

37
- Deprecate the `EnumSchema` type in favor of the `StringSchema` with an

pkgs/dart_mcp/lib/src/api/api.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,12 @@ extension type RequestId( /*String|int*/ Parameter _) {}
225225
///
226226
/// The receiver must promptly respond, or else may be disconnected.
227227
///
228-
/// The request itself has no parameters and should always be just `null`.
229-
extension type PingRequest._(Null _) {
228+
/// The request itself has no parameters.
229+
extension type PingRequest._(Map<String, Object?> _) implements Request {
230230
static const methodName = 'ping';
231+
232+
factory PingRequest({MetaWithProgressToken? meta}) =>
233+
PingRequest._({if (meta != null) '_meta': meta});
231234
}
232235

233236
/// An out-of-band notification used to inform the receiver of a progress

pkgs/dart_mcp/lib/src/shared.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ base class MCPBase {
131131

132132
/// The peer may ping us at any time, and we should respond with an empty
133133
/// response.
134-
///
135-
/// Note that [PingRequest] is always actually just `null`.
136134
EmptyResult _handlePing([PingRequest? _]) => EmptyResult();
137135

138136
/// Handles [ProgressNotification]s and forwards them to the streams returned
@@ -172,11 +170,13 @@ base class MCPBase {
172170
///
173171
/// If the timeout is reached, future values or errors from the ping request
174172
/// are ignored.
175-
Future<bool> ping({Duration timeout = const Duration(seconds: 1)}) =>
176-
sendRequest<EmptyResult>(
177-
PingRequest.methodName,
178-
null,
179-
).then((_) => true).timeout(timeout, onTimeout: () => false);
173+
Future<bool> ping({
174+
Duration timeout = const Duration(seconds: 1),
175+
PingRequest? request,
176+
}) => sendRequest<EmptyResult>(
177+
PingRequest.methodName,
178+
request,
179+
).then((_) => true).timeout(timeout, onTimeout: () => false);
180180

181181
/// If [protocolLogSink] is non-null, emits messages to it for all messages
182182
/// sent over [channel].

pkgs/dart_mcp/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: dart_mcp
2-
version: 0.3.2
2+
version: 0.3.3-wip
33
description: A package for making MCP servers and clients.
44
repository: https://github.com/dart-lang/ai/tree/main/pkgs/dart_mcp
55
issue_tracker: https://github.com/dart-lang/ai/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Adart_mcp

pkgs/dart_mcp/test/client_and_server_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,21 @@ void main() {
127127
);
128128
});
129129

130+
// Regression test for https://github.com/dart-lang/ai/issues/238.
131+
test('client and server can handle ping with non-null parameters', () async {
132+
final environment = TestEnvironment(TestMCPClient(), TestMCPServer.new);
133+
await environment.initializeServer();
134+
135+
await expectLater(
136+
environment.serverConnection.ping(request: PingRequest()),
137+
completes,
138+
);
139+
await expectLater(
140+
environment.server.ping(request: PingRequest()),
141+
completes,
142+
);
143+
});
144+
130145
test(
131146
'server can handle initialized notification with null parameters',
132147
() async {

0 commit comments

Comments
 (0)