mcp-client / client_app.py
Adamxboyle's picture
Initial commit
871943f
Raw History Blame Contribute Delete
1.42 kB
import gradio as gr
import os
from mcp import StdioServerParameters
from smolagents import InferenceClientModel, CodeAgent, ToolCollection, MCPClient
# This will hold the client connection
mcp_client = None
try:
# Connect to the MCP Server running locally
print("Connecting to local MCP server...")
mcp_client = MCPClient(
{"url": "https://Adamxboyle-mcp-sentiment.hf.space/gradio_api/mcp/sse"}
)
tools = mcp_client.get_tools()
print("Successfully connected and got tools.")
# Set up the agent's "brain" using your API token
model = InferenceClientModel(token=os.getenv("HUGGINGFACE_API_TOKEN"))
agent = CodeAgent(tools=[*tools], model=model)
print("Agent created.")
# Create the Gradio Chat UI
demo = gr.ChatInterface(
fn=lambda message, history: str(agent.run(message)),
type="messages",
examples=["What is the sentiment of the text 'I love sunny days'?"],
title="Agent with MCP Tools",
description="This agent can use our sentiment analysis tool to answer questions.",
)
demo.launch()
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure your mcp-sentiment server is running in a separate terminal.")
finally:
# This makes sure the connection is closed when you stop the app
if mcp_client:
print("Disconnecting MCP client.")
mcp_client.disconnect()