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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env -S uv run
|
| 2 |
+
# /// script
|
| 3 |
+
# requires-python = ">=3.10"
|
| 4 |
+
# dependencies = [
|
| 5 |
+
# "datasets",
|
| 6 |
+
# "huggingface_hub",
|
| 7 |
+
# ]
|
| 8 |
+
# ///
|
| 9 |
+
|
| 10 |
+
"""
|
| 11 |
+
Deduplicate MCP clients by name and capabilities.
|
| 12 |
+
|
| 13 |
+
This script takes the existing mcp-clients dataset and creates a deduplicated view
|
| 14 |
+
where rows are unique by (name, capabilities). When multiple versions exist for
|
| 15 |
+
the same (name, capabilities), they are concatenated with "," in a versions column.
|
| 16 |
+
|
| 17 |
+
It also strips " (via mcp-remote X.Y.Z)" suffixes from client names and reports
|
| 18 |
+
the mcp-remote versions encountered.
|
| 19 |
+
|
| 20 |
+
Usage:
|
| 21 |
+
uv run deduplicate_clients.py [--source-repo REPO] [--target-repo REPO]
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
import sys
|
| 25 |
+
import json
|
| 26 |
+
import re
|
| 27 |
+
import argparse
|
| 28 |
+
from datetime import datetime
|
| 29 |
+
from collections import defaultdict
|
| 30 |
+
|
| 31 |
+
from datasets import Dataset, Features, Value, load_dataset
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Regex to match "(via mcp-remote X.Y.Z)" suffix
|
| 35 |
+
MCP_REMOTE_PATTERN = re.compile(r'\s*\(via mcp-remote [\d.]+\)')
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def strip_mcp_remote_suffix(name):
|
| 39 |
+
"""Remove the '(via mcp-remote X.Y.Z)' suffix from client name."""
|
| 40 |
+
match = MCP_REMOTE_PATTERN.search(name)
|
| 41 |
+
if match:
|
| 42 |
+
# Extract the version number from the suffix
|
| 43 |
+
suffix = match.group(0)
|
| 44 |
+
version_match = re.search(r'mcp-remote ([\d.]+)', suffix)
|
| 45 |
+
if version_match:
|
| 46 |
+
version = version_match.group(1)
|
| 47 |
+
return MCP_REMOTE_PATTERN.sub('', name).strip(), version
|
| 48 |
+
return name, None
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def normalize_capabilities(caps):
|
| 52 |
+
"""Normalize capabilities for comparison."""
|
| 53 |
+
if caps is None:
|
| 54 |
+
return None
|
| 55 |
+
if isinstance(caps, str):
|
| 56 |
+
if caps == '{}':
|
| 57 |
+
return {}
|
| 58 |
+
try:
|
| 59 |
+
return json.loads(caps)
|
| 60 |
+
except Exception:
|
| 61 |
+
return caps
|
| 62 |
+
return caps
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def main():
|
| 66 |
+
parser = argparse.ArgumentParser(description="Deduplicate MCP clients by name and capabilities")
|
| 67 |
+
parser.add_argument("--source-repo", default="evalstate/mcp-clients", help="Source repository ID")
|
| 68 |
+
parser.add_argument("--target-repo", default="evalstate/mcp-clients-dedup", help="Target repository ID")
|
| 69 |
+
args = parser.parse_args()
|
| 70 |
+
|
| 71 |
+
print(f"[{datetime.now().isoformat()}] Loading dataset from {args.source_repo}...", file=sys.stderr)
|
| 72 |
+
|
| 73 |
+
# Load the source dataset
|
| 74 |
+
ds = load_dataset(args.source_repo, split='train')
|
| 75 |
+
|
| 76 |
+
print(f"[{datetime.now().isoformat()}] Loaded {len(ds):,} client records", file=sys.stderr)
|
| 77 |
+
|
| 78 |
+
# Track mcp-remote versions found
|
| 79 |
+
mcp_remote_versions = defaultdict(int)
|
| 80 |
+
|
| 81 |
+
# Group by (name, capabilities)
|
| 82 |
+
groups = defaultdict(list)
|
| 83 |
+
|
| 84 |
+
for row in ds:
|
| 85 |
+
# Strip mcp-remote suffix from name
|
| 86 |
+
clean_name, mcp_version = strip_mcp_remote_suffix(row['name'])
|
| 87 |
+
|
| 88 |
+
if mcp_version:
|
| 89 |
+
mcp_remote_versions[mcp_version] += 1
|
| 90 |
+
|
| 91 |
+
# Normalize capabilities for grouping
|
| 92 |
+
caps = row['capabilities']
|
| 93 |
+
if isinstance(caps, str):
|
| 94 |
+
try:
|
| 95 |
+
caps_dict = json.loads(caps)
|
| 96 |
+
caps_key = json.dumps(caps_dict, sort_keys=True)
|
| 97 |
+
except Exception:
|
| 98 |
+
caps_key = caps
|
| 99 |
+
else:
|
| 100 |
+
caps_key = json.dumps(caps, sort_keys=True) if caps else ''
|
| 101 |
+
|
| 102 |
+
key = (clean_name, caps_key)
|
| 103 |
+
groups[key].append({
|
| 104 |
+
'name': clean_name,
|
| 105 |
+
'version': row['version'],
|
| 106 |
+
'capabilities': caps_key,
|
| 107 |
+
'last_seen': row['last_seen'],
|
| 108 |
+
})
|
| 109 |
+
|
| 110 |
+
print(f"[{datetime.now().isoformat()}] Found {len(groups):,} unique (name, capabilities) combinations", file=sys.stderr)
|
| 111 |
+
|
| 112 |
+
# Print mcp-remote version summary
|
| 113 |
+
if mcp_remote_versions:
|
| 114 |
+
total_with_mcp_remote = sum(mcp_remote_versions.values())
|
| 115 |
+
print(f"[{datetime.now().isoformat()}] MCP-remote version summary:", file=sys.stderr)
|
| 116 |
+
print(f" Total records with mcp-remote suffix: {total_with_mcp_remote:,}", file=sys.stderr)
|
| 117 |
+
print(f" Unique mcp-remote versions: {len(mcp_remote_versions)}", file=sys.stderr)
|
| 118 |
+
print(f" Versions encountered:", file=sys.stderr)
|
| 119 |
+
for version in sorted(mcp_remote_versions.keys()):
|
| 120 |
+
print(f" - mcp-remote {version}: {mcp_remote_versions[version]:,} records", file=sys.stderr)
|
| 121 |
+
print()
|
| 122 |
+
|
| 123 |
+
# Create deduplicated records
|
| 124 |
+
deduplicated = []
|
| 125 |
+
|
| 126 |
+
for (name, caps_key), records in groups.items():
|
| 127 |
+
# Find the latest last_seen
|
| 128 |
+
latest_record = max(records, key=lambda x: x['last_seen'])
|
| 129 |
+
|
| 130 |
+
# Collect all unique versions (sorted)
|
| 131 |
+
versions = sorted(set(r['version'] for r in records))
|
| 132 |
+
versions_str = ','.join(versions)
|
| 133 |
+
|
| 134 |
+
deduplicated.append({
|
| 135 |
+
'name': name,
|
| 136 |
+
'versions': versions_str,
|
| 137 |
+
'capabilities': caps_key,
|
| 138 |
+
'last_seen': latest_record['last_seen'],
|
| 139 |
+
})
|
| 140 |
+
|
| 141 |
+
# Sort by last_seen descending (so latest are at the beginning)
|
| 142 |
+
deduplicated.sort(key=lambda x: x['last_seen'], reverse=True)
|
| 143 |
+
|
| 144 |
+
print(f"[{datetime.now().isoformat()}] Created {len(deduplicated):,} deduplicated records", file=sys.stderr)
|
| 145 |
+
|
| 146 |
+
# Create dataset
|
| 147 |
+
features = Features({
|
| 148 |
+
'name': Value('string'),
|
| 149 |
+
'versions': Value('string'),
|
| 150 |
+
'capabilities': Value('string'),
|
| 151 |
+
'last_seen': Value('string'),
|
| 152 |
+
})
|
| 153 |
+
|
| 154 |
+
dataset = Dataset.from_list(deduplicated, features=features)
|
| 155 |
+
|
| 156 |
+
# Push to Hub
|
| 157 |
+
print(f"[{datetime.now().isoformat()}] Pushing deduplicated dataset to {args.target_repo}...", file=sys.stderr)
|
| 158 |
+
|
| 159 |
+
dataset.push_to_hub(
|
| 160 |
+
repo_id=args.target_repo,
|
| 161 |
+
commit_message=f"Update deduplicated dataset ({datetime.now().strftime('%Y-%m-%d %H:%M')})",
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
print(f"[{datetime.now().isoformat()}] Successfully pushed {len(deduplicated):,} deduplicated records to {args.target_repo}", file=sys.stderr)
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
if __name__ == '__main__':
|
| 168 |
+
main()
|