getMyCalendarDataByDate
Retrieve Google Calendar events for a specific date using the Google Calendar API. Query calendar data with date validation and error handling.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- server.js:65-74 (handler)The MCP tool handler function that processes the input date, calls the helper function, and returns the result as JSON-formatted text content.async ({ date }) => { return { content: [ { type: 'text', text: JSON.stringify(await getMyCalendarDataByDate(date)), }, ], }; }
- server.js:14-56 (helper)Helper function implementing the core logic: fetches calendar events for the specified date using Google Calendar API, formats meetings, handles errors.async function getMyCalendarDataByDate(date) { const calendar = google.calendar({ version: 'v3', auth: process.env.GOOGLE_PUBLIC_API_KEY, }); // Calculate the start and end of the given date (UTC) const start = new Date(date); start.setUTCHours(0, 0, 0, 0); const end = new Date(start); end.setUTCDate(end.getUTCDate() + 1); try { const res = await calendar.events.list({ calendarId: process.env.CALENDAR_ID, timeMin: start.toISOString(), timeMax: end.toISOString(), maxResults: 10, singleEvents: true, orderBy: 'startTime', }); const events = res.data.items || []; const meetings = events.map((event) => { const start = event.start.dateTime || event.start.date; return `${event.summary} at ${start}`; }); if (meetings.length > 0) { return { meetings, }; } else { return { meetings: [], }; } } catch (err) { return { error: err.message, }; } }
- server.js:60-64 (schema)Zod schema for the 'date' input parameter with refinement to validate it as a parsable date string.date: z.string().refine( (val) => !isNaN(Date.parse(val)), { message: 'Invalid date format. Please provide a valid date string.', },
- server.js:59-76 (registration)Registration of the 'getMyCalendarDataByDate' tool with the MCP server, including schema and handler.server.tool('getMyCalendarDataByDate', { date: z.string().refine( (val) => !isNaN(Date.parse(val)), { message: 'Invalid date format. Please provide a valid date string.', }, async ({ date }) => { return { content: [ { type: 'text', text: JSON.stringify(await getMyCalendarDataByDate(date)), }, ], }; } ), });