@open-workspace/email 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (221) hide show
  1. package/README.md +22 -22
  2. package/alchemy/index.ts +273 -0
  3. package/drizzle/mailbox/0000_puzzling_dracula.sql +149 -0
  4. package/drizzle/mailbox/0001_mute_jack_flag.sql +12 -0
  5. package/drizzle/mailbox/meta/0000_snapshot.json +1002 -0
  6. package/drizzle/mailbox/meta/0001_snapshot.json +1002 -0
  7. package/drizzle/mailbox/meta/_journal.json +20 -0
  8. package/drizzle/mailbox/migrations.js +12 -0
  9. package/index.ts +3 -1
  10. package/migrations/{0000_email.sql → 0000_dry_moon_knight.sql} +26 -20
  11. package/migrations/meta/0000_snapshot.json +125 -109
  12. package/migrations/meta/_journal.json +2 -2
  13. package/package.json +74 -68
  14. package/shared/attachment-preview.ts +37 -27
  15. package/shared/folder-order.ts +106 -0
  16. package/shared/mail-authentication.ts +8 -3
  17. package/shared/mail-feedback.ts +18 -25
  18. package/shared/mail-headers.ts +114 -39
  19. package/shared/mail-import.ts +38 -0
  20. package/shared/mail-path.ts +42 -0
  21. package/shared/mail-received.ts +174 -31
  22. package/shared/mail-remote.ts +59 -2
  23. package/shared/mail-sender-intel.ts +22 -4
  24. package/shared/mail.ts +77 -28
  25. package/shared/module.ts +33 -0
  26. package/shared/office-preview.ts +14 -2
  27. package/shared/pdfjs-assets.ts +35 -0
  28. package/shared/permissions.ts +73 -0
  29. package/shared/webhooks.ts +41 -0
  30. package/src/admin/blocked-senders.tsx +189 -0
  31. package/src/client.tsx +97 -0
  32. package/src/lib/mail-api.ts +9 -0
  33. package/src/mail/attachment-preview-dialog.tsx +161 -0
  34. package/src/mail/blocked-senders.tsx +138 -0
  35. package/src/mail/compose-window-chrome.tsx +194 -0
  36. package/src/mail/compose-window.tsx +486 -0
  37. package/src/mail/composer-asset-bookmarks.ts +409 -0
  38. package/src/mail/composer-asset.tsx +229 -0
  39. package/src/mail/composer-attachment-list.tsx +138 -0
  40. package/src/mail/composer-editor-document.ts +65 -0
  41. package/src/mail/composer-editor.tsx +284 -0
  42. package/src/mail/composer-linked-attachment-text.ts +12 -0
  43. package/src/mail/composer-recipient-fields.tsx +165 -0
  44. package/src/mail/composer-recipients.ts +208 -0
  45. package/src/mail/composer-session.ts +562 -0
  46. package/src/mail/composer-toolbar.tsx +303 -0
  47. package/src/mail/composer-upload-client.ts +105 -0
  48. package/src/mail/composer-upload-progress.ts +53 -0
  49. package/src/mail/conversation-list.tsx +550 -0
  50. package/src/mail/conversation-loading-skeleton.tsx +119 -0
  51. package/src/mail/conversation-message.tsx +346 -0
  52. package/src/mail/conversation-reply.tsx +470 -0
  53. package/src/mail/conversation-selection-toolbar.tsx +322 -0
  54. package/src/mail/conversation-view.tsx +437 -0
  55. package/src/mail/delivery-indicator.tsx +272 -0
  56. package/src/mail/email-html-body.tsx +203 -0
  57. package/src/mail/folder-glyph.tsx +46 -0
  58. package/src/mail/folder-manager.tsx +532 -0
  59. package/src/mail/format-bytes.ts +7 -0
  60. package/src/mail/mail-app-bar.tsx +253 -0
  61. package/src/mail/mail-background.tsx +12 -0
  62. package/src/mail/mail-dock.tsx +137 -0
  63. package/src/mail/mail-folders.ts +44 -0
  64. package/src/mail/mail-layout.css +236 -0
  65. package/src/mail/mail-location.ts +73 -0
  66. package/src/mail/mail-mailbox-sheet.tsx +275 -0
  67. package/src/mail/mail-nav-rail.tsx +129 -0
  68. package/src/mail/mail-nav.tsx +324 -0
  69. package/src/mail/mail-prefetch.ts +59 -0
  70. package/src/mail/mail-primitives.tsx +66 -0
  71. package/src/mail/mail-queries.ts +209 -0
  72. package/src/mail/mail-query-cache.ts +486 -0
  73. package/src/mail/mail-seed.ts +47 -0
  74. package/src/mail/mail-shell.tsx +126 -0
  75. package/src/mail/mailbox-ai-settings.tsx +185 -0
  76. package/src/mail/mailbox-realtime.tsx +251 -0
  77. package/src/mail/mailbox-route.tsx +15 -0
  78. package/src/mail/message-actions.tsx +353 -0
  79. package/src/mail/message-attachments.tsx +200 -0
  80. package/src/mail/message-format.ts +75 -0
  81. package/src/mail/message-header.tsx +260 -0
  82. package/src/mail/message-translation.tsx +225 -0
  83. package/src/mail/original-message-sheet.tsx +619 -0
  84. package/src/mail/original-message.ts +191 -0
  85. package/src/mail/outbound-notification.ts +53 -0
  86. package/src/mail/pane-splitter.tsx +117 -0
  87. package/src/mail/pdf-preview-toolbar.tsx +152 -0
  88. package/src/mail/pdf-preview.css +44 -0
  89. package/src/mail/pdf-preview.tsx +156 -0
  90. package/src/mail/pdf-translation-layout.ts +95 -0
  91. package/src/mail/print-message.ts +92 -0
  92. package/src/mail/reader-pane.tsx +535 -0
  93. package/src/mail/recipient-combobox.tsx +335 -0
  94. package/src/mail/recipient-field-row.tsx +34 -0
  95. package/src/mail/reply-to-warning.tsx +50 -0
  96. package/src/mail/sanitize-email-html.ts +430 -0
  97. package/src/mail/sender-actions.tsx +149 -0
  98. package/src/mail/sender-authentication.tsx +485 -0
  99. package/src/mail/sender-dialogs.tsx +384 -0
  100. package/src/mail/sender-security.tsx +545 -0
  101. package/src/mail/toolbar-tooltip.tsx +29 -0
  102. package/src/mail/translation-html.ts +49 -0
  103. package/src/mail/translation-language.ts +21 -0
  104. package/src/mail/types.ts +53 -0
  105. package/src/mail/unread-document-indicator.tsx +60 -0
  106. package/src/mail/use-compose-submission.ts +208 -0
  107. package/src/mail/use-composer-attachment-preflight.ts +184 -0
  108. package/src/mail/use-composer-submission.ts +117 -0
  109. package/src/mail/use-conversation-selection.ts +186 -0
  110. package/src/mail/use-mail-controller.ts +427 -0
  111. package/src/mail/use-mail-data.ts +279 -0
  112. package/src/mail/use-mail-safety.ts +155 -0
  113. package/src/mail/use-mail-send.ts +142 -0
  114. package/src/mail/use-pdf-translation-overlay.ts +156 -0
  115. package/src/mail/use-pdf-translation.ts +100 -0
  116. package/src/mail/use-pdf-viewer.ts +215 -0
  117. package/src/mail/use-reply-submission.ts +114 -0
  118. package/src/mail/use-sanitized-email-html.ts +20 -0
  119. package/src/mail/use-visible-message-read.ts +138 -0
  120. package/src/pwa/push-subscription.ts +151 -0
  121. package/src/settings/mail-apps-page.tsx +235 -0
  122. package/src/settings/notifications-page.tsx +291 -0
  123. package/worker/admin/blocked-sender-routes.ts +166 -0
  124. package/worker/admin/schemas.ts +11 -0
  125. package/worker/bindings.d.ts +14 -10
  126. package/worker/db/schema.ts +63 -40
  127. package/worker/jmap/eventsource.ts +84 -16
  128. package/worker/jmap/methods.ts +1630 -524
  129. package/worker/jmap/push-types.ts +27 -0
  130. package/worker/jmap/push.ts +140 -56
  131. package/worker/jmap/routes.ts +366 -105
  132. package/worker/mail/access.ts +258 -21
  133. package/worker/mail/ai-classification.ts +36 -20
  134. package/worker/mail/ai-translation.ts +13 -6
  135. package/worker/mail/auth-headers.ts +395 -137
  136. package/worker/mail/bimi.ts +200 -73
  137. package/worker/mail/bootstrap.ts +79 -0
  138. package/worker/mail/capped-body.ts +36 -0
  139. package/worker/mail/classification-signals.ts +35 -25
  140. package/worker/mail/conversation-cursor.ts +30 -19
  141. package/worker/mail/delivery-events.ts +88 -62
  142. package/worker/mail/delivery-facts.ts +46 -10
  143. package/worker/mail/demo-routes.ts +34 -10
  144. package/worker/mail/doh.ts +62 -23
  145. package/worker/mail/downloads.ts +75 -40
  146. package/worker/mail/edge-cache.ts +155 -0
  147. package/worker/mail/header-metadata.ts +88 -22
  148. package/worker/mail/impersonation.ts +160 -38
  149. package/worker/mail/import-routes.ts +313 -0
  150. package/worker/mail/inbound-delivery.ts +19 -9
  151. package/worker/mail/inbound-threading.ts +18 -8
  152. package/worker/mail/inbound.ts +82 -40
  153. package/worker/mail/ip-intel.ts +110 -27
  154. package/worker/mail/isolate-cache.ts +35 -0
  155. package/worker/mail/link-domains.ts +29 -7
  156. package/worker/mail/mailbox-deletion.ts +10 -4
  157. package/worker/mail/mailbox-directory.ts +16 -6
  158. package/worker/mail/mailing-list.ts +94 -39
  159. package/worker/mail/message-security.ts +90 -15
  160. package/worker/mail/message-view.ts +18 -6
  161. package/worker/mail/office-conversion.ts +31 -9
  162. package/worker/mail/office-preview-container.ts +40 -13
  163. package/worker/mail/office-preview-queue.ts +76 -32
  164. package/worker/mail/org-domain.ts +8 -1
  165. package/worker/mail/outbound-content.ts +71 -26
  166. package/worker/mail/outbound-delivery.ts +31 -17
  167. package/worker/mail/outbound-service.ts +48 -30
  168. package/worker/mail/outbound-threading.ts +30 -17
  169. package/worker/mail/outbound.ts +97 -70
  170. package/worker/mail/outgoing-context.ts +25 -15
  171. package/worker/mail/participants.ts +44 -29
  172. package/worker/mail/push-notifications.ts +271 -173
  173. package/worker/mail/quote-parser.ts +21 -10
  174. package/worker/mail/r2-presigned-urls.ts +31 -25
  175. package/worker/mail/receive.ts +23 -11
  176. package/worker/mail/recipients.ts +6 -1
  177. package/worker/mail/remote-proxy.ts +187 -31
  178. package/worker/mail/remote-routes.ts +87 -0
  179. package/worker/mail/remote-token.ts +104 -0
  180. package/worker/mail/repeat.ts +19 -0
  181. package/worker/mail/reply-plan.ts +62 -33
  182. package/worker/mail/rfc.ts +32 -7
  183. package/worker/mail/routes.ts +870 -541
  184. package/worker/mail/safety-routes.ts +328 -200
  185. package/worker/mail/schemas.ts +84 -105
  186. package/worker/mail/search.ts +1 -0
  187. package/worker/mail/security-settings.ts +41 -9
  188. package/worker/mail/sender-intel.ts +206 -51
  189. package/worker/mail/spamhaus.ts +92 -45
  190. package/worker/mail/unsubscribe.ts +76 -18
  191. package/worker/mail/uploads.ts +126 -93
  192. package/worker/mail/webhook-source.ts +35 -12
  193. package/worker/mail/workspace-safety.ts +97 -38
  194. package/worker/mailbox/demo-html.ts +7 -7
  195. package/worker/mailbox/demo-seed.ts +10 -44
  196. package/worker/mailbox/demo.ts +595 -225
  197. package/worker/mailbox/email-index.ts +53 -0
  198. package/worker/mailbox/folder-model.ts +53 -17
  199. package/worker/mailbox/import-placement.ts +88 -0
  200. package/worker/mailbox/inbox-counts.ts +106 -0
  201. package/worker/mailbox/index.ts +1734 -721
  202. package/worker/mailbox/jmap-store.ts +139 -35
  203. package/worker/mailbox/model.ts +8 -6
  204. package/worker/mailbox/office-preview-jobs.ts +56 -25
  205. package/worker/mailbox/schema.ts +87 -66
  206. package/worker/mailbox/senders.ts +176 -70
  207. package/worker/mcp/mail-action-tools.ts +128 -87
  208. package/worker/mcp/mail-compose-tools.ts +133 -82
  209. package/worker/mcp/mail-query-tools.ts +120 -71
  210. package/worker/mcp/mail-safety-tools.ts +321 -248
  211. package/worker/mcp/schemas.ts +43 -0
  212. package/worker/mcp/tools.ts +13 -10
  213. package/worker/module.ts +43 -18
  214. package/worker/sql.d.ts +8 -0
  215. package/alchemy.ts +0 -142
  216. package/containers/office-preview/Dockerfile +0 -10
  217. package/shared/mail-rules.ts +0 -156
  218. package/worker/admin/report-routes.ts +0 -164
  219. package/worker/mail/reported-messages.ts +0 -68
  220. package/worker/mailbox/migrations.ts +0 -10
  221. package/worker/mailbox/rules.ts +0 -110
@@ -1,13 +1,17 @@
1
- import { McpServer } from "@modelcontextprotocol/server";
1
+ import type {
2
+ ScopedToolRegistrar,
3
+ AccountApiClient,
4
+ } from "@open-workspace/core/module";
5
+ import { apiPath, runTool } from "@open-workspace/core/module";
2
6
  import { z } from "zod";
3
- import { AccountApiClient, apiPath } from "@open-workspace/core/worker/mcp/account-client.ts";
4
- import { runTool } from "@open-workspace/core/worker/mcp/results.ts";
7
+
8
+ import { MAIL_ORGANIZE_SCOPE, MAIL_READ_SCOPE } from "../../shared/permissions";
5
9
  import {
6
10
  conversationIdsSchema,
7
11
  entityIdSchema,
8
12
  mailboxIdSchema,
9
13
  mailboxStateSchema,
10
- } from "@open-workspace/core/worker/mcp/schemas.ts";
14
+ } from "./schemas";
11
15
 
12
16
  const write = {
13
17
  readOnlyHint: false,
@@ -16,12 +20,16 @@ const write = {
16
20
  openWorldHint: false,
17
21
  } as const;
18
22
 
19
- export function registerMailActionTools(server: McpServer, api: AccountApiClient) {
20
- server.registerTool(
23
+ export function registerMailActionTools(
24
+ tool: ScopedToolRegistrar,
25
+ api: AccountApiClient
26
+ ) {
27
+ tool(MAIL_ORGANIZE_SCOPE)(
21
28
  "set_message_read",
22
29
  {
23
30
  title: "Mark message read or unread",
24
- description: "Set the current account's personal read state for one message.",
31
+ description:
32
+ "Set the current account's personal read state for one message.",
25
33
  inputSchema: z.object({
26
34
  mailboxId: mailboxIdSchema,
27
35
  messageId: entityIdSchema,
@@ -29,17 +37,23 @@ export function registerMailActionTools(server: McpServer, api: AccountApiClient
29
37
  }),
30
38
  annotations: write,
31
39
  },
32
- ({ mailboxId, messageId, isRead }) => runTool(() => api.json(apiPath(
33
- `/mail/messages/${encodeURIComponent(messageId)}/read`,
34
- { mailboxId },
35
- ), { method: "PATCH", json: { isRead } })),
40
+ ({ mailboxId, messageId, isRead }) =>
41
+ runTool(() =>
42
+ api.json(
43
+ apiPath(`/mail/messages/${encodeURIComponent(messageId)}/read`, {
44
+ mailboxId,
45
+ }),
46
+ { method: "PATCH", json: { isRead } }
47
+ )
48
+ )
36
49
  );
37
50
 
38
- server.registerTool(
51
+ tool(MAIL_ORGANIZE_SCOPE)(
39
52
  "set_conversations_read",
40
53
  {
41
54
  title: "Mark conversations read or unread",
42
- description: "Set the current account's personal read state for all messages in up to 500 conversations.",
55
+ description:
56
+ "Set the current account's personal read state for all messages in up to 500 conversations.",
43
57
  inputSchema: z.object({
44
58
  mailboxId: mailboxIdSchema,
45
59
  conversationIds: conversationIdsSchema,
@@ -47,50 +61,57 @@ export function registerMailActionTools(server: McpServer, api: AccountApiClient
47
61
  }),
48
62
  annotations: write,
49
63
  },
50
- ({ mailboxId, conversationIds, isRead }) => runTool(() => api.json(apiPath(
51
- "/mail/conversations/bulk",
52
- { mailboxId },
53
- ), {
54
- method: "PATCH",
55
- json: { type: "read", conversationIds, isRead },
56
- })),
64
+ ({ mailboxId, conversationIds, isRead }) =>
65
+ runTool(() =>
66
+ api.json(apiPath("/mail/conversations/bulk", { mailboxId }), {
67
+ method: "PATCH",
68
+ json: { type: "read", conversationIds, isRead },
69
+ })
70
+ )
57
71
  );
58
72
 
59
- server.registerTool(
73
+ tool(MAIL_ORGANIZE_SCOPE)(
60
74
  "move_conversations",
61
75
  {
62
76
  title: "Move conversations",
63
- description: "Move up to 500 conversations between Inbox, Archive, Trash, or a custom folder, or out of Spam. To put mail in Spam use report_conversations (or report_message), which also records the report for the spam classifier.",
64
- inputSchema: z.object({
65
- mailboxId: mailboxIdSchema,
66
- conversationIds: conversationIdsSchema,
67
- sourceFolderId: entityIdSchema,
68
- // Spam goes through reporting so the classifier learns from it.
69
- mailboxState: mailboxStateSchema.exclude(["spam"]).optional(),
70
- folderId: entityIdSchema.nullable().optional(),
71
- }).refine(
72
- (value) => value.mailboxState !== undefined || value.folderId !== undefined,
73
- "Provide mailboxState, folderId, or both",
74
- ),
77
+ description:
78
+ "Move up to 500 conversations between Inbox, Archive, Trash, or a custom folder, or out of Spam. To put mail in Spam use report_conversations (or report_message), which also records the report for the spam classifier.",
79
+ inputSchema: z
80
+ .object({
81
+ mailboxId: mailboxIdSchema,
82
+ conversationIds: conversationIdsSchema,
83
+ sourceFolderId: entityIdSchema,
84
+ // Spam goes through reporting so the classifier learns from it.
85
+ mailboxState: mailboxStateSchema.exclude(["spam"]).optional(),
86
+ folderId: entityIdSchema.nullable().optional(),
87
+ })
88
+ .refine(
89
+ (value) =>
90
+ value.mailboxState !== undefined || value.folderId !== undefined,
91
+ "Provide mailboxState, folderId, or both"
92
+ ),
75
93
  annotations: write,
76
94
  },
77
95
  ({ mailboxId, conversationIds, sourceFolderId, mailboxState, folderId }) =>
78
- runTool(() => api.json(apiPath("/mail/conversations/bulk", { mailboxId }), {
79
- method: "PATCH",
80
- json: {
81
- type: "update",
82
- conversationIds,
83
- sourceFolderId,
84
- update: { mailboxState, folderId },
85
- },
86
- })),
96
+ runTool(() =>
97
+ api.json(apiPath("/mail/conversations/bulk", { mailboxId }), {
98
+ method: "PATCH",
99
+ json: {
100
+ type: "update",
101
+ conversationIds,
102
+ sourceFolderId,
103
+ update: { mailboxState, folderId },
104
+ },
105
+ })
106
+ )
87
107
  );
88
108
 
89
- server.registerTool(
109
+ tool(MAIL_ORGANIZE_SCOPE)(
90
110
  "delete_conversations_permanently",
91
111
  {
92
112
  title: "Permanently delete conversations",
93
- description: "Permanently delete up to 500 conversations. Every conversation must already be in Trash.",
113
+ description:
114
+ "Permanently delete up to 500 conversations. Every conversation must already be in Trash.",
94
115
  inputSchema: z.object({
95
116
  mailboxId: mailboxIdSchema,
96
117
  conversationIds: conversationIdsSchema,
@@ -100,33 +121,37 @@ export function registerMailActionTools(server: McpServer, api: AccountApiClient
100
121
  destructiveHint: true,
101
122
  },
102
123
  },
103
- ({ mailboxId, conversationIds }) => runTool(() => api.json(apiPath(
104
- "/mail/conversations/bulk",
105
- { mailboxId },
106
- ), {
107
- method: "PATCH",
108
- json: { type: "delete_permanently", conversationIds },
109
- })),
124
+ ({ mailboxId, conversationIds }) =>
125
+ runTool(() =>
126
+ api.json(apiPath("/mail/conversations/bulk", { mailboxId }), {
127
+ method: "PATCH",
128
+ json: { type: "delete_permanently", conversationIds },
129
+ })
130
+ )
110
131
  );
111
132
 
112
- server.registerTool(
133
+ tool(MAIL_ORGANIZE_SCOPE)(
113
134
  "create_folder",
114
135
  {
115
136
  title: "Create custom folder",
116
- description: "Create a custom folder shared by everyone with access to the mailbox.",
137
+ description:
138
+ "Create a custom folder shared by everyone with access to the mailbox.",
117
139
  inputSchema: z.object({
118
140
  mailboxId: mailboxIdSchema,
119
141
  name: z.string().trim().min(1).max(80),
120
142
  }),
121
143
  annotations: { ...write, idempotentHint: false },
122
144
  },
123
- ({ mailboxId, name }) => runTool(() => api.json(
124
- `/mail/mailboxes/${encodeURIComponent(mailboxId)}/folders`,
125
- { method: "POST", json: { name } },
126
- )),
145
+ ({ mailboxId, name }) =>
146
+ runTool(() =>
147
+ api.json(`/mail/mailboxes/${encodeURIComponent(mailboxId)}/folders`, {
148
+ method: "POST",
149
+ json: { name },
150
+ })
151
+ )
127
152
  );
128
153
 
129
- server.registerTool(
154
+ tool(MAIL_ORGANIZE_SCOPE)(
130
155
  "rename_folder",
131
156
  {
132
157
  title: "Rename custom folder",
@@ -137,13 +162,16 @@ export function registerMailActionTools(server: McpServer, api: AccountApiClient
137
162
  }),
138
163
  annotations: write,
139
164
  },
140
- ({ mailboxId, folderId, name }) => runTool(() => api.json(
141
- `/mail/mailboxes/${encodeURIComponent(mailboxId)}/folders/${encodeURIComponent(folderId)}`,
142
- { method: "PATCH", json: { name } },
143
- )),
165
+ ({ mailboxId, folderId, name }) =>
166
+ runTool(() =>
167
+ api.json(
168
+ `/mail/mailboxes/${encodeURIComponent(mailboxId)}/folders/${encodeURIComponent(folderId)}`,
169
+ { method: "PATCH", json: { name } }
170
+ )
171
+ )
144
172
  );
145
173
 
146
- server.registerTool(
174
+ tool(MAIL_ORGANIZE_SCOPE)(
147
175
  "delete_folder",
148
176
  {
149
177
  title: "Delete custom folder",
@@ -154,57 +182,70 @@ export function registerMailActionTools(server: McpServer, api: AccountApiClient
154
182
  }),
155
183
  annotations: { ...write, destructiveHint: true },
156
184
  },
157
- ({ mailboxId, folderId }) => runTool(() => api.json(
158
- `/mail/mailboxes/${encodeURIComponent(mailboxId)}/folders/${encodeURIComponent(folderId)}`,
159
- { method: "DELETE" },
160
- )),
185
+ ({ mailboxId, folderId }) =>
186
+ runTool(() =>
187
+ api.json(
188
+ `/mail/mailboxes/${encodeURIComponent(mailboxId)}/folders/${encodeURIComponent(folderId)}`,
189
+ { method: "DELETE" }
190
+ )
191
+ )
161
192
  );
162
193
 
163
- server.registerTool(
194
+ tool(MAIL_ORGANIZE_SCOPE)(
164
195
  "reorder_folders",
165
196
  {
166
- title: "Reorder custom folders",
167
- description: "Set the complete order of custom folders in a mailbox.",
197
+ title: "Reorder folders",
198
+ description:
199
+ "Set the complete order of a mailbox's folders: every custom folder and the system folders sent, archive, spam and trash, each once, in display order. Inbox always stays first and is not listed. System folders can be placed among custom ones.",
168
200
  inputSchema: z.object({
169
201
  mailboxId: mailboxIdSchema,
170
- folderIds: z.array(entityIdSchema).max(100),
202
+ folderIds: z.array(entityIdSchema).max(104),
171
203
  }),
172
204
  annotations: write,
173
205
  },
174
- ({ mailboxId, folderIds }) => runTool(() => api.json(
175
- `/mail/mailboxes/${encodeURIComponent(mailboxId)}/folders/order`,
176
- { method: "PUT", json: { folderIds } },
177
- )),
206
+ ({ mailboxId, folderIds }) =>
207
+ runTool(() =>
208
+ api.json(
209
+ `/mail/mailboxes/${encodeURIComponent(mailboxId)}/folders/order`,
210
+ { method: "PUT", json: { folderIds } }
211
+ )
212
+ )
178
213
  );
179
214
 
180
- server.registerTool(
215
+ tool(MAIL_READ_SCOPE)(
181
216
  "get_mailbox_ai_settings",
182
217
  {
183
218
  title: "Get mailbox AI rules",
184
- description: "Read mailbox-level classification instructions and confidence threshold.",
219
+ description:
220
+ "Read mailbox-level classification instructions and confidence threshold.",
185
221
  inputSchema: z.object({ mailboxId: mailboxIdSchema }),
186
222
  annotations: { ...write, readOnlyHint: true },
187
223
  },
188
- ({ mailboxId }) => runTool(() => api.json(
189
- `/mail/mailboxes/${encodeURIComponent(mailboxId)}/ai`,
190
- )),
224
+ ({ mailboxId }) =>
225
+ runTool(() =>
226
+ api.json(`/mail/mailboxes/${encodeURIComponent(mailboxId)}/ai`)
227
+ )
191
228
  );
192
229
 
193
- server.registerTool(
230
+ tool(MAIL_ORGANIZE_SCOPE)(
194
231
  "update_mailbox_ai_settings",
195
232
  {
196
233
  title: "Update mailbox AI rules",
197
- description: "Update mailbox-level classification instructions and confidence threshold.",
234
+ description:
235
+ "Update mailbox-level classification instructions and confidence threshold.",
198
236
  inputSchema: z.object({
199
237
  mailboxId: mailboxIdSchema,
200
- instructions: z.string().trim().max(4_000),
238
+ instructions: z.string().trim().max(4000),
201
239
  confidenceThreshold: z.number().int().min(50).max(100),
202
240
  }),
203
241
  annotations: write,
204
242
  },
205
- ({ mailboxId, instructions, confidenceThreshold }) => runTool(() => api.json(
206
- `/mail/mailboxes/${encodeURIComponent(mailboxId)}/ai`,
207
- { method: "PUT", json: { instructions, confidenceThreshold } },
208
- )),
243
+ ({ mailboxId, instructions, confidenceThreshold }) =>
244
+ runTool(() =>
245
+ api.json(`/mail/mailboxes/${encodeURIComponent(mailboxId)}/ai`, {
246
+ method: "PUT",
247
+ json: { instructions, confidenceThreshold },
248
+ })
249
+ )
209
250
  );
210
251
  }
@@ -1,13 +1,16 @@
1
- import { McpServer } from "@modelcontextprotocol/server";
1
+ import type {
2
+ ScopedToolRegistrar,
3
+ AccountApiClient,
4
+ } from "@open-workspace/core/module";
5
+ import { emailSchema, apiPath, runTool } from "@open-workspace/core/module";
2
6
  import { z } from "zod";
7
+
3
8
  import {
4
9
  MAX_COMPOSER_ATTACHMENT_BYTES,
5
10
  MAX_MAIL_RECIPIENTS,
6
11
  } from "../../shared/mail";
7
- import { emailSchema } from "@open-workspace/core/worker/auth/schemas.ts";
12
+ import { MAIL_SEND_SCOPE } from "../../shared/permissions";
8
13
  import { dedupeRecipientFields, recipientCount } from "../mail/recipients";
9
- import { AccountApiClient, apiPath } from "@open-workspace/core/worker/mcp/account-client.ts";
10
- import { runTool } from "@open-workspace/core/worker/mcp/results.ts";
11
14
  import {
12
15
  entityIdSchema,
13
16
  mailboxIdSchema,
@@ -15,7 +18,7 @@ import {
15
18
  recipientFields,
16
19
  replyModeSchema,
17
20
  requestId,
18
- } from "@open-workspace/core/worker/mcp/schemas.ts";
21
+ } from "./schemas";
19
22
 
20
23
  const sendAnnotations = {
21
24
  readOnlyHint: false,
@@ -24,44 +27,61 @@ const sendAnnotations = {
24
27
  openWorldHint: true,
25
28
  } as const;
26
29
 
27
- const composeInputSchema = z.object({
28
- mailboxId: mailboxIdSchema,
29
- ...recipientFields,
30
- replyTo: emailSchema.optional(),
31
- subject: z.string().trim().max(998).default(""),
32
- ...messageContentFields,
33
- }).refine(
34
- (value) => recipientCount(dedupeRecipientFields(value)) > 0,
35
- "Add at least one recipient",
36
- ).refine(
37
- (value) => recipientCount(dedupeRecipientFields(value)) <= MAX_MAIL_RECIPIENTS,
38
- `An email can have at most ${MAX_MAIL_RECIPIENTS} recipients`,
39
- );
30
+ const composeInputSchema = z
31
+ .object({
32
+ mailboxId: mailboxIdSchema,
33
+ ...recipientFields,
34
+ replyTo: emailSchema.optional(),
35
+ subject: z.string().trim().max(998).default(""),
36
+ ...messageContentFields,
37
+ })
38
+ .refine(
39
+ (value) => recipientCount(dedupeRecipientFields(value)) > 0,
40
+ "Add at least one recipient"
41
+ )
42
+ .refine(
43
+ (value) =>
44
+ recipientCount(dedupeRecipientFields(value)) <= MAX_MAIL_RECIPIENTS,
45
+ `An email can have at most ${MAX_MAIL_RECIPIENTS} recipients`
46
+ );
40
47
 
41
- const forwardInputSchema = z.object({
42
- mailboxId: mailboxIdSchema,
43
- sourceMessageId: entityIdSchema,
44
- ...recipientFields,
45
- replyTo: emailSchema.optional(),
46
- ...messageContentFields,
47
- }).refine(
48
- (value) => recipientCount(dedupeRecipientFields(value)) > 0,
49
- "Add at least one recipient",
50
- ).refine(
51
- (value) => recipientCount(dedupeRecipientFields(value)) <= MAX_MAIL_RECIPIENTS,
52
- `An email can have at most ${MAX_MAIL_RECIPIENTS} recipients`,
53
- );
48
+ const forwardInputSchema = z
49
+ .object({
50
+ mailboxId: mailboxIdSchema,
51
+ sourceMessageId: entityIdSchema,
52
+ ...recipientFields,
53
+ replyTo: emailSchema.optional(),
54
+ ...messageContentFields,
55
+ })
56
+ .refine(
57
+ (value) => recipientCount(dedupeRecipientFields(value)) > 0,
58
+ "Add at least one recipient"
59
+ )
60
+ .refine(
61
+ (value) =>
62
+ recipientCount(dedupeRecipientFields(value)) <= MAX_MAIL_RECIPIENTS,
63
+ `An email can have at most ${MAX_MAIL_RECIPIENTS} recipients`
64
+ );
54
65
 
55
- export function registerMailComposeTools(server: McpServer, api: AccountApiClient) {
56
- server.registerTool(
66
+ export function registerMailComposeTools(
67
+ tool: ScopedToolRegistrar,
68
+ api: AccountApiClient
69
+ ) {
70
+ tool(MAIL_SEND_SCOPE)(
57
71
  "create_attachment_upload",
58
72
  {
59
73
  title: "Create attachment upload",
60
- description: "Create a 15-minute presigned R2 PUT URL. Upload exactly the declared number of bytes to upload.uploadUrl using upload.headers, then call complete_attachment_upload with upload.id.",
74
+ description:
75
+ "Create a 15-minute presigned R2 PUT URL. Upload exactly the declared number of bytes to upload.uploadUrl using upload.headers, then call complete_attachment_upload with upload.id.",
61
76
  inputSchema: z.object({
62
77
  mailboxId: mailboxIdSchema,
63
78
  filename: z.string().trim().min(1).max(255),
64
- contentType: z.string().trim().min(1).max(255).default("application/octet-stream"),
79
+ contentType: z
80
+ .string()
81
+ .trim()
82
+ .min(1)
83
+ .max(255)
84
+ .default("application/octet-stream"),
65
85
  size: z.number().int().positive().max(MAX_COMPOSER_ATTACHMENT_BYTES),
66
86
  }),
67
87
  annotations: {
@@ -71,19 +91,21 @@ export function registerMailComposeTools(server: McpServer, api: AccountApiClien
71
91
  openWorldHint: false,
72
92
  },
73
93
  },
74
- ({ mailboxId, filename, contentType, size }) => runTool(() =>
75
- api.json(apiPath("/mail/uploads", { mailboxId, directOnly: true }), {
76
- method: "POST",
77
- json: { filename, contentType, size },
78
- })
79
- ),
94
+ ({ mailboxId, filename, contentType, size }) =>
95
+ runTool(() =>
96
+ api.json(apiPath("/mail/uploads", { mailboxId }), {
97
+ method: "POST",
98
+ json: { filename, contentType, size },
99
+ })
100
+ )
80
101
  );
81
102
 
82
- server.registerTool(
103
+ tool(MAIL_SEND_SCOPE)(
83
104
  "complete_attachment_upload",
84
105
  {
85
106
  title: "Complete attachment upload",
86
- description: "Verify and seal a file uploaded with create_attachment_upload. Use the returned upload id in send_email, reply_to_email, or forward_email attachments.",
107
+ description:
108
+ "Verify and seal a file uploaded with create_attachment_upload. Use the returned upload id in send_email, reply_to_email, or forward_email attachments.",
87
109
  inputSchema: z.object({
88
110
  mailboxId: mailboxIdSchema,
89
111
  uploadId: entityIdSchema,
@@ -95,13 +117,18 @@ export function registerMailComposeTools(server: McpServer, api: AccountApiClien
95
117
  openWorldHint: false,
96
118
  },
97
119
  },
98
- ({ mailboxId, uploadId }) => runTool(() => api.json(apiPath(
99
- `/mail/uploads/${encodeURIComponent(uploadId)}/complete`,
100
- { mailboxId },
101
- ), { method: "POST" })),
120
+ ({ mailboxId, uploadId }) =>
121
+ runTool(() =>
122
+ api.json(
123
+ apiPath(`/mail/uploads/${encodeURIComponent(uploadId)}/complete`, {
124
+ mailboxId,
125
+ }),
126
+ { method: "POST" }
127
+ )
128
+ )
102
129
  );
103
130
 
104
- server.registerTool(
131
+ tool(MAIL_SEND_SCOPE)(
105
132
  "discard_attachment_upload",
106
133
  {
107
134
  title: "Discard attachment upload",
@@ -116,31 +143,41 @@ export function registerMailComposeTools(server: McpServer, api: AccountApiClien
116
143
  openWorldHint: false,
117
144
  },
118
145
  },
119
- ({ mailboxId, uploadId }) => runTool(() => api.json(apiPath(
120
- `/mail/uploads/${encodeURIComponent(uploadId)}`,
121
- { mailboxId },
122
- ), { method: "DELETE" })),
146
+ ({ mailboxId, uploadId }) =>
147
+ runTool(() =>
148
+ api.json(
149
+ apiPath(`/mail/uploads/${encodeURIComponent(uploadId)}`, {
150
+ mailboxId,
151
+ }),
152
+ { method: "DELETE" }
153
+ )
154
+ )
123
155
  );
124
156
 
125
- server.registerTool(
157
+ tool(MAIL_SEND_SCOPE)(
126
158
  "send_email",
127
159
  {
128
160
  title: "Send email",
129
- description: "Compose and send a new email from a mailbox where the current account has send access.",
161
+ description:
162
+ "Compose and send a new email from a mailbox where the current account has send access.",
130
163
  inputSchema: composeInputSchema,
131
164
  annotations: sendAnnotations,
132
165
  },
133
- (input) => runTool(() => api.json("/mail/messages", {
134
- method: "POST",
135
- json: { ...input, requestId: requestId(input.requestId) },
136
- })),
166
+ (input) =>
167
+ runTool(() =>
168
+ api.json("/mail/messages", {
169
+ method: "POST",
170
+ json: { ...input, requestId: requestId(input.requestId) },
171
+ })
172
+ )
137
173
  );
138
174
 
139
- server.registerTool(
175
+ tool(MAIL_SEND_SCOPE)(
140
176
  "reply_to_email",
141
177
  {
142
178
  title: "Reply to email",
143
- description: "Reply, reply all, continue a draft-like outgoing thread, or reply to a mailing list.",
179
+ description:
180
+ "Reply, reply all, continue a draft-like outgoing thread, or reply to a mailing list.",
144
181
  inputSchema: z.object({
145
182
  mailboxId: mailboxIdSchema,
146
183
  sourceMessageId: entityIdSchema,
@@ -151,46 +188,60 @@ export function registerMailComposeTools(server: McpServer, api: AccountApiClien
151
188
  }),
152
189
  annotations: sendAnnotations,
153
190
  },
154
- ({ sourceMessageId, ...input }) => runTool(() => api.json(
155
- `/mail/messages/${encodeURIComponent(sourceMessageId)}/replies`,
156
- {
157
- method: "POST",
158
- json: { ...input, requestId: requestId(input.requestId) },
159
- },
160
- )),
191
+ ({ sourceMessageId, ...input }) =>
192
+ runTool(() =>
193
+ api.json(
194
+ `/mail/messages/${encodeURIComponent(sourceMessageId)}/replies`,
195
+ {
196
+ method: "POST",
197
+ json: { ...input, requestId: requestId(input.requestId) },
198
+ }
199
+ )
200
+ )
161
201
  );
162
202
 
163
- server.registerTool(
203
+ tool(MAIL_SEND_SCOPE)(
164
204
  "forward_email",
165
205
  {
166
206
  title: "Forward email",
167
- description: "Forward a message, including its original attachments, to new recipients.",
207
+ description:
208
+ "Forward a message, including its original attachments, to new recipients.",
168
209
  inputSchema: forwardInputSchema,
169
210
  annotations: sendAnnotations,
170
211
  },
171
- ({ sourceMessageId, ...input }) => runTool(() => api.json(
172
- `/mail/messages/${encodeURIComponent(sourceMessageId)}/forward`,
173
- {
174
- method: "POST",
175
- json: { ...input, requestId: requestId(input.requestId) },
176
- },
177
- )),
212
+ ({ sourceMessageId, ...input }) =>
213
+ runTool(() =>
214
+ api.json(
215
+ `/mail/messages/${encodeURIComponent(sourceMessageId)}/forward`,
216
+ {
217
+ method: "POST",
218
+ json: { ...input, requestId: requestId(input.requestId) },
219
+ }
220
+ )
221
+ )
178
222
  );
179
223
 
180
- server.registerTool(
224
+ tool(MAIL_SEND_SCOPE)(
181
225
  "send_email_again",
182
226
  {
183
227
  title: "Retry failed email",
184
- description: "Retry an outgoing message whose delivery is failed or unconfirmed.",
228
+ description:
229
+ "Retry an outgoing message whose delivery is failed or unconfirmed.",
185
230
  inputSchema: z.object({
186
231
  mailboxId: mailboxIdSchema,
187
232
  messageId: entityIdSchema,
188
233
  }),
189
234
  annotations: sendAnnotations,
190
235
  },
191
- ({ mailboxId, messageId }) => runTool(() => api.json(apiPath(
192
- `/mail/messages/${encodeURIComponent(messageId)}/send-again`,
193
- { mailboxId },
194
- ), { method: "POST" })),
236
+ ({ mailboxId, messageId }) =>
237
+ runTool(() =>
238
+ api.json(
239
+ apiPath(
240
+ `/mail/messages/${encodeURIComponent(messageId)}/send-again`,
241
+ { mailboxId }
242
+ ),
243
+ { method: "POST" }
244
+ )
245
+ )
195
246
  );
196
247
  }