|
| 1 | +import asyncio |
| 2 | +import os |
| 3 | +import shutil |
| 4 | +import subprocess |
| 5 | +import time |
| 6 | +import socket |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +from agents import Agent, Runner, gen_trace_id, trace |
| 10 | +from agents.mcp import MCPServer, MCPServerSse |
| 11 | +from agents.model_settings import ModelSettings |
| 12 | + |
| 13 | + |
| 14 | +# async def run(mcp_server: MCPServer): |
| 15 | +# agent = Agent( |
| 16 | +# name="Assistant", |
| 17 | +# instructions="Use the tools to answer the questions.", |
| 18 | +# mcp_servers=[mcp_server], |
| 19 | +# model_settings=ModelSettings(tool_choice="required"), |
| 20 | +# ) |
| 21 | + |
| 22 | +# # Use the `add` tool to add two numbers |
| 23 | +# message = "list files in current directory using python" |
| 24 | +# print(f"Running: {message}") |
| 25 | +# result = await Runner.run(starting_agent=agent, input=message) |
| 26 | +# print(result.final_output) |
| 27 | + |
| 28 | +# # Run the `get_weather` tool |
| 29 | +# message = "Fetch ETH price on 15th June 2025. First pip install libraries needed like yfinance, then write the code and fetch data." |
| 30 | +# print(f"\n\nRunning: {message}") |
| 31 | +# result = await Runner.run(starting_agent=agent, input=message) |
| 32 | +# print(result.final_output) |
| 33 | + |
| 34 | +# # Run the `get_secret_word` tool |
| 35 | +# message = "What's the secret word?" |
| 36 | +# print(f"\n\nRunning: {message}") |
| 37 | +# result = await Runner.run(starting_agent=agent, input=message) |
| 38 | +# print(result.final_output) |
| 39 | + |
| 40 | + |
| 41 | +async def run(mcp_server: MCPServer): |
| 42 | + agent = Agent( |
| 43 | + name="Assistant", |
| 44 | + instructions="Use the tools to answer the questions.", |
| 45 | + mcp_servers=[mcp_server], |
| 46 | + model_settings=ModelSettings(tool_choice="required"), |
| 47 | + ) |
| 48 | + |
| 49 | + while True: |
| 50 | + message = input("Enter your prompt (or 'exit' to quit): ") |
| 51 | + if message.lower() == 'exit': |
| 52 | + print("Exiting...") |
| 53 | + break |
| 54 | + |
| 55 | + print(f"Running: {message}") |
| 56 | + result = await Runner.run(starting_agent=agent, input=message) |
| 57 | + print(result.final_output) |
| 58 | + |
| 59 | +def resolve_with_system_dns(hostname): |
| 60 | + try: |
| 61 | + return socket.gethostbyname(hostname) |
| 62 | + except socket.gaierror as e: |
| 63 | + print(f"Error resolving {hostname}: {e}") |
| 64 | + return None |
| 65 | + |
| 66 | + |
| 67 | +async def main(): |
| 68 | + hostname = "coderunner.local" |
| 69 | + address = resolve_with_system_dns(hostname) |
| 70 | + async with MCPServerSse( |
| 71 | + name="SSE Python Server", |
| 72 | + params={ |
| 73 | + "url": f"http://{address}:8222/sse", |
| 74 | + "sse_read_timeout": 60, |
| 75 | + "timeout": 60, |
| 76 | + }, |
| 77 | + ) as server: |
| 78 | + trace_id = gen_trace_id() |
| 79 | + with trace(workflow_name="SSE Example", trace_id=trace_id): |
| 80 | + print(f"View trace: https://platform.openai.com/traces/trace?trace_id={trace_id}\n") |
| 81 | + await run(server) |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + asyncio.run(main()) |
0 commit comments