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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
stable and MCP 2026 schemas.
- Rejected form elicitation schemas that provide legacy `enumNames` without the
required string `enum`.
- Rejected `ElicitResult.content` when the result action is `decline` or
`cancel`.

## 2.2.0

Expand Down
33 changes: 31 additions & 2 deletions lib/src/types/elicitation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,16 @@ class ElicitResult implements BaseResultData {
throw FormatException('Invalid elicitation action: $action');
}

final content = _parseElicitResultContent(json['content']);
_validateElicitResultContentForAction(
action,
content,
formatException: true,
);

return ElicitResult(
action: action,
content: _parseElicitResultContent(json['content']),
content: content,
url: json['url'] as String?,
elicitationId: json['elicitationId'] as String?,
meta: (json['_meta'] as Map?)?.cast<String, dynamic>(),
Expand All @@ -278,9 +285,11 @@ class ElicitResult implements BaseResultData {

@override
Map<String, dynamic> toJson() {
final resultAction = action;
_validateElicitResultContentForAction(resultAction, content);
_validateElicitResultContent(content);
return {
'action': action,
'action': resultAction,
if (content != null) 'content': content,
if (meta != null) '_meta': meta,
};
Expand Down Expand Up @@ -638,6 +647,26 @@ void _validateElicitResultContent(
}
}

void _validateElicitResultContentForAction(
String action,
Map<String, dynamic>? content, {
bool formatException = false,
}) {
if (content == null || action == 'accept') {
return;
}
if (formatException) {
throw const FormatException(
'ElicitResult.content is only allowed when action is accept.',
);
}
throw ArgumentError.value(
content,
'content',
'ElicitResult.content is only allowed when action is accept.',
);
}

void _validateUrlElicitations(
List<ElicitRequest> elicitations, {
bool formatException = false,
Expand Down
18 changes: 18 additions & 0 deletions test/elicitation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,15 @@ void main() {
}),
throwsA(isA<FormatException>()),
);
expect(
() => ElicitResult.fromJson({
'action': 'decline',
'content': {
'name': 'Alice',
},
}),
throwsA(isA<FormatException>()),
);
expect(
() => const ElicitResult(
action: 'accept',
Expand All @@ -1069,6 +1078,15 @@ void main() {
).toJson(),
throwsA(isA<ArgumentError>()),
);
expect(
() => const ElicitResult(
action: 'cancel',
content: {
'name': 'Alice',
},
).toJson(),
throwsA(isA<ArgumentError>()),
);
});

test('URLElicitationRequiredErrorData validates URL-only entries', () {
Expand Down