@vespermcp/mcp-server 1.2.19 → 1.2.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,6 +9,7 @@ import tempfile
9
9
  from dataclasses import dataclass
10
10
  from pathlib import Path
11
11
  from typing import Any, Awaitable, Callable, Dict, Iterable, List, Optional
12
+ from urllib.parse import urlparse
12
13
 
13
14
  import aiohttp
14
15
 
@@ -25,7 +26,7 @@ except Exception: # pragma: no cover
25
26
  wds = None
26
27
 
27
28
 
28
- IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp", ".bmp", ".gif", ".tiff"}
29
+ IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp", ".bmp", ".gif", ".tiff", ".tif", ".svg"}
29
30
 
30
31
 
31
32
  @dataclass
@@ -98,22 +99,88 @@ class AssetDownloader:
98
99
 
99
100
  @staticmethod
100
101
  def find_image_column(dataset: Any) -> Optional[str]:
102
+ """Auto-detect the image column in a HuggingFace dataset.
103
+
104
+ Detection strategy (in priority order):
105
+ 1. HF Feature type: columns with Image() feature type
106
+ 2. Known column names: 'image', 'img', 'photo', 'image_url', etc.
107
+ 3. URL pattern detection: columns containing image URLs (http(s)://...jpg)
108
+ 4. Path pattern detection: columns with file paths ending in image extensions
109
+ """
110
+ # Strategy 1: Check HF Feature types (most reliable)
101
111
  features = getattr(dataset, "features", None)
102
112
  if features:
103
113
  for name, feature in features.items():
104
- feature_name = feature.__class__.__name__.lower()
105
- feature_repr = str(feature).lower()
106
- if feature_name == "image" or "image(" in feature_repr:
107
- return str(name)
108
- lower = str(name).lower()
109
- if lower in {"image", "images", "img", "image_path", "image_url", "url"}:
114
+ feat_cls = feature.__class__.__name__.lower()
115
+ feat_str = str(feature).lower()
116
+ if feat_cls == "image" or "image(" in feat_str:
110
117
  return str(name)
111
118
 
112
- candidate_columns = ["image", "images", "img", "image_path", "image_url", "url", "file_name", "filepath"]
119
+ # Strategy 2: Check known column names
113
120
  cols = getattr(dataset, "column_names", []) or []
114
- for c in candidate_columns:
121
+
122
+ # Exact match first (highest priority names)
123
+ priority_exact = ["image", "img", "photo", "picture", "images"]
124
+ for c in priority_exact:
115
125
  if c in cols:
116
126
  return c
127
+
128
+ # Partial match (column names containing image-related keywords)
129
+ priority_partial = [
130
+ "image_path", "image_url", "img_path", "img_url",
131
+ "image_file", "file_name", "filepath", "filename",
132
+ "photo_url", "picture_url", "thumbnail",
133
+ "url", "path", "file",
134
+ ]
135
+ for target in priority_partial:
136
+ for c in cols:
137
+ if c.lower() == target:
138
+ return c
139
+
140
+ # Strategy 3: Sample values to detect URL/path patterns
141
+ try:
142
+ sample_size = min(5, len(dataset)) if hasattr(dataset, "__len__") else 5
143
+ if sample_size > 0:
144
+ for c in cols:
145
+ is_image_col = False
146
+ for i in range(sample_size):
147
+ try:
148
+ val = dataset[i][c]
149
+ except Exception:
150
+ break
151
+
152
+ if val is None:
153
+ continue
154
+
155
+ # PIL Image object
156
+ if hasattr(val, "save") and hasattr(val, "size"):
157
+ is_image_col = True
158
+ break
159
+
160
+ # Dict with image data
161
+ if isinstance(val, dict) and any(k in val for k in ("bytes", "path", "url")):
162
+ is_image_col = True
163
+ break
164
+
165
+ # String: URL or file path
166
+ if isinstance(val, str):
167
+ val_lower = val.lower()
168
+ # Check for image URLs
169
+ if val_lower.startswith(("http://", "https://")) and any(
170
+ ext in val_lower.split("?")[0] for ext in IMAGE_EXTENSIONS
171
+ ):
172
+ is_image_col = True
173
+ break
174
+ # Check for file paths with image extensions
175
+ if any(val_lower.endswith(ext) for ext in IMAGE_EXTENSIONS):
176
+ is_image_col = True
177
+ break
178
+
179
+ if is_image_col:
180
+ return c
181
+ except Exception:
182
+ pass
183
+
117
184
  return None
118
185
 
119
186
  async def download_assets(
@@ -214,20 +281,73 @@ class AssetDownloader:
214
281
  image_column: Optional[str],
215
282
  ) -> Dict[str, int]:
216
283
  from datasets import load_dataset # validated in download_assets()
284
+ import warnings
285
+ warnings.filterwarnings("ignore", message=".*trust_remote_code.*")
217
286
 
218
287
  await self._emit("start", {"source": "huggingface", "repo_id": repo_id})
219
288
 
220
- try:
221
- ds = load_dataset(repo_id, split="train")
222
- except Exception:
223
- dd = load_dataset(repo_id)
224
- first_split = list(dd.keys())[0]
225
- ds = dd[first_split]
289
+ token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_TOKEN") or None
290
+
291
+ # Try loading with multiple strategies
292
+ ds = None
293
+ load_errors = []
294
+
295
+ for trust_rc in [True, False]:
296
+ for split_name in ["train", "test", "validation"]:
297
+ try:
298
+ kwargs = {"path": repo_id, "split": split_name}
299
+ if trust_rc:
300
+ kwargs["trust_remote_code"] = True
301
+ if token:
302
+ kwargs["token"] = token
303
+ ds = load_dataset(**kwargs)
304
+ break
305
+ except Exception as e:
306
+ msg = str(e)
307
+ # Immediately raise auth errors
308
+ if any(x in msg for x in ["401", "403", "gated", "Unauthorized"]):
309
+ raise RuntimeError(
310
+ f"Authentication required for '{repo_id}'. "
311
+ "This dataset may be gated or private. "
312
+ "Use the configure_keys tool to set HF_TOKEN, then retry."
313
+ )
314
+ load_errors.append(msg)
315
+ continue
316
+ if ds is not None:
317
+ break
318
+
319
+ # Fallback: load without split
320
+ if ds is None:
321
+ try:
322
+ kwargs = {"path": repo_id, "trust_remote_code": True}
323
+ if token:
324
+ kwargs["token"] = token
325
+ dd = load_dataset(**kwargs)
326
+ from datasets import DatasetDict
327
+ if isinstance(dd, DatasetDict):
328
+ first_split = list(dd.keys())[0]
329
+ ds = dd[first_split]
330
+ else:
331
+ ds = dd
332
+ except Exception as e:
333
+ msg = str(e)
334
+ if any(x in msg for x in ["401", "403", "gated", "Unauthorized"]):
335
+ raise RuntimeError(
336
+ f"Authentication required for '{repo_id}'. "
337
+ "Use the configure_keys tool to set HF_TOKEN, then retry."
338
+ )
339
+ combined = "; ".join(load_errors[:3])
340
+ raise RuntimeError(
341
+ f"Failed to load HuggingFace dataset '{repo_id}': {msg}. "
342
+ f"Previous attempts: {combined}"
343
+ )
226
344
 
227
345
  col = image_column or self.find_image_column(ds)
228
346
  if not col:
229
347
  raise RuntimeError(
230
- "No image column detected in HuggingFace dataset. Provide image_column or use fallback strategy with URL column."
348
+ f"No image column detected in HuggingFace dataset '{repo_id}'. "
349
+ "Available columns: " + ", ".join(getattr(ds, "column_names", [])) + ". "
350
+ "Provide image_column parameter explicitly."
231
351
  )
232
352
 
233
353
  total = len(ds) if hasattr(ds, "__len__") else 0
@@ -236,33 +356,63 @@ class AssetDownloader:
236
356
  downloaded = 0
237
357
  failed = 0
238
358
 
239
- with metadata_file.open("w", encoding="utf-8") as mf, errors_file.open("w", encoding="utf-8") as ef:
240
- for idx, row in enumerate(ds):
241
- if max_items and idx >= max_items:
242
- break
243
- try:
244
- out_name = f"{idx:08d}.jpg"
245
- out_path = images_dir / out_name
246
- self._save_image_value(row.get(col), out_path)
247
-
248
- record = {
249
- "dataset_id": dataset_id,
250
- "index": idx,
251
- "image_path": str(out_path),
252
- "source": "huggingface",
253
- "repo_id": repo_id,
254
- }
255
- mf.write(json.dumps(record, ensure_ascii=False) + "\n")
256
- downloaded += 1
257
- if downloaded % 50 == 0:
258
- await self._emit("progress", {"downloaded": downloaded, "failed": failed, "target": target})
259
- except Exception as e:
260
- failed += 1
261
- ef.write(json.dumps({"index": idx, "error": str(e)}, ensure_ascii=False) + "\n")
359
+ # Create an aiohttp session for URL-based images
360
+ session = None
361
+
362
+ try:
363
+ with metadata_file.open("w", encoding="utf-8") as mf, errors_file.open("w", encoding="utf-8") as ef:
364
+ for idx, row in enumerate(ds):
365
+ if max_items and idx >= max_items:
366
+ break
367
+ try:
368
+ out_name = f"{idx:08d}.jpg"
369
+ out_path = images_dir / out_name
370
+ value = row.get(col)
371
+
372
+ # Handle URL-based images inline
373
+ if isinstance(value, dict) and value.get("url") and not value.get("bytes") and not value.get("path"):
374
+ url = value["url"]
375
+ if session is None:
376
+ session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=60))
377
+ await self._download_image_from_url(session, url, out_path)
378
+ elif isinstance(value, str) and value.startswith(("http://", "https://")):
379
+ if session is None:
380
+ session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=60))
381
+ await self._download_image_from_url(session, value, out_path)
382
+ else:
383
+ self._save_image_value(value, out_path)
384
+
385
+ record = {
386
+ "dataset_id": dataset_id,
387
+ "index": idx,
388
+ "image_path": str(out_path),
389
+ "source": "huggingface",
390
+ "repo_id": repo_id,
391
+ }
392
+ mf.write(json.dumps(record, ensure_ascii=False) + "\n")
393
+ downloaded += 1
394
+ if downloaded % 50 == 0:
395
+ await self._emit("progress", {"downloaded": downloaded, "failed": failed, "target": target})
396
+ except Exception as e:
397
+ failed += 1
398
+ ef.write(json.dumps({"index": idx, "error": str(e)}, ensure_ascii=False) + "\n")
399
+ finally:
400
+ if session is not None:
401
+ await session.close()
262
402
 
263
403
  await self._emit("done", {"downloaded": downloaded, "failed": failed})
264
404
  return {"downloaded": downloaded, "failed": failed}
265
405
 
406
+ async def _download_image_from_url(self, session: aiohttp.ClientSession, url: str, out_path: Path) -> None:
407
+ """Download an image from a URL to a local path."""
408
+ async with session.get(url) as response:
409
+ if response.status != 200:
410
+ raise RuntimeError(f"HTTP {response.status} downloading {url}")
411
+ data = await response.read()
412
+ if not data:
413
+ raise RuntimeError(f"Empty response from {url}")
414
+ out_path.write_bytes(data)
415
+
266
416
  async def _download_kaggle(
267
417
  self,
268
418
  kaggle_ref: str,
@@ -393,30 +543,66 @@ class AssetDownloader:
393
543
 
394
544
  @staticmethod
395
545
  def _save_image_value(value: Any, out_path: Path) -> None:
546
+ """Save an image value to disk. Handles multiple image representations:
547
+ - PIL Image objects (have .save method)
548
+ - dict with 'bytes' key (raw image bytes)
549
+ - dict with 'path' key (local file path)
550
+ - bytes/bytearray (raw image data)
551
+ - str (local file path)
552
+ """
396
553
  if value is None:
397
554
  raise ValueError("empty image value")
398
555
 
399
- if hasattr(value, "save"):
556
+ # PIL Image object
557
+ if hasattr(value, "save") and hasattr(value, "size"):
400
558
  value.save(out_path)
401
559
  return
402
560
 
561
+ # Raw bytes
562
+ if isinstance(value, (bytes, bytearray)):
563
+ out_path.write_bytes(value)
564
+ return
565
+
566
+ # Dict with image data
403
567
  if isinstance(value, dict):
404
568
  if value.get("bytes"):
405
- out_path.write_bytes(value["bytes"])
406
- return
407
- if value.get("path") and os.path.exists(value["path"]):
408
- shutil.copy2(value["path"], out_path)
569
+ raw = value["bytes"]
570
+ if isinstance(raw, (bytes, bytearray)):
571
+ out_path.write_bytes(raw)
572
+ else:
573
+ # Could be a list of ints
574
+ out_path.write_bytes(bytes(raw))
409
575
  return
576
+ if value.get("path"):
577
+ p = str(value["path"])
578
+ if os.path.exists(p):
579
+ shutil.copy2(p, out_path)
580
+ return
581
+ raise ValueError(f"Image path not found: {p}")
410
582
  if value.get("url"):
411
- raise ValueError("image URL requires URL downloader fallback")
583
+ raise ValueError("image URL detected — use async URL downloader")
412
584
 
585
+ # String: local file path
413
586
  if isinstance(value, str):
414
587
  if os.path.exists(value):
415
588
  shutil.copy2(value, out_path)
416
589
  return
417
- raise ValueError("string image value is not a local path")
590
+ if value.startswith(("http://", "https://")):
591
+ raise ValueError("image URL detected — use async URL downloader")
592
+ raise ValueError(f"Image path not found: {value}")
593
+
594
+ # numpy array (common in some datasets)
595
+ try:
596
+ import numpy as np
597
+ if isinstance(value, np.ndarray):
598
+ from PIL import Image
599
+ img = Image.fromarray(value)
600
+ img.save(out_path)
601
+ return
602
+ except (ImportError, Exception):
603
+ pass
418
604
 
419
- raise ValueError(f"Unsupported image value type: {type(value)}")
605
+ raise ValueError(f"Unsupported image value type: {type(value).__name__}")
420
606
 
421
607
  async def _write_webdataset(self, dataset_dir: Path, images_dir: Path, metadata_file: Path) -> None:
422
608
  """Write a webdataset-compatible tar archive.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vespermcp/mcp-server",
3
- "version": "1.2.19",
3
+ "version": "1.2.21",
4
4
  "description": "AI-powered dataset discovery, quality analysis, and preparation MCP server with multimodal support (text, image, audio, video)",
5
5
  "type": "module",
6
6
  "main": "build/index.js",
@@ -3,9 +3,14 @@ import asyncio
3
3
  import json
4
4
  import os
5
5
  import sys
6
+ import warnings
6
7
  from pathlib import Path
7
8
  from typing import Any, Dict
8
9
 
10
+ # Suppress noisy HF warnings
11
+ warnings.filterwarnings("ignore", message=".*trust_remote_code.*")
12
+ warnings.filterwarnings("ignore", message=".*legacy.*")
13
+
9
14
  CURRENT_DIR = Path(__file__).resolve().parent
10
15
  if str(CURRENT_DIR) not in sys.path:
11
16
  sys.path.insert(0, str(CURRENT_DIR))
@@ -24,6 +29,11 @@ async def _run_download(args: argparse.Namespace) -> Dict[str, Any]:
24
29
  workers = int(payload.get("workers") or 8)
25
30
  recipes_dir = payload.get("recipes_dir")
26
31
 
32
+ # Auto-set HF token from payload if provided
33
+ token = payload.get("token") or payload.get("hf_token")
34
+ if token:
35
+ os.environ["HF_TOKEN"] = str(token)
36
+
27
37
  downloader = AssetDownloader(output_root=output_root, workers=workers, recipes_dir=recipes_dir)
28
38
 
29
39
  result = await downloader.download_assets(
@@ -66,7 +76,16 @@ def main() -> None:
66
76
 
67
77
  _print({"ok": False, "error": f"Unknown action: {args.action}"})
68
78
  except Exception as e:
69
- _print({"ok": False, "error": str(e)})
79
+ error_msg = str(e)
80
+ # Provide actionable error messages
81
+ if "401" in error_msg or "403" in error_msg or "Unauthorized" in error_msg:
82
+ error_msg = (
83
+ "Authentication required. This dataset may be gated/private. "
84
+ "Use configure_keys tool to set HF_TOKEN, then retry."
85
+ )
86
+ elif "No image column" in error_msg:
87
+ error_msg += " Hint: specify image_column parameter with the name of the column containing images."
88
+ _print({"ok": False, "error": error_msg})
70
89
 
71
90
 
72
91
  if __name__ == "__main__":