Spaces:
Running
Running
Update frontend.py
Browse files- frontend.py +16 -42
frontend.py
CHANGED
|
@@ -1,15 +1,9 @@
|
|
| 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="π€",
|
|
@@ -17,7 +11,6 @@ st.set_page_config(
|
|
| 17 |
initial_sidebar_state="expanded"
|
| 18 |
)
|
| 19 |
|
| 20 |
-
# Custom CSS for better chat appearance
|
| 21 |
st.markdown("""
|
| 22 |
<style>
|
| 23 |
.stChatMessage {
|
|
@@ -50,14 +43,20 @@ if "messages" not in st.session_state:
|
|
| 50 |
|
| 51 |
# Sidebar
|
| 52 |
with st.sidebar:
|
| 53 |
-
st.title("π€ MCP
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
available_tools = st.session_state.get("available_tools", [])
|
| 63 |
for tool in available_tools:
|
|
@@ -67,7 +66,6 @@ with st.sidebar:
|
|
| 67 |
|
| 68 |
st.markdown("---")
|
| 69 |
|
| 70 |
-
# Example queries
|
| 71 |
st.markdown("### π‘ Example Queries")
|
| 72 |
example_queries = [
|
| 73 |
"What's the price of AAPL?",
|
|
@@ -85,21 +83,12 @@ with st.sidebar:
|
|
| 85 |
|
| 86 |
st.markdown("---")
|
| 87 |
|
| 88 |
-
# Clear chat button
|
| 89 |
if st.button("ποΈ Clear Chat", type="secondary"):
|
| 90 |
st.session_state.messages = []
|
| 91 |
st.session_state.history = []
|
| 92 |
st.rerun()
|
| 93 |
|
| 94 |
-
# Info section
|
| 95 |
-
st.markdown("### βΉοΈ About")
|
| 96 |
-
st.markdown("""
|
| 97 |
-
This chat interface connects to:
|
| 98 |
-
- **Math Server**: Basic arithmetic operations
|
| 99 |
-
- **Stock Server**: Real-time market data
|
| 100 |
|
| 101 |
-
The agent uses LangChain and MCP to intelligently route your queries to the appropriate tools.
|
| 102 |
-
""")
|
| 103 |
|
| 104 |
# Main chat interface
|
| 105 |
st.title("π¬ MCP Agent Chat")
|
|
@@ -123,96 +112,81 @@ async def initialize_agent():
|
|
| 123 |
return False
|
| 124 |
return True
|
| 125 |
|
| 126 |
-
# Process user message
|
| 127 |
async def process_user_message(user_input: str):
|
| 128 |
"""Process the user's message and get response from agent"""
|
| 129 |
agent = get_agent()
|
| 130 |
|
| 131 |
-
|
| 132 |
st.session_state.history.append({"role": "user", "content": user_input})
|
| 133 |
|
| 134 |
try:
|
| 135 |
-
|
| 136 |
response = await agent.process_message(user_input, st.session_state.history)
|
| 137 |
|
| 138 |
-
|
| 139 |
st.session_state.history.append({"role": "assistant", "content": response})
|
| 140 |
|
| 141 |
return response
|
| 142 |
except Exception as e:
|
| 143 |
return f"Error: {str(e)}"
|
| 144 |
|
| 145 |
-
|
| 146 |
for message in st.session_state.messages:
|
| 147 |
with st.chat_message(message["role"]):
|
| 148 |
st.markdown(message["content"])
|
| 149 |
|
| 150 |
-
|
| 151 |
if st.session_state.pending_query:
|
| 152 |
query = st.session_state.pending_query
|
| 153 |
st.session_state.pending_query = None # Clear it
|
| 154 |
|
| 155 |
-
# Add to messages
|
| 156 |
st.session_state.messages.append({"role": "user", "content": query})
|
| 157 |
|
| 158 |
-
# Process the query
|
| 159 |
async def process_example():
|
| 160 |
if not st.session_state.agent_initialized:
|
| 161 |
if not await initialize_agent():
|
| 162 |
return "Failed to initialize agent. Please check the servers."
|
| 163 |
return await process_user_message(query)
|
| 164 |
|
| 165 |
-
# Get response
|
| 166 |
with st.spinner("Processing..."):
|
| 167 |
response = asyncio.run(process_example())
|
| 168 |
|
| 169 |
-
# Add response to messages
|
| 170 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 171 |
|
| 172 |
-
# Rerun to display the new messages
|
| 173 |
st.rerun()
|
| 174 |
|
| 175 |
-
# Chat input
|
| 176 |
if prompt := st.chat_input("Type your message here..."):
|
| 177 |
-
# Add user message to chat
|
| 178 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 179 |
|
| 180 |
-
# Display user message
|
| 181 |
with st.chat_message("user"):
|
| 182 |
st.markdown(prompt)
|
| 183 |
|
| 184 |
-
# Get and display assistant response
|
| 185 |
with st.chat_message("assistant"):
|
| 186 |
message_placeholder = st.empty()
|
| 187 |
|
| 188 |
-
# Run async function
|
| 189 |
async def get_response():
|
| 190 |
-
# Initialize agent if needed
|
| 191 |
if not st.session_state.agent_initialized:
|
| 192 |
if not await initialize_agent():
|
| 193 |
return "Failed to initialize agent. Please check the servers."
|
| 194 |
|
| 195 |
-
# Process message
|
| 196 |
return await process_user_message(prompt)
|
| 197 |
|
| 198 |
-
# Execute async function
|
| 199 |
with st.spinner("Thinking..."):
|
| 200 |
response = asyncio.run(get_response())
|
| 201 |
|
| 202 |
message_placeholder.markdown(response)
|
| 203 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 204 |
|
| 205 |
-
# Auto-initialize agent on first load
|
| 206 |
if not st.session_state.agent_initialized:
|
| 207 |
with st.spinner("π§ Initializing agent..."):
|
| 208 |
asyncio.run(initialize_agent())
|
| 209 |
|
| 210 |
-
# Footer
|
| 211 |
st.markdown("---")
|
| 212 |
st.markdown(
|
| 213 |
"""
|
| 214 |
<div style='text-align: center; color: #666;'>
|
| 215 |
-
Powered by LangChain, MCP, and Hugging Face
|
|
|
|
| 216 |
</div>
|
| 217 |
""",
|
| 218 |
unsafe_allow_html=True
|
|
|
|
| 1 |
# frontend.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import asyncio
|
| 4 |
from backend import get_agent, MCPAgent
|
| 5 |
import time
|
| 6 |
|
|
|
|
| 7 |
st.set_page_config(
|
| 8 |
page_title="MCP Agent Chat",
|
| 9 |
page_icon="π€",
|
|
|
|
| 11 |
initial_sidebar_state="expanded"
|
| 12 |
)
|
| 13 |
|
|
|
|
| 14 |
st.markdown("""
|
| 15 |
<style>
|
| 16 |
.stChatMessage {
|
|
|
|
| 43 |
|
| 44 |
# Sidebar
|
| 45 |
with st.sidebar:
|
| 46 |
+
st.title("π€ MCP AI Algo Trade App Demo")
|
| 47 |
+
st.markdown("### βΉοΈ About")
|
| 48 |
+
st.markdown("""
|
| 49 |
+
This chat interface connects to:
|
| 50 |
+
- **Math Server**: Basic arithmetic operations
|
| 51 |
+
- **Stock Server**: Mock Real-time market data
|
| 52 |
+
|
| 53 |
+
The agent uses LangChain and MCP to intelligently route your queries to the appropriate tools.
|
| 54 |
+
""")
|
| 55 |
st.markdown("---")
|
| 56 |
|
|
|
|
| 57 |
if st.session_state.agent_initialized:
|
| 58 |
st.success("β
Agent Connected")
|
| 59 |
|
|
|
|
| 60 |
st.markdown("### π οΈ Available Tools")
|
| 61 |
available_tools = st.session_state.get("available_tools", [])
|
| 62 |
for tool in available_tools:
|
|
|
|
| 66 |
|
| 67 |
st.markdown("---")
|
| 68 |
|
|
|
|
| 69 |
st.markdown("### π‘ Example Queries")
|
| 70 |
example_queries = [
|
| 71 |
"What's the price of AAPL?",
|
|
|
|
| 83 |
|
| 84 |
st.markdown("---")
|
| 85 |
|
|
|
|
| 86 |
if st.button("ποΈ Clear Chat", type="secondary"):
|
| 87 |
st.session_state.messages = []
|
| 88 |
st.session_state.history = []
|
| 89 |
st.rerun()
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
|
|
|
|
|
|
| 92 |
|
| 93 |
# Main chat interface
|
| 94 |
st.title("π¬ MCP Agent Chat")
|
|
|
|
| 112 |
return False
|
| 113 |
return True
|
| 114 |
|
|
|
|
| 115 |
async def process_user_message(user_input: str):
|
| 116 |
"""Process the user's message and get response from agent"""
|
| 117 |
agent = get_agent()
|
| 118 |
|
| 119 |
+
|
| 120 |
st.session_state.history.append({"role": "user", "content": user_input})
|
| 121 |
|
| 122 |
try:
|
| 123 |
+
|
| 124 |
response = await agent.process_message(user_input, st.session_state.history)
|
| 125 |
|
| 126 |
+
|
| 127 |
st.session_state.history.append({"role": "assistant", "content": response})
|
| 128 |
|
| 129 |
return response
|
| 130 |
except Exception as e:
|
| 131 |
return f"Error: {str(e)}"
|
| 132 |
|
| 133 |
+
|
| 134 |
for message in st.session_state.messages:
|
| 135 |
with st.chat_message(message["role"]):
|
| 136 |
st.markdown(message["content"])
|
| 137 |
|
| 138 |
+
|
| 139 |
if st.session_state.pending_query:
|
| 140 |
query = st.session_state.pending_query
|
| 141 |
st.session_state.pending_query = None # Clear it
|
| 142 |
|
|
|
|
| 143 |
st.session_state.messages.append({"role": "user", "content": query})
|
| 144 |
|
|
|
|
| 145 |
async def process_example():
|
| 146 |
if not st.session_state.agent_initialized:
|
| 147 |
if not await initialize_agent():
|
| 148 |
return "Failed to initialize agent. Please check the servers."
|
| 149 |
return await process_user_message(query)
|
| 150 |
|
|
|
|
| 151 |
with st.spinner("Processing..."):
|
| 152 |
response = asyncio.run(process_example())
|
| 153 |
|
|
|
|
| 154 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 155 |
|
|
|
|
| 156 |
st.rerun()
|
| 157 |
|
|
|
|
| 158 |
if prompt := st.chat_input("Type your message here..."):
|
|
|
|
| 159 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 160 |
|
|
|
|
| 161 |
with st.chat_message("user"):
|
| 162 |
st.markdown(prompt)
|
| 163 |
|
|
|
|
| 164 |
with st.chat_message("assistant"):
|
| 165 |
message_placeholder = st.empty()
|
| 166 |
|
|
|
|
| 167 |
async def get_response():
|
|
|
|
| 168 |
if not st.session_state.agent_initialized:
|
| 169 |
if not await initialize_agent():
|
| 170 |
return "Failed to initialize agent. Please check the servers."
|
| 171 |
|
|
|
|
| 172 |
return await process_user_message(prompt)
|
| 173 |
|
|
|
|
| 174 |
with st.spinner("Thinking..."):
|
| 175 |
response = asyncio.run(get_response())
|
| 176 |
|
| 177 |
message_placeholder.markdown(response)
|
| 178 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 179 |
|
|
|
|
| 180 |
if not st.session_state.agent_initialized:
|
| 181 |
with st.spinner("π§ Initializing agent..."):
|
| 182 |
asyncio.run(initialize_agent())
|
| 183 |
|
|
|
|
| 184 |
st.markdown("---")
|
| 185 |
st.markdown(
|
| 186 |
"""
|
| 187 |
<div style='text-align: center; color: #666;'>
|
| 188 |
+
Powered by LangGraph, LangChain, MCP, and Hugging Face
|
| 189 |
+
Developed by Lorentz Yeung
|
| 190 |
</div>
|
| 191 |
""",
|
| 192 |
unsafe_allow_html=True
|