Spaces:
Sleeping
Sleeping
AdilzhanB
commited on
Commit
Β·
78fb420
0
Parent(s):
Merge remote changes with local files
Browse files- .gitattributes +35 -0
- README.md +14 -0
- app.py +483 -0
- requirements.txt +4 -0
.gitattributes
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: MCP Client Distributor
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: pink
|
| 5 |
+
colorTo: pink
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.34.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
short_description: MCP Chatbot with custom client distribution
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,483 @@
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
from typing import List, Dict, Any
|
| 5 |
+
|
| 6 |
+
try:
|
| 7 |
+
from smolagents import InferenceClientModel, CodeAgent, MCPClient
|
| 8 |
+
HAS_SMOLAGENTS = True
|
| 9 |
+
except ImportError:
|
| 10 |
+
HAS_SMOLAGENTS = False
|
| 11 |
+
|
| 12 |
+
class SimpleMCPClient:
|
| 13 |
+
"""Simple MCP Client for Hugging Face Spaces"""
|
| 14 |
+
|
| 15 |
+
def __init__(self):
|
| 16 |
+
self.current_client = None
|
| 17 |
+
self.current_agent = None
|
| 18 |
+
self.conversation_history = []
|
| 19 |
+
|
| 20 |
+
# Default MCP server
|
| 21 |
+
self.default_server_url = "https://abidlabs-mcp-tool-http.hf.space/gradio_api/mcp/sse"
|
| 22 |
+
|
| 23 |
+
def connect_to_server(self, server_url: str = None) -> tuple[bool, str]:
|
| 24 |
+
"""Connect to MCP server"""
|
| 25 |
+
url = server_url or self.default_server_url
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
if HAS_SMOLAGENTS:
|
| 29 |
+
# Disconnect existing connection
|
| 30 |
+
if self.current_client:
|
| 31 |
+
try:
|
| 32 |
+
self.current_client.disconnect()
|
| 33 |
+
except:
|
| 34 |
+
pass
|
| 35 |
+
|
| 36 |
+
# Create new connection
|
| 37 |
+
self.current_client = MCPClient({"url": url})
|
| 38 |
+
tools = self.current_client.get_tools()
|
| 39 |
+
|
| 40 |
+
# Create agent
|
| 41 |
+
model = InferenceClientModel(token=os.getenv("HF_TOKEN"))
|
| 42 |
+
self.current_agent = CodeAgent(
|
| 43 |
+
tools=[*tools],
|
| 44 |
+
model=model,
|
| 45 |
+
additional_authorized_imports=["json", "ast", "urllib", "base64"]
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
return True, f"β
Connected successfully! Available tools: {len(tools)}"
|
| 49 |
+
else:
|
| 50 |
+
# Fallback for when smolagents is not available
|
| 51 |
+
return True, "β
Connected (simulated - smolagents not available)"
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return False, f"β Connection failed: {str(e)}"
|
| 55 |
+
|
| 56 |
+
def chat_with_agent(self, message: str) -> str:
|
| 57 |
+
"""Chat with the connected agent"""
|
| 58 |
+
if not message.strip():
|
| 59 |
+
return "β Please enter a message."
|
| 60 |
+
|
| 61 |
+
if not self.current_agent and HAS_SMOLAGENTS:
|
| 62 |
+
return "β Please connect to a server first."
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
if HAS_SMOLAGENTS and self.current_agent:
|
| 66 |
+
response = str(self.current_agent.run(message))
|
| 67 |
+
else:
|
| 68 |
+
# Fallback response
|
| 69 |
+
response = f"Simulated response: I received your message '{message}' but cannot process it without smolagents."
|
| 70 |
+
|
| 71 |
+
# Add to history
|
| 72 |
+
self.conversation_history.append({
|
| 73 |
+
"user": message,
|
| 74 |
+
"assistant": response
|
| 75 |
+
})
|
| 76 |
+
|
| 77 |
+
return response
|
| 78 |
+
|
| 79 |
+
except Exception as e:
|
| 80 |
+
error_msg = f"β Error: {str(e)}"
|
| 81 |
+
self.conversation_history.append({
|
| 82 |
+
"user": message,
|
| 83 |
+
"assistant": error_msg
|
| 84 |
+
})
|
| 85 |
+
return error_msg
|
| 86 |
+
|
| 87 |
+
def get_conversation_history(self) -> List[List[str]]:
|
| 88 |
+
"""Get conversation history in Gradio ChatInterface format"""
|
| 89 |
+
history = []
|
| 90 |
+
for conv in self.conversation_history:
|
| 91 |
+
history.append([conv["user"], conv["assistant"]])
|
| 92 |
+
return history
|
| 93 |
+
|
| 94 |
+
def disconnect(self):
|
| 95 |
+
"""Disconnect from server"""
|
| 96 |
+
if self.current_client and HAS_SMOLAGENTS:
|
| 97 |
+
try:
|
| 98 |
+
self.current_client.disconnect()
|
| 99 |
+
except:
|
| 100 |
+
pass
|
| 101 |
+
self.current_client = None
|
| 102 |
+
self.current_agent = None
|
| 103 |
+
|
| 104 |
+
# Global client instance
|
| 105 |
+
simple_client = SimpleMCPClient()
|
| 106 |
+
|
| 107 |
+
# Gradio Interface Functions
|
| 108 |
+
def connect_wrapper(server_url: str) -> str:
|
| 109 |
+
"""Wrapper for connection"""
|
| 110 |
+
success, message = simple_client.connect_to_server(server_url)
|
| 111 |
+
return message
|
| 112 |
+
|
| 113 |
+
def chat_wrapper(message: str, history: List[List[str]]) -> str:
|
| 114 |
+
"""Wrapper for chat"""
|
| 115 |
+
return simple_client.chat_with_agent(message)
|
| 116 |
+
|
| 117 |
+
# Pre-defined servers for easy selection
|
| 118 |
+
SERVER_OPTIONS = {
|
| 119 |
+
"MCP Tools Collection": "https://abidlabs-mcp-tool-http.hf.space/gradio_api/mcp/sse",
|
| 120 |
+
# "Sentiment Analyzer": "https://huggingface.co/spaces/Adilbai/MCP-sentiment-analyzer/gradio_api/mcp/sse", # Removed sentiment analyzer
|
| 121 |
+
"Custom": ""
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
# Example prompts
|
| 125 |
+
EXAMPLE_PROMPTS = [
|
| 126 |
+
"What is the prime factorization of 68?",
|
| 127 |
+
"Calculate the square root of 144",
|
| 128 |
+
# "Analyze the sentiment of: 'I love this product!'", # Removed sentiment example
|
| 129 |
+
"Convert 'Hello World' to base64",
|
| 130 |
+
"What tools are available?",
|
| 131 |
+
"Help me with a math problem"
|
| 132 |
+
]
|
| 133 |
+
|
| 134 |
+
# Create Gradio Interface
|
| 135 |
+
with gr.Blocks(
|
| 136 |
+
theme=gr.themes.Soft(),
|
| 137 |
+
title="π Simple MCP Client",
|
| 138 |
+
css="""
|
| 139 |
+
.gradio-container {
|
| 140 |
+
max-width: 1200px !important;
|
| 141 |
+
margin: auto !important;
|
| 142 |
+
}
|
| 143 |
+
.main-header {
|
| 144 |
+
text-align: center;
|
| 145 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 146 |
+
-webkit-background-clip: text;
|
| 147 |
+
-webkit-text-fill-color: transparent;
|
| 148 |
+
background-clip: text;
|
| 149 |
+
font-size: 2.5em;
|
| 150 |
+
font-weight: bold;
|
| 151 |
+
margin-bottom: 20px;
|
| 152 |
+
}
|
| 153 |
+
"""
|
| 154 |
+
) as demo:
|
| 155 |
+
|
| 156 |
+
gr.HTML("""
|
| 157 |
+
<div class="main-header">
|
| 158 |
+
π Simple MCP Client
|
| 159 |
+
</div>
|
| 160 |
+
<p style="text-align: center; font-size: 1.2em; color: #666; margin-bottom: 30px;">
|
| 161 |
+
Connect to MCP servers and chat with AI agents
|
| 162 |
+
</p>
|
| 163 |
+
""")
|
| 164 |
+
|
| 165 |
+
with gr.Tabs():
|
| 166 |
+
# Main Chat Tab
|
| 167 |
+
with gr.Tab("π¬ Agent Chat"):
|
| 168 |
+
with gr.Row():
|
| 169 |
+
with gr.Column(scale=2):
|
| 170 |
+
# Chat interface
|
| 171 |
+
chatbot = gr.Chatbot(
|
| 172 |
+
label="π€ MCP Agent Chat",
|
| 173 |
+
height=400,
|
| 174 |
+
show_label=True
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
msg = gr.Textbox(
|
| 178 |
+
label="Your Message",
|
| 179 |
+
placeholder="Type your message here...",
|
| 180 |
+
lines=2,
|
| 181 |
+
max_lines=5
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
with gr.Row():
|
| 185 |
+
send_btn = gr.Button("π€ Send", variant="primary", size="lg")
|
| 186 |
+
clear_btn = gr.Button("ποΈ Clear Chat", variant="secondary")
|
| 187 |
+
|
| 188 |
+
with gr.Column(scale=1):
|
| 189 |
+
gr.Markdown("### π Server Connection")
|
| 190 |
+
|
| 191 |
+
server_select = gr.Dropdown(
|
| 192 |
+
choices=list(SERVER_OPTIONS.keys()),
|
| 193 |
+
value="MCP Tools Collection" if "MCP Tools Collection" in SERVER_OPTIONS else (list(SERVER_OPTIONS.keys())[0] if SERVER_OPTIONS else "Custom"), # Adjusted default value
|
| 194 |
+
label="Select Server"
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
custom_url = gr.Textbox(
|
| 198 |
+
label="Custom Server URL",
|
| 199 |
+
placeholder="https://your-server.com/gradio_api/mcp/sse",
|
| 200 |
+
visible=(server_select.value == "Custom") # Adjusted visibility based on initial value
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
connect_btn = gr.Button("π Connect to Server", variant="primary")
|
| 204 |
+
|
| 205 |
+
connection_status = gr.Textbox(
|
| 206 |
+
label="Connection Status",
|
| 207 |
+
value="Not connected",
|
| 208 |
+
interactive=False,
|
| 209 |
+
lines=3
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
gr.Markdown("### π― Example Prompts")
|
| 213 |
+
example_dropdown = gr.Dropdown(
|
| 214 |
+
choices=EXAMPLE_PROMPTS,
|
| 215 |
+
label="Select an example",
|
| 216 |
+
value=None
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
# Server Management Tab
|
| 220 |
+
with gr.Tab("π₯οΈ Server Management"):
|
| 221 |
+
gr.Markdown("## π§ MCP Server Configuration")
|
| 222 |
+
|
| 223 |
+
with gr.Row():
|
| 224 |
+
with gr.Column():
|
| 225 |
+
gr.Markdown("### π Available Servers")
|
| 226 |
+
|
| 227 |
+
for name, url in SERVER_OPTIONS.items():
|
| 228 |
+
if name != "Custom":
|
| 229 |
+
gr.Markdown(f"""
|
| 230 |
+
**{name}**
|
| 231 |
+
- URL: `{url}`
|
| 232 |
+
- Status: {'π’ Available' if url else 'π΄ Not configured'}
|
| 233 |
+
""")
|
| 234 |
+
|
| 235 |
+
with gr.Column():
|
| 236 |
+
gr.Markdown("### β Add Custom Server")
|
| 237 |
+
|
| 238 |
+
custom_name = gr.Textbox(
|
| 239 |
+
label="Server Name",
|
| 240 |
+
placeholder="My Custom Server"
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
+
custom_server_url = gr.Textbox(
|
| 244 |
+
label="Server URL",
|
| 245 |
+
placeholder="https://your-mcp-server.com/gradio_api/mcp/sse"
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
test_connection_btn = gr.Button("π§ͺ Test Connection", variant="secondary")
|
| 249 |
+
|
| 250 |
+
test_result = gr.Textbox(
|
| 251 |
+
label="Test Result",
|
| 252 |
+
interactive=False,
|
| 253 |
+
lines=2
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
+
# Help & About Tab
|
| 257 |
+
with gr.Tab("βΉοΈ Help & About"):
|
| 258 |
+
gr.Markdown("""
|
| 259 |
+
## π Simple MCP Client
|
| 260 |
+
|
| 261 |
+
This is a streamlined **Model Context Protocol (MCP) Client** for connecting to MCP servers and interacting with AI agents.
|
| 262 |
+
|
| 263 |
+
### π Quick Start
|
| 264 |
+
|
| 265 |
+
1. **Connect to a Server:**
|
| 266 |
+
- Select a pre-configured server from the dropdown
|
| 267 |
+
- Click "Connect to Server"
|
| 268 |
+
- Wait for connection confirmation
|
| 269 |
+
|
| 270 |
+
2. **Start Chatting:**
|
| 271 |
+
- Type your message in the text box
|
| 272 |
+
- Click "Send" or press Enter
|
| 273 |
+
- The AI agent will respond using available MCP tools
|
| 274 |
+
|
| 275 |
+
3. **Try Examples:**
|
| 276 |
+
- Use the "Example Prompts" dropdown for inspiration
|
| 277 |
+
- Different servers have different capabilities
|
| 278 |
+
|
| 279 |
+
### π§ Pre-configured Servers
|
| 280 |
+
|
| 281 |
+
**MCP Tools Collection:** General-purpose tools including math, text processing, and utilities
|
| 282 |
+
|
| 283 |
+
### π― Example Use Cases
|
| 284 |
+
|
| 285 |
+
- **Math Calculations:** "What is the prime factorization of 68?"
|
| 286 |
+
- **Text Processing:** "Convert 'Hello World' to base64"
|
| 287 |
+
- **Data Analysis:** "Calculate statistics for [1,2,3,4,5]"
|
| 288 |
+
|
| 289 |
+
### π§ Technical Requirements
|
| 290 |
+
|
| 291 |
+
- **smolagents:** For full MCP client functionality
|
| 292 |
+
- **Hugging Face Token:** Set `HF_TOKEN` environment variable
|
| 293 |
+
- **Internet Connection:** Required for server communication
|
| 294 |
+
|
| 295 |
+
### π Troubleshooting
|
| 296 |
+
|
| 297 |
+
**Connection Issues:**
|
| 298 |
+
- Verify server URL is correct
|
| 299 |
+
- Check internet connectivity
|
| 300 |
+
- Ensure Hugging Face token is valid
|
| 301 |
+
|
| 302 |
+
**Chat Issues:**
|
| 303 |
+
- Make sure you're connected to a server
|
| 304 |
+
- Try reconnecting if responses are slow
|
| 305 |
+
- Check if the server supports your request type
|
| 306 |
+
|
| 307 |
+
---
|
| 308 |
+
|
| 309 |
+
**Current Status:**
|
| 310 |
+
- smolagents Available: 'β
Yes'
|
| 311 |
+
- MCP Protocol Version: 2024-11-05
|
| 312 |
+
- Client Version: 1.0.0
|
| 313 |
+
""")
|
| 314 |
+
|
| 315 |
+
# Event Handlers
|
| 316 |
+
|
| 317 |
+
# Show/hide custom URL input based on server selection
|
| 318 |
+
def update_custom_url_visibility(server_name):
|
| 319 |
+
return gr.update(visible=(server_name == "Custom"))
|
| 320 |
+
|
| 321 |
+
server_select.change(
|
| 322 |
+
fn=update_custom_url_visibility,
|
| 323 |
+
inputs=server_select,
|
| 324 |
+
outputs=custom_url
|
| 325 |
+
)
|
| 326 |
+
|
| 327 |
+
# Connect to server
|
| 328 |
+
def connect_to_selected_server(server_name, custom_url_value):
|
| 329 |
+
if server_name == "Custom":
|
| 330 |
+
url = custom_url_value
|
| 331 |
+
else:
|
| 332 |
+
url = SERVER_OPTIONS.get(server_name)
|
| 333 |
+
|
| 334 |
+
if not url:
|
| 335 |
+
return "β Please provide a server URL"
|
| 336 |
+
|
| 337 |
+
return connect_wrapper(url)
|
| 338 |
+
|
| 339 |
+
connect_btn.click(
|
| 340 |
+
fn=connect_to_selected_server,
|
| 341 |
+
inputs=[server_select, custom_url],
|
| 342 |
+
outputs=connection_status
|
| 343 |
+
)
|
| 344 |
+
|
| 345 |
+
# Chat functionality
|
| 346 |
+
def chat_function(message, history):
|
| 347 |
+
if not message.strip():
|
| 348 |
+
return history, "" # Return current history and empty message box
|
| 349 |
+
|
| 350 |
+
response = simple_client.chat_with_agent(message)
|
| 351 |
+
# The history is managed by Gradio's Chatbot by returning a list of lists
|
| 352 |
+
# We need to append to the history list passed in and return it
|
| 353 |
+
new_history = history + [[message, response]]
|
| 354 |
+
return new_history, "" # Return updated history and clear message box
|
| 355 |
+
|
| 356 |
+
send_btn.click(
|
| 357 |
+
fn=chat_function,
|
| 358 |
+
inputs=[msg, chatbot],
|
| 359 |
+
outputs=[chatbot, msg]
|
| 360 |
+
)
|
| 361 |
+
|
| 362 |
+
msg.submit(
|
| 363 |
+
fn=chat_function,
|
| 364 |
+
inputs=[msg, chatbot],
|
| 365 |
+
outputs=[chatbot, msg]
|
| 366 |
+
)
|
| 367 |
+
|
| 368 |
+
# Clear chat
|
| 369 |
+
def clear_chat_history():
|
| 370 |
+
simple_client.conversation_history = [] # Clear internal history
|
| 371 |
+
return [], "" # Clear chatbot display and message input
|
| 372 |
+
|
| 373 |
+
clear_btn.click(
|
| 374 |
+
fn=clear_chat_history,
|
| 375 |
+
inputs=None, # No inputs needed
|
| 376 |
+
outputs=[chatbot, msg] # Clear chatbot and message box
|
| 377 |
+
)
|
| 378 |
+
|
| 379 |
+
# Example prompts
|
| 380 |
+
example_dropdown.change(
|
| 381 |
+
fn=lambda x: x if x else "",
|
| 382 |
+
inputs=example_dropdown,
|
| 383 |
+
outputs=msg
|
| 384 |
+
)
|
| 385 |
+
|
| 386 |
+
# Test connection
|
| 387 |
+
def test_custom_connection(url):
|
| 388 |
+
if not url.strip():
|
| 389 |
+
return "β Please enter a URL to test"
|
| 390 |
+
|
| 391 |
+
# Temporarily connect and disconnect to test
|
| 392 |
+
original_client = simple_client.current_client
|
| 393 |
+
original_agent = simple_client.current_agent
|
| 394 |
+
|
| 395 |
+
success, message = simple_client.connect_to_server(url)
|
| 396 |
+
|
| 397 |
+
# Restore original state if it was connected
|
| 398 |
+
if original_client:
|
| 399 |
+
simple_client.current_client = original_client
|
| 400 |
+
simple_client.current_agent = original_agent
|
| 401 |
+
else: # if it wasn't connected before, disconnect the test connection
|
| 402 |
+
simple_client.disconnect()
|
| 403 |
+
|
| 404 |
+
return message
|
| 405 |
+
|
| 406 |
+
test_connection_btn.click(
|
| 407 |
+
fn=test_custom_connection,
|
| 408 |
+
inputs=custom_server_url,
|
| 409 |
+
outputs=test_result
|
| 410 |
+
)
|
| 411 |
+
|
| 412 |
+
# Auto-connect to default server on startup
|
| 413 |
+
def auto_connect():
|
| 414 |
+
"""Auto-connect to default server"""
|
| 415 |
+
# Ensure default_server_url is valid after changes to SERVER_OPTIONS
|
| 416 |
+
default_server_key = "MCP Tools Collection" # Or another default if this is removed
|
| 417 |
+
if default_server_key in SERVER_OPTIONS:
|
| 418 |
+
url_to_connect = SERVER_OPTIONS[default_server_key]
|
| 419 |
+
success, message = simple_client.connect_to_server(url_to_connect)
|
| 420 |
+
print(f"Auto-connect to '{default_server_key}': {message}")
|
| 421 |
+
elif SERVER_OPTIONS: # Connect to the first available if default is gone
|
| 422 |
+
first_server_key = list(SERVER_OPTIONS.keys())[0]
|
| 423 |
+
if first_server_key != "Custom":
|
| 424 |
+
url_to_connect = SERVER_OPTIONS[first_server_key]
|
| 425 |
+
success, message = simple_client.connect_to_server(url_to_connect)
|
| 426 |
+
print(f"Auto-connect to '{first_server_key}': {message}")
|
| 427 |
+
else:
|
| 428 |
+
print("Auto-connect: No default server available to connect.")
|
| 429 |
+
else:
|
| 430 |
+
print("Auto-connect: No servers configured.")
|
| 431 |
+
|
| 432 |
+
|
| 433 |
+
# Launch configuration
|
| 434 |
+
if __name__ == "__main__":
|
| 435 |
+
print("π Starting Simple MCP Client...")
|
| 436 |
+
print(f"π smolagents available: {HAS_SMOLAGENTS}")
|
| 437 |
+
|
| 438 |
+
# Update HAS_SMOLAGENTS in Help Tab dynamically
|
| 439 |
+
help_tab_index = -1
|
| 440 |
+
for i, tab_item in enumerate(demo.children):
|
| 441 |
+
if isinstance(tab_item, gr.layouts.Tabs):
|
| 442 |
+
for j, tab_child in enumerate(tab_item.children):
|
| 443 |
+
if isinstance(tab_child, gr.layouts.Tab) and tab_child.label == "βΉοΈ Help & About":
|
| 444 |
+
help_tab_index = (i,j)
|
| 445 |
+
break
|
| 446 |
+
if help_tab_index != -1:
|
| 447 |
+
break
|
| 448 |
+
|
| 449 |
+
if help_tab_index != -1:
|
| 450 |
+
# This is a bit hacky as direct modification of Markdown content post-render is complex.
|
| 451 |
+
# The markdown content is static here. We'll update the string before demo.launch()
|
| 452 |
+
# The markdown string itself was already modified above.
|
| 453 |
+
# For dynamic updates in Gradio, one would typically use gr.Markdown().update(value="new markdown")
|
| 454 |
+
# but this is for initial setup.
|
| 455 |
+
|
| 456 |
+
# The string for Markdown is already updated.
|
| 457 |
+
# The line below was an attempt to dynamically update, but it's not needed as the string is changed prior to launch.
|
| 458 |
+
# demo.children[help_tab_index[0]].children[help_tab_index[1]].children[0].value = demo.children[help_tab_index[0]].children[help_tab_index[1]].children[0].value.replace(
|
| 459 |
+
# "smolagents Available: 'β
Yes'", f"smolagents Available: {'β
Yes' if HAS_SMOLAGENTS else 'β No'}"
|
| 460 |
+
# )
|
| 461 |
+
# Instead, we ensure the markdown string itself reflects HAS_SMOLAGENTS if it were dynamic.
|
| 462 |
+
# For this request, the static string modification is sufficient.
|
| 463 |
+
pass
|
| 464 |
+
|
| 465 |
+
|
| 466 |
+
print("π Auto-connecting to a server...")
|
| 467 |
+
auto_connect()
|
| 468 |
+
|
| 469 |
+
# Determine if running on HF Spaces
|
| 470 |
+
is_hf_space = os.getenv("SPACE_ID") is not None
|
| 471 |
+
|
| 472 |
+
if is_hf_space:
|
| 473 |
+
print("π Running on Hugging Face Spaces")
|
| 474 |
+
demo.launch()
|
| 475 |
+
else:
|
| 476 |
+
print("π Web UI: http://localhost:7860")
|
| 477 |
+
demo.launch(
|
| 478 |
+
server_name="127.0.0.1",
|
| 479 |
+
server_port=7860,
|
| 480 |
+
show_error=True,
|
| 481 |
+
inbrowser=True,
|
| 482 |
+
debug=True
|
| 483 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.44.0
|
| 2 |
+
smolagents>=0.3.0
|
| 3 |
+
smolagents[mcp]>=0.3.0
|
| 4 |
+
mcp>=0.3.0
|