File size: 6,795 Bytes
a6bfba7 |
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 |
#!/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")
|