File size: 1,812 Bytes
363cda9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3370983
 
363cda9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
Test script to interact with the Google Calendar MCP server.
===============================================================
# Only run server
# >>> python src/mcp_servers/examples/gcalendar/interact_calendar_spawn.py
"""


import asyncio
from langchain_mcp_adapters.client import MultiServerMCPClient
from langchain.agents import create_agent
from langchain_openai.chat_models.base import ChatOpenAI
from .settings import GoogleCalendarSettings
from dotenv import load_dotenv
import sys

load_dotenv()

MODEL = ChatOpenAI(model="gpt-4o", temperature=0)


settings = GoogleCalendarSettings()

CALENDAR_MCP_DIR = settings.calendar_mcp_dir
CREDS = settings.creds
TOKEN = settings.token

async def main():
    client = MultiServerMCPClient({
        "calendar": {
            "command": sys.executable,
            "args": [
                f"{CALENDAR_MCP_DIR}/run_server.py",
                "--creds-file-path", str(CREDS),
                "--token-path", str(TOKEN),
            ],
            "transport": "stdio",
        }
    })

    tools = await client.get_tools()
    agent = create_agent(MODEL, tools)

    result = await agent.ainvoke({
        "messages": [
            {
                "role": "system",
                "content": (
                    "You are a scheduling assistant authorized to use Google Calendar MCP tools. "
                    "You can list, create, and analyze events."
                ),
            },
            {
                "role": "user",
                "content": (
                    "Create a calendar event for the coming Friday at 3pm titled "
                    "'Kick-off of the Agent MCP Hackathon'."
                ),
            },
        ]
    })

    print(result["messages"][-1].content)

if __name__ == "__main__":
    asyncio.run(main())