File size: 3,444 Bytes
a36a386
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
859b1a5
 
 
 
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
# πŸ“– 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:

```json
{ 
  "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:

```json
{ 
  "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:

```python
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:

- Building an MCP Client with Gradio - https://www.gradio.app/guides/building-an-mcp-client-with-gradio
- Gradio MCP Integration - Hugging Face MCP Course - https://huggingface.co/learn/mcp-course/en/unit1/gradio-mcp

Use this space as MCP and create you own extensions