|
| 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, MCPServerSse |
| 10 | +from agents.model_settings import ModelSettings |
| 11 | + |
| 12 | + |
| 13 | +async def run(mcp_server: MCPServer): |
| 14 | + agent = Agent( |
| 15 | + name="Assistant", |
| 16 | + instructions="Use the tools to answer the questions.", |
| 17 | + mcp_servers=[mcp_server], |
| 18 | + model_settings=ModelSettings(tool_choice="required"), |
| 19 | + ) |
| 20 | + |
| 21 | + # Use the `add` tool to add two numbers |
| 22 | + message = "Add these numbers: 7 and 22." |
| 23 | + print(f"Running: {message}") |
| 24 | + result = await Runner.run(starting_agent=agent, input=message) |
| 25 | + print(result.final_output) |
| 26 | + |
| 27 | + # Run the `get_weather` tool |
| 28 | + message = "What's the weather in Tokyo?" |
| 29 | + print(f"\n\nRunning: {message}") |
| 30 | + result = await Runner.run(starting_agent=agent, input=message) |
| 31 | + print(result.final_output) |
| 32 | + |
| 33 | + # Run the `get_secret_word` tool |
| 34 | + message = "What's the secret word?" |
| 35 | + print(f"\n\nRunning: {message}") |
| 36 | + result = await Runner.run(starting_agent=agent, input=message) |
| 37 | + print(result.final_output) |
| 38 | + |
| 39 | + |
| 40 | +async def main(): |
| 41 | + async with MCPServerSse( |
| 42 | + name="SSE Python Server", |
| 43 | + params={ |
| 44 | + "url": "http://localhost:8000/sse", |
| 45 | + }, |
| 46 | + ) as server: |
| 47 | + trace_id = gen_trace_id() |
| 48 | + with trace(workflow_name="SSE Example", trace_id=trace_id): |
| 49 | + print(f"View trace: https://platform.openai.com/traces/{trace_id}\n") |
| 50 | + await run(server) |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + # Let's make sure the user has uv installed |
| 55 | + if not shutil.which("uv"): |
| 56 | + raise RuntimeError( |
| 57 | + "uv is not installed. Please install it: https://docs.astral.sh/uv/getting-started/installation/" |
| 58 | + ) |
| 59 | + |
| 60 | + # We'll run the SSE server in a subprocess. Usually this would be a remote server, but for this |
| 61 | + # demo, we'll run it locally at http://localhost:8000/sse |
| 62 | + process: subprocess.Popen[Any] | None = None |
| 63 | + try: |
| 64 | + this_dir = os.path.dirname(os.path.abspath(__file__)) |
| 65 | + server_file = os.path.join(this_dir, "server.py") |
| 66 | + |
| 67 | + print("Starting SSE server at http://localhost:8000/sse ...") |
| 68 | + |
| 69 | + # Run `uv run server.py` to start the SSE server |
| 70 | + process = subprocess.Popen(["uv", "run", server_file]) |
| 71 | + # Give it 3 seconds to start |
| 72 | + time.sleep(3) |
| 73 | + |
| 74 | + print("SSE server started. Running example...\n\n") |
| 75 | + except Exception as e: |
| 76 | + print(f"Error starting SSE server: {e}") |
| 77 | + exit(1) |
| 78 | + |
| 79 | + try: |
| 80 | + asyncio.run(main()) |
| 81 | + finally: |
| 82 | + if process: |
| 83 | + process.terminate() |
0 commit comments