forked from taylorwilsdon/google_workspace_mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms_tools.py
More file actions
484 lines (399 loc) · 16.1 KB
/
forms_tools.py
File metadata and controls
484 lines (399 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
"""
Google Forms MCP Tools
This module provides MCP tools for interacting with Google Forms API.
"""
import logging
import asyncio
from typing import List, Optional, Dict, Any
from fastmcp.tools.tool import ToolResult
from auth.service_decorator import require_google_service
from core.server import server
from core.structured_output import create_tool_result
from core.utils import handle_http_errors
from gforms.forms_models import (
FormsCreateResult,
FormsGetResult,
FormsQuestionSummary,
FormsPublishSettingsResult,
FormsResponseResult,
FormsAnswerDetail,
FormsListResponsesResult,
FormsResponseSummary,
FormsBatchUpdateResult,
FormsBatchUpdateReply,
FORMS_CREATE_RESULT_SCHEMA,
FORMS_GET_RESULT_SCHEMA,
FORMS_PUBLISH_SETTINGS_RESULT_SCHEMA,
FORMS_RESPONSE_RESULT_SCHEMA,
FORMS_LIST_RESPONSES_RESULT_SCHEMA,
FORMS_BATCH_UPDATE_RESULT_SCHEMA,
)
logger = logging.getLogger(__name__)
@server.tool(output_schema=FORMS_CREATE_RESULT_SCHEMA)
@handle_http_errors("create_form", service_type="forms")
@require_google_service("forms", "forms")
async def create_form(
service,
title: str,
description: Optional[str] = None,
document_title: Optional[str] = None,
) -> ToolResult:
"""
Create a new form using the title given in the provided form message in the request.
Args:
title (str): The title of the form.
description (Optional[str]): The description of the form.
document_title (Optional[str]): The document title (shown in browser tab).
Returns:
ToolResult: Confirmation message with form ID and edit URL.
Also includes structured_content for machine parsing.
"""
logger.info(f"[create_form] Invoked. Title: {title}")
form_body: Dict[str, Any] = {"info": {"title": title}}
if description:
form_body["info"]["description"] = description
if document_title:
form_body["info"]["document_title"] = document_title
created_form = await asyncio.to_thread(
service.forms().create(body=form_body).execute
)
form_id = created_form.get("formId")
form_title = created_form.get("info", {}).get("title", title)
edit_url = f"https://docs.google.com/forms/d/{form_id}/edit"
responder_url = created_form.get(
"responderUri", f"https://docs.google.com/forms/d/{form_id}/viewform"
)
confirmation_message = f"Successfully created form '{form_title}'. Form ID: {form_id}. Edit URL: {edit_url}. Responder URL: {responder_url}"
logger.info(f"Form created successfully. ID: {form_id}")
structured_result = FormsCreateResult(
form_id=form_id,
title=form_title,
edit_url=edit_url,
responder_url=responder_url,
)
return create_tool_result(text=confirmation_message, data=structured_result)
@server.tool(output_schema=FORMS_GET_RESULT_SCHEMA)
@handle_http_errors("get_form", is_read_only=True, service_type="forms")
@require_google_service("forms", "forms")
async def get_form(service, form_id: str) -> ToolResult:
"""
Get a form.
Args:
form_id (str): The ID of the form to retrieve.
Returns:
ToolResult: Form details including title, description, questions, and URLs.
Also includes structured_content for machine parsing.
"""
logger.info(f"[get_form] Invoked. Form ID: {form_id}")
form = await asyncio.to_thread(service.forms().get(formId=form_id).execute)
form_info = form.get("info", {})
title = form_info.get("title", "No Title")
description = form_info.get("description", "No Description")
document_title = form_info.get("documentTitle", title)
edit_url = f"https://docs.google.com/forms/d/{form_id}/edit"
responder_url = form.get(
"responderUri", f"https://docs.google.com/forms/d/{form_id}/viewform"
)
items = form.get("items", [])
questions_summary = []
structured_questions = []
for i, item in enumerate(items, 1):
item_title = item.get("title", f"Question {i}")
is_required = (
item.get("questionItem", {}).get("question", {}).get("required", False)
)
required_text = " (Required)" if is_required else ""
questions_summary.append(f" {i}. {item_title}{required_text}")
structured_questions.append(
FormsQuestionSummary(index=i, title=item_title, required=is_required)
)
questions_text = (
"\n".join(questions_summary) if questions_summary else " No questions found"
)
result = f"""Form Details:
- Title: "{title}"
- Description: "{description}"
- Document Title: "{document_title}"
- Form ID: {form_id}
- Edit URL: {edit_url}
- Responder URL: {responder_url}
- Questions ({len(items)} total):
{questions_text}"""
logger.info(f"Successfully retrieved form. ID: {form_id}")
structured_result = FormsGetResult(
form_id=form_id,
title=title,
description=description,
document_title=document_title,
edit_url=edit_url,
responder_url=responder_url,
questions=structured_questions,
)
return create_tool_result(text=result, data=structured_result)
@server.tool(output_schema=FORMS_PUBLISH_SETTINGS_RESULT_SCHEMA)
@handle_http_errors("set_publish_settings", service_type="forms")
@require_google_service("forms", "forms")
async def set_publish_settings(
service,
form_id: str,
publish_as_template: bool = False,
require_authentication: bool = False,
) -> ToolResult:
"""
Updates the publish settings of a form.
Args:
form_id (str): The ID of the form to update publish settings for.
publish_as_template (bool): Whether to publish as a template. Defaults to False.
require_authentication (bool): Whether to require authentication to view/submit. Defaults to False.
Returns:
ToolResult: Confirmation message of the successful publish settings update.
Also includes structured_content for machine parsing.
"""
logger.info(f"[set_publish_settings] Invoked. Form ID: {form_id}")
settings_body = {
"publishAsTemplate": publish_as_template,
"requireAuthentication": require_authentication,
}
await asyncio.to_thread(
service.forms().setPublishSettings(formId=form_id, body=settings_body).execute
)
confirmation_message = f"Successfully updated publish settings for form {form_id}. Publish as template: {publish_as_template}, Require authentication: {require_authentication}"
logger.info(f"Publish settings updated successfully. Form ID: {form_id}")
structured_result = FormsPublishSettingsResult(
form_id=form_id,
publish_as_template=publish_as_template,
require_authentication=require_authentication,
)
return create_tool_result(text=confirmation_message, data=structured_result)
@server.tool(output_schema=FORMS_RESPONSE_RESULT_SCHEMA)
@handle_http_errors("get_form_response", is_read_only=True, service_type="forms")
@require_google_service("forms", "forms")
async def get_form_response(service, form_id: str, response_id: str) -> ToolResult:
"""
Get one response from the form.
Args:
form_id (str): The ID of the form.
response_id (str): The ID of the response to retrieve.
Returns:
ToolResult: Response details including answers and metadata.
Also includes structured_content for machine parsing.
"""
logger.info(
f"[get_form_response] Invoked. Form ID: {form_id}, Response ID: {response_id}"
)
response = await asyncio.to_thread(
service.forms().responses().get(formId=form_id, responseId=response_id).execute
)
result_response_id = response.get("responseId", "Unknown")
create_time = response.get("createTime", "Unknown")
last_submitted_time = response.get("lastSubmittedTime", "Unknown")
answers = response.get("answers", {})
answer_details = []
structured_answers = []
for question_id, answer_data in answers.items():
question_response = answer_data.get("textAnswers", {}).get("answers", [])
if question_response:
answer_text = ", ".join([ans.get("value", "") for ans in question_response])
answer_details.append(f" Question ID {question_id}: {answer_text}")
else:
answer_text = "No answer provided"
answer_details.append(f" Question ID {question_id}: No answer provided")
structured_answers.append(
FormsAnswerDetail(question_id=question_id, answer_text=answer_text)
)
answers_text = "\n".join(answer_details) if answer_details else " No answers found"
result = f"""Form Response Details:
- Form ID: {form_id}
- Response ID: {result_response_id}
- Created: {create_time}
- Last Submitted: {last_submitted_time}
- Answers:
{answers_text}"""
logger.info(f"Successfully retrieved response. Response ID: {result_response_id}")
structured_result = FormsResponseResult(
form_id=form_id,
response_id=result_response_id,
create_time=create_time,
last_submitted_time=last_submitted_time,
answers=structured_answers,
)
return create_tool_result(text=result, data=structured_result)
@server.tool(output_schema=FORMS_LIST_RESPONSES_RESULT_SCHEMA)
@handle_http_errors("list_form_responses", is_read_only=True, service_type="forms")
@require_google_service("forms", "forms")
async def list_form_responses(
service,
form_id: str,
page_size: int = 10,
page_token: Optional[str] = None,
) -> ToolResult:
"""
List a form's responses.
Args:
form_id (str): The ID of the form.
page_size (int): Maximum number of responses to return. Defaults to 10.
page_token (Optional[str]): Token for retrieving next page of results.
Returns:
ToolResult: List of responses with basic details and pagination info.
Also includes structured_content for machine parsing.
"""
logger.info(f"[list_form_responses] Invoked. Form ID: {form_id}")
params = {"formId": form_id, "pageSize": page_size}
if page_token:
params["pageToken"] = page_token
responses_result = await asyncio.to_thread(
service.forms().responses().list(**params).execute
)
responses = responses_result.get("responses", [])
next_page_token = responses_result.get("nextPageToken")
if not responses:
structured_result = FormsListResponsesResult(
form_id=form_id,
total_returned=0,
responses=[],
next_page_token=None,
)
return create_tool_result(
text=f"No responses found for form {form_id}.",
data=structured_result,
)
response_details = []
structured_responses = []
for i, response in enumerate(responses, 1):
response_id = response.get("responseId", "Unknown")
create_time = response.get("createTime", "Unknown")
last_submitted_time = response.get("lastSubmittedTime", "Unknown")
answers_count = len(response.get("answers", {}))
response_details.append(
f" {i}. Response ID: {response_id} | Created: {create_time} | Last Submitted: {last_submitted_time} | Answers: {answers_count}"
)
structured_responses.append(
FormsResponseSummary(
response_id=response_id,
create_time=create_time,
last_submitted_time=last_submitted_time,
answer_count=answers_count,
)
)
pagination_info = (
f"\nNext page token: {next_page_token}"
if next_page_token
else "\nNo more pages."
)
result = f"""Form Responses:
- Form ID: {form_id}
- Total responses returned: {len(responses)}
- Responses:
{chr(10).join(response_details)}{pagination_info}"""
logger.info(
f"Successfully retrieved {len(responses)} responses. Form ID: {form_id}"
)
structured_result = FormsListResponsesResult(
form_id=form_id,
total_returned=len(responses),
responses=structured_responses,
next_page_token=next_page_token,
)
return create_tool_result(text=result, data=structured_result)
# Internal implementation function for testing
async def _batch_update_form_impl(
service: Any,
form_id: str,
requests: List[Dict[str, Any]],
) -> tuple[str, FormsBatchUpdateResult]:
"""Internal implementation for batch_update_form.
Applies batch updates to a Google Form using the Forms API batchUpdate method.
Args:
service: Google Forms API service client.
form_id: The ID of the form to update.
requests: List of update request dictionaries.
Returns:
Tuple of (formatted string with batch update results, structured result).
"""
body = {"requests": requests}
result = await asyncio.to_thread(
service.forms().batchUpdate(formId=form_id, body=body).execute
)
replies = result.get("replies", [])
edit_url = f"https://docs.google.com/forms/d/{form_id}/edit"
confirmation_message = f"""Batch Update Completed:
- Form ID: {form_id}
- URL: {edit_url}
- Requests Applied: {len(requests)}
- Replies Received: {len(replies)}"""
structured_replies = []
if replies:
confirmation_message += "\n\nUpdate Results:"
for i, reply in enumerate(replies, 1):
if "createItem" in reply:
item_id = reply["createItem"].get("itemId", "Unknown")
question_ids = reply["createItem"].get("questionId", [])
question_info = (
f" (Question IDs: {', '.join(question_ids)})"
if question_ids
else ""
)
confirmation_message += (
f"\n Request {i}: Created item {item_id}{question_info}"
)
structured_replies.append(
FormsBatchUpdateReply(
request_index=i,
operation="createItem",
item_id=item_id,
question_ids=question_ids if question_ids else [],
)
)
else:
confirmation_message += f"\n Request {i}: Operation completed"
structured_replies.append(
FormsBatchUpdateReply(
request_index=i,
operation="completed",
)
)
structured_result = FormsBatchUpdateResult(
form_id=form_id,
edit_url=edit_url,
requests_applied=len(requests),
replies_received=len(replies),
replies=structured_replies,
)
return confirmation_message, structured_result
@server.tool(output_schema=FORMS_BATCH_UPDATE_RESULT_SCHEMA)
@handle_http_errors("batch_update_form", service_type="forms")
@require_google_service("forms", "forms")
async def batch_update_form(
service,
user_google_email: str,
form_id: str,
requests: List[Dict[str, Any]],
) -> ToolResult:
"""
Apply batch updates to a Google Form.
Supports adding, updating, and deleting form items, as well as updating
form metadata and settings. This is the primary method for modifying form
content after creation.
Args:
form_id (str): The ID of the form to update.
requests (List[Dict[str, Any]]): List of update requests to apply.
Supported request types:
- createItem: Add a new question or content item
- updateItem: Modify an existing item
- deleteItem: Remove an item
- moveItem: Reorder an item
- updateFormInfo: Update form title/description
- updateSettings: Modify form settings (e.g., quiz mode)
Returns:
ToolResult: Details about the batch update operation results.
Also includes structured_content for machine parsing.
"""
logger.info(
f"[batch_update_form] Invoked. Email: '{user_google_email}', "
f"Form ID: '{form_id}', Requests: {len(requests)}"
)
text_result, structured_result = await _batch_update_form_impl(
service, form_id, requests
)
logger.info(f"Batch update completed successfully for {user_google_email}")
return create_tool_result(text=text_result, data=structured_result)