mcpToolName function

String mcpToolName(
  1. String server,
  2. String tool
)

The registered name of tool from server: mcp__<server>__<tool> with provider-hostile characters flattened to _ and the result clamped to 64 characters (the common provider tool-name limit).

Implementation

String mcpToolName(String server, String tool) {
  String sanitize(String value) {
    final cleaned = value.replaceAll(RegExp('[^a-zA-Z0-9_-]+'), '_');
    return cleaned.isEmpty ? 'x' : cleaned;
  }

  final name = 'mcp__${sanitize(server)}__${sanitize(tool)}';
  return name.length <= 64 ? name : name.substring(0, 64);
}