w
This commit is contained in:
+75
-25
@@ -9,10 +9,18 @@ export function registerAvailabilityTools(server: McpServer): void {
|
||||
description: "Find free time slots in your own calendar",
|
||||
inputSchema: z.object({
|
||||
startDate: z.string().describe("Start date in YYYY-MM-DD format"),
|
||||
endDate: z.string().optional().describe("End date in YYYY-MM-DD format (defaults to startDate)"),
|
||||
durationMinutes: z.number().default(30).describe("Minimum slot duration in minutes (default 30)"),
|
||||
startHour: z.number().default(9).describe("Working day start hour (0-23, default 9)"),
|
||||
endHour: z.number().default(18).describe("Working day end hour (0-23, default 18)"),
|
||||
endDate: z.string().optional().describe(
|
||||
"End date in YYYY-MM-DD format (defaults to startDate)",
|
||||
),
|
||||
durationMinutes: z.number().default(30).describe(
|
||||
"Minimum slot duration in minutes (default 30)",
|
||||
),
|
||||
startHour: z.number().default(9).describe(
|
||||
"Working day start hour (0-23, default 9)",
|
||||
),
|
||||
endHour: z.number().default(18).describe(
|
||||
"Working day end hour (0-23, default 18)",
|
||||
),
|
||||
}),
|
||||
},
|
||||
async ({ startDate, endDate, durationMinutes, startHour, endHour }) => {
|
||||
@@ -27,9 +35,13 @@ export function registerAvailabilityTools(server: McpServer): void {
|
||||
ed,
|
||||
);
|
||||
|
||||
const body = data?.Envelope?.Body?.GetUserAvailabilityResponse ?? data?.Envelope?.Body ?? {};
|
||||
const freeBusyArray = body?.FreeBusyResponseArray?.FreeBusyResponse ?? [];
|
||||
const responses = Array.isArray(freeBusyArray) ? freeBusyArray : [freeBusyArray];
|
||||
const body = data?.Envelope?.Body?.GetUserAvailabilityResponse
|
||||
?? data?.Envelope?.Body ?? {};
|
||||
const freeBusyArray = body?.FreeBusyResponseArray?.FreeBusyResponse
|
||||
?? [];
|
||||
const responses = Array.isArray(freeBusyArray)
|
||||
? freeBusyArray
|
||||
: [freeBusyArray];
|
||||
|
||||
const busyPeriods: { start: Date; end: Date }[] = [];
|
||||
|
||||
@@ -63,7 +75,10 @@ export function registerAvailabilityTools(server: McpServer): void {
|
||||
);
|
||||
|
||||
return {
|
||||
content: [{ type: "text" as const, text: JSON.stringify({ freeSlots }) }],
|
||||
content: [{
|
||||
type: "text" as const,
|
||||
text: JSON.stringify({ freeSlots }),
|
||||
}],
|
||||
};
|
||||
} catch (error: any) {
|
||||
return {
|
||||
@@ -81,25 +96,43 @@ export function registerAvailabilityTools(server: McpServer): void {
|
||||
{
|
||||
description: "Find meeting times that work for multiple people",
|
||||
inputSchema: z.object({
|
||||
emails: z.string().describe("Comma-separated email addresses of attendees"),
|
||||
emails: z.string().describe(
|
||||
"Comma-separated email addresses of attendees",
|
||||
),
|
||||
startDate: z.string().describe("Start date in YYYY-MM-DD format"),
|
||||
endDate: z.string().optional().describe("End date in YYYY-MM-DD format (defaults to startDate)"),
|
||||
durationMinutes: z.number().default(30).describe("Minimum slot duration in minutes (default 30)"),
|
||||
startHour: z.number().default(9).describe("Working day start hour (0-23, default 9)"),
|
||||
endHour: z.number().default(18).describe("Working day end hour (0-23, default 18)"),
|
||||
endDate: z.string().optional().describe(
|
||||
"End date in YYYY-MM-DD format (defaults to startDate)",
|
||||
),
|
||||
durationMinutes: z.number().default(30).describe(
|
||||
"Minimum slot duration in minutes (default 30)",
|
||||
),
|
||||
startHour: z.number().default(9).describe(
|
||||
"Working day start hour (0-23, default 9)",
|
||||
),
|
||||
endHour: z.number().default(18).describe(
|
||||
"Working day end hour (0-23, default 18)",
|
||||
),
|
||||
}),
|
||||
},
|
||||
async ({ emails, startDate, endDate, durationMinutes, startHour, endHour }) => {
|
||||
async (
|
||||
{ emails, startDate, endDate, durationMinutes, startHour, endHour },
|
||||
) => {
|
||||
try {
|
||||
const client = new EwsClient(loadConfig());
|
||||
const emailList = emails.split(",").map((e) => e.trim()).filter(Boolean);
|
||||
const emailList = emails.split(",").map((e) => e.trim()).filter(
|
||||
Boolean,
|
||||
);
|
||||
const ed = endDate || startDate;
|
||||
|
||||
const data = await client.getUserAvailability(emailList, startDate, ed);
|
||||
|
||||
const body = data?.Envelope?.Body?.GetUserAvailabilityResponse ?? data?.Envelope?.Body ?? {};
|
||||
const freeBusyArray = body?.FreeBusyResponseArray?.FreeBusyResponse ?? [];
|
||||
const responses = Array.isArray(freeBusyArray) ? freeBusyArray : [freeBusyArray];
|
||||
const body = data?.Envelope?.Body?.GetUserAvailabilityResponse
|
||||
?? data?.Envelope?.Body ?? {};
|
||||
const freeBusyArray = body?.FreeBusyResponseArray?.FreeBusyResponse
|
||||
?? [];
|
||||
const responses = Array.isArray(freeBusyArray)
|
||||
? freeBusyArray
|
||||
: [freeBusyArray];
|
||||
|
||||
const allBusy: { start: Date; end: Date }[] = [];
|
||||
const attendeeInfo: any[] = [];
|
||||
@@ -114,8 +147,14 @@ export function registerAvailabilityTools(server: McpServer): void {
|
||||
const startTime = new Date(`${startDate}T00:00:00`);
|
||||
const busyPeriods = parseFreeBusyString(mergedFb, startTime);
|
||||
const busyCount = busyPeriods.length;
|
||||
const freeCount = mergedFb.split("").filter((c: string) => c === "0").length;
|
||||
attendeeInfo.push({ email, busySlots: busyCount, freeSlots: freeCount });
|
||||
const freeCount = mergedFb.split("").filter((c: string) =>
|
||||
c === "0"
|
||||
).length;
|
||||
attendeeInfo.push({
|
||||
email,
|
||||
busySlots: busyCount,
|
||||
freeSlots: freeCount,
|
||||
});
|
||||
allBusy.push(...busyPeriods);
|
||||
} else {
|
||||
const calEvents = fbView?.CalendarEventArray?.CalendarEvent ?? [];
|
||||
@@ -125,7 +164,10 @@ export function registerAvailabilityTools(server: McpServer): void {
|
||||
const startStr = ev.StartTime ?? "";
|
||||
const endStr = ev.EndTime ?? "";
|
||||
if (startStr && endStr) {
|
||||
allBusy.push({ start: new Date(startStr), end: new Date(endStr) });
|
||||
allBusy.push({
|
||||
start: new Date(startStr),
|
||||
end: new Date(endStr),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -184,10 +226,14 @@ function parseFreeBusyString(
|
||||
return periods;
|
||||
}
|
||||
|
||||
function mergeBusyPeriods(periods: { start: Date; end: Date }[]): { start: Date; end: Date }[] {
|
||||
function mergeBusyPeriods(
|
||||
periods: { start: Date; end: Date }[],
|
||||
): { start: Date; end: Date }[] {
|
||||
if (!periods.length) return [];
|
||||
|
||||
const sorted = [...periods].sort((a, b) => a.start.getTime() - b.start.getTime());
|
||||
const sorted = [...periods].sort((a, b) =>
|
||||
a.start.getTime() - b.start.getTime()
|
||||
);
|
||||
const merged: { start: Date; end: Date }[] = [{ ...sorted[0] }];
|
||||
|
||||
for (let i = 1; i < sorted.length; i++) {
|
||||
@@ -210,7 +256,10 @@ function findFreeSlots(
|
||||
endHour: number,
|
||||
durationMinutes: number,
|
||||
): Record<string, { start: string; end: string; durationMinutes: number }[]> {
|
||||
const result: Record<string, { start: string; end: string; durationMinutes: number }[]> = {};
|
||||
const result: Record<
|
||||
string,
|
||||
{ start: string; end: string; durationMinutes: number }[]
|
||||
> = {};
|
||||
|
||||
const current = new Date(startDate);
|
||||
current.setDate(current.getDate());
|
||||
@@ -237,7 +286,8 @@ function findFreeSlots(
|
||||
|
||||
const merged = mergeBusyPeriods(dayBusy);
|
||||
|
||||
const slots: { start: string; end: string; durationMinutes: number }[] = [];
|
||||
const slots: { start: string; end: string; durationMinutes: number }[] =
|
||||
[];
|
||||
let cursor = new Date(dayStart);
|
||||
|
||||
for (const bp of merged) {
|
||||
|
||||
Reference in New Issue
Block a user