registerTool method
Register tool to call impl when invoked.
If this server is already initialized and still connected to a client, then the client will be notified that the tools list has changed.
Throws a StateError if there is already a Tool registered with the same name.
If validateArguments is true, then request arguments are automatically
validated against the tools input schema.
Implementation
void registerTool(
Tool tool,
FutureOr<CallToolResult> Function(CallToolRequest) impl, {
bool validateArguments = true,
}) {
if (_registeredTools.containsKey(tool.name)) {
throw StateError(
'Failed to register tool ${tool.name}, it is already registered',
);
}
_registeredTools[tool.name] = tool;
_registeredToolImpls[tool.name] =
validateArguments
? (request) {
final errors = tool.inputSchema.validate(
request.arguments ?? const <String, Object?>{},
);
if (errors.isNotEmpty) {
return CallToolResult(
content: [
for (final error in errors)
Content.text(text: error.toErrorString()),
],
isError: true,
);
}
return impl(request);
}
: impl;
if (ready) {
_notifyToolListChanged();
}
}