workspace-mcp 1.0.1__py3-none-any.whl → 1.0.2__py3-none-any.whl

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.
auth/service_decorator.py CHANGED
@@ -193,29 +193,37 @@ def require_google_service(
193
193
  # Original authentication logic is handled automatically
194
194
  """
195
195
  def decorator(func: Callable) -> Callable:
196
+ # Inspect the original function signature
197
+ original_sig = inspect.signature(func)
198
+ params = list(original_sig.parameters.values())
199
+
200
+ # The decorated function must have 'service' as its first parameter.
201
+ if not params or params[0].name != 'service':
202
+ raise TypeError(
203
+ f"Function '{func.__name__}' decorated with @require_google_service "
204
+ "must have 'service' as its first parameter."
205
+ )
206
+
207
+ # Create a new signature for the wrapper that excludes the 'service' parameter.
208
+ # This is the signature that FastMCP will see.
209
+ wrapper_sig = original_sig.replace(parameters=params[1:])
210
+
196
211
  @wraps(func)
197
212
  async def wrapper(*args, **kwargs):
198
- # Extract user_google_email from function parameters
199
- sig = inspect.signature(func)
200
- param_names = list(sig.parameters.keys())
213
+ # Note: `args` and `kwargs` are now the arguments for the *wrapper*,
214
+ # which does not include 'service'.
201
215
 
202
- # Find user_google_email parameter
203
- user_google_email = None
204
- if 'user_google_email' in kwargs:
205
- user_google_email = kwargs['user_google_email']
206
- else:
207
- # Look for user_google_email in positional args
208
- try:
209
- user_email_index = param_names.index('user_google_email')
210
- if user_email_index < len(args):
211
- user_google_email = args[user_email_index]
212
- except ValueError:
213
- pass
216
+ # Extract user_google_email from the arguments passed to the wrapper
217
+ bound_args = wrapper_sig.bind(*args, **kwargs)
218
+ bound_args.apply_defaults()
219
+ user_google_email = bound_args.arguments.get('user_google_email')
214
220
 
215
221
  if not user_google_email:
216
- raise Exception("user_google_email parameter is required but not found")
222
+ # This should ideally not be reached if 'user_google_email' is a required parameter
223
+ # in the function signature, but it's a good safeguard.
224
+ raise Exception("'user_google_email' parameter is required but was not found.")
217
225
 
218
- # Get service configuration
226
+ # Get service configuration from the decorator's arguments
219
227
  if service_type not in SERVICE_CONFIGS:
220
228
  raise Exception(f"Unknown service type: {service_type}")
221
229
 
@@ -226,7 +234,7 @@ def require_google_service(
226
234
  # Resolve scopes
227
235
  resolved_scopes = _resolve_scopes(scopes)
228
236
 
229
- # Check cache first if enabled
237
+ # --- Service Caching and Authentication Logic (largely unchanged) ---
230
238
  service = None
231
239
  actual_user_email = user_google_email
232
240
 
@@ -236,7 +244,6 @@ def require_google_service(
236
244
  if cached_result:
237
245
  service, actual_user_email = cached_result
238
246
 
239
- # If not cached, authenticate
240
247
  if service is None:
241
248
  try:
242
249
  tool_name = func.__name__
@@ -247,30 +254,22 @@ def require_google_service(
247
254
  user_google_email=user_google_email,
248
255
  required_scopes=resolved_scopes,
249
256
  )
250
-
251
- # Cache the service if caching is enabled
252
257
  if cache_enabled:
253
258
  cache_key = _get_cache_key(user_google_email, service_name, service_version, resolved_scopes)
254
259
  _cache_service(cache_key, service, actual_user_email)
255
-
256
260
  except GoogleAuthenticationError as e:
257
261
  raise Exception(str(e))
258
262
 
259
- # Inject service as first parameter
260
- if 'service' in param_names:
261
- kwargs['service'] = service
262
- else:
263
- # Insert service as first positional argument
264
- args = (service,) + args
265
-
266
- # Call the original function with refresh error handling
263
+ # --- Call the original function with the service object injected ---
267
264
  try:
268
- return await func(*args, **kwargs)
265
+ # Prepend the fetched service object to the original arguments
266
+ return await func(service, *args, **kwargs)
269
267
  except RefreshError as e:
270
- # Handle token refresh errors gracefully
271
268
  error_message = _handle_token_refresh_error(e, actual_user_email, service_name)
272
269
  raise Exception(error_message)
273
270
 
271
+ # Set the wrapper's signature to the one without 'service'
272
+ wrapper.__signature__ = wrapper_sig
274
273
  return wrapper
275
274
  return decorator
276
275
 
core/utils.py CHANGED
@@ -160,3 +160,39 @@ def extract_office_xml_text(file_bytes: bytes, mime_type: str) -> Optional[str]:
160
160
  except Exception as e:
161
161
  logger.error(f"Failed to extract office XML text for {mime_type}: {e}", exc_info=True)
162
162
  return None
163
+
164
+ import functools
165
+ from googleapiclient.errors import HttpError
166
+
167
+ def handle_http_errors(tool_name: str):
168
+ """
169
+ A decorator to handle Google API HttpErrors in a standardized way.
170
+
171
+ It wraps a tool function, catches HttpError, logs a detailed error message,
172
+ and raises a generic Exception with a user-friendly message.
173
+
174
+ Args:
175
+ tool_name (str): The name of the tool being decorated (e.g., 'list_calendars').
176
+ This is used for logging purposes.
177
+ """
178
+ def decorator(func):
179
+ @functools.wraps(func)
180
+ async def wrapper(*args, **kwargs):
181
+ try:
182
+ return await func(*args, **kwargs)
183
+ except HttpError as error:
184
+ user_google_email = kwargs.get('user_google_email', 'N/A')
185
+ message = (
186
+ f"API error in {tool_name}: {error}. "
187
+ f"You might need to re-authenticate for user '{user_google_email}'. "
188
+ f"LLM: Try 'start_google_auth' with the user's email and the appropriate service_name."
189
+ )
190
+ logger.error(message, exc_info=True)
191
+ raise Exception(message)
192
+ except Exception as e:
193
+ # Catch any other unexpected errors
194
+ message = f"An unexpected error occurred in {tool_name}: {e}"
195
+ logger.exception(message)
196
+ raise Exception(message)
197
+ return wrapper
198
+ return decorator