discover method
Discovers an MCP 2026 server without creating a protocol session.
Implementation
Future<McpStatelessDiscoveryResult> discover({
Object? id,
Map<String, String> headers = const <String, String>{},
}) async {
final response = await request(
'server/discover',
id: id,
protocolVersion: latestProtocolVersion,
headers: headers,
);
final result = _jsonRpcResultFrom(response, method: 'server/discover');
final rawVersions = result['supportedVersions'];
if (rawVersions is! List ||
rawVersions.isEmpty ||
rawVersions.any((version) => version is! String || version.isEmpty)) {
throw const FormatException(
'server/discover supportedVersions must be a non-empty string array',
);
}
final capabilities = _jsonMapFrom(
result['capabilities'],
label: 'server/discover capabilities',
);
final metadata = result['_meta'];
final serverInfoValue = metadata is Map
? _jsonMapFrom(
metadata,
label: 'server/discover result metadata',
)['io.modelcontextprotocol/serverInfo']
: null;
final serverInfo = serverInfoValue == null
? null
: _jsonMapFrom(serverInfoValue, label: 'server/discover server info');
final instructions = result['instructions'];
if (instructions != null && instructions is! String) {
throw const FormatException(
'server/discover instructions must be a string',
);
}
final ttlMs = result['ttlMs'];
if (ttlMs != null && (ttlMs is! int || ttlMs < 0)) {
throw const FormatException(
'server/discover ttlMs must be a non-negative integer',
);
}
final cacheScope = result['cacheScope'];
if (cacheScope != null && cacheScope is! String) {
throw const FormatException(
'server/discover cacheScope must be a string',
);
}
return McpStatelessDiscoveryResult(
supportedVersions: List<String>.unmodifiable(rawVersions.cast<String>()),
capabilities: capabilities,
serverInfo: serverInfo,
instructions: instructions as String?,
ttlMs: ttlMs as int?,
cacheScope: cacheScope as String?,
);
}