Stack_Overflow_MCP_Server / demo_mcp_client.py
MarkNawar's picture
Upload folder using huggingface_hub
a6bfba7 verified
#!/usr/bin/env python3
"""
MCP Client Demo for Stack Overflow Server
Demonstrates how to interact with the Stack Overflow MCP server programmatically.
"""
import asyncio
import json
from typing import Dict, Any
# This is a demonstration of how an MCP client would interact with our server
# In practice, you would use a proper MCP client library
async def demo_mcp_calls():
"""
Demonstrate various MCP calls to the Stack Overflow server.
This simulates what an AI assistant like Claude would do.
"""
print("πŸ€– Stack Overflow MCP Server Demo")
print("=" * 50)
# Demo 1: General Search
print("\n1️⃣ GENERAL SEARCH DEMO")
print("Query: 'Django pagination best practices'")
print("Tags: ['python', 'django']")
print("Expected: High-quality Django pagination solutions")
# Demo 2: Error Search
print("\n2️⃣ ERROR SEARCH DEMO")
print("Error: 'TypeError: NoneType object has no attribute'")
print("Language: Python")
print("Expected: Common solutions for NoneType errors")
# Demo 3: Question Retrieval
print("\n3️⃣ QUESTION RETRIEVAL DEMO")
print("Question ID: 11227809")
print("Expected: Famous 'Why is processing a sorted array faster?' question")
# Demo 4: Stack Trace Analysis
print("\n4️⃣ STACK TRACE ANALYSIS DEMO")
print("Stack Trace: 'ReferenceError: useState is not defined'")
print("Language: JavaScript")
print("Expected: React hooks solutions")
# Demo 5: Advanced Search
print("\n5️⃣ ADVANCED SEARCH DEMO")
print("Query: 'memory optimization'")
print("Tags: ['c++', 'performance']")
print("Min Score: 50")
print("Expected: High-quality C++ performance answers")
print("\n" + "=" * 50)
print("🎯 All demos completed!")
print("πŸ’‘ These are the types of searches our MCP server can handle")
print("πŸš€ Try them in the web interface or via MCP client!")
def demo_gradio_api_calls():
"""
Demonstrate how the Gradio API exposes the MCP functionality.
"""
print("\n🎨 GRADIO API INTEGRATION")
print("=" * 50)
# Show the available API endpoints
api_endpoints = {
"search_by_query_sync": {
"description": "General Stack Overflow search",
"inputs": ["query", "tags", "min_score", "has_accepted_answer", "limit", "response_format"],
"example": {
"query": "Django pagination best practices",
"tags": "python,django",
"min_score": 5,
"has_accepted_answer": True,
"limit": 5,
"response_format": "markdown"
}
},
"search_by_error_sync": {
"description": "Error-specific search",
"inputs": ["error_message", "language", "technologies", "min_score", "has_accepted_answer", "limit", "response_format"],
"example": {
"error_message": "TypeError: 'NoneType' object has no attribute",
"language": "python",
"technologies": "flask,sqlalchemy",
"min_score": 0,
"has_accepted_answer": True,
"limit": 5,
"response_format": "markdown"
}
},
"get_question_sync": {
"description": "Get specific question by ID",
"inputs": ["question_id", "include_comments", "response_format"],
"example": {
"question_id": "11227809",
"include_comments": True,
"response_format": "markdown"
}
},
"analyze_stack_trace_sync": {
"description": "Analyze stack traces",
"inputs": ["stack_trace", "language", "min_score", "has_accepted_answer", "limit", "response_format"],
"example": {
"stack_trace": "ReferenceError: useState is not defined\n at Component.render",
"language": "javascript",
"min_score": 5,
"has_accepted_answer": True,
"limit": 3,
"response_format": "markdown"
}
},
"advanced_search_sync": {
"description": "Advanced search with comprehensive filters",
"inputs": ["query", "tags", "excluded_tags", "min_score", "title", "body", "min_answers", "has_accepted_answer", "min_views", "sort_by", "limit", "response_format"],
"example": {
"query": "memory optimization",
"tags": "c++,performance",
"excluded_tags": "beginner",
"min_score": 50,
"title": "",
"body": "",
"min_answers": 1,
"has_accepted_answer": False,
"min_views": 1000,
"sort_by": "votes",
"limit": 5,
"response_format": "markdown"
}
}
}
for endpoint, info in api_endpoints.items():
print(f"\nπŸ”§ {endpoint}")
print(f" πŸ“ {info['description']}")
print(f" πŸ“Š Example:")
for key, value in info['example'].items():
print(f" {key}: {value}")
def demo_mcp_integration():
"""
Show how to integrate with MCP clients like Claude Desktop.
"""
print("\nπŸ”— MCP CLIENT INTEGRATION")
print("=" * 50)
{
}
claude_config = {
"mcpServers": {
"stackoverflow": {
"command": "npx",
"args": [
"mcp-remote",
"https://c44b366466c774a9d5.gradio.live/gradio_api/mcp/sse"
]
}
}
}
print("πŸ’» Claude Desktop Configuration:")
print(json.dumps(claude_config, indent=2))
print("\nπŸ€– Example AI Assistant Prompts:")
prompts = [
"Search Stack Overflow for Django pagination best practices",
"Find solutions for the error 'TypeError: NoneType object has no attribute'",
"Get Stack Overflow question 11227809",
"Analyze this JavaScript error: ReferenceError: useState is not defined",
"Find high-scored C++ memory optimization questions"
]
for i, prompt in enumerate(prompts, 1):
print(f" {i}. {prompt}")
if __name__ == "__main__":
print("πŸš€ Starting Stack Overflow MCP Server Demo...")
# Run the async demo
asyncio.run(demo_mcp_calls())
# Show Gradio API integration
demo_gradio_api_calls()
# Show MCP client integration
demo_mcp_integration()
print("\n🎯 Demo completed! Visit the web interface to try it live:")
print("🌐 https://c44b366466c774a9d5.gradio.live")
print("πŸ”— MCP Endpoint: https://c44b366466c774a9d5.gradio.live/gradio_api/mcp/sse")