|
| 1 | +# Copyright Envoy AI Gateway Authors |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# The full text of the Apache license is available in the LICENSE file at |
| 4 | +# the root of the repo. |
| 5 | + |
| 6 | +# run like this: uv run --exact -q --env-file .env agent.py |
| 7 | +# |
| 8 | +# Customizing the ".env" like: |
| 9 | +# |
| 10 | +# OPENAI_BASE_URL=http://localhost:1975/v1 |
| 11 | +# OPENAI_API_KEY=unused |
| 12 | +# CHAT_MODEL=qwen3:4b |
| 13 | +# |
| 14 | +# MCP_URL=http://localhost:1975/mcp |
| 15 | +# |
| 16 | +# OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318 |
| 17 | +# OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf |
| 18 | +# |
| 19 | +# /// script |
| 20 | +# dependencies = [ |
| 21 | +# "openai-agents", |
| 22 | +# "httpx", |
| 23 | +# "mcp", |
| 24 | +# "openinference-instrumentation-openai-agents", |
| 25 | +# "opentelemetry-instrumentation-httpx", |
| 26 | +# "openinference-instrumentation-mcp", |
| 27 | +# ] |
| 28 | +# /// |
| 29 | + |
| 30 | +from opentelemetry.instrumentation import auto_instrumentation |
| 31 | + |
| 32 | +# This must precede any other imports you want to instrument! |
| 33 | +auto_instrumentation.initialize() |
| 34 | + |
| 35 | +import argparse |
| 36 | +import asyncio |
| 37 | +import os |
| 38 | +import sys |
| 39 | + |
| 40 | +from agents import ( |
| 41 | + Agent, |
| 42 | + OpenAIProvider, |
| 43 | + RunConfig, |
| 44 | + Runner, |
| 45 | + Tool, |
| 46 | +) |
| 47 | +from agents.mcp import MCPServerStreamableHttp, MCPUtil |
| 48 | + |
| 49 | + |
| 50 | +async def run_agent(prompt: str, model_name: str, tools: list[Tool]): |
| 51 | + model = OpenAIProvider(use_responses=False).get_model(model_name) |
| 52 | + agent = Agent(name="Assistant", model=model, tools=tools) |
| 53 | + result = await Runner.run( |
| 54 | + starting_agent=agent, |
| 55 | + input=prompt, |
| 56 | + run_config=RunConfig(workflow_name="envoy-ai-gateway"), |
| 57 | + ) |
| 58 | + print(result.final_output) |
| 59 | + |
| 60 | + |
| 61 | +async def main(prompt: str, model_name: str, mcp_url: str): |
| 62 | + if not mcp_url: |
| 63 | + await run_agent(prompt, model_name, []) |
| 64 | + return |
| 65 | + |
| 66 | + async with MCPServerStreamableHttp({"url": mcp_url,"timeout": 300.0},cache_tools_list=True) as server: |
| 67 | + tools = await server.list_tools() |
| 68 | + util = MCPUtil() |
| 69 | + tools = [util.to_function_tool(tool, server, False) for tool in tools] |
| 70 | + await run_agent(prompt, model_name, tools) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + parser = argparse.ArgumentParser("Example Agent with Tools") |
| 75 | + parser.add_argument("prompt", help="Prompt to be evaluated.", default=sys.stdin, type=argparse.FileType('r'), nargs='?') |
| 76 | + parser.add_argument("--model", help="Model to use.", default=os.getenv("CHAT_MODEL"), type=str) |
| 77 | + parser.add_argument("--mcp-url", help="MCP Server to connect to.", default=os.getenv("MCP_URL"), type=str) |
| 78 | + args = parser.parse_args() |
| 79 | + prompt = args.prompt.read() |
| 80 | + |
| 81 | + print(f"Prompt: {prompt}") |
| 82 | + print(f"Using model: {args.model}") |
| 83 | + print(f"Using MCP URL: {args.mcp_url}") |
| 84 | + |
| 85 | + asyncio.run(main(prompt, args.model, args.mcp_url)) |
0 commit comments