File size: 9,904 Bytes
48a7f49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Connecting to Your MCP Server on Hugging Face Spaces

This guide explains how to connect to your Gradio MCP server deployed on Hugging Face Spaces, based on real-world testing and troubleshooting.

**⚠️ Important:** This guide reflects actual testing results and working solutions, not just theoretical approaches.

---

## 1. Find Your MCP Endpoint

After deploying your Gradio app to Hugging Face Spaces, your MCP endpoint will be:

```
https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/gradio_api/mcp/sse
```

**Example:**  
If your username is `freemansel` and your space is `mcp-sentiment`, your endpoint is:

```
https://freemansel-mcp-sentiment.hf.space/gradio_api/mcp/sse
```

**Testing the endpoint:** You can verify the SSE stream is working by visiting the URL in a browser - you should see periodic ping messages every 15 seconds.

---

## 2. βœ… Working Solutions (Tested & Verified)

### A. smolagents MCPClient ⭐ **RECOMMENDED**

**Performance:** ~0.11 seconds per request (fastest!)  
**Status:** βœ… Works perfectly  
**Protocol:** Native MCP via smolagents

Based on the [Hugging Face MCP Course](https://huggingface.co/learn/mcp-course/unit2/gradio-client), this is the proper way to use MCP.

#### Installation
```bash
pdm add "smolagents[mcp]"
# or
pip install "smolagents[mcp]"
```

#### Python Code
```python
#!/usr/bin/env python3
"""
Working MCP client using smolagents.
Performance: ~0.11 seconds per request
"""

import time
from smolagents.mcp_client import MCPClient

def analyze_sentiment_mcp(text):
    """Analyze sentiment using MCP protocol."""
    mcp_client = None
    try:
        mcp_client = MCPClient(
            {"url": "https://freemansel-mcp-sentiment.hf.space/gradio_api/mcp/sse"}
        )
        
        tools = mcp_client.get_tools()
        sentiment_tool = tools[0]  # The sentiment analysis tool
        
        result = sentiment_tool(text=text)
        return result
        
    finally:
        if mcp_client:
            mcp_client.disconnect()

# Example usage
result = analyze_sentiment_mcp("I love this product!")
print(f"Polarity: {result['polarity']}")
print(f"Assessment: {result['assessment']}")
# Output: Polarity: 0.69, Assessment: positive
```

**Why this works:** smolagents handles all the MCP protocol complexity automatically, including the SSE connection and handshake.

---

### B. Gradio Client πŸ”„ **BACKUP SOLUTION**

**Performance:** ~1.3 seconds per request  
**Status:** βœ… Very reliable backup  
**Protocol:** Direct Gradio API access

This bypasses MCP entirely and connects directly to the Gradio API.

#### Installation
```bash
pdm add gradio_client
# or  
pip install gradio_client
```

#### Python Code
```python
#!/usr/bin/env python3
"""
Backup solution using Gradio client.
Performance: ~1.3 seconds per request
"""

import time
from gradio_client import Client

def analyze_sentiment_gradio(text):
    """Analyze sentiment using Gradio client."""
    client = Client("https://freemansel-mcp-sentiment.hf.space")
    result = client.predict(text, api_name="/predict")
    return result

# Example usage
result = analyze_sentiment_gradio("I love this product!")
print(f"Polarity: {result['polarity']}")
print(f"Assessment: {result['assessment']}")
# Output: Polarity: 0.69, Assessment: positive
```

**Why this works:** Direct API access to Gradio's built-in endpoints, no MCP protocol involved.

---

## 3. ❌ Known Issues & Failed Approaches

### A. Low-Level MCP Clients (Don't Use)

**❌ Problem:** The following approach causes `session.initialize()` to hang indefinitely:

```python
# ❌ THIS HANGS - DON'T USE
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client

async def broken_mcp_approach():
    server_params = StdioServerParameters(
        command="npx",
        args=["mcp-remote", "https://freemansel-mcp-sentiment.hf.space/gradio_api/mcp/sse"]
    )
    
    async with stdio_client(server_params) as (read, write):
        async with ClientSession(read, write) as session:
            # ❌ This line hangs forever (tested timeout: 45+ seconds)
            await session.initialize()  
```

**Root Cause:** 
- The low-level MCP client requires manual protocol handling
- `session.initialize()` times out during MCP handshake
- Even though `mcp-remote` connects successfully, the protocol negotiation fails

**Evidence:** 
- STDIO client connects instantly (0.0s)
- Session creation works fine
- Failure occurs specifically in `session.initialize()`
- Error: "unhandled errors in a TaskGroup (1 sub-exception)"

---

### B. Direct HTTP Requests (Don't Work)

**❌ Problem:** MCP endpoints don't accept standard HTTP POST requests:

```python
# ❌ THIS FAILS - DON'T USE
import requests

url = "https://freemansel-mcp-sentiment.hf.space/gradio_api/call/sentiment_analysis"
payload = {"data": ["I love this product!"]}
response = requests.post(url, json=payload)
# Result: 500 Internal Server Error or 405 Method Not Allowed
```

**Why it fails:**
- MCP servers use Server-Sent Events (SSE) protocol
- Standard REST API endpoints are not exposed when `mcp_server=True`
- The `/gradio_api/mcp/sse` endpoint only accepts SSE connections

---

### C. mcp-remote with Manual Clients (Problematic)

**⚠️ Partial Success:** `mcp-remote` proxy connects but has protocol issues:

```bash
# This establishes SSE connection but MCP protocol still fails
npx mcp-remote https://freemansel-mcp-sentiment.hf.space/gradio_api/mcp/sse
```

**What happens:**
1. βœ… HTTP attempt fails (405 Method Not Allowed) - expected
2. βœ… SSE fallback succeeds - connection established  
3. ❌ MCP protocol handshake fails - timeout in client

**Evidence from testing:**
```
[8216] Using transport strategy: http-first
[8216] Received error: Error POSTing to endpoint (HTTP 405): Method Not Allowed
[8216] Recursively reconnecting for reason: falling-back-to-alternate-transport
[8216] Using transport strategy: sse-only
[8216] Connected to remote server using SSEClientTransport
[8216] Local STDIO server running
[8216] Proxy established successfully
```

---

## 4. Performance Comparison

| Method | Speed | Reliability | Protocol | Status |
|--------|-------|-------------|----------|---------|
| **smolagents MCP** | **0.11s** | βœ… Excellent | Native MCP | ⭐ Best |
| **Gradio Client** | 1.3s | βœ… Very Good | Direct API | βœ… Backup |
| Low-level MCP | ❌ Timeout | ❌ Broken | Manual MCP | ❌ Don't use |
| HTTP Requests | ❌ 405 Error | ❌ Broken | REST | ❌ Don't use |

---

## 5. Real-World Troubleshooting

### A. Import Debugging

If you have import issues, create this debugging script:

```python
#!/usr/bin/env python3
"""Debug MCP imports"""

def check_imports():
    try:
        import smolagents.mcp_client
        print("βœ… smolagents available - use smolagents approach")
        return True
    except ImportError:
        print("❌ Install: pip install 'smolagents[mcp]'")
        
    try:
        import gradio_client
        print("βœ… gradio_client available - use backup approach")
        return True
    except ImportError:
        print("❌ Install: pip install gradio_client")
        
    return False

check_imports()
```

### B. Connection Testing

Test the SSE endpoint directly in your browser:
```
https://freemansel-mcp-sentiment.hf.space/gradio_api/mcp/sse
```

You should see:
```
: ping - 2025-05-23 18:36:12.808434+00:00
: ping - 2025-05-23 18:36:27.809481+00:00
```

If you don't see pings, the Hugging Face Space might be sleeping.

### C. Common Error Messages

**"unhandled errors in a TaskGroup"** β†’ Use smolagents instead of low-level MCP  
**"HTTP 405 Method Not Allowed"** β†’ Use Gradio client instead of HTTP requests  
**"Connection timeout"** β†’ Check if HF Space is running, try Gradio client  
**"Import error: smolagents"** β†’ Install with `pip install "smolagents[mcp]"`

---

## 6. Ready-to-Use Scripts

Complete, tested scripts are available in the `usage/` folder:

```bash
# Primary solution (fastest)
pdm run python usage/sentiment_mcp.py

# Backup solution (reliable)
pdm run python usage/sentiment_gradio.py

# Debug imports if needed
pdm run python usage/debug_imports.py
```

---

## 7. Cursor AI Integration

For Cursor AI, use the working smolagents approach:

```json
{
  "mcpServers": {
    "sentiment-analysis": {
      "command": "python",
      "args": [
        "-c",
        "from smolagents.mcp_client import MCPClient; client = MCPClient({'url': 'https://freemansel-mcp-sentiment.hf.space/gradio_api/mcp/sse'}); tools = client.get_tools(); result = tools[0](text=input('Text: ')); print(f'Sentiment: {result}')"
      ]
    }
  }
}
```

**Note:** This is a simplified example. For production use, create a proper MCP server script.

---

## 8. Key Learnings from Real Testing

1. **Always use high-level clients** - `smolagents.MCPClient` works, low-level `mcp.ClientSession` doesn't
2. **Follow official documentation** - The [Hugging Face MCP Course](https://huggingface.co/learn/mcp-course/unit2/gradio-client) shows the right approach
3. **Have a backup plan** - Gradio client provides reliable fallback when MCP has issues
4. **Test everything** - Theoretical solutions often don't work in practice
5. **Performance varies dramatically** - smolagents (0.11s) vs Gradio (1.3s) vs broken approaches (timeout)

---

## 9. References

- [Hugging Face MCP Course](https://huggingface.co/learn/mcp-course/unit2/gradio-client) - **Use this approach**
- [smolagents Documentation](https://huggingface.co/docs/smolagents) - High-level MCP client
- [Gradio Client Documentation](https://gradio.app/guides/getting-started-with-the-python-client/) - Backup approach
- [MCP Protocol Documentation](https://modelcontextprotocol.io/) - Low-level details

---

**βœ… This guide is based on extensive real-world testing and provides only verified, working solutions!**