@mutallip/google-workspace-mcp 0.0.0-stage → 2.8.1
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/LICENSE +21 -0
- package/README.md +353 -2
- package/README.npm.md +34 -0
- package/auth/__init__.py +1 -0
- package/auth/auth_info_middleware.py +419 -0
- package/auth/client_secrets.py +76 -0
- package/auth/credential_store.py +598 -0
- package/auth/external_oauth_provider.py +369 -0
- package/auth/gateway_identity.py +164 -0
- package/auth/google_auth.py +1550 -0
- package/auth/google_oauth_provider.py +20 -0
- package/auth/mcp_session_middleware.py +95 -0
- package/auth/oauth21_session_store.py +1332 -0
- package/auth/oauth_callback_server.py +466 -0
- package/auth/oauth_config.py +693 -0
- package/auth/oauth_proxy_config.py +122 -0
- package/auth/oauth_responses.py +230 -0
- package/auth/oauth_types.py +92 -0
- package/auth/permissions.py +330 -0
- package/auth/port_resolver.py +153 -0
- package/auth/request_identity.py +89 -0
- package/auth/scopes.py +369 -0
- package/auth/service_decorator.py +1109 -0
- package/bin/google-workspace-mcp.cjs +123 -0
- package/core/__init__.py +1 -0
- package/core/api_enablement.py +108 -0
- package/core/attachment_storage.py +496 -0
- package/core/camel_case_middleware.py +98 -0
- package/core/cli.py +161 -0
- package/core/comments.py +609 -0
- package/core/config.py +57 -0
- package/core/context.py +43 -0
- package/core/file_limits.py +632 -0
- package/core/gcs_attachment_storage.py +253 -0
- package/core/http_utils.py +339 -0
- package/core/log_formatter.py +270 -0
- package/core/portable_schema_middleware.py +67 -0
- package/core/server.py +1192 -0
- package/core/startup_ui.py +239 -0
- package/core/storage.py +23 -0
- package/core/telemetry.py +194 -0
- package/core/tool_descriptions.py +19 -0
- package/core/tool_registry.py +311 -0
- package/core/tool_tier_loader.py +197 -0
- package/core/tool_tiers.yaml +179 -0
- package/core/toolset_header_middleware.py +67 -0
- package/core/utils.py +1537 -0
- package/core/valkey_storage.py +271 -0
- package/core/warning_filters.py +19 -0
- package/fastmcp_server.py +198 -0
- package/gappsscript/README.md +512 -0
- package/gappsscript/TESTING.md +254 -0
- package/gappsscript/__init__.py +0 -0
- package/gappsscript/apps_script_tools.py +1940 -0
- package/gcalendar/__init__.py +1 -0
- package/gcalendar/calendar_helpers.py +1323 -0
- package/gcalendar/calendar_tools.py +2252 -0
- package/gchat/__init__.py +7 -0
- package/gchat/chat_helpers.py +410 -0
- package/gchat/chat_tools.py +798 -0
- package/gcontacts/__init__.py +1 -0
- package/gcontacts/contacts_helpers.py +765 -0
- package/gcontacts/contacts_tools.py +1685 -0
- package/gdocs/__init__.py +0 -0
- package/gdocs/docs_anchors.py +163 -0
- package/gdocs/docs_comments.py +89 -0
- package/gdocs/docs_helpers.py +2201 -0
- package/gdocs/docs_links.py +87 -0
- package/gdocs/docs_markdown.py +828 -0
- package/gdocs/docs_markdown_writer.py +592 -0
- package/gdocs/docs_plain_text.py +463 -0
- package/gdocs/docs_structure.py +621 -0
- package/gdocs/docs_tables.py +425 -0
- package/gdocs/docs_tools.py +3300 -0
- package/gdocs/managers/__init__.py +18 -0
- package/gdocs/managers/batch_operation_manager.py +1694 -0
- package/gdocs/managers/header_footer_manager.py +526 -0
- package/gdocs/managers/table_operation_manager.py +488 -0
- package/gdocs/managers/validation_manager.py +1504 -0
- package/gdocs/operation_schemas.py +594 -0
- package/gdrive/__init__.py +0 -0
- package/gdrive/drive_helpers.py +1738 -0
- package/gdrive/drive_tools.py +3085 -0
- package/gforms/__init__.py +3 -0
- package/gforms/forms_tools.py +575 -0
- package/gmail/__init__.py +1 -0
- package/gmail/gmail_helpers.py +2107 -0
- package/gmail/gmail_tools.py +4895 -0
- package/gmail/gmail_web_mime.py +441 -0
- package/gsearch/__init__.py +0 -0
- package/gsearch/search_tools.py +253 -0
- package/gsheets/__init__.py +31 -0
- package/gsheets/sheets_helpers.py +2236 -0
- package/gsheets/sheets_tools.py +3280 -0
- package/gslides/__init__.py +0 -0
- package/gslides/slides_digest.py +390 -0
- package/gslides/slides_helpers.py +392 -0
- package/gslides/slides_markup.py +770 -0
- package/gslides/slides_tools.py +376 -0
- package/gtasks/__init__.py +5 -0
- package/gtasks/tasks_tools.py +936 -0
- package/main.py +1170 -0
- package/package.json +43 -4
- package/pyproject.toml +173 -0
- package/uv.lock +2504 -0
|
@@ -0,0 +1,575 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Google Forms MCP Tools
|
|
3
|
+
|
|
4
|
+
This module provides MCP tools for interacting with Google Forms API.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import logging
|
|
8
|
+
import asyncio
|
|
9
|
+
import json
|
|
10
|
+
from typing import List, Optional, Dict, Any
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
from googleapiclient.errors import HttpError
|
|
14
|
+
from mcp.types import ToolAnnotations
|
|
15
|
+
|
|
16
|
+
from auth.service_decorator import require_google_service
|
|
17
|
+
from core.server import server
|
|
18
|
+
from core.utils import handle_http_errors
|
|
19
|
+
|
|
20
|
+
logger = logging.getLogger(__name__)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _extract_option_values(options: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
|
24
|
+
"""Extract valid option objects from Forms choice option objects.
|
|
25
|
+
|
|
26
|
+
Returns the full option dicts (preserving fields like ``isOther``,
|
|
27
|
+
``image``, ``goToAction``, and ``goToSectionId``) while filtering
|
|
28
|
+
out entries that lack a truthy ``value``.
|
|
29
|
+
"""
|
|
30
|
+
return [option for option in options if option.get("value")]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _get_question_type(question: Dict[str, Any]) -> str:
|
|
34
|
+
"""Infer a stable question/item type label from a Forms question payload."""
|
|
35
|
+
choice_question = question.get("choiceQuestion")
|
|
36
|
+
if choice_question:
|
|
37
|
+
return choice_question.get("type", "CHOICE")
|
|
38
|
+
|
|
39
|
+
text_question = question.get("textQuestion")
|
|
40
|
+
if text_question:
|
|
41
|
+
return "PARAGRAPH" if text_question.get("paragraph") else "TEXT"
|
|
42
|
+
|
|
43
|
+
if "rowQuestion" in question:
|
|
44
|
+
return "GRID_ROW"
|
|
45
|
+
if "scaleQuestion" in question:
|
|
46
|
+
return "SCALE"
|
|
47
|
+
if "dateQuestion" in question:
|
|
48
|
+
return "DATE"
|
|
49
|
+
if "timeQuestion" in question:
|
|
50
|
+
return "TIME"
|
|
51
|
+
if "fileUploadQuestion" in question:
|
|
52
|
+
return "FILE_UPLOAD"
|
|
53
|
+
if "ratingQuestion" in question:
|
|
54
|
+
return "RATING"
|
|
55
|
+
|
|
56
|
+
return "QUESTION"
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _serialize_form_item(item: Dict[str, Any], index: int) -> Dict[str, Any]:
|
|
60
|
+
"""Serialize a Forms item with the key metadata agents need for edits."""
|
|
61
|
+
serialized_item: Dict[str, Any] = {
|
|
62
|
+
"index": index,
|
|
63
|
+
"itemId": item.get("itemId"),
|
|
64
|
+
"title": item.get("title", f"Question {index}"),
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if item.get("description"):
|
|
68
|
+
serialized_item["description"] = item["description"]
|
|
69
|
+
|
|
70
|
+
if "questionItem" in item:
|
|
71
|
+
question = item.get("questionItem", {}).get("question", {})
|
|
72
|
+
serialized_item["type"] = _get_question_type(question)
|
|
73
|
+
serialized_item["required"] = question.get("required", False)
|
|
74
|
+
|
|
75
|
+
question_id = question.get("questionId")
|
|
76
|
+
if question_id:
|
|
77
|
+
serialized_item["questionId"] = question_id
|
|
78
|
+
|
|
79
|
+
choice_question = question.get("choiceQuestion")
|
|
80
|
+
if choice_question:
|
|
81
|
+
serialized_item["options"] = _extract_option_values(
|
|
82
|
+
choice_question.get("options", [])
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
return serialized_item
|
|
86
|
+
|
|
87
|
+
if "questionGroupItem" in item:
|
|
88
|
+
question_group = item.get("questionGroupItem", {})
|
|
89
|
+
columns = _extract_option_values(
|
|
90
|
+
question_group.get("grid", {}).get("columns", {}).get("options", [])
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
rows = []
|
|
94
|
+
for question in question_group.get("questions", []):
|
|
95
|
+
row: Dict[str, Any] = {
|
|
96
|
+
"title": question.get("rowQuestion", {}).get("title", "")
|
|
97
|
+
}
|
|
98
|
+
row_question_id = question.get("questionId")
|
|
99
|
+
if row_question_id:
|
|
100
|
+
row["questionId"] = row_question_id
|
|
101
|
+
row["required"] = question.get("required", False)
|
|
102
|
+
rows.append(row)
|
|
103
|
+
|
|
104
|
+
serialized_item["type"] = "GRID"
|
|
105
|
+
serialized_item["grid"] = {"rows": rows, "columns": columns}
|
|
106
|
+
return serialized_item
|
|
107
|
+
|
|
108
|
+
if "pageBreakItem" in item:
|
|
109
|
+
serialized_item["type"] = "PAGE_BREAK"
|
|
110
|
+
elif "textItem" in item:
|
|
111
|
+
serialized_item["type"] = "TEXT_ITEM"
|
|
112
|
+
elif "imageItem" in item:
|
|
113
|
+
serialized_item["type"] = "IMAGE"
|
|
114
|
+
elif "videoItem" in item:
|
|
115
|
+
serialized_item["type"] = "VIDEO"
|
|
116
|
+
else:
|
|
117
|
+
serialized_item["type"] = "UNKNOWN"
|
|
118
|
+
|
|
119
|
+
return serialized_item
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
@server.tool(
|
|
123
|
+
title="Create Form",
|
|
124
|
+
annotations=ToolAnnotations(
|
|
125
|
+
readOnlyHint=False,
|
|
126
|
+
destructiveHint=False,
|
|
127
|
+
idempotentHint=False,
|
|
128
|
+
openWorldHint=True,
|
|
129
|
+
),
|
|
130
|
+
)
|
|
131
|
+
@handle_http_errors("create_form", service_type="forms")
|
|
132
|
+
@require_google_service("forms", "forms")
|
|
133
|
+
async def create_form(
|
|
134
|
+
service,
|
|
135
|
+
user_google_email: str,
|
|
136
|
+
title: str,
|
|
137
|
+
description: Optional[str] = None,
|
|
138
|
+
document_title: Optional[str] = None,
|
|
139
|
+
) -> str:
|
|
140
|
+
"""
|
|
141
|
+
Create a new form using the title given in the provided form message in the request.
|
|
142
|
+
|
|
143
|
+
Args:
|
|
144
|
+
user_google_email (str): The user's Google email address. Required.
|
|
145
|
+
title (str): The title of the form.
|
|
146
|
+
description (Optional[str]): The description of the form.
|
|
147
|
+
document_title (Optional[str]): The document title (shown in browser tab).
|
|
148
|
+
|
|
149
|
+
Returns:
|
|
150
|
+
str: Confirmation message with form ID and edit URL.
|
|
151
|
+
"""
|
|
152
|
+
logger.info(
|
|
153
|
+
f"[create_form] Invoked. Email: '{user_google_email}', title_len={len(title)}"
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
form_body: Dict[str, Any] = {"info": {"title": title}}
|
|
157
|
+
|
|
158
|
+
if document_title:
|
|
159
|
+
form_body["info"]["documentTitle"] = document_title
|
|
160
|
+
|
|
161
|
+
created_form = await asyncio.to_thread(
|
|
162
|
+
service.forms().create(body=form_body).execute
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
form_id = created_form.get("formId")
|
|
166
|
+
edit_url = f"https://docs.google.com/forms/d/{form_id}/edit"
|
|
167
|
+
responder_url = created_form.get(
|
|
168
|
+
"responderUri", f"https://docs.google.com/forms/d/{form_id}/viewform"
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
confirmation_message = f"Successfully created form '{created_form.get('info', {}).get('title', title)}' for {user_google_email}. Form ID: {form_id}. Edit URL: {edit_url}. Responder URL: {responder_url}"
|
|
172
|
+
logger.info(f"Form created successfully for {user_google_email}. ID: {form_id}")
|
|
173
|
+
|
|
174
|
+
if description:
|
|
175
|
+
# The form already exists, so surface its ID rather than inviting a duplicate create.
|
|
176
|
+
try:
|
|
177
|
+
await asyncio.to_thread(
|
|
178
|
+
service.forms()
|
|
179
|
+
.batchUpdate(
|
|
180
|
+
formId=form_id,
|
|
181
|
+
body={
|
|
182
|
+
"requests": [
|
|
183
|
+
{
|
|
184
|
+
"updateFormInfo": {
|
|
185
|
+
"info": {"description": description},
|
|
186
|
+
"updateMask": "description",
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
]
|
|
190
|
+
},
|
|
191
|
+
)
|
|
192
|
+
.execute
|
|
193
|
+
)
|
|
194
|
+
except HttpError as error:
|
|
195
|
+
logger.error(
|
|
196
|
+
f"[create_form] Description update failed for {form_id}: {error}"
|
|
197
|
+
)
|
|
198
|
+
return f"{confirmation_message}. Warning: the description was not applied ({error}). Set it with batch_update_form on form ID {form_id} instead of calling create_form again."
|
|
199
|
+
|
|
200
|
+
return confirmation_message
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
@server.tool(
|
|
204
|
+
title="Get Form",
|
|
205
|
+
annotations=ToolAnnotations(
|
|
206
|
+
readOnlyHint=True,
|
|
207
|
+
destructiveHint=False,
|
|
208
|
+
idempotentHint=True,
|
|
209
|
+
openWorldHint=True,
|
|
210
|
+
),
|
|
211
|
+
)
|
|
212
|
+
@handle_http_errors("get_form", is_read_only=True, service_type="forms")
|
|
213
|
+
@require_google_service("forms", "forms")
|
|
214
|
+
async def get_form(service, user_google_email: str, form_id: str) -> str:
|
|
215
|
+
"""
|
|
216
|
+
Get a form.
|
|
217
|
+
|
|
218
|
+
Args:
|
|
219
|
+
user_google_email (str): The user's Google email address. Required.
|
|
220
|
+
form_id (str): The ID of the form to retrieve.
|
|
221
|
+
|
|
222
|
+
Returns:
|
|
223
|
+
str: Form details including title, description, questions, and URLs.
|
|
224
|
+
"""
|
|
225
|
+
logger.info(f"[get_form] Invoked. Email: '{user_google_email}', Form ID: {form_id}")
|
|
226
|
+
|
|
227
|
+
form = await asyncio.to_thread(service.forms().get(formId=form_id).execute)
|
|
228
|
+
|
|
229
|
+
form_info = form.get("info", {})
|
|
230
|
+
title = form_info.get("title", "No Title")
|
|
231
|
+
description = form_info.get("description", "No Description")
|
|
232
|
+
document_title = form_info.get("documentTitle", title)
|
|
233
|
+
|
|
234
|
+
edit_url = f"https://docs.google.com/forms/d/{form_id}/edit"
|
|
235
|
+
responder_url = form.get(
|
|
236
|
+
"responderUri", f"https://docs.google.com/forms/d/{form_id}/viewform"
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
items = form.get("items", [])
|
|
240
|
+
serialized_items = [
|
|
241
|
+
_serialize_form_item(item, i) for i, item in enumerate(items, 1)
|
|
242
|
+
]
|
|
243
|
+
|
|
244
|
+
items_summary = []
|
|
245
|
+
for serialized_item in serialized_items:
|
|
246
|
+
item_index = serialized_item["index"]
|
|
247
|
+
item_title = serialized_item.get("title", f"Item {item_index}")
|
|
248
|
+
item_type = serialized_item.get("type", "UNKNOWN")
|
|
249
|
+
required_text = " (Required)" if serialized_item.get("required") else ""
|
|
250
|
+
items_summary.append(
|
|
251
|
+
f" {item_index}. {item_title} [{item_type}]{required_text}"
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
items_summary_text = (
|
|
255
|
+
"\n".join(items_summary) if items_summary else " No items found"
|
|
256
|
+
)
|
|
257
|
+
items_text = json.dumps(serialized_items, indent=2) if serialized_items else "[]"
|
|
258
|
+
|
|
259
|
+
result = f"""Form Details for {user_google_email}:
|
|
260
|
+
- Title: "{title}"
|
|
261
|
+
- Description: "{description}"
|
|
262
|
+
- Document Title: "{document_title}"
|
|
263
|
+
- Form ID: {form_id}
|
|
264
|
+
- Edit URL: {edit_url}
|
|
265
|
+
- Responder URL: {responder_url}
|
|
266
|
+
- Items ({len(items)} total):
|
|
267
|
+
{items_summary_text}
|
|
268
|
+
- Items (structured):
|
|
269
|
+
{items_text}"""
|
|
270
|
+
|
|
271
|
+
logger.info(f"Successfully retrieved form for {user_google_email}. ID: {form_id}")
|
|
272
|
+
return result
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
@server.tool(
|
|
276
|
+
title="Set Publish Settings",
|
|
277
|
+
annotations=ToolAnnotations(
|
|
278
|
+
readOnlyHint=False,
|
|
279
|
+
destructiveHint=False,
|
|
280
|
+
idempotentHint=False,
|
|
281
|
+
openWorldHint=True,
|
|
282
|
+
),
|
|
283
|
+
)
|
|
284
|
+
@handle_http_errors("set_publish_settings", service_type="forms")
|
|
285
|
+
@require_google_service("forms", "forms")
|
|
286
|
+
async def set_publish_settings(
|
|
287
|
+
service,
|
|
288
|
+
user_google_email: str,
|
|
289
|
+
form_id: str,
|
|
290
|
+
is_published: bool = True,
|
|
291
|
+
is_accepting_responses: bool = True,
|
|
292
|
+
) -> str:
|
|
293
|
+
"""
|
|
294
|
+
Updates the publish settings of a form.
|
|
295
|
+
|
|
296
|
+
Args:
|
|
297
|
+
user_google_email (str): The user's Google email address. Required.
|
|
298
|
+
form_id (str): The ID of the form to update publish settings for.
|
|
299
|
+
is_published (bool): Whether the form is published and visible to responders. Defaults to True.
|
|
300
|
+
is_accepting_responses (bool): Whether the form accepts responses. Only takes effect when the form is published. Defaults to True.
|
|
301
|
+
|
|
302
|
+
Returns:
|
|
303
|
+
str: Confirmation message of the successful publish settings update.
|
|
304
|
+
"""
|
|
305
|
+
logger.info(
|
|
306
|
+
f"[set_publish_settings] Invoked. Email: '{user_google_email}', Form ID: {form_id}"
|
|
307
|
+
)
|
|
308
|
+
|
|
309
|
+
settings_body = {
|
|
310
|
+
"publishSettings": {
|
|
311
|
+
"publishState": {
|
|
312
|
+
"isPublished": is_published,
|
|
313
|
+
"isAcceptingResponses": is_accepting_responses,
|
|
314
|
+
}
|
|
315
|
+
},
|
|
316
|
+
"updateMask": "publishState",
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
await asyncio.to_thread(
|
|
320
|
+
service.forms().setPublishSettings(formId=form_id, body=settings_body).execute
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
confirmation_message = f"Successfully updated publish settings for form {form_id} for {user_google_email}. Published: {is_published}, Accepting responses: {is_accepting_responses}"
|
|
324
|
+
logger.info(
|
|
325
|
+
f"Publish settings updated successfully for {user_google_email}. Form ID: {form_id}"
|
|
326
|
+
)
|
|
327
|
+
return confirmation_message
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
@server.tool(
|
|
331
|
+
title="Get Form Response",
|
|
332
|
+
annotations=ToolAnnotations(
|
|
333
|
+
readOnlyHint=True,
|
|
334
|
+
destructiveHint=False,
|
|
335
|
+
idempotentHint=True,
|
|
336
|
+
openWorldHint=True,
|
|
337
|
+
),
|
|
338
|
+
)
|
|
339
|
+
@handle_http_errors("get_form_response", is_read_only=True, service_type="forms")
|
|
340
|
+
@require_google_service("forms", "forms")
|
|
341
|
+
async def get_form_response(
|
|
342
|
+
service, user_google_email: str, form_id: str, response_id: str
|
|
343
|
+
) -> str:
|
|
344
|
+
"""
|
|
345
|
+
Get one response from the form.
|
|
346
|
+
|
|
347
|
+
Args:
|
|
348
|
+
user_google_email (str): The user's Google email address. Required.
|
|
349
|
+
form_id (str): The ID of the form.
|
|
350
|
+
response_id (str): The ID of the response to retrieve.
|
|
351
|
+
|
|
352
|
+
Returns:
|
|
353
|
+
str: Response details including answers and metadata.
|
|
354
|
+
"""
|
|
355
|
+
logger.info(
|
|
356
|
+
f"[get_form_response] Invoked. Email: '{user_google_email}', Form ID: {form_id}, Response ID: {response_id}"
|
|
357
|
+
)
|
|
358
|
+
|
|
359
|
+
response = await asyncio.to_thread(
|
|
360
|
+
service.forms().responses().get(formId=form_id, responseId=response_id).execute
|
|
361
|
+
)
|
|
362
|
+
|
|
363
|
+
response_id = response.get("responseId", "Unknown")
|
|
364
|
+
create_time = response.get("createTime", "Unknown")
|
|
365
|
+
last_submitted_time = response.get("lastSubmittedTime", "Unknown")
|
|
366
|
+
|
|
367
|
+
answers = response.get("answers", {})
|
|
368
|
+
answer_details = []
|
|
369
|
+
for question_id, answer_data in answers.items():
|
|
370
|
+
question_response = answer_data.get("textAnswers", {}).get("answers", [])
|
|
371
|
+
uploaded_files = answer_data.get("fileUploadAnswers", {}).get("answers", [])
|
|
372
|
+
if question_response:
|
|
373
|
+
answer_text = ", ".join([ans.get("value", "") for ans in question_response])
|
|
374
|
+
elif uploaded_files:
|
|
375
|
+
answer_text = ", ".join(
|
|
376
|
+
f"{f.get('fileName', 'Unnamed file')} (file ID: {f.get('fileId', 'Unknown')})"
|
|
377
|
+
for f in uploaded_files
|
|
378
|
+
)
|
|
379
|
+
else:
|
|
380
|
+
answer_text = "No answer provided"
|
|
381
|
+
answer_details.append(f" Question ID {question_id}: {answer_text}")
|
|
382
|
+
|
|
383
|
+
answers_text = "\n".join(answer_details) if answer_details else " No answers found"
|
|
384
|
+
|
|
385
|
+
result = f"""Form Response Details for {user_google_email}:
|
|
386
|
+
- Form ID: {form_id}
|
|
387
|
+
- Response ID: {response_id}
|
|
388
|
+
- Created: {create_time}
|
|
389
|
+
- Last Submitted: {last_submitted_time}
|
|
390
|
+
- Answers:
|
|
391
|
+
{answers_text}"""
|
|
392
|
+
|
|
393
|
+
logger.info(
|
|
394
|
+
f"Successfully retrieved response for {user_google_email}. Response ID: {response_id}"
|
|
395
|
+
)
|
|
396
|
+
return result
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
@server.tool(
|
|
400
|
+
title="List Form Responses",
|
|
401
|
+
annotations=ToolAnnotations(
|
|
402
|
+
readOnlyHint=True,
|
|
403
|
+
destructiveHint=False,
|
|
404
|
+
idempotentHint=True,
|
|
405
|
+
openWorldHint=True,
|
|
406
|
+
),
|
|
407
|
+
)
|
|
408
|
+
@handle_http_errors("list_form_responses", is_read_only=True, service_type="forms")
|
|
409
|
+
@require_google_service("forms", "forms")
|
|
410
|
+
async def list_form_responses(
|
|
411
|
+
service,
|
|
412
|
+
user_google_email: str,
|
|
413
|
+
form_id: str,
|
|
414
|
+
page_size: int = 10,
|
|
415
|
+
page_token: Optional[str] = None,
|
|
416
|
+
) -> str:
|
|
417
|
+
"""
|
|
418
|
+
List a form's responses.
|
|
419
|
+
|
|
420
|
+
Args:
|
|
421
|
+
user_google_email (str): The user's Google email address. Required.
|
|
422
|
+
form_id (str): The ID of the form.
|
|
423
|
+
page_size (int): Maximum number of responses to return. Defaults to 10.
|
|
424
|
+
page_token (Optional[str]): Token for retrieving next page of results.
|
|
425
|
+
|
|
426
|
+
Returns:
|
|
427
|
+
str: List of responses with basic details and pagination info.
|
|
428
|
+
"""
|
|
429
|
+
logger.info(
|
|
430
|
+
f"[list_form_responses] Invoked. Email: '{user_google_email}', Form ID: {form_id}"
|
|
431
|
+
)
|
|
432
|
+
|
|
433
|
+
params = {"formId": form_id, "pageSize": page_size}
|
|
434
|
+
if page_token:
|
|
435
|
+
params["pageToken"] = page_token
|
|
436
|
+
|
|
437
|
+
responses_result = await asyncio.to_thread(
|
|
438
|
+
service.forms().responses().list(**params).execute
|
|
439
|
+
)
|
|
440
|
+
|
|
441
|
+
responses = responses_result.get("responses", [])
|
|
442
|
+
next_page_token = responses_result.get("nextPageToken")
|
|
443
|
+
|
|
444
|
+
if not responses:
|
|
445
|
+
return f"No responses found for form {form_id} for {user_google_email}."
|
|
446
|
+
|
|
447
|
+
response_details = []
|
|
448
|
+
for i, response in enumerate(responses, 1):
|
|
449
|
+
response_id = response.get("responseId", "Unknown")
|
|
450
|
+
create_time = response.get("createTime", "Unknown")
|
|
451
|
+
last_submitted_time = response.get("lastSubmittedTime", "Unknown")
|
|
452
|
+
|
|
453
|
+
answers_count = len(response.get("answers", {}))
|
|
454
|
+
response_details.append(
|
|
455
|
+
f" {i}. Response ID: {response_id} | Created: {create_time} | Last Submitted: {last_submitted_time} | Answers: {answers_count}"
|
|
456
|
+
)
|
|
457
|
+
|
|
458
|
+
pagination_info = (
|
|
459
|
+
f"\nNext page token: {next_page_token}"
|
|
460
|
+
if next_page_token
|
|
461
|
+
else "\nNo more pages."
|
|
462
|
+
)
|
|
463
|
+
|
|
464
|
+
result = f"""Form Responses for {user_google_email}:
|
|
465
|
+
- Form ID: {form_id}
|
|
466
|
+
- Total responses returned: {len(responses)}
|
|
467
|
+
- Responses:
|
|
468
|
+
{chr(10).join(response_details)}{pagination_info}"""
|
|
469
|
+
|
|
470
|
+
logger.info(
|
|
471
|
+
f"Successfully retrieved {len(responses)} responses for {user_google_email}. Form ID: {form_id}"
|
|
472
|
+
)
|
|
473
|
+
return result
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
# Internal implementation function for testing
|
|
477
|
+
async def _batch_update_form_impl(
|
|
478
|
+
service: Any,
|
|
479
|
+
form_id: str,
|
|
480
|
+
requests: List[Dict[str, Any]],
|
|
481
|
+
) -> str:
|
|
482
|
+
"""Internal implementation for batch_update_form.
|
|
483
|
+
|
|
484
|
+
Applies batch updates to a Google Form using the Forms API batchUpdate method.
|
|
485
|
+
|
|
486
|
+
Args:
|
|
487
|
+
service: Google Forms API service client.
|
|
488
|
+
form_id: The ID of the form to update.
|
|
489
|
+
requests: List of update request dictionaries.
|
|
490
|
+
|
|
491
|
+
Returns:
|
|
492
|
+
Formatted string with batch update results.
|
|
493
|
+
"""
|
|
494
|
+
body = {"requests": requests}
|
|
495
|
+
|
|
496
|
+
result = await asyncio.to_thread(
|
|
497
|
+
service.forms().batchUpdate(formId=form_id, body=body).execute
|
|
498
|
+
)
|
|
499
|
+
|
|
500
|
+
replies = result.get("replies", [])
|
|
501
|
+
|
|
502
|
+
confirmation_message = f"""Batch Update Completed:
|
|
503
|
+
- Form ID: {form_id}
|
|
504
|
+
- URL: https://docs.google.com/forms/d/{form_id}/edit
|
|
505
|
+
- Requests Applied: {len(requests)}
|
|
506
|
+
- Replies Received: {len(replies)}"""
|
|
507
|
+
|
|
508
|
+
if replies:
|
|
509
|
+
confirmation_message += "\n\nUpdate Results:"
|
|
510
|
+
for i, reply in enumerate(replies, 1):
|
|
511
|
+
if "createItem" in reply:
|
|
512
|
+
item_id = reply["createItem"].get("itemId", "Unknown")
|
|
513
|
+
question_ids = reply["createItem"].get("questionId", [])
|
|
514
|
+
question_info = (
|
|
515
|
+
f" (Question IDs: {', '.join(question_ids)})"
|
|
516
|
+
if question_ids
|
|
517
|
+
else ""
|
|
518
|
+
)
|
|
519
|
+
confirmation_message += (
|
|
520
|
+
f"\n Request {i}: Created item {item_id}{question_info}"
|
|
521
|
+
)
|
|
522
|
+
else:
|
|
523
|
+
confirmation_message += f"\n Request {i}: Operation completed"
|
|
524
|
+
|
|
525
|
+
return confirmation_message
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
@server.tool(
|
|
529
|
+
title="Batch Update Form",
|
|
530
|
+
annotations=ToolAnnotations(
|
|
531
|
+
readOnlyHint=False,
|
|
532
|
+
destructiveHint=True,
|
|
533
|
+
idempotentHint=False,
|
|
534
|
+
openWorldHint=True,
|
|
535
|
+
),
|
|
536
|
+
)
|
|
537
|
+
@handle_http_errors("batch_update_form", service_type="forms")
|
|
538
|
+
@require_google_service("forms", "forms")
|
|
539
|
+
async def batch_update_form(
|
|
540
|
+
service,
|
|
541
|
+
user_google_email: str,
|
|
542
|
+
form_id: str,
|
|
543
|
+
requests: List[Dict[str, Any]],
|
|
544
|
+
) -> str:
|
|
545
|
+
"""
|
|
546
|
+
Apply batch updates to a Google Form.
|
|
547
|
+
|
|
548
|
+
Supports adding, updating, and deleting form items, as well as updating
|
|
549
|
+
form metadata and settings. This is the primary method for modifying form
|
|
550
|
+
content after creation.
|
|
551
|
+
|
|
552
|
+
Args:
|
|
553
|
+
user_google_email (str): The user's Google email address. Required.
|
|
554
|
+
form_id (str): The ID of the form to update.
|
|
555
|
+
requests (List[Dict[str, Any]]): List of update requests to apply.
|
|
556
|
+
Supported request types:
|
|
557
|
+
- createItem: Add a new question or content item
|
|
558
|
+
- updateItem: Modify an existing item
|
|
559
|
+
- deleteItem: Remove an item
|
|
560
|
+
- moveItem: Reorder an item
|
|
561
|
+
- updateFormInfo: Update form title/description
|
|
562
|
+
- updateSettings: Modify form settings (e.g., quiz mode)
|
|
563
|
+
|
|
564
|
+
Returns:
|
|
565
|
+
str: Details about the batch update operation results.
|
|
566
|
+
"""
|
|
567
|
+
logger.info(
|
|
568
|
+
f"[batch_update_form] Invoked. Email: '{user_google_email}', "
|
|
569
|
+
f"Form ID: '{form_id}', Requests: {len(requests)}"
|
|
570
|
+
)
|
|
571
|
+
|
|
572
|
+
result = await _batch_update_form_impl(service, form_id, requests)
|
|
573
|
+
|
|
574
|
+
logger.info(f"Batch update completed successfully for {user_google_email}")
|
|
575
|
+
return result
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# This file marks the 'gmail' directory as a Python package.
|