callTool method
Invokes a Tool returned from the ListToolsResult.
Implementation
Future<CallToolResult> callTool(CallToolRequest request) async {
try {
return await sendRequestWithInputs(CallToolRequest.methodName, request);
} on RpcException catch (e) {
// If we are set up to try and auto handle url elicitation and we get
// an error that the url elicitation is required, we will try and handle
// it and then retry the request a single time.
final data = e.data;
if (_elicitationUrlSupport?.autoHandleUrlElicitationRequired == true &&
e.code == McpErrorCodes.urlElicitationRequired &&
data is Map &&
// The schema makes `mode` required on a url elicitation, so a
// payload naming anything else does not belong on this path. Read
// it off the raw map, since the error is whatever the peer sent.
data[Keys.mode] == ElicitationMode.url.name) {
// `RpcException.serialize` spreads the error data into an untyped map
// literal (adding a `request` key), so the map arriving here is not a
// `Map<String, Object?>` and a representation type check against
// `ElicitRequest` would fail. Restore the typed view instead.
final elicitRequest = data.cast<String, Object?>() as ElicitRequest;
final elicitationComplete =
elicitRequest.onElicitationComplete(this).firstOrNull;
final elicitResult = await _elicitationUrlSupport!.handleElicitation(
elicitRequest,
this,
);
if (elicitResult.action == ElicitationAction.accept) {
await elicitationComplete;
return await sendRequestWithInputs(
CallToolRequest.methodName,
request,
);
}
}
rethrow;
}
}