onSamplingRequest method
Register a handler for server-initiated sampling/createMessage
requests. The driver app (Claude Desktop / Claude Code / a vibe-style
IDE) fulfils the prompt with its own LLM and returns the spec
CreateMessageResult (role, content, model, optional
stopReason).
Calling this advertises the sampling client capability during the
next initialize handshake.
Implementation
void onSamplingRequest(
Future<CreateMessageResult> Function(CreateMessageRequest request) handler,
) {
_requestHandlers['sampling/createMessage'] = (params) async {
final req = CreateMessageRequest.fromJson(params);
final result = await handler(req);
return result.toJson();
};
}