ysharma HF Staff commited on
Commit
fdd3874
·
verified ·
1 Parent(s): 69fa6dd

Update mcp_spaces_finder.py

Browse files
Files changed (1) hide show
  1. mcp_spaces_finder.py +90 -11
mcp_spaces_finder.py CHANGED
@@ -1,22 +1,21 @@
1
  """
2
- Simple HuggingFace MCP Spaces Finder Module
3
  A minimal module to discover MCP servers on HuggingFace Spaces.
4
- Just a dropdown that displays the selected value in a textbox.
 
5
  Usage:
6
  from mcp_spaces_finder import create_simple_mcp_selector
7
 
8
  # One-liner in your Gradio app
9
  dropdown, textbox = create_simple_mcp_selector()
10
  """
11
-
12
  import gradio as gr
13
  from huggingface_hub import list_spaces
14
  import time
15
  from typing import List, Tuple
16
- from concurrent.futures import ThreadPoolExecutor, as_completed
17
 
18
  class SimpleMCPFinder:
19
- """Simple MCP spaces finder with caching."""
20
 
21
  def __init__(self, cache_duration: int = 300):
22
  self.cache = None
@@ -24,7 +23,7 @@ class SimpleMCPFinder:
24
  self.cache_duration = cache_duration
25
 
26
  def get_mcp_spaces(self) -> List[str]:
27
- """Get list of running MCP space IDs."""
28
 
29
  # Check cache
30
  if (self.cache is not None and
@@ -32,14 +31,15 @@ class SimpleMCPFinder:
32
  time.time() - self.cache_time < self.cache_duration):
33
  return self.cache
34
 
35
- print("Fetching MCP spaces...")
36
 
37
- # Get MCP spaces sorted by likes
 
38
  spaces = list(list_spaces(
39
  filter="mcp-server",
40
  sort="likes",
41
  direction=-1,
42
- limit=1000,
43
  full=True
44
  ))
45
 
@@ -52,6 +52,51 @@ class SimpleMCPFinder:
52
 
53
  print(f"Found {len(space_ids)} MCP spaces")
54
  return space_ids
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  # Global instance
57
  _finder = SimpleMCPFinder()
@@ -80,7 +125,9 @@ def create_simple_mcp_selector(
80
  dropdown = gr.Dropdown(
81
  choices=spaces,
82
  label=f"{dropdown_label} ({len(spaces)} available)",
83
- value=None
 
 
84
  )
85
 
86
  # Create textbox to display selection
@@ -105,4 +152,36 @@ def create_simple_mcp_selector(
105
  def refresh_mcp_spaces():
106
  """Clear cache to force refresh."""
107
  _finder.cache = None
108
- _finder.cache_time = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  """
2
+ Simple HuggingFace MCP Spaces Finder Module - Corrected Version
3
  A minimal module to discover MCP servers on HuggingFace Spaces.
4
+ Fixed to fetch ALL available MCP servers using proper pagination.
5
+
6
  Usage:
7
  from mcp_spaces_finder import create_simple_mcp_selector
8
 
9
  # One-liner in your Gradio app
10
  dropdown, textbox = create_simple_mcp_selector()
11
  """
 
12
  import gradio as gr
13
  from huggingface_hub import list_spaces
14
  import time
15
  from typing import List, Tuple
 
16
 
17
  class SimpleMCPFinder:
18
+ """Simple MCP spaces finder with caching and proper pagination."""
19
 
20
  def __init__(self, cache_duration: int = 300):
21
  self.cache = None
 
23
  self.cache_duration = cache_duration
24
 
25
  def get_mcp_spaces(self) -> List[str]:
26
+ """Get list of ALL running MCP space IDs using proper pagination."""
27
 
28
  # Check cache
29
  if (self.cache is not None and
 
31
  time.time() - self.cache_time < self.cache_duration):
32
  return self.cache
33
 
34
+ print("Fetching ALL MCP spaces...")
35
 
36
+ # Get ALL MCP spaces by setting a much higher limit
37
+ # The HF API supports pagination, so we set limit high enough to get all
38
  spaces = list(list_spaces(
39
  filter="mcp-server",
40
  sort="likes",
41
  direction=-1,
42
+ limit=5000, # Increased from 1000 to capture all ~2500 spaces
43
  full=True
44
  ))
45
 
 
52
 
53
  print(f"Found {len(space_ids)} MCP spaces")
54
  return space_ids
55
+
56
+ def get_mcp_spaces_paginated(self) -> List[str]:
57
+ """Alternative method: Get ALL MCP spaces using explicit pagination if needed."""
58
+
59
+ # Check cache
60
+ if (self.cache is not None and
61
+ self.cache_time is not None and
62
+ time.time() - self.cache_time < self.cache_duration):
63
+ return self.cache
64
+
65
+ print("Fetching ALL MCP spaces with pagination...")
66
+
67
+ all_space_ids = []
68
+ limit_per_page = 1000
69
+
70
+ # Keep fetching until we get all spaces
71
+ # Note: HuggingFace API handles pagination internally with the iterator
72
+ try:
73
+ spaces = list(list_spaces(
74
+ filter="mcp-server",
75
+ sort="likes",
76
+ direction=-1,
77
+ limit=None, # No limit to get all
78
+ full=True
79
+ ))
80
+ all_space_ids = [space.id for space in spaces]
81
+
82
+ except Exception as e:
83
+ print(f"Error with unlimited fetch, trying with high limit: {e}")
84
+ # Fallback to high limit
85
+ spaces = list(list_spaces(
86
+ filter="mcp-server",
87
+ sort="likes",
88
+ direction=-1,
89
+ limit=5000, # High limit as fallback
90
+ full=True
91
+ ))
92
+ all_space_ids = [space.id for space in spaces]
93
+
94
+ # Cache results
95
+ self.cache = all_space_ids
96
+ self.cache_time = time.time()
97
+
98
+ print(f"Found {len(all_space_ids)} MCP spaces total")
99
+ return all_space_ids
100
 
101
  # Global instance
102
  _finder = SimpleMCPFinder()
 
125
  dropdown = gr.Dropdown(
126
  choices=spaces,
127
  label=f"{dropdown_label} ({len(spaces)} available)",
128
+ value=None,
129
+ allow_custom_value=True, # Allow users to type custom space IDs
130
+ info="Choose from discovered MCP spaces or type a custom space ID"
131
  )
132
 
133
  # Create textbox to display selection
 
152
  def refresh_mcp_spaces():
153
  """Clear cache to force refresh."""
154
  _finder.cache = None
155
+ _finder.cache_time = None
156
+
157
+ def test_space_exists(space_id: str) -> bool:
158
+ """Test if a specific space exists in our discovered list."""
159
+ spaces = _finder.get_mcp_spaces()
160
+ return space_id in spaces
161
+
162
+ def debug_search_for_spaces(space_ids: List[str]):
163
+ """Debug function to check if specific spaces are found."""
164
+ spaces = _finder.get_mcp_spaces()
165
+ print(f"Total MCP spaces found: {len(spaces)}")
166
+
167
+ for space_id in space_ids:
168
+ if space_id in spaces:
169
+ print(f"✅ Found: {space_id}")
170
+ else:
171
+ print(f"❌ Missing: {space_id}")
172
+
173
+ # Show first 10 spaces for reference
174
+ print(f"\nFirst 10 spaces found:")
175
+ for i, space in enumerate(spaces[:10]):
176
+ print(f" {i+1}. {space}")
177
+
178
+ if __name__ == "__main__":
179
+ # Test the specific spaces you mentioned
180
+ test_spaces = [
181
+ "ysharma/Kokoro-TTS-mcp-test",
182
+ "ysharma/ltx-video-distilled",
183
+ "ysharma/dalle-3-xl-lora-v2"
184
+ ]
185
+
186
+ print("Testing MCP space discovery...")
187
+ debug_search_for_spaces(test_spaces)