mcp-sentiment / SOLUTION.md
phil71x
feat: Update dependencies and add new packages for sentiment analysis
48a7f49

A newer version of the Gradio SDK is available: 5.42.0

Upgrade

MCP Sentiment Analysis - Solution

Problem Summary

Your original working_mcp_test.py script was hanging because of an issue with the MCP (Model Context Protocol) connection process. Specifically:

  1. βœ… STDIO client connection - This worked fine (0.0s)
  2. βœ… ClientSession creation - This also worked fine
  3. ❌ Session initialization - This was timing out after 30-45 seconds

The issue was in the session.initialize() step, which handles the MCP protocol handshake between your client and the remote server.

Root Cause

The problem was using the wrong MCP client:

  • ❌ Low-level mcp.ClientSession - requires manual protocol handling and times out
  • βœ… High-level smolagents.mcp_client.MCPClient - handles all protocol complexity automatically

Based on the Hugging Face MCP Course, the smolagents library provides the proper high-level interface for MCP connections.

βœ… PRIMARY SOLUTION: smolagents MCPClient

This is the best solution - fast, reliable, and uses proper MCP protocol.

Installation

pdm add "smolagents[mcp]"

Code: usage/sentiment_mcp.py

#!/usr/bin/env python3
"""
MCP Sentiment Analysis using smolagents MCPClient.

To run this script:
    pdm run python usage/sentiment_mcp.py
"""

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()

def main():
    test_texts = [
        "I love this product! It's amazing!",
        "This is terrible. I hate it.",
        "It's okay, nothing special.",
    ]

    for i, text in enumerate(test_texts, 1):
        print(f"Test {i}: '{text}'")
        
        start_time = time.time()
        result = analyze_sentiment_mcp(text)
        elapsed = time.time() - start_time
        
        print(f"  πŸ“Š Polarity: {result['polarity']}")
        print(f"  πŸ“Š Subjectivity: {result['subjectivity']}")
        print(f"  πŸ“Š Assessment: {result['assessment']}")
        print(f"  ⏱️  Time: {elapsed:.2f}s")
        print()

if __name__ == "__main__":
    main()

Results with smolagents

Test 1: 'I love this product! It's amazing!'
  πŸ“Š Polarity: 0.69
  πŸ“Š Subjectivity: 0.75
  πŸ“Š Assessment: positive
  ⏱️  Time: 0.11s

Test 2: 'This is terrible. I hate it.'
  πŸ“Š Polarity: -0.9
  πŸ“Š Subjectivity: 0.95
  πŸ“Š Assessment: negative
  ⏱️  Time: 0.11s

Performance: ~0.11 seconds per request! πŸš€

πŸ”„ BACKUP SOLUTION: Gradio Client

If you prefer not to use MCP protocol, the Gradio client approach also works reliably.

File: usage/sentiment_gradio.py

#!/usr/bin/env python3
"""
Gradio Sentiment Analysis (Backup Solution).

To run this script:
    pdm run python usage/sentiment_gradio.py
"""

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

def main():
    test_texts = [
        "I love this product! It's amazing!",
        "This is terrible. I hate it.",
        "It's okay, nothing special.",
    ]

    client = Client("https://freemansel-mcp-sentiment.hf.space")
    
    for i, text in enumerate(test_texts, 1):
        print(f"Test {i}: '{text}'")
        
        start_time = time.time()
        result = client.predict(text, api_name="/predict")
        elapsed = time.time() - start_time
        
        print(f"  πŸ“Š Polarity: {result.get('polarity', 'N/A')}")
        print(f"  πŸ“Š Subjectivity: {result.get('subjectivity', 'N/A')}")
        print(f"  πŸ“Š Assessment: {result.get('assessment', 'N/A')}")
        print(f"  ⏱️  Time: {elapsed:.2f}s")
        print()

if __name__ == "__main__":
    main()

Performance: ~1.3 seconds per request.

Comparison

Method Setup Speed Protocol Recommended
smolagents MCP pdm add "smolagents[mcp]" 0.11s βœ… Native MCP ⭐ Best
Gradio Client gradio_client (already installed) 1.3s Direct API βœ… Good backup
Low-level MCP ❌ ❌ Timeout ❌ Broken ❌ Don't use

Running the Solutions

Primary (smolagents):

cd /c/Users/phil7/Code/mcp-sentiment
pdm add "smolagents[mcp]"  # If not already installed
pdm run python usage/sentiment_mcp.py

Backup (Gradio):

cd /c/Users/phil7/Code/mcp-sentiment
pdm run python usage/sentiment_gradio.py

Debugging:

# If you have import issues
pdm run python usage/debug_imports.py

Key Learnings

  1. Use High-Level Clients: Always prefer smolagents.MCPClient over low-level mcp.ClientSession
  2. Follow Official Docs: The Hugging Face MCP Course provides the correct approach
  3. MCP Works Great: When used properly, MCP is actually faster than direct API calls!
  4. Protocol Abstraction Matters: High-level libraries handle complex protocol details

Conclusion

The smolagents MCPClient is the optimal solution, providing:

  • βœ… Fastest performance (0.11s vs 1.3s)
  • βœ… Proper MCP protocol usage
  • βœ… No connection issues
  • βœ… Clean, maintainable code

The original issue was simply using the wrong level of MCP client. The Hugging Face documentation showed us the right way! πŸŽ‰