|
| 1 | +import asyncio |
| 2 | +import os |
| 3 | +import shutil |
| 4 | +import subprocess |
| 5 | +import time |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from agents import Agent, Runner, gen_trace_id, trace |
| 9 | +from agents.mcp import MCPServer, MCPServerStreamableHttp |
| 10 | +from agents.model_settings import ModelSettings |
| 11 | + |
| 12 | + |
| 13 | +async def get_instructions_from_prompt(mcp_server: MCPServer, prompt_name: str, **kwargs) -> str: |
| 14 | + """Get agent instructions by calling MCP prompt endpoint (user-controlled)""" |
| 15 | + print(f"Getting instructions from prompt: {prompt_name}") |
| 16 | + |
| 17 | + try: |
| 18 | + prompt_result = await mcp_server.get_prompt(prompt_name, kwargs) |
| 19 | + content = prompt_result.messages[0].content |
| 20 | + if hasattr(content, 'text'): |
| 21 | + instructions = content.text |
| 22 | + else: |
| 23 | + instructions = str(content) |
| 24 | + print("Generated instructions") |
| 25 | + return instructions |
| 26 | + except Exception as e: |
| 27 | + print(f"Failed to get instructions: {e}") |
| 28 | + return f"You are a helpful assistant. Error: {e}" |
| 29 | + |
| 30 | + |
| 31 | +async def demo_code_review(mcp_server: MCPServer): |
| 32 | + """Demo: Code review with user-selected prompt""" |
| 33 | + print("=== CODE REVIEW DEMO ===") |
| 34 | + |
| 35 | + # User explicitly selects prompt and parameters |
| 36 | + instructions = await get_instructions_from_prompt( |
| 37 | + mcp_server, |
| 38 | + "generate_code_review_instructions", |
| 39 | + focus="security vulnerabilities", |
| 40 | + language="python", |
| 41 | + ) |
| 42 | + |
| 43 | + agent = Agent( |
| 44 | + name="Code Reviewer Agent", |
| 45 | + instructions=instructions, # Instructions from MCP prompt |
| 46 | + model_settings=ModelSettings(tool_choice="auto"), |
| 47 | + ) |
| 48 | + |
| 49 | + message = """Please review this code: |
| 50 | +
|
| 51 | +def process_user_input(user_input): |
| 52 | + command = f"echo {user_input}" |
| 53 | + os.system(command) |
| 54 | + return "Command executed" |
| 55 | +
|
| 56 | +""" |
| 57 | + |
| 58 | + print(f"Running: {message[:60]}...") |
| 59 | + result = await Runner.run(starting_agent=agent, input=message) |
| 60 | + print(result.final_output) |
| 61 | + print("\n" + "=" * 50 + "\n") |
| 62 | + |
| 63 | + |
| 64 | +async def show_available_prompts(mcp_server: MCPServer): |
| 65 | + """Show available prompts for user selection""" |
| 66 | + print("=== AVAILABLE PROMPTS ===") |
| 67 | + |
| 68 | + prompts_result = await mcp_server.list_prompts() |
| 69 | + print("User can select from these prompts:") |
| 70 | + for i, prompt in enumerate(prompts_result.prompts, 1): |
| 71 | + print(f" {i}. {prompt.name} - {prompt.description}") |
| 72 | + print() |
| 73 | + |
| 74 | + |
| 75 | +async def main(): |
| 76 | + async with MCPServerStreamableHttp( |
| 77 | + name="Simple Prompt Server", |
| 78 | + params={"url": "http://localhost:8000/mcp"}, |
| 79 | + ) as server: |
| 80 | + trace_id = gen_trace_id() |
| 81 | + with trace(workflow_name="Simple Prompt Demo", trace_id=trace_id): |
| 82 | + print(f"Trace: https://platform.openai.com/traces/trace?trace_id={trace_id}\n") |
| 83 | + |
| 84 | + await show_available_prompts(server) |
| 85 | + await demo_code_review(server) |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + if not shutil.which("uv"): |
| 90 | + raise RuntimeError("uv is not installed") |
| 91 | + |
| 92 | + process: subprocess.Popen[Any] | None = None |
| 93 | + try: |
| 94 | + this_dir = os.path.dirname(os.path.abspath(__file__)) |
| 95 | + server_file = os.path.join(this_dir, "server.py") |
| 96 | + |
| 97 | + print("Starting Simple Prompt Server...") |
| 98 | + process = subprocess.Popen(["uv", "run", server_file]) |
| 99 | + time.sleep(3) |
| 100 | + print("Server started\n") |
| 101 | + except Exception as e: |
| 102 | + print(f"Error starting server: {e}") |
| 103 | + exit(1) |
| 104 | + |
| 105 | + try: |
| 106 | + asyncio.run(main()) |
| 107 | + finally: |
| 108 | + if process: |
| 109 | + process.terminate() |
| 110 | + print("Server terminated.") |
0 commit comments