Spaces:
Running
Running
Delete frontend.py
Browse files- frontend.py +0 -216
frontend.py
DELETED
@@ -1,216 +0,0 @@
|
|
1 |
-
# frontend.py
|
2 |
-
"""
|
3 |
-
Streamlit frontend for MCP Agent
|
4 |
-
Provides a chat interface for interacting with the MCP backend
|
5 |
-
"""
|
6 |
-
|
7 |
-
import streamlit as st
|
8 |
-
import asyncio
|
9 |
-
from backend import get_agent, MCPAgent
|
10 |
-
import time
|
11 |
-
|
12 |
-
# Page configuration
|
13 |
-
st.set_page_config(
|
14 |
-
page_title="MCP Agent Chat",
|
15 |
-
page_icon="🤖",
|
16 |
-
layout="wide",
|
17 |
-
initial_sidebar_state="expanded"
|
18 |
-
)
|
19 |
-
|
20 |
-
# Custom CSS for better chat appearance
|
21 |
-
st.markdown("""
|
22 |
-
<style>
|
23 |
-
.stChatMessage {
|
24 |
-
padding: 1rem;
|
25 |
-
border-radius: 0.5rem;
|
26 |
-
margin-bottom: 1rem;
|
27 |
-
}
|
28 |
-
.user-message {
|
29 |
-
background-color: #e3f2fd;
|
30 |
-
}
|
31 |
-
.assistant-message {
|
32 |
-
background-color: #f5f5f5;
|
33 |
-
}
|
34 |
-
.sidebar-info {
|
35 |
-
padding: 1rem;
|
36 |
-
background-color: #f0f2f6;
|
37 |
-
border-radius: 0.5rem;
|
38 |
-
margin-bottom: 1rem;
|
39 |
-
}
|
40 |
-
</style>
|
41 |
-
""", unsafe_allow_html=True)
|
42 |
-
|
43 |
-
# Initialize session state
|
44 |
-
if "messages" not in st.session_state:
|
45 |
-
st.session_state.messages = []
|
46 |
-
st.session_state.history = [] # For agent history
|
47 |
-
st.session_state.agent_initialized = False
|
48 |
-
st.session_state.available_tools = []
|
49 |
-
st.session_state.pending_query = None # For example queries
|
50 |
-
|
51 |
-
# Sidebar
|
52 |
-
with st.sidebar:
|
53 |
-
st.title("🤖 MCP Agent")
|
54 |
-
st.markdown("---")
|
55 |
-
|
56 |
-
# Status indicator
|
57 |
-
if st.session_state.agent_initialized:
|
58 |
-
st.success("✅ Agent Connected")
|
59 |
-
|
60 |
-
# Show available tools
|
61 |
-
st.markdown("### 🛠️ Available Tools")
|
62 |
-
for tool in st.session_state.available_tools:
|
63 |
-
st.markdown(f"• `{tool}`")
|
64 |
-
else:
|
65 |
-
st.info("⏳ Agent Initializing...")
|
66 |
-
|
67 |
-
st.markdown("---")
|
68 |
-
|
69 |
-
# Example queries
|
70 |
-
st.markdown("### 💡 Example Queries")
|
71 |
-
example_queries = [
|
72 |
-
"What's the price of AAPL?",
|
73 |
-
"Show me the market summary",
|
74 |
-
"Get news about TSLA",
|
75 |
-
"Calculate 25 * 4",
|
76 |
-
"What's 100 divided by 7?",
|
77 |
-
"Add 456 and 789"
|
78 |
-
]
|
79 |
-
|
80 |
-
for query in example_queries:
|
81 |
-
if st.button(query, key=f"example_{query}"):
|
82 |
-
st.session_state.pending_query = query
|
83 |
-
st.rerun()
|
84 |
-
|
85 |
-
st.markdown("---")
|
86 |
-
|
87 |
-
# Clear chat button
|
88 |
-
if st.button("🗑️ Clear Chat", type="secondary"):
|
89 |
-
st.session_state.messages = []
|
90 |
-
st.session_state.history = []
|
91 |
-
st.rerun()
|
92 |
-
|
93 |
-
# Info section
|
94 |
-
st.markdown("### ℹ️ About")
|
95 |
-
st.markdown("""
|
96 |
-
This chat interface connects to:
|
97 |
-
- **Math Server**: Basic arithmetic operations
|
98 |
-
- **Stock Server**: Real-time market data
|
99 |
-
|
100 |
-
The agent uses LangChain and MCP to intelligently route your queries to the appropriate tools.
|
101 |
-
""")
|
102 |
-
|
103 |
-
# Main chat interface
|
104 |
-
st.title("💬 MCP Agent Chat")
|
105 |
-
st.markdown("Ask me about stocks, math calculations, or general questions!")
|
106 |
-
|
107 |
-
# Initialize agent asynchronously
|
108 |
-
async def initialize_agent():
|
109 |
-
"""Initialize the agent if not already done"""
|
110 |
-
if not st.session_state.agent_initialized:
|
111 |
-
agent = get_agent()
|
112 |
-
with st.spinner("🔧 Initializing MCP servers..."):
|
113 |
-
try:
|
114 |
-
tools = await agent.initialize()
|
115 |
-
st.session_state.available_tools = tools
|
116 |
-
st.session_state.agent_initialized = True
|
117 |
-
return True
|
118 |
-
except Exception as e:
|
119 |
-
st.error(f"Failed to initialize agent: {str(e)}")
|
120 |
-
st.info("Please make sure the stock server is running: `python stockserver.py`")
|
121 |
-
return False
|
122 |
-
return True
|
123 |
-
|
124 |
-
# Process user message
|
125 |
-
async def process_user_message(user_input: str):
|
126 |
-
"""Process the user's message and get response from agent"""
|
127 |
-
agent = get_agent()
|
128 |
-
|
129 |
-
# Add user message to history
|
130 |
-
st.session_state.history.append({"role": "user", "content": user_input})
|
131 |
-
|
132 |
-
try:
|
133 |
-
# Get response from agent
|
134 |
-
response = await agent.process_message(user_input, st.session_state.history)
|
135 |
-
|
136 |
-
# Add assistant response to history
|
137 |
-
st.session_state.history.append({"role": "assistant", "content": response})
|
138 |
-
|
139 |
-
return response
|
140 |
-
except Exception as e:
|
141 |
-
return f"Error: {str(e)}"
|
142 |
-
|
143 |
-
# Display chat messages
|
144 |
-
for message in st.session_state.messages:
|
145 |
-
with st.chat_message(message["role"]):
|
146 |
-
st.markdown(message["content"])
|
147 |
-
|
148 |
-
# Process pending query from example buttons
|
149 |
-
if st.session_state.pending_query:
|
150 |
-
query = st.session_state.pending_query
|
151 |
-
st.session_state.pending_query = None # Clear it
|
152 |
-
|
153 |
-
# Add to messages
|
154 |
-
st.session_state.messages.append({"role": "user", "content": query})
|
155 |
-
|
156 |
-
# Process the query
|
157 |
-
async def process_example():
|
158 |
-
if not st.session_state.agent_initialized:
|
159 |
-
if not await initialize_agent():
|
160 |
-
return "Failed to initialize agent. Please check the servers."
|
161 |
-
return await process_user_message(query)
|
162 |
-
|
163 |
-
# Get response
|
164 |
-
with st.spinner("Processing..."):
|
165 |
-
response = asyncio.run(process_example())
|
166 |
-
|
167 |
-
# Add response to messages
|
168 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
169 |
-
|
170 |
-
# Rerun to display the new messages
|
171 |
-
st.rerun()
|
172 |
-
|
173 |
-
# Chat input
|
174 |
-
if prompt := st.chat_input("Type your message here..."):
|
175 |
-
# Add user message to chat
|
176 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
177 |
-
|
178 |
-
# Display user message
|
179 |
-
with st.chat_message("user"):
|
180 |
-
st.markdown(prompt)
|
181 |
-
|
182 |
-
# Get and display assistant response
|
183 |
-
with st.chat_message("assistant"):
|
184 |
-
message_placeholder = st.empty()
|
185 |
-
|
186 |
-
# Run async function
|
187 |
-
async def get_response():
|
188 |
-
# Initialize agent if needed
|
189 |
-
if not st.session_state.agent_initialized:
|
190 |
-
if not await initialize_agent():
|
191 |
-
return "Failed to initialize agent. Please check the servers."
|
192 |
-
|
193 |
-
# Process message
|
194 |
-
return await process_user_message(prompt)
|
195 |
-
|
196 |
-
# Execute async function
|
197 |
-
with st.spinner("Thinking..."):
|
198 |
-
response = asyncio.run(get_response())
|
199 |
-
|
200 |
-
message_placeholder.markdown(response)
|
201 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
202 |
-
|
203 |
-
# Initialize agent on first load
|
204 |
-
if not st.session_state.agent_initialized:
|
205 |
-
asyncio.run(initialize_agent())
|
206 |
-
|
207 |
-
# Footer
|
208 |
-
st.markdown("---")
|
209 |
-
st.markdown(
|
210 |
-
"""
|
211 |
-
<div style='text-align: center; color: #666;'>
|
212 |
-
Powered by LangChain, MCP, and Hugging Face 🤗
|
213 |
-
</div>
|
214 |
-
""",
|
215 |
-
unsafe_allow_html=True
|
216 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|