|
| 1 | +import argparse |
| 2 | +import asyncio |
| 3 | +import json |
| 4 | +import os |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | +from agents import Agent, HostedMCPTool, Runner |
| 8 | + |
| 9 | +# import logging |
| 10 | +# logging.basicConfig(level=logging.DEBUG) |
| 11 | + |
| 12 | + |
| 13 | +async def main(verbose: bool, stream: bool): |
| 14 | + # 1. Visit https://developers.google.com/oauthplayground/ |
| 15 | + # 2. Input https://www.googleapis.com/auth/calendar.events as the required scope |
| 16 | + # 3. Grab the acccess token starting with "ya29." |
| 17 | + authorization = os.environ["GOOGLE_CALENDAR_AUTHORIZATION"] |
| 18 | + agent = Agent( |
| 19 | + name="Assistant", |
| 20 | + instructions="You are a helpful assistant that can help a user with their calendar.", |
| 21 | + tools=[ |
| 22 | + HostedMCPTool( |
| 23 | + tool_config={ |
| 24 | + "type": "mcp", |
| 25 | + "server_label": "google_calendar", |
| 26 | + # see https://platform.openai.com/docs/guides/tools-connectors-mcp#connectors |
| 27 | + "connector_id": "connector_googlecalendar", |
| 28 | + "authorization": authorization, |
| 29 | + "require_approval": "never", |
| 30 | + } |
| 31 | + ) |
| 32 | + ], |
| 33 | + ) |
| 34 | + |
| 35 | + today = datetime.now().strftime("%Y-%m-%d") |
| 36 | + if stream: |
| 37 | + result = Runner.run_streamed(agent, f"What is my schedule for {today}?") |
| 38 | + async for event in result.stream_events(): |
| 39 | + if event.type == "raw_response_event": |
| 40 | + if event.data.type.startswith("response.output_item"): |
| 41 | + print(json.dumps(event.data.to_dict(), indent=2)) |
| 42 | + if event.data.type.startswith("response.mcp"): |
| 43 | + print(json.dumps(event.data.to_dict(), indent=2)) |
| 44 | + if event.data.type == "response.output_text.delta": |
| 45 | + print(event.data.delta, end="", flush=True) |
| 46 | + print() |
| 47 | + else: |
| 48 | + res = await Runner.run(agent, f"What is my schedule for {today}?") |
| 49 | + print(res.final_output) |
| 50 | + |
| 51 | + if verbose: |
| 52 | + for item in res.new_items: |
| 53 | + print(item) |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + parser = argparse.ArgumentParser() |
| 58 | + parser.add_argument("--verbose", action="store_true", default=False) |
| 59 | + parser.add_argument("--stream", action="store_true", default=False) |
| 60 | + args = parser.parse_args() |
| 61 | + |
| 62 | + asyncio.run(main(args.verbose, args.stream)) |
0 commit comments