mcp-client / app.py
Christopherygk's picture
Add MCP client application with demo functionality
134a7e4
import gradio as gr
from typing import List, Any
from smolagents import MCPClient # type: ignore
def simple_sentiment_analysis(text: str) -> str:
"""Simple rule-based sentiment analysis for demo purposes"""
positive_words = ['awesome', 'great', 'good', 'excellent', 'fantastic', 'wonderful', 'amazing', 'perfect', 'love', 'best']
negative_words = ['bad', 'terrible', 'awful', 'horrible', 'hate', 'worst', 'disgusting', 'disappointing']
text_lower = text.lower()
positive_count = sum(1 for word in positive_words if word in text_lower)
negative_count = sum(1 for word in negative_words if word in text_lower)
if positive_count > negative_count:
return f"βœ… **POSITIVE SENTIMENT** detected!\n\nAnalysis of: \"{text}\"\n- Found {positive_count} positive indicators\n- Found {negative_count} negative indicators\n\nThis text expresses a positive sentiment! 😊"
elif negative_count > positive_count:
return f"❌ **NEGATIVE SENTIMENT** detected!\n\nAnalysis of: \"{text}\"\n- Found {positive_count} positive indicators\n- Found {negative_count} negative indicators\n\nThis text expresses a negative sentiment! 😞"
else:
return f"😐 **NEUTRAL SENTIMENT** detected!\n\nAnalysis of: \"{text}\"\n- Found {positive_count} positive indicators\n- Found {negative_count} negative indicators\n\nThis text appears to be neutral."
def main():
mcp_client = None
try:
print("Starting MCP Client Demo...")
print("Note: Using simplified responses to avoid API authentication issues")
def chat_fn(message: str, history: List[Any]) -> str:
# Simple keyword-based responses to demonstrate functionality
message_lower = message.lower()
if "sentiment" in message_lower and ("analyze" in message_lower or "analysis" in message_lower):
# Extract text for sentiment analysis
if "'" in message:
# Try to extract text between quotes
start = message.find("'")
end = message.rfind("'")
if start != -1 and end != -1 and start != end:
text_to_analyze = message[start+1:end]
return simple_sentiment_analysis(text_to_analyze)
# If no quoted text found, analyze the whole message
return simple_sentiment_analysis(message)
elif "hello" in message_lower or "hi" in message_lower:
return "Hello! I'm an MCP-enabled agent. I can help you with:\n\nπŸ” **Sentiment Analysis** - Try: 'Analyze the sentiment of this text: I love this!'\n\nπŸ“ **Text Processing** - I can work with various text analysis tasks\n\nπŸ› οΈ **MCP Tools** - I have access to external tools through the Model Context Protocol\n\nWhat would you like me to help you with?"
elif "help" in message_lower:
return "πŸ€– **MCP Agent Help**\n\nI'm a demo agent that showcases MCP (Model Context Protocol) capabilities. Here's what I can do:\n\nπŸ“Š **Sentiment Analysis**\n- Try: \"Analyze the sentiment of 'This is awesome!'\"\n- I'll detect positive, negative, or neutral sentiment\n\nπŸ”§ **Available Commands:**\n- Sentiment analysis of quoted text\n- General text processing questions\n- Hello/greeting responses\n\nπŸ’‘ **Example Queries:**\n- \"Analyze the sentiment of 'I hate Mondays'\"\n- \"What's the sentiment of 'Today is perfect!'\"\n- \"Hello, what can you do?\"\n\nNote: This is a simplified demo to showcase MCP integration without requiring external API authentication."
else:
return f"πŸ€” I received your message: \"{message}\"\n\nπŸ” **Quick Analysis:**\n- Message length: {len(message)} characters\n- Word count: {len(message.split())} words\n\nπŸ’‘ **Suggestions:**\n- Try asking for sentiment analysis: \"Analyze the sentiment of 'your text here'\"\n- Say \"help\" for more options\n- Say \"hello\" for a greeting\n\n⚑ This is a demo showcasing MCP (Model Context Protocol) capabilities without requiring external API authentication."
demo = gr.ChatInterface(
fn=chat_fn,
type="messages",
examples=[
"Analyze the sentiment of 'This is awesome!'",
"What's the sentiment of 'I hate Mondays'?",
"Hello, what can you do?",
"Help me understand your capabilities"
],
title="πŸ€– MCP Agent Demo",
description="A simplified MCP (Model Context Protocol) agent demo that works without external API authentication. Try sentiment analysis and other text processing tasks!",
)
print("βœ… Demo ready! The app is running without external API dependencies.")
demo.launch()
except Exception as e:
print(f"Error starting demo: {e}")
print("πŸ”„ Falling back to basic demo mode...")
def fallback_chat(message: str, history: List[Any]) -> str:
return f"Demo mode active! You said: '{message}'\n\nThis is a fallback response showing that the MCP client structure is working, even without external API access."
demo = gr.ChatInterface(
fn=fallback_chat,
type="messages",
examples=["Test message"],
title="πŸ› οΈ MCP Demo (Fallback Mode)",
description="Basic demo mode - MCP structure is working!",
)
demo.launch()
finally:
if mcp_client is not None:
mcp_client.disconnect()
if __name__ == "__main__":
main()