UsingHF-MCP / mcp_endpoint_guide.md
Chris4K's picture
Update mcp_endpoint_guide.md
859b1a5 verified

πŸ“– The Quest Begins: Unveiling the MCP Endpoint

Your expedition commenced with a simple GET request to the enigmatic URL:

https://huggingface.co/mcp

The server's response was a cryptic message:

{ 
  "jsonrpc": "2.0", 
  "error": { 
    "code": -32000, 
    "message": "Method not allowed. Use POST for stateless JSON-RPC requests." 
  }, 
  "id": null 
}

This indicated that the server adheres to the JSON-RPC 2.0 protocol and expects POST requests for interaction.

🧠 Deciphering the Protocol: JSON-RPC 2.0 and MCP

The Model Context Protocol (MCP) standardizes interactions between clients and servers, facilitating seamless communication with AI models and tools. It employs JSON-RPC 2.0 for message formatting and supports transport mechanisms like HTTP with Server-Sent Events (SSE) and stdio for local communication.

πŸ” Discovering Available Tools

Adjusting your approach, you sent a POST request with the appropriate headers and payload:

{ 
  "jsonrpc": "2.0", 
  "method": "tools/list", 
  "params": {}, 
  "id": 1 
}

The server responded with a list of available tools, each accompanied by detailed descriptions and input schemas:

  • space_search: Search for Hugging Face Spaces.
  • model_detail: Retrieve detailed information about a specific model.
  • paper_search: Search for machine learning research papers.
  • dataset_search: Find datasets on Hugging Face.
  • dataset_detail: Get detailed information about a specific dataset.
  • duplicate_space: Duplicate a Hugging Face Space.

πŸ› οΈ Crafting the Gradio Client

To interact with these tools in a user-friendly manner, you can construct a Gradio interface. Here's a simplified example focusing on the space_search tool:

import gradio as gr
import requests
import json

MCP_URL = "https://huggingface.co/mcp"
HEADERS = {
    "Content-Type": "application/json",
    "Accept": "application/json, text/event-stream"
}

def call_mcp_tool(method, params):
    payload = {
        "jsonrpc": "2.0",
        "method": method,
        "params": params,
        "id": 1
    }
    response = requests.post(MCP_URL, headers=HEADERS, data=json.dumps(payload))
    try:
        return response.json()
    except Exception as e:
        return {"error": str(e), "raw_response": response.text}

def space_search(query, limit=10, mcp=False):
    params = {
        "query": query,
        "limit": limit,
        "mcp": mcp
    }
    result = call_mcp_tool("tools/call", {"tool": "space_search", "input": params})
    return result

with gr.Blocks() as demo:
    gr.Markdown("# MCP Gradio Client - Space Search")
    
    query = gr.Textbox(label="Query")
    limit = gr.Slider(1, 50, value=10, label="Limit")
    mcp = gr.Checkbox(label="MCP Only")
    output = gr.JSON()
    
    btn = gr.Button("Search")
    btn.click(space_search, inputs=[query, limit, mcp], outputs=output)

demo.launch()

This interface allows users to input search queries and view results directly from the MCP server.

πŸ”— Further Exploration

For a comprehensive understanding and additional examples, consider exploring the following resources:

Use this space as MCP and create you own extensions