Skip to content

Commit d1d1508

Browse files
committed
fix
1 parent 775a074 commit d1d1508

File tree

9 files changed

+318
-87
lines changed

9 files changed

+318
-87
lines changed

apple-mcp.dxt

26.2 KB
Binary file not shown.

index.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,12 @@ function initServer() {
260260
};
261261
}
262262
} catch (error) {
263+
const errorMessage = error instanceof Error ? error.message : String(error);
263264
return {
264265
content: [
265266
{
266267
type: "text",
267-
text: `Error accessing contacts: ${error instanceof Error ? error.message : String(error)}`,
268+
text: errorMessage.includes("access") ? errorMessage : `Error accessing contacts: ${errorMessage}`,
268269
},
269270
],
270271
isError: true,
@@ -352,11 +353,12 @@ function initServer() {
352353
throw new Error(`Unknown operation: ${operation}`);
353354
}
354355
} catch (error) {
356+
const errorMessage = error instanceof Error ? error.message : String(error);
355357
return {
356358
content: [
357359
{
358360
type: "text",
359-
text: `Error accessing notes: ${error instanceof Error ? error.message : String(error)}`,
361+
text: errorMessage.includes("access") ? errorMessage : `Error accessing notes: ${errorMessage}`,
360362
},
361363
],
362364
isError: true,
@@ -491,11 +493,12 @@ function initServer() {
491493
throw new Error(`Unknown operation: ${args.operation}`);
492494
}
493495
} catch (error) {
496+
const errorMessage = error instanceof Error ? error.message : String(error);
494497
return {
495498
content: [
496499
{
497500
type: "text",
498-
text: `Error with messages operation: ${error instanceof Error ? error.message : String(error)}`,
501+
text: errorMessage.includes("access") ? errorMessage : `Error with messages operation: ${errorMessage}`,
499502
},
500503
],
501504
isError: true,
@@ -809,11 +812,12 @@ end tell`;
809812
throw new Error(`Unknown operation: ${args.operation}`);
810813
}
811814
} catch (error) {
815+
const errorMessage = error instanceof Error ? error.message : String(error);
812816
return {
813817
content: [
814818
{
815819
type: "text",
816-
text: `Error with mail operation: ${error instanceof Error ? error.message : String(error)}`,
820+
text: errorMessage.includes("access") ? errorMessage : `Error with mail operation: ${errorMessage}`,
817821
},
818822
],
819823
isError: true,
@@ -934,11 +938,12 @@ end tell`;
934938
};
935939
} catch (error) {
936940
console.error("Error in reminders tool:", error);
941+
const errorMessage = error instanceof Error ? error.message : String(error);
937942
return {
938943
content: [
939944
{
940945
type: "text",
941-
text: `Error in reminders tool: ${error}`,
946+
text: errorMessage.includes("access") ? errorMessage : `Error in reminders tool: ${errorMessage}`,
942947
},
943948
],
944949
isError: true,
@@ -1079,11 +1084,12 @@ end tell`;
10791084
throw new Error(`Unknown calendar operation: ${operation}`);
10801085
}
10811086
} catch (error) {
1087+
const errorMessage = error instanceof Error ? error.message : String(error);
10821088
return {
10831089
content: [
10841090
{
10851091
type: "text",
1086-
text: `Error in calendar tool: ${error instanceof Error ? error.message : String(error)}`,
1092+
text: errorMessage.includes("access") ? errorMessage : `Error in calendar tool: ${errorMessage}`,
10871093
},
10881094
],
10891095
isError: true,
@@ -1258,11 +1264,12 @@ end tell`;
12581264
throw new Error(`Unknown maps operation: ${operation}`);
12591265
}
12601266
} catch (error) {
1267+
const errorMessage = error instanceof Error ? error.message : String(error);
12611268
return {
12621269
content: [
12631270
{
12641271
type: "text",
1265-
text: `Error in maps tool: ${error instanceof Error ? error.message : String(error)}`,
1272+
text: errorMessage.includes("access") ? errorMessage : `Error in maps tool: ${errorMessage}`,
12661273
},
12671274
],
12681275
isError: true,

utils/calendar.ts

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,33 @@ end tell`;
3939
}
4040
}
4141

42+
/**
43+
* Request Calendar app access and provide instructions if not available
44+
*/
45+
async function requestCalendarAccess(): Promise<{ hasAccess: boolean; message: string }> {
46+
try {
47+
// First check if we already have access
48+
const hasAccess = await checkCalendarAccess();
49+
if (hasAccess) {
50+
return {
51+
hasAccess: true,
52+
message: "Calendar access is already granted."
53+
};
54+
}
55+
56+
// If no access, provide clear instructions
57+
return {
58+
hasAccess: false,
59+
message: "Calendar access is required but not granted. Please:\n1. Open System Settings > Privacy & Security > Automation\n2. Find your terminal/app in the list and enable 'Calendar'\n3. Alternatively, open System Settings > Privacy & Security > Calendars\n4. Add your terminal/app to the allowed applications\n5. Restart your terminal and try again"
60+
};
61+
} catch (error) {
62+
return {
63+
hasAccess: false,
64+
message: `Error checking Calendar access: ${error instanceof Error ? error.message : String(error)}`
65+
};
66+
}
67+
}
68+
4269
/**
4370
* Get calendar events in a specified date range
4471
* @param limit Optional limit on the number of results (default 10)
@@ -53,9 +80,9 @@ async function getEvents(
5380
try {
5481
console.error("getEvents - Starting to fetch calendar events");
5582

56-
if (!await checkCalendarAccess()) {
57-
console.error("getEvents - Failed to access Calendar app");
58-
return [];
83+
const accessResult = await requestCalendarAccess();
84+
if (!accessResult.hasAccess) {
85+
throw new Error(accessResult.message);
5986
}
6087
console.error("getEvents - Calendar access check passed");
6188

@@ -128,8 +155,9 @@ async function searchEvents(
128155
toDate?: string
129156
): Promise<CalendarEvent[]> {
130157
try {
131-
if (!await checkCalendarAccess()) {
132-
return [];
158+
const accessResult = await requestCalendarAccess();
159+
if (!accessResult.hasAccess) {
160+
throw new Error(accessResult.message);
133161
}
134162

135163
console.error(`searchEvents - Processing calendars for search: "${searchText}"`);
@@ -193,10 +221,11 @@ async function createEvent(
193221
calendarName?: string
194222
): Promise<{ success: boolean; message: string; eventId?: string }> {
195223
try {
196-
if (!await checkCalendarAccess()) {
224+
const accessResult = await requestCalendarAccess();
225+
if (!accessResult.hasAccess) {
197226
return {
198227
success: false,
199-
message: "Cannot access Calendar app. Please grant access in System Settings > Privacy & Security > Automation."
228+
message: accessResult.message
200229
};
201230
}
202231

@@ -287,10 +316,11 @@ end tell`;
287316
*/
288317
async function openEvent(eventId: string): Promise<{ success: boolean; message: string }> {
289318
try {
290-
if (!await checkCalendarAccess()) {
319+
const accessResult = await requestCalendarAccess();
320+
if (!accessResult.hasAccess) {
291321
return {
292322
success: false,
293-
message: "Cannot access Calendar app. Please grant access in System Settings > Privacy & Security > Automation."
323+
message: accessResult.message
294324
};
295325
}
296326

@@ -328,7 +358,8 @@ const calendar = {
328358
searchEvents,
329359
openEvent,
330360
getEvents,
331-
createEvent
361+
createEvent,
362+
requestCalendarAccess
332363
};
333364

334365
export default calendar;

utils/contacts.ts

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,35 @@ end tell`;
2626
}
2727
}
2828

29+
async function requestContactsAccess(): Promise<{ hasAccess: boolean; message: string }> {
30+
try {
31+
// First check if we already have access
32+
const hasAccess = await checkContactsAccess();
33+
if (hasAccess) {
34+
return {
35+
hasAccess: true,
36+
message: "Contacts access is already granted."
37+
};
38+
}
39+
40+
// If no access, provide clear instructions
41+
return {
42+
hasAccess: false,
43+
message: "Contacts access is required but not granted. Please:\n1. Open System Settings > Privacy & Security > Automation\n2. Find your terminal/app in the list and enable 'Contacts'\n3. Alternatively, open System Settings > Privacy & Security > Contacts\n4. Add your terminal/app to the allowed applications\n5. Restart your terminal and try again"
44+
};
45+
} catch (error) {
46+
return {
47+
hasAccess: false,
48+
message: `Error checking Contacts access: ${error instanceof Error ? error.message : String(error)}`
49+
};
50+
}
51+
}
52+
2953
async function getAllNumbers(): Promise<{ [key: string]: string[] }> {
3054
try {
31-
if (!(await checkContactsAccess())) {
32-
return {};
55+
const accessResult = await requestContactsAccess();
56+
if (!accessResult.hasAccess) {
57+
throw new Error(accessResult.message);
3358
}
3459

3560
const script = `
@@ -103,8 +128,9 @@ end tell`;
103128

104129
async function findNumber(name: string): Promise<string[]> {
105130
try {
106-
if (!(await checkContactsAccess())) {
107-
return [];
131+
const accessResult = await requestContactsAccess();
132+
if (!accessResult.hasAccess) {
133+
throw new Error(accessResult.message);
108134
}
109135

110136
if (!name || name.trim() === "") {
@@ -299,8 +325,9 @@ end tell`;
299325

300326
async function findContactByPhone(phoneNumber: string): Promise<string | null> {
301327
try {
302-
if (!(await checkContactsAccess())) {
303-
return null;
328+
const accessResult = await requestContactsAccess();
329+
if (!accessResult.hasAccess) {
330+
throw new Error(accessResult.message);
304331
}
305332

306333
if (!phoneNumber || phoneNumber.trim() === "") {
@@ -390,4 +417,4 @@ end tell`;
390417
}
391418
}
392419

393-
export default { getAllNumbers, findNumber, findContactByPhone };
420+
export default { getAllNumbers, findNumber, findContactByPhone, requestContactsAccess };

utils/mail.ts

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,41 @@ end tell`;
3939
}
4040
}
4141

42+
/**
43+
* Request Mail app access and provide instructions if not available
44+
*/
45+
async function requestMailAccess(): Promise<{ hasAccess: boolean; message: string }> {
46+
try {
47+
// First check if we already have access
48+
const hasAccess = await checkMailAccess();
49+
if (hasAccess) {
50+
return {
51+
hasAccess: true,
52+
message: "Mail access is already granted."
53+
};
54+
}
55+
56+
// If no access, provide clear instructions
57+
return {
58+
hasAccess: false,
59+
message: "Mail access is required but not granted. Please:\n1. Open System Settings > Privacy & Security > Automation\n2. Find your terminal/app in the list and enable 'Mail'\n3. Make sure Mail app is running and configured with at least one account\n4. Restart your terminal and try again"
60+
};
61+
} catch (error) {
62+
return {
63+
hasAccess: false,
64+
message: `Error checking Mail access: ${error instanceof Error ? error.message : String(error)}`
65+
};
66+
}
67+
}
68+
4269
/**
4370
* Get unread emails from Mail app (limited for performance)
4471
*/
4572
async function getUnreadMails(limit = 10): Promise<EmailMessage[]> {
4673
try {
47-
if (!(await checkMailAccess())) {
48-
return [];
74+
const accessResult = await requestMailAccess();
75+
if (!accessResult.hasAccess) {
76+
throw new Error(accessResult.message);
4977
}
5078

5179
const maxEmails = Math.min(limit, CONFIG.MAX_EMAILS);
@@ -135,8 +163,9 @@ async function searchMails(
135163
limit = 10,
136164
): Promise<EmailMessage[]> {
137165
try {
138-
if (!(await checkMailAccess())) {
139-
return [];
166+
const accessResult = await requestMailAccess();
167+
if (!accessResult.hasAccess) {
168+
throw new Error(accessResult.message);
140169
}
141170

142171
if (!searchTerm || searchTerm.trim() === "") {
@@ -236,10 +265,9 @@ async function sendMail(
236265
bcc?: string,
237266
): Promise<string | undefined> {
238267
try {
239-
if (!(await checkMailAccess())) {
240-
throw new Error(
241-
"Cannot access Mail app. Please make sure Mail is running and configured.",
242-
);
268+
const accessResult = await requestMailAccess();
269+
if (!accessResult.hasAccess) {
270+
throw new Error(accessResult.message);
243271
}
244272

245273
// Validate inputs
@@ -309,8 +337,9 @@ end tell`;
309337
*/
310338
async function getMailboxes(): Promise<string[]> {
311339
try {
312-
if (!(await checkMailAccess())) {
313-
return [];
340+
const accessResult = await requestMailAccess();
341+
if (!accessResult.hasAccess) {
342+
throw new Error(accessResult.message);
314343
}
315344

316345
const script = `
@@ -348,8 +377,9 @@ end tell`;
348377
*/
349378
async function getAccounts(): Promise<string[]> {
350379
try {
351-
if (!(await checkMailAccess())) {
352-
return [];
380+
const accessResult = await requestMailAccess();
381+
if (!accessResult.hasAccess) {
382+
throw new Error(accessResult.message);
353383
}
354384

355385
const script = `
@@ -387,8 +417,9 @@ end tell`;
387417
*/
388418
async function getMailboxesForAccount(accountName: string): Promise<string[]> {
389419
try {
390-
if (!(await checkMailAccess())) {
391-
return [];
420+
const accessResult = await requestMailAccess();
421+
if (!accessResult.hasAccess) {
422+
throw new Error(accessResult.message);
392423
}
393424

394425
if (!accountName || !accountName.trim()) {
@@ -444,8 +475,9 @@ async function getLatestMails(
444475
limit = 5,
445476
): Promise<EmailMessage[]> {
446477
try {
447-
if (!(await checkMailAccess())) {
448-
return [];
478+
const accessResult = await requestMailAccess();
479+
if (!accessResult.hasAccess) {
480+
throw new Error(accessResult.message);
449481
}
450482

451483
const script = `
@@ -557,4 +589,5 @@ export default {
557589
getAccounts,
558590
getMailboxesForAccount,
559591
getLatestMails,
592+
requestMailAccess,
560593
};

0 commit comments

Comments
 (0)