Skip to content
Closed
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
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@
clamping malformed wire values to zero.
- Validated MRTR `inputResponses` as `CreateMessageResult`, `ListRootsResult`,
or `ElicitResult` instead of accepting arbitrary result objects.
- Allowed finite numeric `ElicitResult.content` values to match the stable and
MCP 2026 `string | number | boolean | string[]` schema.
- Restricted numeric `ElicitResult.content` values to integers, matching the
stable and MCP 2026 `string | integer | boolean | string[]` schemas while
still accepting whole-number JSON numeric values.
- Rejected form elicitation schemas that provide legacy `enumNames` without the
required string `enum`.
- Rejected `ElicitResult.content` when the result action is `decline` or
Expand Down
28 changes: 19 additions & 9 deletions lib/src/types/elicitation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,10 @@ class ElicitResult implements BaseResultData {
Map<String, dynamic> toJson() {
final resultAction = action;
_validateElicitResultContentForAction(resultAction, content);
_validateElicitResultContent(content);
final normalizedContent = _normalizeElicitResultContent(content);
return {
'action': resultAction,
if (content != null) 'content': content,
if (normalizedContent != null) 'content': normalizedContent,
if (meta != null) '_meta': readJsonObject(meta, 'ElicitResult._meta'),
};
}
Expand Down Expand Up @@ -726,39 +726,49 @@ Map<String, dynamic>? _parseElicitResultContent(Object? content) {
throw const FormatException('ElicitResult.content must be an object.');
}
final result = content.cast<String, dynamic>();
_validateElicitResultContent(result, formatException: true);
return result;
return _normalizeElicitResultContent(result, formatException: true);
}

void _validateElicitResultContent(
Map<String, dynamic>? _normalizeElicitResultContent(
Map<String, dynamic>? content, {
bool formatException = false,
}) {
if (content == null) {
return;
return null;
}
final normalized = <String, dynamic>{};
for (final entry in content.entries) {
final value = entry.value;
if (value is String || value is bool) {
normalized[entry.key] = value;
continue;
}
if (value is int) {
normalized[entry.key] = value;
continue;
}
if (value is num && value.isFinite) {
if (value is double &&
value.isFinite &&
value == value.truncateToDouble()) {
normalized[entry.key] = value.toInt();
continue;
}
if (value is List && value.every((item) => item is String)) {
normalized[entry.key] = List<String>.from(value);
continue;
}
if (formatException) {
throw FormatException(
'ElicitResult.content.${entry.key} must be string, finite number, boolean, or string[]',
'ElicitResult.content.${entry.key} must be string, integer, boolean, or string[]',
);
}
throw ArgumentError.value(
value,
'content.${entry.key}',
'ElicitResult content values must be string, finite number, boolean, or string[]',
'ElicitResult content values must be string, integer, boolean, or string[]',
);
}
return normalized;
}

void _validateElicitResultContentForAction(
Expand Down
21 changes: 15 additions & 6 deletions test/elicitation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@ void main() {
'action': 'accept',
'content': {
'text': 'value',
'count': 3,
'count': 3.0,
'confirmed': true,
'selections': ['a', 'b'],
},
Expand All @@ -1143,13 +1143,13 @@ void main() {
throwsA(isA<FormatException>()),
);
expect(
ElicitResult.fromJson({
() => ElicitResult.fromJson({
'action': 'accept',
'content': {
'ratio': 0.5,
},
}).content?['ratio'],
0.5,
}),
throwsA(isA<FormatException>()),
);
expect(
() => ElicitResult.fromJson({
Expand All @@ -1170,13 +1170,22 @@ void main() {
throwsA(isA<ArgumentError>()),
);
expect(
const ElicitResult(
() => const ElicitResult(
action: 'accept',
content: {
'ratio': 0.5,
},
).toJson(),
throwsA(isA<ArgumentError>()),
);
expect(
const ElicitResult(
action: 'accept',
content: {
'count': 3.0,
},
).toJson()['content'],
containsPair('ratio', 0.5),
containsPair('count', 3),
);
expect(
() => const ElicitResult(
Expand Down
24 changes: 21 additions & 3 deletions test/mcp_2025_11_25_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -330,17 +330,17 @@ void main() {
action: 'accept',
content: {
'text': 'answer',
'confidence': 0.75,
'confidence': 75,
'selection': ['a', 'b'], // List<String>
},
);
expect(result.content?['confidence'], 0.75);
expect(result.content?['confidence'], 75);
expect(result.content?['selection'], isA<List>());
expect((result.content?['selection'] as List).first, 'a');

final json = result.toJson();
final deserialized = ElicitResult.fromJson(json);
expect(deserialized.content?['confidence'], 0.75);
expect(deserialized.content?['confidence'], 75);
expect((deserialized.content?['selection'] as List).last, 'b');
});

Expand Down Expand Up @@ -1421,6 +1421,24 @@ void main() {
).toJson(),
throwsA(isA<ArgumentError>()),
);
expect(
() => ElicitResult.fromJson({
'action': 'accept',
'content': {
'fractional': 1.5,
},
}),
throwsA(isA<FormatException>()),
);
expect(
() => const ElicitResult(
action: 'accept',
content: {
'fractional': 1.5,
},
).toJson(),
throwsA(isA<ArgumentError>()),
);
expect(
() => URLElicitationRequiredErrorData.fromJson({
'elicitations': [
Expand Down
14 changes: 14 additions & 0 deletions test/mcp_2026_07_28_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -428,13 +428,27 @@ void main() {
}),
throwsA(isA<FormatException>()),
);
expect(
() => ElicitResult.fromJson({
'action': 'accept',
'content': {'score': 1.5},
}),
throwsA(isA<FormatException>()),
);
expect(
() => const ElicitResult(
action: 'accept',
content: {'score': double.nan},
).toJson(),
throwsA(isA<ArgumentError>()),
);
expect(
() => const ElicitResult(
action: 'accept',
content: {'score': 1.5},
).toJson(),
throwsA(isA<ArgumentError>()),
);
});

test('rejects non-JSON sampling object values', () {
Expand Down