Class: Gmail::MCPServer

Inherits:
Object
  • Object
show all
Defined in:
lib/utilities/gmail/mcp.rb

Instance Method Summary collapse

Constructor Details

#initializeMCPServer

Returns a new instance of MCPServer.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/utilities/gmail/mcp.rb', line 10

def initialize
  @prompts = [
    {
      name: "check_email",
      description: "Check your email inbox for new messages",
      arguments: [
        {
          name: "filter",
          description: "Optional filter (e.g., 'unread', 'from:[email protected]', 'subject:urgent')",
          required: false
        }
      ]
    },
    {
      name: "compose_email",
      description: "Compose and send a new email",
      arguments: [
        {
          name: "to",
          description: "Recipient email address",
          required: true
        },
        {
          name: "subject",
          description: "Email subject line",
          required: true
        },
        {
          name: "body",
          description: "Email body content",
          required: true
        }
      ]
    },
    {
      name: "email_search",
      description: "Search through your emails for specific content",
      arguments: [
        {
          name: "query",
          description: "Search query (e.g., keywords, sender, subject filters)",
          required: true
        }
      ]
    },
    {
      name: "email_management",
      description: "Manage emails (mark as read/unread, archive, label, etc.)",
      arguments: [
        {
          name: "action",
          description: "Action to perform (read, unread, archive, trash, label)",
          required: true
        },
        {
          name: "email_id",
          description: "ID of the email to manage",
          required: true
        }
      ]
    }
  ]

  @tools = {
    "test_connection" => {
      name: "test_connection",
      description: "Test the Gmail API connection",
      inputSchema: {
        type: "object",
        properties: {},
        required: []
      }
    },
    "list_emails" => {
      name: "list_emails",
      description: "List emails with filtering by date range, sender, subject, labels, read/unread status",
      inputSchema: {
        type: "object",
        properties: {
          start_date: {
            type: "string",
            description: "Start date in YYYY-MM-DD format"
          },
          end_date: {
            type: "string",
            description: "End date in YYYY-MM-DD format"
          },
          max_results: {
            type: "number",
            description: "Maximum number of emails to return (default: 20)"
          },
          sender: {
            type: "string",
            description: "Filter by sender email address"
          },
          subject: {
            type: "string",
            description: "Filter by subject content"
          },
          labels: {
            type: "string",
            description: "Filter by labels (comma-separated)"
          },
          read_status: {
            type: "string",
            description: "Filter by read status (read/unread)"
          }
        },
        required: []
      }
    },
    "search_emails" => {
      name: "search_emails",
      description: "Search emails using Gmail search syntax",
      inputSchema: {
        type: "object",
        properties: {
          query: {
            type: "string",
            description: "Search query using Gmail search syntax"
          },
          max_results: {
            type: "number",
            description: "Maximum number of emails to return (default: 10)"
          }
        },
        required: ["query"]
      }
    },
    "get_email_content" => {
      name: "get_email_content",
      description: "Get full content of a specific email including body, headers, and attachments info",
      inputSchema: {
        type: "object",
        properties: {
          email_id: {
            type: "string",
            description: "Gmail message ID"
          }
        },
        required: ["email_id"]
      }
    },
    "send_email" => {
      name: "send_email",
      description: "Send a new email",
      inputSchema: {
        type: "object",
        properties: {
          to: {
            type: "string",
            description: "Recipient email address"
          },
          subject: {
            type: "string",
            description: "Email subject"
          },
          body: {
            type: "string",
            description: "Email body content"
          },
          cc: {
            type: "string",
            description: "CC email address"
          },
          bcc: {
            type: "string",
            description: "BCC email address"
          },
          reply_to: {
            type: "string",
            description: "Reply-to email address"
          }
        },
        required: ["to", "subject", "body"]
      }
    },
    "reply_to_email" => {
      name: "reply_to_email",
      description: "Reply to an existing email",
      inputSchema: {
        type: "object",
        properties: {
          email_id: {
            type: "string",
            description: "Gmail message ID to reply to"
          },
          body: {
            type: "string",
            description: "Reply body content"
          },
          include_quoted: {
            type: "boolean",
            description: "Include quoted original message (default: true)"
          }
        },
        required: ["email_id", "body"]
      }
    },
    "mark_as_read" => {
      name: "mark_as_read",
      description: "Mark an email as read",
      inputSchema: {
        type: "object",
        properties: {
          email_id: {
            type: "string",
            description: "Gmail message ID"
          }
        },
        required: ["email_id"]
      }
    },
    "mark_as_unread" => {
      name: "mark_as_unread",
      description: "Mark an email as unread",
      inputSchema: {
        type: "object",
        properties: {
          email_id: {
            type: "string",
            description: "Gmail message ID"
          }
        },
        required: ["email_id"]
      }
    },
    "add_label" => {
      name: "add_label",
      description: "Add a label to an email",
      inputSchema: {
        type: "object",
        properties: {
          email_id: {
            type: "string",
            description: "Gmail message ID"
          },
          label: {
            type: "string",
            description: "Label name to add"
          }
        },
        required: ["email_id", "label"]
      }
    },
    "remove_label" => {
      name: "remove_label",
      description: "Remove a label from an email",
      inputSchema: {
        type: "object",
        properties: {
          email_id: {
            type: "string",
            description: "Gmail message ID"
          },
          label: {
            type: "string",
            description: "Label name to remove"
          }
        },
        required: ["email_id", "label"]
      }
    },
    "archive_email" => {
      name: "archive_email",
      description: "Archive an email (remove from inbox)",
      inputSchema: {
        type: "object",
        properties: {
          email_id: {
            type: "string",
            description: "Gmail message ID"
          }
        },
        required: ["email_id"]
      }
    },
    "trash_email" => {
      name: "trash_email",
      description: "Move an email to trash",
      inputSchema: {
        type: "object",
        properties: {
          email_id: {
            type: "string",
            description: "Gmail message ID"
          }
        },
        required: ["email_id"]
      }
    }
  }
end

Instance Method Details

#runObject



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/utilities/gmail/mcp.rb', line 304

def run
  # Disable stdout buffering for immediate response
  $stdout.sync = true

  # Log startup to file instead of stdout to avoid protocol interference
  Mcpeasy::Config.ensure_config_dirs
  File.write(Mcpeasy::Config.log_file_path("gmail", "startup"), "#{Time.now}: Gmail MCP Server starting on stdio\n", mode: "a")
  while (line = $stdin.gets)
    handle_request(line.strip)
  end
rescue Interrupt
  # Silent shutdown
rescue => e
  # Log to a file instead of stderr to avoid protocol interference
  File.write(Mcpeasy::Config.log_file_path("gmail", "error"), "#{Time.now}: #{e.message}\n#{e.backtrace.join("\n")}\n", mode: "a")
end