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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@
- 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.
- Made form elicitation number-schema keyword validation protocol-aware:
stable 2025 keeps integer-only `minimum`, `maximum`, and `default` values,
while MCP 2026 accepts fractional number keywords.
- 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
25 changes: 20 additions & 5 deletions lib/src/client/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,18 @@ class McpClient extends Protocol {
}
return result;
},
(id, params, meta) => JsonRpcElicitRequest(
id: id,
elicitParams: ElicitRequest.fromJson(params ?? {}),
meta: meta,
),
(id, params, meta) {
final protocolVersion = _protocolVersionForIncomingRequest(meta);
return JsonRpcElicitRequest(
id: id,
elicitParams: ElicitRequest.fromJson(
params ?? {},
protocolVersion: protocolVersion,
),
meta: meta,
protocolVersion: protocolVersion,
);
},
);
}

Expand Down Expand Up @@ -623,6 +630,14 @@ class McpClient extends Protocol {
/// Gets the negotiated protocol version after connection.
String? getProtocolVersion() => _negotiatedProtocolVersion;

String? _protocolVersionForIncomingRequest(Map<String, dynamic>? meta) {
final protocolVersion = meta?[McpMetaKey.protocolVersion];
if (protocolVersion is String) {
return protocolVersion;
}
return _negotiatedProtocolVersion ?? _preferredProtocolVersion;
}

@override
bool isRecognizedResultType(String resultType) {
if (super.isRecognizedResultType(resultType)) {
Expand Down
92 changes: 69 additions & 23 deletions lib/src/types/elicitation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ class ElicitRequest {
}) : mode = ElicitationMode.url,
requestedSchema = null;

factory ElicitRequest.fromJson(Map<String, dynamic> json) {
factory ElicitRequest.fromJson(
Map<String, dynamic> json, {
String? protocolVersion,
}) {
final modeValue = json['mode'];
if (modeValue != null && modeValue is! String) {
throw const FormatException('Elicitation mode must be a string.');
Expand Down Expand Up @@ -143,7 +146,10 @@ class ElicitRequest {
if (requestedSchemaJson is! Map<String, dynamic>) {
throw const FormatException('Form elicitation requires requestedSchema.');
}
_validateFormRequestedSchemaJson(requestedSchemaJson);
_validateFormRequestedSchemaJson(
requestedSchemaJson,
protocolVersion: protocolVersion,
);
if (url != null) {
throw const FormatException('Form elicitation must not include url.');
}
Expand All @@ -161,7 +167,7 @@ class ElicitRequest {
);
}

void _validateShape() {
void _validateShape({String? protocolVersion}) {
if (isUrlMode) {
if (requestedSchema != null) {
throw ArgumentError(
Expand All @@ -181,7 +187,10 @@ class ElicitRequest {
if (requestedSchema == null) {
throw ArgumentError('Form elicitation requires requestedSchema.');
}
_validateFormRequestedSchema(requestedSchema!);
_validateFormRequestedSchema(
requestedSchema!,
protocolVersion: protocolVersion,
);
if (url != null) {
throw ArgumentError('Form elicitation must not include url.');
}
Expand All @@ -190,8 +199,8 @@ class ElicitRequest {
}
}

Map<String, dynamic> toJson() {
_validateShape();
Map<String, dynamic> toJson({String? protocolVersion}) {
_validateShape(protocolVersion: protocolVersion);
return {
if (mode != null) 'mode': mode!.name,
'message': message,
Expand All @@ -218,18 +227,29 @@ class JsonRpcElicitRequest extends JsonRpcRequest {
required super.id,
required this.elicitParams,
super.meta,
}) : super(method: Method.elicitationCreate, params: elicitParams.toJson());
String? protocolVersion,
}) : super(
method: Method.elicitationCreate,
params: elicitParams.toJson(
protocolVersion: protocolVersion ?? _protocolVersionFromMeta(meta),
),
);

factory JsonRpcElicitRequest.fromJson(Map<String, dynamic> json) {
final paramsMap = json['params'] as Map<String, dynamic>?;
if (paramsMap == null) {
throw const FormatException("Missing params for elicit request");
}
final meta = extractRequestMeta(json);
final protocolVersion = _protocolVersionFromMeta(meta);
return JsonRpcElicitRequest(
id: parseRequestId(json['id']),
elicitParams: ElicitRequest.fromJson(paramsMap),
elicitParams: ElicitRequest.fromJson(
paramsMap,
protocolVersion: protocolVersion,
),
meta: meta,
protocolVersion: protocolVersion,
);
}
}
Expand Down Expand Up @@ -432,11 +452,20 @@ typedef ElicitRequestParams = ElicitRequest;
@Deprecated('Use ElicitationCompleteNotification instead')
typedef ElicitationCompleteParams = ElicitationCompleteNotification;

void _validateFormRequestedSchema(ElicitationInputSchema schema) {
_validateFormRequestedSchemaJson(schema.toJson());
void _validateFormRequestedSchema(
ElicitationInputSchema schema, {
String? protocolVersion,
}) {
_validateFormRequestedSchemaJson(
schema.toJson(),
protocolVersion: protocolVersion,
);
}

void _validateFormRequestedSchemaJson(Map<String, dynamic> json) {
void _validateFormRequestedSchemaJson(
Map<String, dynamic> json, {
String? protocolVersion,
}) {
_ensureAllowedKeys(
json,
const {r'$schema', 'type', 'properties', 'required'},
Expand Down Expand Up @@ -467,6 +496,7 @@ void _validateFormRequestedSchemaJson(Map<String, dynamic> json) {
_validatePrimitiveSchema(
(entry.value as Map).cast<String, dynamic>(),
'ElicitRequest.requestedSchema.properties.${entry.key}',
protocolVersion: protocolVersion,
);
}
final required = json['required'];
Expand All @@ -478,7 +508,11 @@ void _validateFormRequestedSchemaJson(Map<String, dynamic> json) {
}
}

void _validatePrimitiveSchema(Map<String, dynamic> json, String context) {
void _validatePrimitiveSchema(
Map<String, dynamic> json,
String context, {
String? protocolVersion,
}) {
final type = json['type'];
switch (type) {
case 'string':
Expand All @@ -499,7 +533,11 @@ void _validatePrimitiveSchema(Map<String, dynamic> json, String context) {
context,
);
_validatePrimitiveBaseKeywords(json, context);
_validateNumberSchemaKeywords(json, context, type as String);
_validateNumberSchemaKeywords(
json,
context,
protocolVersion: protocolVersion,
);
return;
case 'boolean':
_ensureAllowedKeys(
Expand Down Expand Up @@ -532,22 +570,21 @@ void _validatePrimitiveBaseKeywords(

void _validateNumberSchemaKeywords(
Map<String, dynamic> json,
String context,
String type,
) {
if (type == 'integer') {
_validateOptionalIntegerKeyword(json, 'default', context);
String context, {
String? protocolVersion,
}) {
if (!_usesDraftNumberSchemaKeywords(protocolVersion)) {
for (final key in const ['default', 'minimum', 'maximum']) {
_validateOptionalIntegerKeyword(json, key, context);
}
return;
}

for (final key in const ['minimum', 'maximum']) {
for (final key in const ['default', 'minimum', 'maximum']) {
if (json[key] != null) {
readFiniteNumber(json[key], '$context.$key');
}
}

if (type == 'number' && json['default'] != null) {
readFiniteNumber(json['default'], '$context.default');
}
}

void _validateStringOrSingleEnumSchema(
Expand Down Expand Up @@ -718,6 +755,15 @@ void _ensureAllowedKeys(
}
}

String? _protocolVersionFromMeta(Map<String, dynamic>? meta) {
final protocolVersion = meta?[McpMetaKey.protocolVersion];
return protocolVersion is String ? protocolVersion : null;
}

bool _usesDraftNumberSchemaKeywords(String? protocolVersion) {
return protocolVersion != null && isStatelessProtocolVersion(protocolVersion);
}

Map<String, dynamic>? _parseElicitResultContent(Object? content) {
if (content == null) {
return null;
Expand Down
14 changes: 11 additions & 3 deletions lib/src/types/json_rpc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,9 @@ class InputRequest {

/// Creates an embedded `elicitation/create` input request.
factory InputRequest.elicit(ElicitRequest params) {
final inputParams = params.toJson()..remove('task');
final inputParams = params.toJson(
protocolVersion: latestDraftProtocolVersion,
)..remove('task');
return InputRequest._(
method: Method.elicitationCreate,
params: inputParams,
Expand Down Expand Up @@ -753,7 +755,10 @@ class InputRequest {
'legacy task metadata',
);
}
ElicitRequest.fromJson(params);
ElicitRequest.fromJson(
params,
protocolVersion: latestDraftProtocolVersion,
);
return InputRequest._(method: method, params: params);
case Method.samplingCreateMessage:
final params = _readRequiredJsonObject(
Expand Down Expand Up @@ -811,7 +816,10 @@ class InputRequest {
if (method != Method.elicitationCreate || params == null) {
throw StateError('InputRequest is not an elicitation/create request');
}
return ElicitRequest.fromJson(params!);
return ElicitRequest.fromJson(
params!,
protocolVersion: latestDraftProtocolVersion,
);
}

/// The typed params for an embedded `sampling/createMessage` request.
Expand Down
Loading