Spaces:
Sleeping
Sleeping
Upload mcp_endpoint_guide.md
Browse files- mcp_endpoint_guide.md +108 -0
mcp_endpoint_guide.md
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 📖 The Quest Begins: Unveiling the MCP Endpoint
|
| 2 |
+
|
| 3 |
+
Your expedition commenced with a simple GET request to the enigmatic URL:
|
| 4 |
+
|
| 5 |
+
```
|
| 6 |
+
https://huggingface.co/mcp
|
| 7 |
+
```
|
| 8 |
+
|
| 9 |
+
The server's response was a cryptic message:
|
| 10 |
+
|
| 11 |
+
```json
|
| 12 |
+
{
|
| 13 |
+
"jsonrpc": "2.0",
|
| 14 |
+
"error": {
|
| 15 |
+
"code": -32000,
|
| 16 |
+
"message": "Method not allowed. Use POST for stateless JSON-RPC requests."
|
| 17 |
+
},
|
| 18 |
+
"id": null
|
| 19 |
+
}
|
| 20 |
+
```
|
| 21 |
+
|
| 22 |
+
This indicated that the server adheres to the JSON-RPC 2.0 protocol and expects POST requests for interaction.
|
| 23 |
+
|
| 24 |
+
## 🧠 Deciphering the Protocol: JSON-RPC 2.0 and MCP
|
| 25 |
+
|
| 26 |
+
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.
|
| 27 |
+
|
| 28 |
+
## 🔍 Discovering Available Tools
|
| 29 |
+
|
| 30 |
+
Adjusting your approach, you sent a POST request with the appropriate headers and payload:
|
| 31 |
+
|
| 32 |
+
```json
|
| 33 |
+
{
|
| 34 |
+
"jsonrpc": "2.0",
|
| 35 |
+
"method": "tools/list",
|
| 36 |
+
"params": {},
|
| 37 |
+
"id": 1
|
| 38 |
+
}
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
The server responded with a list of available tools, each accompanied by detailed descriptions and input schemas:
|
| 42 |
+
|
| 43 |
+
- **space_search**: Search for Hugging Face Spaces.
|
| 44 |
+
- **model_detail**: Retrieve detailed information about a specific model.
|
| 45 |
+
- **paper_search**: Search for machine learning research papers.
|
| 46 |
+
- **dataset_search**: Find datasets on Hugging Face.
|
| 47 |
+
- **dataset_detail**: Get detailed information about a specific dataset.
|
| 48 |
+
- **duplicate_space**: Duplicate a Hugging Face Space.
|
| 49 |
+
|
| 50 |
+
## 🛠️ Crafting the Gradio Client
|
| 51 |
+
|
| 52 |
+
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:
|
| 53 |
+
|
| 54 |
+
```python
|
| 55 |
+
import gradio as gr
|
| 56 |
+
import requests
|
| 57 |
+
import json
|
| 58 |
+
|
| 59 |
+
MCP_URL = "https://huggingface.co/mcp"
|
| 60 |
+
HEADERS = {
|
| 61 |
+
"Content-Type": "application/json",
|
| 62 |
+
"Accept": "application/json, text/event-stream"
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
def call_mcp_tool(method, params):
|
| 66 |
+
payload = {
|
| 67 |
+
"jsonrpc": "2.0",
|
| 68 |
+
"method": method,
|
| 69 |
+
"params": params,
|
| 70 |
+
"id": 1
|
| 71 |
+
}
|
| 72 |
+
response = requests.post(MCP_URL, headers=HEADERS, data=json.dumps(payload))
|
| 73 |
+
try:
|
| 74 |
+
return response.json()
|
| 75 |
+
except Exception as e:
|
| 76 |
+
return {"error": str(e), "raw_response": response.text}
|
| 77 |
+
|
| 78 |
+
def space_search(query, limit=10, mcp=False):
|
| 79 |
+
params = {
|
| 80 |
+
"query": query,
|
| 81 |
+
"limit": limit,
|
| 82 |
+
"mcp": mcp
|
| 83 |
+
}
|
| 84 |
+
result = call_mcp_tool("tools/call", {"tool": "space_search", "input": params})
|
| 85 |
+
return result
|
| 86 |
+
|
| 87 |
+
with gr.Blocks() as demo:
|
| 88 |
+
gr.Markdown("# MCP Gradio Client - Space Search")
|
| 89 |
+
|
| 90 |
+
query = gr.Textbox(label="Query")
|
| 91 |
+
limit = gr.Slider(1, 50, value=10, label="Limit")
|
| 92 |
+
mcp = gr.Checkbox(label="MCP Only")
|
| 93 |
+
output = gr.JSON()
|
| 94 |
+
|
| 95 |
+
btn = gr.Button("Search")
|
| 96 |
+
btn.click(space_search, inputs=[query, limit, mcp], outputs=output)
|
| 97 |
+
|
| 98 |
+
demo.launch()
|
| 99 |
+
```
|
| 100 |
+
|
| 101 |
+
This interface allows users to input search queries and view results directly from the MCP server.
|
| 102 |
+
|
| 103 |
+
## 🔗 Further Exploration
|
| 104 |
+
|
| 105 |
+
For a comprehensive understanding and additional examples, consider exploring the following resources:
|
| 106 |
+
|
| 107 |
+
- Building an MCP Client with Gradio
|
| 108 |
+
- Gradio MCP Integration - Hugging Face MCP Course
|