Skip to content

Fix notification bug where null parameters caused type error #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkgs/dart_mcp/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
- Update workflow example to show thinking spinner and input and output token
usage.
- Update file system example to support relative paths.
- Fix a bug in notification handling where leaving off the parameters caused a
type exception.

## 0.2.0

Expand Down
2 changes: 1 addition & 1 deletion pkgs/dart_mcp/lib/src/client/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ base class ServerConnection extends MCPBase {
}

/// Called after a successful call to [initialize].
void notifyInitialized(InitializedNotification notification) =>
void notifyInitialized([InitializedNotification? notification]) =>
sendNotification(InitializedNotification.methodName, notification);

/// Initializes the server, this should be done before anything else.
Expand Down
4 changes: 3 additions & 1 deletion pkgs/dart_mcp/lib/src/shared.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ base class MCPBase {
void Function(T) impl,
) => _peer.registerMethod(
name,
(Parameters p) => impl((p.value as Map?)?.cast<String, Object?>() as T),
(Parameters p) => impl(
(p.value as Map? ?? <String, Object?>{}).cast<String, Object?>() as T,
),
);

/// Sends a notification to the peer.
Expand Down
29 changes: 29 additions & 0 deletions pkgs/dart_mcp/test/client_and_server_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,35 @@ void main() {
);
});

test(
'server can handle initialized notification with null parameters',
() async {
final serverLog = StreamController<String>();
final environment = TestEnvironment(
TestMCPClient(),
(c) => TestMCPServer(c, protocolLogSink: serverLog.sink),
);
await environment.serverConnection.initialize(
InitializeRequest(
protocolVersion: ProtocolVersion.latestSupported,
capabilities: environment.client.capabilities,
clientInfo: environment.client.implementation,
),
);
// Send a notification that doesn't have any parameters.
environment.serverConnection.notifyInitialized();
await environment.server.initialized;
expect(
serverLog.stream,
emitsInOrder([
allOf(startsWith('<<<'), contains('initialize')),
allOf(startsWith('>>>'), contains('serverInfo')),
allOf(startsWith('<<<'), contains('notifications/initialized')),
]),
);
},
);

test('clients can handle progress notifications', () async {
final environment = TestEnvironment(
TestMCPClient(),
Expand Down