Spaces:
Runtime error
Runtime error
Commit ·
471d861
1
Parent(s): 8929bec
Improve call to action when disconnected to the mcp server. Try closing the client when terminating the app
Browse files- mcp_remote_client.py +60 -51
mcp_remote_client.py
CHANGED
|
@@ -142,6 +142,7 @@ class MCPClientWrapper:
|
|
| 142 |
|
| 143 |
async def disconnect(self):
|
| 144 |
if self.exit_stack:
|
|
|
|
| 145 |
await self.exit_stack.aclose()
|
| 146 |
self.exit_stack = None
|
| 147 |
self.session = None
|
|
@@ -154,7 +155,7 @@ class MCPClientWrapper:
|
|
| 154 |
{"role": "user", "content": message},
|
| 155 |
{
|
| 156 |
"role": "assistant",
|
| 157 |
-
"content": "Please connect to an MCP server first.",
|
| 158 |
},
|
| 159 |
]
|
| 160 |
yield messages, gr.Textbox(value="")
|
|
@@ -423,60 +424,68 @@ def gradio_interface(
|
|
| 423 |
|
| 424 |
# Disable auto-dark mode by setting theme to None
|
| 425 |
with gr.Blocks(title="WDI MCP Client", css=custom_css, theme=None) as demo:
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
with gr.
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
with gr.Row(equal_height=True):
|
| 458 |
-
msg = gr.Textbox(
|
| 459 |
-
label=None,
|
| 460 |
-
placeholder="Ask about what indicators are available for a specific topic (e.g., What's the definition of GDP?)",
|
| 461 |
-
scale=4,
|
| 462 |
-
show_label=False,
|
| 463 |
)
|
| 464 |
-
# clear_btn = gr.Button("Clear Chat", scale=1)
|
| 465 |
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 471 |
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 480 |
# demo.unload(client.disconnect)
|
| 481 |
|
| 482 |
return demo
|
|
|
|
| 142 |
|
| 143 |
async def disconnect(self):
|
| 144 |
if self.exit_stack:
|
| 145 |
+
print("Disconnecting from MCP server...")
|
| 146 |
await self.exit_stack.aclose()
|
| 147 |
self.exit_stack = None
|
| 148 |
self.session = None
|
|
|
|
| 155 |
{"role": "user", "content": message},
|
| 156 |
{
|
| 157 |
"role": "assistant",
|
| 158 |
+
"content": "Please connect to an MCP server first by reloading the page.",
|
| 159 |
},
|
| 160 |
]
|
| 161 |
yield messages, gr.Textbox(value="")
|
|
|
|
| 424 |
|
| 425 |
# Disable auto-dark mode by setting theme to None
|
| 426 |
with gr.Blocks(title="WDI MCP Client", css=custom_css, theme=None) as demo:
|
| 427 |
+
try:
|
| 428 |
+
gr.Markdown("# Development Data Chat")
|
| 429 |
+
# gr.Markdown("Connect to the WDI MCP server and chat with the assistant")
|
| 430 |
+
|
| 431 |
+
with gr.Accordion(
|
| 432 |
+
"Connect to the WDI MCP server and chat with the assistant",
|
| 433 |
+
open=False,
|
| 434 |
+
visible=server_path_or_url.endswith(".py"),
|
| 435 |
+
):
|
| 436 |
+
with gr.Row(equal_height=True):
|
| 437 |
+
with gr.Column(scale=4):
|
| 438 |
+
server_path = gr.Textbox(
|
| 439 |
+
label="Server Script Path",
|
| 440 |
+
placeholder="Enter path to server script (e.g., wdi_mcp_server.py)",
|
| 441 |
+
value=server_path_or_url,
|
| 442 |
+
)
|
| 443 |
+
with gr.Column(scale=1):
|
| 444 |
+
connect_btn = gr.Button("Connect")
|
| 445 |
+
|
| 446 |
+
status = gr.Textbox(label="Connection Status", interactive=False)
|
| 447 |
+
|
| 448 |
+
chatbot = gr.Chatbot(
|
| 449 |
+
value=[],
|
| 450 |
+
height="81vh",
|
| 451 |
+
type="messages",
|
| 452 |
+
show_copy_button=False,
|
| 453 |
+
avatar_images=("img/small-user.png", "img/small-robot.png"),
|
| 454 |
+
autoscroll=True,
|
| 455 |
+
layout="panel",
|
| 456 |
+
placeholder="Ask development data questions!",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
)
|
|
|
|
| 458 |
|
| 459 |
+
with gr.Row(equal_height=True):
|
| 460 |
+
msg = gr.Textbox(
|
| 461 |
+
label=None,
|
| 462 |
+
placeholder="Ask about what indicators are available for a specific topic (e.g., What's the definition of GDP?)",
|
| 463 |
+
scale=4,
|
| 464 |
+
show_label=False,
|
| 465 |
+
)
|
| 466 |
+
# clear_btn = gr.Button("Clear Chat", scale=1)
|
| 467 |
+
|
| 468 |
+
# connect_btn.click(client.connect, inputs=server_path, outputs=status)
|
| 469 |
+
# Automatically call client.connect(...) as soon as the interface loads
|
| 470 |
+
demo.load(
|
| 471 |
+
fn=client.connect,
|
| 472 |
+
inputs=server_path,
|
| 473 |
+
outputs=status,
|
| 474 |
+
show_progress="full",
|
| 475 |
+
)
|
| 476 |
|
| 477 |
+
msg.submit(
|
| 478 |
+
client.process_message,
|
| 479 |
+
[msg, chatbot],
|
| 480 |
+
[chatbot, msg],
|
| 481 |
+
concurrency_limit=10,
|
| 482 |
+
)
|
| 483 |
+
# clear_btn.click(lambda: [], None, chatbot)
|
| 484 |
|
| 485 |
+
except KeyboardInterrupt:
|
| 486 |
+
print("Keyboard interrupt received. Disconnecting from MCP server...")
|
| 487 |
+
asyncio.run(client.disconnect())
|
| 488 |
+
raise KeyboardInterrupt
|
| 489 |
# demo.unload(client.disconnect)
|
| 490 |
|
| 491 |
return demo
|