Spaces:
Runtime error
Runtime error
File size: 17,059 Bytes
e6c3213 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
import gradio as gr
import requests
from typing import List, Dict, Optional
from huggingface_hub import HfApi
import os
from dotenv import load_dotenv
import csv
from pinecone import Pinecone
from openai import OpenAI
# Load environment variables
load_dotenv()
# Initialize HF API with token if available
HF_TOKEN = os.getenv("HF_TOKEN")
api = HfApi(token=HF_TOKEN) if HF_TOKEN else HfApi()
def keyword_search_hf_spaces(query: str = "", limit: int = 3) -> Dict:
"""
Search for MCPs in Hugging Face Spaces.
Args:
query: Search query string
limit: Maximum number of results to return (default: 3)
Returns:
Dictionary containing search results with MCP information
"""
try:
print(f"Debug - Search query: '{query}'") # Debug log
# Use list_spaces API with mcp-server filter and sort by likes
spaces = list(api.list_spaces(
search=query,
sort="likes",
direction=-1, # Descending order
filter="mcp-server"
))
results = []
for space in spaces[:limit]: # Process up to limit matches
try:
space_info = {
"id": space.id,
"likes": space.likes,
"trending_score": space.trending_score,
"source": "huggingface"
}
results.append(space_info)
except Exception as e:
print(f"Error processing space {space.id}: {str(e)}")
continue
return {
"results": results,
"total": len(results)
}
except Exception as e:
print(f"Debug - Critical error in keyword_search_hf_spaces: {str(e)}")
return {
"error": str(e),
"results": [],
"total": 0
}
def keyword_search_smithery(query: str = "", limit: int = 3) -> Dict:
"""
Search for MCPs in Smithery Registry.
Args:
query: Search query string
limit: Maximum number of results to return (default: 3)
Returns:
Dictionary containing search results with MCP information
"""
try:
# Get Smithery token from environment
SMITHERY_TOKEN = os.getenv("SMITHERY_TOKEN")
if not SMITHERY_TOKEN:
return {
"error": "SMITHERY_TOKEN not found",
"results": [],
"total": 0
}
# Prepare headers and query parameters
headers = {
'Authorization': f'Bearer {SMITHERY_TOKEN}'
}
# Add filters for deployed and verified servers
search_query = f"{query} is:deployed"
params = {
'q': search_query,
'page': 1,
'pageSize': 100 # Get maximum results
}
# Make API request
response = requests.get(
'https://registry.smithery.ai/servers',
headers=headers,
params=params
)
if response.status_code != 200:
return {
"error": f"Smithery API error: {response.status_code}",
"results": [],
"total": 0
}
# Parse response
data = response.json()
results = []
# Sort servers by useCount and take top results up to limit
servers = sorted(data.get('servers', []), key=lambda x: x.get('useCount', 0), reverse=True)[:limit]
for server in servers:
server_info = {
"id": server.get('qualifiedName'),
"name": server.get('displayName'),
"description": server.get('description'),
"likes": server.get('useCount', 0),
"source": "smithery"
}
results.append(server_info)
return {
"results": results,
"total": len(results)
}
except Exception as e:
return {
"error": str(e),
"results": [],
"total": 0
}
def keyword_search(query: str, sources: List[str], limit: int = 3) -> Dict:
"""
Search for MCPs using keyword matching.
Args:
query: Keyword search query
sources: List of sources to search from ('huggingface', 'smithery')
limit: Maximum number of results to return (default: 3)
Returns:
Dictionary containing combined search results
"""
all_results = []
if "huggingface" in sources:
hf_results = keyword_search_hf_spaces(query, limit)
all_results.extend(hf_results.get("results", []))
if "smithery" in sources:
smithery_results = keyword_search_smithery(query, limit)
all_results.extend(smithery_results.get("results", []))
return {
"results": all_results,
"total": len(all_results),
"search_type": "keyword"
}
def embedding_search_hf_spaces(query: str = "", limit: int = 3) -> Dict:
"""
Search for MCPs in Hugging Face Spaces using semantic embedding matching.
Args:
query: Natural language search query
limit: Maximum number of results to return (default: 3)
Returns:
Dictionary containing search results with MCP information
"""
try:
# Initialize Pinecone and OpenAI
pinecone_api_key = os.getenv('PINECONE_API_KEY')
openai_api_key = os.getenv('OPENAI_API_KEY')
if not pinecone_api_key or not openai_api_key:
return {
"error": "API keys not found",
"results": [],
"total": 0
}
# Initialize clients
pc = Pinecone(api_key=pinecone_api_key)
index = pc.Index("hf-mcp")
client = OpenAI(api_key=openai_api_key)
# Generate embedding using OpenAI
response = client.embeddings.create(
input=query,
model="text-embedding-3-large"
)
query_embedding = response.data[0].embedding
# Search in Pinecone using the generated embedding
results = index.query(
namespace="",
vector=query_embedding,
top_k=limit
)
# Process results and get detailed information
space_results = []
if not results.matches:
return {
"results": [],
"total": 0
}
for match in results.matches:
space_id = match.id
try:
# Remove 'spaces/' prefix if present
repo_id = space_id.replace('spaces/', '')
# Get space information from HF API
space = api.space_info(repo_id)
space_info = {
"id": space.id,
"likes": space.likes,
"trending_score": space.trending_score,
"source": "huggingface",
"score": match.score # Add similarity score
}
space_results.append(space_info)
except Exception as e:
continue
return {
"results": space_results,
"total": len(space_results)
}
except Exception as e:
return {
"error": str(e),
"results": [],
"total": 0
}
def embedding_search_smithery(query: str = "", limit: int = 3) -> Dict:
"""
Search for MCPs in Smithery Registry using semantic embedding matching.
Args:
query: Natural language search query
limit: Maximum number of results to return (default: 3)
Returns:
Dictionary containing search results with MCP information
"""
try:
# Initialize Pinecone and OpenAI
from pinecone import Pinecone
from openai import OpenAI
import os
pinecone_api_key = os.getenv('PINECONE_API_KEY')
openai_api_key = os.getenv('OPENAI_API_KEY')
smithery_token = os.getenv('SMITHERY_TOKEN')
if not pinecone_api_key or not openai_api_key or not smithery_token:
return {
"error": "API keys not found",
"results": [],
"total": 0
}
# Initialize clients
pc = Pinecone(api_key=pinecone_api_key)
index = pc.Index("smithery-mcp")
client = OpenAI(api_key=openai_api_key)
# Generate embedding using OpenAI
response = client.embeddings.create(
input=query,
model="text-embedding-3-large"
)
query_embedding = response.data[0].embedding
# Search in Pinecone using the generated embedding
results = index.query(
namespace="",
vector=query_embedding,
top_k=limit
)
# Process results and get detailed information from Smithery
server_results = []
if not results.matches:
return {
"results": [],
"total": 0
}
# Prepare headers for Smithery API
headers = {
'Authorization': f'Bearer {smithery_token}'
}
for match in results.matches:
server_id = match.id
try:
# Get server information from Smithery API
response = requests.get(
f'https://registry.smithery.ai/servers/{server_id}',
headers=headers
)
if response.status_code != 200:
continue
server = response.json()
server_info = {
"id": server.get('qualifiedName'),
"name": server.get('displayName'),
"description": server.get('description'),
"likes": server.get('useCount', 0),
"source": "smithery",
"score": match.score # Add similarity score
}
server_results.append(server_info)
except Exception as e:
continue
return {
"results": server_results,
"total": len(server_results)
}
except Exception as e:
return {
"error": str(e),
"results": [],
"total": 0
}
def embedding_search(query: str, sources: List[str], limit: int = 3) -> Dict:
"""
Search for MCPs using semantic embedding matching.
Args:
query: Natural language search query
sources: List of sources to search from ('huggingface', 'smithery')
limit: Maximum number of results to return (default: 3)
Returns:
Dictionary containing combined search results
"""
all_results = []
if "huggingface" in sources:
try:
hf_results = embedding_search_hf_spaces(query, limit)
all_results.extend(hf_results.get("results", []))
except Exception as e:
# Fallback to keyword search if vector search fails
hf_results = keyword_search_hf_spaces(query, limit)
all_results.extend(hf_results.get("results", []))
if "smithery" in sources:
try:
smithery_results = embedding_search_smithery(query, limit)
all_results.extend(smithery_results.get("results", []))
except Exception as e:
# Fallback to keyword search if vector search fails
smithery_results = keyword_search_smithery(query, limit)
all_results.extend(smithery_results.get("results", []))
return {
"results": all_results,
"total": len(all_results),
"search_type": "embedding"
}
# Create the Gradio interface
with gr.Blocks(title="π¦ Router MCP", css="""
#client_radio {
margin-top: 0 !important;
padding-top: 0 !important;
}
#client_radio .radio-group {
gap: 0.5rem !important;
}
""") as demo:
gr.Markdown("# π¦ Router MCP")
gr.Markdown("### Search MCP compatible spaces using natural language")
with gr.Row():
with gr.Column():
query_input = gr.Textbox(
label="Describe the MCP Server you're looking for",
placeholder="e.g., 'I need an MCP Server that can generate images'"
)
gr.Markdown("### Select sources to search")
hf_checkbox = gr.Checkbox(label="Hugging Face Spaces", value=True)
smithery_checkbox = gr.Checkbox(label="Smithery", value=False)
registry_checkbox = gr.Checkbox(label="Registry (Coming Soon)", value=False, interactive=False)
result_limit = gr.Number(
label="Maximum number of results for each source",
value=3,
minimum=1,
maximum=20,
step=1
)
gr.Markdown("### Select your MCP Client")
client_radio = gr.Radio(
choices=["Cursor", "Windsurf", "Claude Desktop", "VS Code", "Gradio"],
label="",
value="Cursor",
interactive=True,
elem_id="client_radio"
)
with gr.Row():
keyword_search_button = gr.Button("Keyword Search")
embedding_search_button = gr.Button("Semantic Search")
with gr.Column():
results_output = gr.JSON(label="Search Results")
# Set up event handlers
def get_sources():
return ["huggingface" if hf_checkbox.value else "", "smithery" if smithery_checkbox.value else ""]
def handle_keyword_mcp_search(query: str, hf: bool, sm: bool, limit: int) -> Dict:
"""
Handle keyword-based search for MCP servers across selected sources. If the client (such as Cursor or Claude) encounters a task it cannot handle, it can use MCP to search for other tools that can.
Args:
query (str): The search query string to find matching MCP servers
hf (bool): Whether to include Hugging Face Spaces in the search (converted to "huggingface" string if True)
sm (bool): Whether to include Smithery in the search (converted to "smithery" string if True)
limit (int): Maximum number of results to return per source (default: 3)
Returns:
Dict: A dictionary containing the search results with the following keys:
- results: List of found MCP servers
- total: Total number of results
- search_type: Type of search performed ("keyword")
"""
return keyword_search(
query,
["huggingface" if hf else "", "smithery" if sm else ""],
int(limit)
)
def handle_embedding_mcp_search(query: str, hf: bool, sm: bool, limit: int) -> Dict:
"""
Handle semantic embedding-based search for MCP servers across selected sources. If the client (such as Cursor or Claude) encounters a task it cannot handle, it can use MCP to search for other tools that can.
Args:
query (str): The natural language search query to find semantically similar MCP servers
hf (bool): Whether to include Hugging Face Spaces in the search (converted to "huggingface" string if True)
sm (bool): Whether to include Smithery in the search (converted to "smithery" string if True)
limit (int): Maximum number of results to return per source (default: 3)
Returns:
Dict: A dictionary containing the search results with the following keys:
- results: List of found MCP servers with similarity scores
- total: Total number of results
- search_type: Type of search performed ("embedding")
"""
return embedding_search(
query,
["huggingface" if hf else "", "smithery" if sm else ""],
int(limit)
)
keyword_search_button.click(
fn=handle_keyword_mcp_search,
inputs=[query_input, hf_checkbox, smithery_checkbox, result_limit],
outputs=results_output
)
embedding_search_button.click(
fn=handle_embedding_mcp_search,
inputs=[query_input, hf_checkbox, smithery_checkbox, result_limit],
outputs=results_output
)
# query_input.submit(
# fn=handle_embedding_search,
# inputs=[query_input, hf_checkbox, smithery_checkbox, result_limit],
# outputs=results_output
# )
if __name__ == "__main__":
demo.launch(mcp_server=True) |