|
| 1 | +# https://github.com/modelcontextprotocol/servers/tree/main/src/filesystem |
| 2 | + |
| 3 | +import streamlit as st |
| 4 | +import asyncio |
| 5 | +import os |
| 6 | +import shutil |
| 7 | +import tempfile |
| 8 | + |
| 9 | +from agents import Agent, Runner, gen_trace_id, trace |
| 10 | +from agents.mcp import MCPServer, MCPServerStdio |
| 11 | + |
| 12 | + |
| 13 | +# Create a sample file for demonstration if needed |
| 14 | +def ensure_sample_files(): |
| 15 | + current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 16 | + samples_dir = os.path.join(current_dir, "sample_files") |
| 17 | + |
| 18 | + # Create the directory if it doesn't exist |
| 19 | + os.makedirs(samples_dir, exist_ok=True) |
| 20 | + |
| 21 | + # Create a sample WWDC predictions file |
| 22 | + predictions_file = os.path.join(samples_dir, "wwdc25_predictions.md") |
| 23 | + if not os.path.exists(predictions_file): |
| 24 | + with open(predictions_file, "w") as f: |
| 25 | + f.write("# WWDC25 Predictions\n\n") |
| 26 | + f.write("1. Apple Intelligence features for iPad\n") |
| 27 | + f.write("2. New Apple Watch with health sensors\n") |
| 28 | + f.write("3. Vision Pro 2 announcement\n") |
| 29 | + f.write("4. iOS 18 with advanced customization\n") |
| 30 | + f.write("5. macOS 15 with AI features\n") |
| 31 | + |
| 32 | + # Create a sample WWDC activities file |
| 33 | + activities_file = os.path.join(samples_dir, "wwdc_activities.txt") |
| 34 | + if not os.path.exists(activities_file): |
| 35 | + with open(activities_file, "w") as f: |
| 36 | + f.write("My favorite WWDC activities:\n\n") |
| 37 | + f.write("1. Attending sessions\n") |
| 38 | + f.write("2. Labs with Apple engineers\n") |
| 39 | + f.write("3. Networking events\n") |
| 40 | + f.write("4. Exploring new APIs\n") |
| 41 | + f.write("5. Hands-on demos\n") |
| 42 | + |
| 43 | + return samples_dir |
| 44 | + |
| 45 | + |
| 46 | +# Using a separate event loop to run async code in Streamlit |
| 47 | +class AsyncRunner: |
| 48 | + @staticmethod |
| 49 | + def run_async(func, *args, **kwargs): |
| 50 | + loop = asyncio.new_event_loop() |
| 51 | + asyncio.set_event_loop(loop) |
| 52 | + try: |
| 53 | + return loop.run_until_complete(func(*args, **kwargs)) |
| 54 | + finally: |
| 55 | + loop.close() |
| 56 | + |
| 57 | + |
| 58 | +# Function to run a query with error handling |
| 59 | +def run_agent_query(query): |
| 60 | + try: |
| 61 | + # Create a fresh server and agent for each query |
| 62 | + samples_dir = ensure_sample_files() |
| 63 | + |
| 64 | + async def run_query(): |
| 65 | + server = None |
| 66 | + try: |
| 67 | + server = MCPServerStdio( |
| 68 | + name="Filesystem Server, via npx", |
| 69 | + params={ |
| 70 | + "command": "npx", |
| 71 | + "args": ["-y", "@modelcontextprotocol/server-filesystem", samples_dir], |
| 72 | + }, |
| 73 | + ) |
| 74 | + |
| 75 | + # Enter the server context |
| 76 | + mcp_server = await server.__aenter__() |
| 77 | + |
| 78 | + agent = Agent( |
| 79 | + name="Assistant for Content in Files", |
| 80 | + instructions="Use the tools to read the filesystem and answer questions based on those files.", |
| 81 | + mcp_servers=[mcp_server], |
| 82 | + ) |
| 83 | + |
| 84 | + trace_id = gen_trace_id() |
| 85 | + with trace(workflow_name="MCP Filesystem Query", trace_id=trace_id): |
| 86 | + result = await Runner.run(starting_agent=agent, input=query) |
| 87 | + return result.final_output, trace_id |
| 88 | + finally: |
| 89 | + # Make sure to properly exit the server context |
| 90 | + if server: |
| 91 | + await server.__aexit__(None, None, None) |
| 92 | + |
| 93 | + return AsyncRunner.run_async(run_query) |
| 94 | + except Exception as e: |
| 95 | + st.error(f"Error processing query: {str(e)}") |
| 96 | + return f"Failed to process query: {str(e)}", None |
| 97 | + |
| 98 | + |
| 99 | +def main(): |
| 100 | + st.title("File Explorer Assistant") |
| 101 | + st.write("This app uses an AI agent to read files and answer questions about them.") |
| 102 | + |
| 103 | + # Ensure sample files exist |
| 104 | + ensure_sample_files() |
| 105 | + |
| 106 | + # Input area for user queries |
| 107 | + query = st.text_area("Ask me about the files:", height=100) |
| 108 | + |
| 109 | + if st.button("Submit"): |
| 110 | + if query: |
| 111 | + with st.spinner("Processing your request..."): |
| 112 | + result, trace_id = run_agent_query(query) |
| 113 | + |
| 114 | + if trace_id: |
| 115 | + st.write("### Response:") |
| 116 | + st.write(result) |
| 117 | + |
| 118 | + trace_url = f"https://platform.openai.com/traces/trace?trace_id={trace_id}" |
| 119 | + st.write(f"[View trace]({trace_url})") |
| 120 | + |
| 121 | + # Sample queries |
| 122 | + st.sidebar.header("Sample Queries") |
| 123 | + if st.sidebar.button("List all files"): |
| 124 | + with st.spinner("Processing..."): |
| 125 | + result, trace_id = run_agent_query("Read the files and list them.") |
| 126 | + if trace_id: |
| 127 | + st.write("### Files in the system:") |
| 128 | + st.write(result) |
| 129 | + |
| 130 | + if st.sidebar.button("WWDC Activities"): |
| 131 | + with st.spinner("Processing..."): |
| 132 | + result, trace_id = run_agent_query("What are my favorite WWDC activities?") |
| 133 | + if trace_id: |
| 134 | + st.write("### WWDC Activities:") |
| 135 | + st.write(result) |
| 136 | + |
| 137 | + if st.sidebar.button("WWDC25 Predictions"): |
| 138 | + with st.spinner("Processing..."): |
| 139 | + result, trace_id = run_agent_query("Look at my wwdc25 predictions. List the predictions that are most likely to be true.") |
| 140 | + if trace_id: |
| 141 | + st.write("### WWDC25 Predictions Analysis:") |
| 142 | + st.write(result) |
| 143 | + |
| 144 | + |
| 145 | +if __name__ == "__main__": |
| 146 | + # Let's make sure the user has npx installed |
| 147 | + if not shutil.which("npx"): |
| 148 | + st.error("npx is not installed. Please install it with `npm install -g npx`.") |
| 149 | + else: |
| 150 | + main() |
0 commit comments