File size: 14,068 Bytes
e750673
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import gradio as gr
import asyncio
import os
import json
import tiktoken
from typing import List, Dict, Any
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from openai import AsyncOpenAI

# Token encoding
enc = tiktoken.get_encoding("o200k_base")

def count_tokens(text: str) -> int:
    return len(enc.encode(text))

class DemoSession:
    def __init__(self, mode: str):
        self.mode = mode
        self.server_process = None
        self.session = None
        self.client = AsyncOpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
        self.history = []
        
        # Metrics
        self.initial_tokens = 0
        self.runtime_tokens = 0
        self.tool_calls_count = 0
        self.tools_list = []
        
        # Context
        self.exit_stack = None

    async def start(self):
        # Set up server parameters
        server_params = StdioServerParameters(
            command="python3",
            args=["app/server/main.py"],
            env={**os.environ, "MCP_MODE": self.mode}
        )
        
        # We need to manually manage the context manager to keep it alive across chat turns
        # This is tricky in Gradio. 
        # For simplicity in this demo, we might start a new session per message or 
        # use a global/stateful session if possible.
        # However, the best way for Gradio async is to yield from a generator.
        pass

# Since keeping the stdio connection open across Gradio interactions is complex without a custom worker,
# and this is a demo, we will instantiate the agent AND run the full interaction for each prompt 
# if we want to keep it simple. BUT, that resets the session auth in Progressive mode.
# So we MUST keep the session alive.

# Strategy:
# Use a global (or per-user State) object that holds the running session.
# But Gradio's State doesn't support async context managers well.
# We will use a queue-based approach or just launch the session inside the respond function 
# and accept that it's a "one-shot" conversation (history is fed back in, but MCP session resets).
# WAIT. If MCP session resets, the "Authorization" state in the server is lost.
# So "Progressive Disclosure" won't work if we reset the server every turn.
# The Server keeps state in memory.

# We need a persistent server process.
# We can start the server process GLOBALLY (or per user session start) and communicate with it.
# However, for the hackathon demo, maybe a Single-Turn or Multi-Turn within one function call is easier?
# No, a Chat interface implies multi-turn.

# Solution:
# Create a class that runs the server in background and exposes methods.
# We can use `subprocess.Popen` manually and wrap it in an MCP client that reads/writes to stdin/stdout.
# `mcp.client.stdio.stdio_client` is a context manager.
# We can "enter" the context manager and keep it open.

import contextlib

class PersistentAgent:
    def __init__(self, mode, api_key):
        self.mode = mode
        self.api_key = api_key
        self.stack = contextlib.AsyncExitStack()
        self.session = None
        self.tools = None
        self.metrics = {"initial": 0, "runtime": 0}
        self.messages = []
        if not api_key:
            raise ValueError("API Key is required")
        self.openai = AsyncOpenAI(api_key=api_key)

    async def initialize(self):
        server_params = StdioServerParameters(
            command="python3",
            args=["app/server/main.py"],
            env={**os.environ, "MCP_MODE": self.mode, "PYTHONUNBUFFERED": "1"}
        )
        
        # Note: stdio_client is context manager that spawns tasks.
        # We need to be careful about task context in Gradio.
        # For now, we will rely on Gradio's event loop.
        
        self.read, self.write = await self.stack.enter_async_context(stdio_client(server_params))
        self.session = await self.stack.enter_async_context(ClientSession(self.read, self.write))
        await self.session.initialize()
        
        # Initial Load Stats
        tools_result = await self.session.list_tools()
        self.tools = tools_result.tools
        
        # Safe resource listing (might fail in standard mode if not implemented, though we implemented empty list)
        try:
            resources_result = await self.session.list_resources()
            resources_json = json.dumps([r.model_dump() for r in resources_result.resources], indent=2)
        except:
            resources_json = ""
        
        # Calculate Initial Tokens
        tools_json = json.dumps([t.model_dump() for t in self.tools], indent=2)
        system_base = "You are a helpful assistant..."
        
        self.metrics["initial"] = count_tokens(tools_json) + count_tokens(resources_json) + count_tokens(system_base)
        
        # Setup System Prompt
        tool_desc_str = "\n".join([f"  - {t.name}: {t.description}" for t in self.tools])
        self.system_prompt = f"{system_base}\n\nYou have access to these tools:\n\n{tool_desc_str}\n\nUse tools when needed to answer questions."
        
        if self.mode == 'progressive':
             self.system_prompt += """

IMPORTANT - Tool Usage Workflow:
This server uses progressive disclosure for tools. Follow this exact workflow:

1. PICK the right tool based on the descriptions above (they tell you WHAT each tool does)
2. FETCH the full tool description using read_resource with the specific tool name
   Example: read_resource(resource_uri="resource:///tool_descriptions?tools=TOOL_NAME")
3. CALL the tool using the parameters you just learned

DO NOT try to fetch tool_descriptions without specifying which tool you want (?tools=TOOL_NAME).
The tool descriptions above are sufficient for choosing which tool you need.
You fetch the full description to learn the parameters and authorize the tool."""

        self.messages = [{"role": "system", "content": self.system_prompt}]

    async def chat(self, user_message):
        self.messages.append({"role": "user", "content": user_message})
        
        # Yield user message first? No, Gradio handles that.
        # We yield intermediate steps from the agent.
        
        logs = []
        
        while True:
            # Prepare tools for OpenAI
            openai_tools = []
            for t in self.tools:
                # Fix schema for progressive mode: OpenAI requires 'properties' field
                schema = t.inputSchema
                if schema.get("type") == "object" and "properties" not in schema:
                    # Add empty properties for valid OpenAI schema
                    schema = {"type": "object", "properties": {}}
                
                openai_tools.append({
                    "type": "function",
                    "function": {
                        "name": t.name,
                        "description": t.description,
                        "parameters": schema
                    }
                })
            
            if self.mode == 'progressive':
                openai_tools.append({
                    "type": "function",
                    "function": {
                        "name": "read_resource",
                        "description": "Read tool descriptions.",
                        "parameters": {
                            "type": "object",
                            "properties": {"uri": {"type": "string"}},
                            "required": ["uri"]
                        }
                    }
                })

            response = await self.openai.chat.completions.create(
                model="gpt-4o",
                messages=self.messages,
                tools=openai_tools,
                tool_choice="auto"
            )
            
            msg = response.choices[0].message
            self.messages.append(msg)
            
            if msg.content:
                yield msg.content, logs, self.metrics

            if not msg.tool_calls:
                break
                
            for tool_call in msg.tool_calls:
                fn_name = tool_call.function.name
                fn_args = json.loads(tool_call.function.arguments)
                
                log_entry = f"πŸ› οΈ **Tool Call:** `{fn_name}`"
                logs.append(log_entry)
                yield None, logs, self.metrics
                
                result_content = ""
                
                if fn_name == "read_resource":
                    uri = fn_args.get("uri")
                    # Ensure URI is a string (not AnyUrl object)
                    uri_str = str(uri) if uri else ""
                    logs.append(f"πŸ“₯ **Fetching:** `{uri_str}`")
                    yield None, logs, self.metrics
                    
                    try:
                        res = await self.session.read_resource(uri_str)
                        content = res.contents[0].text
                        tokens = count_tokens(content)
                        self.metrics["runtime"] += tokens
                        logs.append(f"πŸ“Š **Loaded:** {tokens} tokens")
                        result_content = content
                    except Exception as e:
                        result_content = json.dumps({"error": str(e)})
                        logs.append(f"❌ **Error:** {e}")

                else:
                    try:
                        res = await self.session.call_tool(fn_name, fn_args)
                        content = res.content[0].text
                        if "TOOL_DESCRIPTION_REQUIRED" in content:
                             logs.append("⚠️ **Auth Error:** Need fetch")
                        else:
                             logs.append("βœ… **Success**")
                        result_content = content
                    except Exception as e:
                         result_content = str(e)
                         logs.append(f"❌ **Error:** {e}")
                
                self.messages.append({
                    "role": "tool",
                    "tool_call_id": tool_call.id,
                    "content": result_content
                })
                yield None, logs, self.metrics

    async def close(self):
        await self.stack.aclose()

# Global vars to hold current session (Warning: Single user demo only)
current_agent = None

async def start_agent(mode, api_key):
    global current_agent
    # We must be careful about closing across tasks.
    # For this demo, if we are in a new task, closing the old one might be tricky.
    # But usually Gradio runs in one loop.
    if current_agent:
        try:
            await current_agent.close()
        except RuntimeError:
            # Ignore "task different" error if we force close
            pass
        except Exception as e:
             print(f"Warning closing old agent: {e}")
    
    try:
        current_agent = PersistentAgent(mode, api_key)
        await current_agent.initialize()
        return f"Started in {mode.upper()} mode.", [], current_agent.metrics
    except Exception as e:
        return f"Error starting agent: {str(e)}", [], {}

async def process_message(message, history):
    if not current_agent:
        yield "Please enter your API key and click Start Server first.", [], {}
        return

    full_response = ""
    async for content, logs, metrics in current_agent.chat(message):
        if content:
            full_response += content
            # Gradio Chat expects (user, bot) tuples list, but we are using return-based chatbot usually?
            # Wait, in Gradio streaming, we yield the accumulated response.
            # But we also want to update the logs sidebar.
            yield full_response, logs, metrics
        else:
            yield full_response, logs, metrics

# UI Construction
with gr.Blocks(title="MCP Progressive Disclosure Demo") as demo:
    gr.Markdown("# MCP Progressive Disclosure Demo πŸš€")
    gr.Markdown("Compare the token usage between Standard (Load All) and Progressive (Lazy Load) MCP servers.")
    
    with gr.Row():
        with gr.Column(scale=1):
            api_key_input = gr.Textbox(
                label="OpenAI API Key", 
                placeholder="sk-...", 
                type="password",
                value=os.environ.get("OPENAI_API_KEY", "")
            )
            mode_radio = gr.Radio(["standard", "progressive"], label="Mode", value="standard")
            start_btn = gr.Button("Start/Restart Server")
            status_output = gr.Markdown("")
            
            metrics_json = gr.JSON(label="Token Metrics")
            logs_box = gr.JSON(label="Activity Logs")
            
        with gr.Column(scale=2):
            # Gradio 6 Chatbot expects messages format by default (no type parameter needed)
            chatbot = gr.Chatbot(label="Conversation")
            msg = gr.Textbox(label="Your Message")
            clear = gr.Button("Clear")
            
            # Wire the clear button
            clear.click(lambda: [], None, chatbot, queue=False)

    # State
    # We need to handle the async flow.
    
    async def on_start(mode, api_key):
        status, _, metrics = await start_agent(mode, api_key)
        return status, metrics, []

    async def on_message(message, history):
        # Gradio 6 expects messages format: [{"role": "user", "content": "..."}, ...]
        history = history or []
        history.append({"role": "user", "content": message})
        history.append({"role": "assistant", "content": ""})
        
        async for content, logs, metrics in process_message(message, history):
             history[-1]["content"] = content
             yield "", history, metrics, logs # Clear textbox
             
    # Event wiring
    start_btn.click(on_start, inputs=[mode_radio, api_key_input], outputs=[status_output, metrics_json, logs_box])
    
    msg.submit(on_message, inputs=[msg, chatbot], outputs=[msg, chatbot, metrics_json, logs_box])

if __name__ == "__main__":
    demo.queue()
    demo.launch(inbrowser=False)