@node2flow/gmail-mcp 1.0.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.
- package/Dockerfile +10 -0
- package/LICENSE +21 -0
- package/README.md +166 -0
- package/dist/gmail-client.d.ts +161 -0
- package/dist/gmail-client.d.ts.map +1 -0
- package/dist/gmail-client.js +365 -0
- package/dist/gmail-client.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +167 -0
- package/dist/index.js.map +1 -0
- package/dist/server.d.ts +13 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +307 -0
- package/dist/server.js.map +1 -0
- package/dist/tools.d.ts +6 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +468 -0
- package/dist/tools.js.map +1 -0
- package/dist/types.d.ts +184 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/dist/worker.d.ts +8 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +69 -0
- package/dist/worker.js.map +1 -0
- package/docker-compose.yml +9 -0
- package/package.json +51 -0
- package/smithery-config.json +18 -0
- package/src/gmail-client.ts +507 -0
- package/src/index.ts +184 -0
- package/src/server.ts +353 -0
- package/src/tools.ts +474 -0
- package/src/types.ts +238 -0
- package/src/worker.ts +88 -0
- package/tsconfig.json +18 -0
- package/wrangler.toml +14 -0
package/src/server.ts
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared MCP Server — used by both Node.js (index.ts) and CF Worker (worker.ts)
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
6
|
+
import { ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
7
|
+
import { GmailClient } from './gmail-client.js';
|
|
8
|
+
import { TOOLS } from './tools.js';
|
|
9
|
+
|
|
10
|
+
export interface GmailMcpConfig {
|
|
11
|
+
clientId: string;
|
|
12
|
+
clientSecret: string;
|
|
13
|
+
refreshToken: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function handleToolCall(
|
|
17
|
+
toolName: string,
|
|
18
|
+
args: Record<string, unknown>,
|
|
19
|
+
client: GmailClient
|
|
20
|
+
) {
|
|
21
|
+
switch (toolName) {
|
|
22
|
+
// ========== Messages ==========
|
|
23
|
+
case 'gmail_list_messages':
|
|
24
|
+
return client.listMessages({
|
|
25
|
+
q: args.q as string | undefined,
|
|
26
|
+
labelIds: args.label_ids as string[] | undefined,
|
|
27
|
+
maxResults: args.max_results as number | undefined,
|
|
28
|
+
pageToken: args.page_token as string | undefined,
|
|
29
|
+
includeSpamTrash: args.include_spam_trash as boolean | undefined,
|
|
30
|
+
});
|
|
31
|
+
case 'gmail_get_message':
|
|
32
|
+
return client.getMessage({
|
|
33
|
+
id: args.id as string,
|
|
34
|
+
format: args.format as string | undefined,
|
|
35
|
+
metadataHeaders: args.metadata_headers as string[] | undefined,
|
|
36
|
+
});
|
|
37
|
+
case 'gmail_send_message':
|
|
38
|
+
return client.sendMessage({
|
|
39
|
+
to: args.to as string,
|
|
40
|
+
subject: args.subject as string,
|
|
41
|
+
body: args.body as string,
|
|
42
|
+
cc: args.cc as string | undefined,
|
|
43
|
+
bcc: args.bcc as string | undefined,
|
|
44
|
+
html: args.html as string | undefined,
|
|
45
|
+
in_reply_to: args.in_reply_to as string | undefined,
|
|
46
|
+
references: args.references as string | undefined,
|
|
47
|
+
thread_id: args.thread_id as string | undefined,
|
|
48
|
+
});
|
|
49
|
+
case 'gmail_delete_message':
|
|
50
|
+
return client.deleteMessage({ id: args.id as string });
|
|
51
|
+
case 'gmail_trash_message':
|
|
52
|
+
return client.trashMessage({ id: args.id as string });
|
|
53
|
+
case 'gmail_untrash_message':
|
|
54
|
+
return client.untrashMessage({ id: args.id as string });
|
|
55
|
+
case 'gmail_modify_message':
|
|
56
|
+
return client.modifyMessage({
|
|
57
|
+
id: args.id as string,
|
|
58
|
+
addLabelIds: args.add_label_ids as string[] | undefined,
|
|
59
|
+
removeLabelIds: args.remove_label_ids as string[] | undefined,
|
|
60
|
+
});
|
|
61
|
+
case 'gmail_batch_delete':
|
|
62
|
+
return client.batchDeleteMessages({
|
|
63
|
+
ids: args.ids as string[],
|
|
64
|
+
});
|
|
65
|
+
case 'gmail_batch_modify':
|
|
66
|
+
return client.batchModifyMessages({
|
|
67
|
+
ids: args.ids as string[],
|
|
68
|
+
addLabelIds: args.add_label_ids as string[] | undefined,
|
|
69
|
+
removeLabelIds: args.remove_label_ids as string[] | undefined,
|
|
70
|
+
});
|
|
71
|
+
case 'gmail_get_attachment':
|
|
72
|
+
return client.getAttachment({
|
|
73
|
+
messageId: args.message_id as string,
|
|
74
|
+
attachmentId: args.attachment_id as string,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// ========== Drafts ==========
|
|
78
|
+
case 'gmail_list_drafts':
|
|
79
|
+
return client.listDrafts({
|
|
80
|
+
maxResults: args.max_results as number | undefined,
|
|
81
|
+
pageToken: args.page_token as string | undefined,
|
|
82
|
+
q: args.q as string | undefined,
|
|
83
|
+
});
|
|
84
|
+
case 'gmail_get_draft':
|
|
85
|
+
return client.getDraft({
|
|
86
|
+
id: args.id as string,
|
|
87
|
+
format: args.format as string | undefined,
|
|
88
|
+
});
|
|
89
|
+
case 'gmail_create_draft':
|
|
90
|
+
return client.createDraft({
|
|
91
|
+
to: args.to as string,
|
|
92
|
+
subject: args.subject as string,
|
|
93
|
+
body: args.body as string,
|
|
94
|
+
cc: args.cc as string | undefined,
|
|
95
|
+
bcc: args.bcc as string | undefined,
|
|
96
|
+
html: args.html as string | undefined,
|
|
97
|
+
thread_id: args.thread_id as string | undefined,
|
|
98
|
+
});
|
|
99
|
+
case 'gmail_update_draft':
|
|
100
|
+
return client.updateDraft({
|
|
101
|
+
id: args.id as string,
|
|
102
|
+
to: args.to as string,
|
|
103
|
+
subject: args.subject as string,
|
|
104
|
+
body: args.body as string,
|
|
105
|
+
cc: args.cc as string | undefined,
|
|
106
|
+
bcc: args.bcc as string | undefined,
|
|
107
|
+
html: args.html as string | undefined,
|
|
108
|
+
thread_id: args.thread_id as string | undefined,
|
|
109
|
+
});
|
|
110
|
+
case 'gmail_delete_draft':
|
|
111
|
+
return client.deleteDraft({ id: args.id as string });
|
|
112
|
+
case 'gmail_send_draft':
|
|
113
|
+
return client.sendDraft({ id: args.id as string });
|
|
114
|
+
|
|
115
|
+
// ========== Labels ==========
|
|
116
|
+
case 'gmail_list_labels':
|
|
117
|
+
return client.listLabels();
|
|
118
|
+
case 'gmail_get_label':
|
|
119
|
+
return client.getLabel({ id: args.id as string });
|
|
120
|
+
case 'gmail_create_label':
|
|
121
|
+
return client.createLabel({
|
|
122
|
+
name: args.name as string,
|
|
123
|
+
messageListVisibility: args.message_list_visibility as string | undefined,
|
|
124
|
+
labelListVisibility: args.label_list_visibility as string | undefined,
|
|
125
|
+
backgroundColor: args.background_color as string | undefined,
|
|
126
|
+
textColor: args.text_color as string | undefined,
|
|
127
|
+
});
|
|
128
|
+
case 'gmail_update_label':
|
|
129
|
+
return client.updateLabel({
|
|
130
|
+
id: args.id as string,
|
|
131
|
+
name: args.name as string | undefined,
|
|
132
|
+
messageListVisibility: args.message_list_visibility as string | undefined,
|
|
133
|
+
labelListVisibility: args.label_list_visibility as string | undefined,
|
|
134
|
+
backgroundColor: args.background_color as string | undefined,
|
|
135
|
+
textColor: args.text_color as string | undefined,
|
|
136
|
+
});
|
|
137
|
+
case 'gmail_delete_label':
|
|
138
|
+
return client.deleteLabel({ id: args.id as string });
|
|
139
|
+
|
|
140
|
+
// ========== Threads ==========
|
|
141
|
+
case 'gmail_list_threads':
|
|
142
|
+
return client.listThreads({
|
|
143
|
+
q: args.q as string | undefined,
|
|
144
|
+
labelIds: args.label_ids as string[] | undefined,
|
|
145
|
+
maxResults: args.max_results as number | undefined,
|
|
146
|
+
pageToken: args.page_token as string | undefined,
|
|
147
|
+
includeSpamTrash: args.include_spam_trash as boolean | undefined,
|
|
148
|
+
});
|
|
149
|
+
case 'gmail_get_thread':
|
|
150
|
+
return client.getThread({
|
|
151
|
+
id: args.id as string,
|
|
152
|
+
format: args.format as string | undefined,
|
|
153
|
+
});
|
|
154
|
+
case 'gmail_modify_thread':
|
|
155
|
+
return client.modifyThread({
|
|
156
|
+
id: args.id as string,
|
|
157
|
+
addLabelIds: args.add_label_ids as string[] | undefined,
|
|
158
|
+
removeLabelIds: args.remove_label_ids as string[] | undefined,
|
|
159
|
+
});
|
|
160
|
+
case 'gmail_trash_thread':
|
|
161
|
+
return client.trashThread({ id: args.id as string });
|
|
162
|
+
case 'gmail_untrash_thread':
|
|
163
|
+
return client.untrashThread({ id: args.id as string });
|
|
164
|
+
|
|
165
|
+
// ========== Settings ==========
|
|
166
|
+
case 'gmail_get_profile':
|
|
167
|
+
return client.getProfile();
|
|
168
|
+
case 'gmail_update_vacation':
|
|
169
|
+
return client.updateVacation({
|
|
170
|
+
enableAutoReply: args.enable_auto_reply as boolean,
|
|
171
|
+
responseSubject: args.response_subject as string | undefined,
|
|
172
|
+
responseBodyPlainText: args.response_body_plain_text as string | undefined,
|
|
173
|
+
responseBodyHtml: args.response_body_html as string | undefined,
|
|
174
|
+
restrictToContacts: args.restrict_to_contacts as boolean | undefined,
|
|
175
|
+
restrictToDomain: args.restrict_to_domain as boolean | undefined,
|
|
176
|
+
startTime: args.start_time as string | undefined,
|
|
177
|
+
endTime: args.end_time as string | undefined,
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
default:
|
|
181
|
+
throw new Error(`Unknown tool: ${toolName}`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function createServer(config?: GmailMcpConfig) {
|
|
186
|
+
const server = new McpServer({
|
|
187
|
+
name: 'gmail-mcp',
|
|
188
|
+
version: '1.0.0',
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
let client: GmailClient | null = null;
|
|
192
|
+
|
|
193
|
+
for (const tool of TOOLS) {
|
|
194
|
+
server.registerTool(
|
|
195
|
+
tool.name,
|
|
196
|
+
{
|
|
197
|
+
description: tool.description,
|
|
198
|
+
inputSchema: tool.inputSchema as any,
|
|
199
|
+
annotations: tool.annotations,
|
|
200
|
+
},
|
|
201
|
+
async (args: Record<string, unknown>) => {
|
|
202
|
+
const clientId =
|
|
203
|
+
config?.clientId ||
|
|
204
|
+
(args as Record<string, unknown>).GOOGLE_CLIENT_ID as string;
|
|
205
|
+
const clientSecret =
|
|
206
|
+
config?.clientSecret ||
|
|
207
|
+
(args as Record<string, unknown>).GOOGLE_CLIENT_SECRET as string;
|
|
208
|
+
const refreshToken =
|
|
209
|
+
config?.refreshToken ||
|
|
210
|
+
(args as Record<string, unknown>).GOOGLE_REFRESH_TOKEN as string;
|
|
211
|
+
|
|
212
|
+
if (!clientId || !clientSecret || !refreshToken) {
|
|
213
|
+
return {
|
|
214
|
+
content: [{ type: 'text' as const, text: 'Error: GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, and GOOGLE_REFRESH_TOKEN are all required.' }],
|
|
215
|
+
isError: true,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (!client || config?.clientId !== clientId) {
|
|
220
|
+
client = new GmailClient({ clientId, clientSecret, refreshToken });
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
try {
|
|
224
|
+
const result = await handleToolCall(tool.name, args, client);
|
|
225
|
+
const text = result === undefined ? '{"success": true}' : JSON.stringify(result, null, 2);
|
|
226
|
+
return {
|
|
227
|
+
content: [{ type: 'text' as const, text }],
|
|
228
|
+
isError: false,
|
|
229
|
+
};
|
|
230
|
+
} catch (error) {
|
|
231
|
+
return {
|
|
232
|
+
content: [{ type: 'text' as const, text: `Error: ${error instanceof Error ? error.message : String(error)}` }],
|
|
233
|
+
isError: true,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Register prompts
|
|
241
|
+
server.prompt(
|
|
242
|
+
'compose-and-send',
|
|
243
|
+
'Guide for composing and sending emails, managing drafts',
|
|
244
|
+
async () => ({
|
|
245
|
+
messages: [{
|
|
246
|
+
role: 'user' as const,
|
|
247
|
+
content: {
|
|
248
|
+
type: 'text' as const,
|
|
249
|
+
text: [
|
|
250
|
+
'You are a Gmail email assistant.',
|
|
251
|
+
'',
|
|
252
|
+
'Sending emails:',
|
|
253
|
+
'1. **Send directly** — gmail_send_message with to, subject, body',
|
|
254
|
+
'2. **HTML emails** — Include html parameter for rich formatting',
|
|
255
|
+
'3. **Reply to thread** — Set thread_id, in_reply_to (Message-ID header), and references',
|
|
256
|
+
'4. **CC/BCC** — Comma-separate multiple addresses',
|
|
257
|
+
'',
|
|
258
|
+
'Working with drafts:',
|
|
259
|
+
'1. **Create draft** — gmail_create_draft (same params as send)',
|
|
260
|
+
'2. **Update draft** — gmail_update_draft replaces the entire draft',
|
|
261
|
+
'3. **Send draft** — gmail_send_draft with the draft ID',
|
|
262
|
+
'4. **List drafts** — gmail_list_drafts to see all drafts',
|
|
263
|
+
'',
|
|
264
|
+
'Tips:',
|
|
265
|
+
'- For replies, always get the original message first to extract Message-ID and thread_id',
|
|
266
|
+
'- Use gmail_get_thread to see the full conversation before replying',
|
|
267
|
+
'- HTML body is sent alongside plain text as multipart/alternative',
|
|
268
|
+
].join('\n'),
|
|
269
|
+
},
|
|
270
|
+
}],
|
|
271
|
+
}),
|
|
272
|
+
);
|
|
273
|
+
|
|
274
|
+
server.prompt(
|
|
275
|
+
'search-and-organize',
|
|
276
|
+
'Guide for searching emails, managing labels, and organizing the mailbox',
|
|
277
|
+
async () => ({
|
|
278
|
+
messages: [{
|
|
279
|
+
role: 'user' as const,
|
|
280
|
+
content: {
|
|
281
|
+
type: 'text' as const,
|
|
282
|
+
text: [
|
|
283
|
+
'You are a Gmail organization assistant.',
|
|
284
|
+
'',
|
|
285
|
+
'Search syntax (q parameter):',
|
|
286
|
+
'- from:user@example.com — Messages from a sender',
|
|
287
|
+
'- to:user@example.com — Messages to a recipient',
|
|
288
|
+
'- subject:"meeting notes" — Subject contains text',
|
|
289
|
+
'- has:attachment — Messages with attachments',
|
|
290
|
+
'- is:unread / is:starred / is:important',
|
|
291
|
+
'- label:custom-label — Messages with a specific label',
|
|
292
|
+
'- after:2026/01/01 / before:2026/12/31 — Date range',
|
|
293
|
+
'- newer_than:2d / older_than:1y — Relative dates',
|
|
294
|
+
'- filename:pdf — Attachments by type',
|
|
295
|
+
'- Combine: "from:boss@company.com has:attachment is:unread"',
|
|
296
|
+
'',
|
|
297
|
+
'Organizing with labels:',
|
|
298
|
+
'1. **List labels** — gmail_list_labels to see all labels',
|
|
299
|
+
'2. **Create label** — gmail_create_label with nested support ("Projects/Active")',
|
|
300
|
+
'3. **Apply labels** — gmail_modify_message to add/remove labels',
|
|
301
|
+
'4. **Mark as read** — Remove "UNREAD" label',
|
|
302
|
+
'5. **Star message** — Add "STARRED" label',
|
|
303
|
+
'6. **Batch operations** — gmail_batch_modify for bulk label changes',
|
|
304
|
+
'',
|
|
305
|
+
'System labels: INBOX, SENT, DRAFT, SPAM, TRASH, UNREAD, STARRED, IMPORTANT, CATEGORY_PERSONAL, CATEGORY_SOCIAL, CATEGORY_PROMOTIONS, CATEGORY_UPDATES, CATEGORY_FORUMS',
|
|
306
|
+
].join('\n'),
|
|
307
|
+
},
|
|
308
|
+
}],
|
|
309
|
+
}),
|
|
310
|
+
);
|
|
311
|
+
|
|
312
|
+
// Register resource
|
|
313
|
+
server.resource(
|
|
314
|
+
'server-info',
|
|
315
|
+
'gmail://server-info',
|
|
316
|
+
{
|
|
317
|
+
description: 'Connection status and available tools for this Gmail MCP server',
|
|
318
|
+
mimeType: 'application/json',
|
|
319
|
+
},
|
|
320
|
+
async () => ({
|
|
321
|
+
contents: [{
|
|
322
|
+
uri: 'gmail://server-info',
|
|
323
|
+
mimeType: 'application/json',
|
|
324
|
+
text: JSON.stringify({
|
|
325
|
+
name: 'gmail-mcp',
|
|
326
|
+
version: '1.0.0',
|
|
327
|
+
connected: !!config,
|
|
328
|
+
has_oauth: !!(config?.clientId),
|
|
329
|
+
tools_available: TOOLS.length,
|
|
330
|
+
tool_categories: {
|
|
331
|
+
messages: 10,
|
|
332
|
+
drafts: 6,
|
|
333
|
+
labels: 5,
|
|
334
|
+
threads: 5,
|
|
335
|
+
settings: 2,
|
|
336
|
+
},
|
|
337
|
+
}, null, 2),
|
|
338
|
+
}],
|
|
339
|
+
}),
|
|
340
|
+
);
|
|
341
|
+
|
|
342
|
+
// Override tools/list handler to return raw JSON Schema with property descriptions
|
|
343
|
+
(server as any).server.setRequestHandler(ListToolsRequestSchema, () => ({
|
|
344
|
+
tools: TOOLS.map(tool => ({
|
|
345
|
+
name: tool.name,
|
|
346
|
+
description: tool.description,
|
|
347
|
+
inputSchema: tool.inputSchema,
|
|
348
|
+
annotations: tool.annotations,
|
|
349
|
+
})),
|
|
350
|
+
}));
|
|
351
|
+
|
|
352
|
+
return server;
|
|
353
|
+
}
|