Skip to content

Commit d02e6e0

Browse files
committed
Fix notification bug where null parameters caused type error
1 parent c94bfc5 commit d02e6e0

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

pkgs/dart_mcp/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
- Update workflow example to show thinking spinner and input and output token
55
usage.
66
- Update file system example to support relative paths.
7+
- Fix a bug in notification handling where leaving off the parameters caused a
8+
type exception.
79

810
## 0.2.0
911

pkgs/dart_mcp/lib/src/client/client.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ base class ServerConnection extends MCPBase {
257257
}
258258

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

263263
/// Initializes the server, this should be done before anything else.

pkgs/dart_mcp/lib/src/shared.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ base class MCPBase {
6868
void Function(T) impl,
6969
) => _peer.registerMethod(
7070
name,
71-
(Parameters p) => impl((p.value as Map?)?.cast<String, Object?>() as T),
71+
(Parameters p) => impl(
72+
(p.value as Map? ?? <String, Object?>{}).cast<String, Object?>() as T,
73+
),
7274
);
7375

7476
/// Sends a notification to the peer.

pkgs/dart_mcp/test/client_and_server_test.dart

+29
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,35 @@ void main() {
121121
);
122122
});
123123

124+
test(
125+
'server can handle initialized notification with null parameters',
126+
() async {
127+
final serverLog = StreamController<String>();
128+
final environment = TestEnvironment(
129+
TestMCPClient(),
130+
(c) => TestMCPServer(c, protocolLogSink: serverLog.sink),
131+
);
132+
await environment.serverConnection.initialize(
133+
InitializeRequest(
134+
protocolVersion: ProtocolVersion.latestSupported,
135+
capabilities: environment.client.capabilities,
136+
clientInfo: environment.client.implementation,
137+
),
138+
);
139+
// Send a notification that doesn't have any parameters.
140+
environment.serverConnection.notifyInitialized();
141+
await environment.server.initialized;
142+
expect(
143+
serverLog.stream,
144+
emitsInOrder([
145+
allOf(startsWith('<<<'), contains('initialize')),
146+
allOf(startsWith('>>>'), contains('serverInfo')),
147+
allOf(startsWith('<<<'), contains('notifications/initialized')),
148+
]),
149+
);
150+
},
151+
);
152+
124153
test('clients can handle progress notifications', () async {
125154
final environment = TestEnvironment(
126155
TestMCPClient(),

0 commit comments

Comments
 (0)