apply ruff formatting
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""
|
||||
Google Chat MCP Tools Package
|
||||
"""
|
||||
|
||||
from . import chat_tools
|
||||
|
||||
__all__ = ['chat_tools']
|
||||
__all__ = ["chat_tools"]
|
||||
|
||||
@@ -3,6 +3,7 @@ Google Chat MCP Tools
|
||||
|
||||
This module provides MCP tools for interacting with Google Chat API.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import asyncio
|
||||
from typing import Optional
|
||||
@@ -16,6 +17,7 @@ from core.utils import handle_http_errors
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@server.tool()
|
||||
@require_google_service("chat", "chat_read")
|
||||
@handle_http_errors("list_spaces", service_type="chat")
|
||||
@@ -23,7 +25,7 @@ async def list_spaces(
|
||||
service,
|
||||
user_google_email: str,
|
||||
page_size: int = 100,
|
||||
space_type: str = "all" # "all", "room", "dm"
|
||||
space_type: str = "all", # "all", "room", "dm"
|
||||
) -> str:
|
||||
"""
|
||||
Lists Google Chat spaces (rooms and direct messages) accessible to the user.
|
||||
@@ -44,23 +46,22 @@ async def list_spaces(
|
||||
if filter_param:
|
||||
request_params["filter"] = filter_param
|
||||
|
||||
response = await asyncio.to_thread(
|
||||
service.spaces().list(**request_params).execute
|
||||
)
|
||||
response = await asyncio.to_thread(service.spaces().list(**request_params).execute)
|
||||
|
||||
spaces = response.get('spaces', [])
|
||||
spaces = response.get("spaces", [])
|
||||
if not spaces:
|
||||
return f"No Chat spaces found for type '{space_type}'."
|
||||
|
||||
output = [f"Found {len(spaces)} Chat spaces (type: {space_type}):"]
|
||||
for space in spaces:
|
||||
space_name = space.get('displayName', 'Unnamed Space')
|
||||
space_id = space.get('name', '')
|
||||
space_type_actual = space.get('spaceType', 'UNKNOWN')
|
||||
space_name = space.get("displayName", "Unnamed Space")
|
||||
space_id = space.get("name", "")
|
||||
space_type_actual = space.get("spaceType", "UNKNOWN")
|
||||
output.append(f"- {space_name} (ID: {space_id}, Type: {space_type_actual})")
|
||||
|
||||
return "\n".join(output)
|
||||
|
||||
|
||||
@server.tool()
|
||||
@require_google_service("chat", "chat_read")
|
||||
@handle_http_errors("get_messages", service_type="chat")
|
||||
@@ -69,7 +70,7 @@ async def get_messages(
|
||||
user_google_email: str,
|
||||
space_id: str,
|
||||
page_size: int = 50,
|
||||
order_by: str = "createTime desc"
|
||||
order_by: str = "createTime desc",
|
||||
) -> str:
|
||||
"""
|
||||
Retrieves messages from a Google Chat space.
|
||||
@@ -80,30 +81,27 @@ async def get_messages(
|
||||
logger.info(f"[get_messages] Space ID: '{space_id}' for user '{user_google_email}'")
|
||||
|
||||
# Get space info first
|
||||
space_info = await asyncio.to_thread(
|
||||
service.spaces().get(name=space_id).execute
|
||||
)
|
||||
space_name = space_info.get('displayName', 'Unknown Space')
|
||||
space_info = await asyncio.to_thread(service.spaces().get(name=space_id).execute)
|
||||
space_name = space_info.get("displayName", "Unknown Space")
|
||||
|
||||
# Get messages
|
||||
response = await asyncio.to_thread(
|
||||
service.spaces().messages().list(
|
||||
parent=space_id,
|
||||
pageSize=page_size,
|
||||
orderBy=order_by
|
||||
).execute
|
||||
service.spaces()
|
||||
.messages()
|
||||
.list(parent=space_id, pageSize=page_size, orderBy=order_by)
|
||||
.execute
|
||||
)
|
||||
|
||||
messages = response.get('messages', [])
|
||||
messages = response.get("messages", [])
|
||||
if not messages:
|
||||
return f"No messages found in space '{space_name}' (ID: {space_id})."
|
||||
|
||||
output = [f"Messages from '{space_name}' (ID: {space_id}):\n"]
|
||||
for msg in messages:
|
||||
sender = msg.get('sender', {}).get('displayName', 'Unknown Sender')
|
||||
create_time = msg.get('createTime', 'Unknown Time')
|
||||
text_content = msg.get('text', 'No text content')
|
||||
msg_name = msg.get('name', '')
|
||||
sender = msg.get("sender", {}).get("displayName", "Unknown Sender")
|
||||
create_time = msg.get("createTime", "Unknown Time")
|
||||
text_content = msg.get("text", "No text content")
|
||||
msg_name = msg.get("name", "")
|
||||
|
||||
output.append(f"[{create_time}] {sender}:")
|
||||
output.append(f" {text_content}")
|
||||
@@ -111,6 +109,7 @@ async def get_messages(
|
||||
|
||||
return "\n".join(output)
|
||||
|
||||
|
||||
@server.tool()
|
||||
@require_google_service("chat", "chat_write")
|
||||
@handle_http_errors("send_message", service_type="chat")
|
||||
@@ -119,7 +118,7 @@ async def send_message(
|
||||
user_google_email: str,
|
||||
space_id: str,
|
||||
message_text: str,
|
||||
thread_key: Optional[str] = None
|
||||
thread_key: Optional[str] = None,
|
||||
) -> str:
|
||||
"""
|
||||
Sends a message to a Google Chat space.
|
||||
@@ -129,29 +128,27 @@ async def send_message(
|
||||
"""
|
||||
logger.info(f"[send_message] Email: '{user_google_email}', Space: '{space_id}'")
|
||||
|
||||
message_body = {
|
||||
'text': message_text
|
||||
}
|
||||
message_body = {"text": message_text}
|
||||
|
||||
# Add thread key if provided (for threaded replies)
|
||||
request_params = {
|
||||
'parent': space_id,
|
||||
'body': message_body
|
||||
}
|
||||
request_params = {"parent": space_id, "body": message_body}
|
||||
if thread_key:
|
||||
request_params['threadKey'] = thread_key
|
||||
request_params["threadKey"] = thread_key
|
||||
|
||||
message = await asyncio.to_thread(
|
||||
service.spaces().messages().create(**request_params).execute
|
||||
)
|
||||
|
||||
message_name = message.get('name', '')
|
||||
create_time = message.get('createTime', '')
|
||||
message_name = message.get("name", "")
|
||||
create_time = message.get("createTime", "")
|
||||
|
||||
msg = f"Message sent to space '{space_id}' by {user_google_email}. Message ID: {message_name}, Time: {create_time}"
|
||||
logger.info(f"Successfully sent message to space '{space_id}' by {user_google_email}")
|
||||
logger.info(
|
||||
f"Successfully sent message to space '{space_id}' by {user_google_email}"
|
||||
)
|
||||
return msg
|
||||
|
||||
|
||||
@server.tool()
|
||||
@require_google_service("chat", "chat_read")
|
||||
@handle_http_errors("search_messages", service_type="chat")
|
||||
@@ -160,7 +157,7 @@ async def search_messages(
|
||||
user_google_email: str,
|
||||
query: str,
|
||||
space_id: Optional[str] = None,
|
||||
page_size: int = 25
|
||||
page_size: int = 25,
|
||||
) -> str:
|
||||
"""
|
||||
Searches for messages in Google Chat spaces by text content.
|
||||
@@ -173,13 +170,12 @@ async def search_messages(
|
||||
# If specific space provided, search within that space
|
||||
if space_id:
|
||||
response = await asyncio.to_thread(
|
||||
service.spaces().messages().list(
|
||||
parent=space_id,
|
||||
pageSize=page_size,
|
||||
filter=f'text:"{query}"'
|
||||
).execute
|
||||
service.spaces()
|
||||
.messages()
|
||||
.list(parent=space_id, pageSize=page_size, filter=f'text:"{query}"')
|
||||
.execute
|
||||
)
|
||||
messages = response.get('messages', [])
|
||||
messages = response.get("messages", [])
|
||||
context = f"space '{space_id}'"
|
||||
else:
|
||||
# Search across all accessible spaces (this may require iterating through spaces)
|
||||
@@ -187,21 +183,22 @@ async def search_messages(
|
||||
spaces_response = await asyncio.to_thread(
|
||||
service.spaces().list(pageSize=100).execute
|
||||
)
|
||||
spaces = spaces_response.get('spaces', [])
|
||||
spaces = spaces_response.get("spaces", [])
|
||||
|
||||
messages = []
|
||||
for space in spaces[:10]: # Limit to first 10 spaces to avoid timeout
|
||||
try:
|
||||
space_messages = await asyncio.to_thread(
|
||||
service.spaces().messages().list(
|
||||
parent=space.get('name'),
|
||||
pageSize=5,
|
||||
filter=f'text:"{query}"'
|
||||
).execute
|
||||
service.spaces()
|
||||
.messages()
|
||||
.list(
|
||||
parent=space.get("name"), pageSize=5, filter=f'text:"{query}"'
|
||||
)
|
||||
.execute
|
||||
)
|
||||
space_msgs = space_messages.get('messages', [])
|
||||
space_msgs = space_messages.get("messages", [])
|
||||
for msg in space_msgs:
|
||||
msg['_space_name'] = space.get('displayName', 'Unknown')
|
||||
msg["_space_name"] = space.get("displayName", "Unknown")
|
||||
messages.extend(space_msgs)
|
||||
except HttpError:
|
||||
continue # Skip spaces we can't access
|
||||
@@ -212,10 +209,10 @@ async def search_messages(
|
||||
|
||||
output = [f"Found {len(messages)} messages matching '{query}' in {context}:"]
|
||||
for msg in messages:
|
||||
sender = msg.get('sender', {}).get('displayName', 'Unknown Sender')
|
||||
create_time = msg.get('createTime', 'Unknown Time')
|
||||
text_content = msg.get('text', 'No text content')
|
||||
space_name = msg.get('_space_name', 'Unknown Space')
|
||||
sender = msg.get("sender", {}).get("displayName", "Unknown Sender")
|
||||
create_time = msg.get("createTime", "Unknown Time")
|
||||
text_content = msg.get("text", "No text content")
|
||||
space_name = msg.get("_space_name", "Unknown Space")
|
||||
|
||||
# Truncate long messages
|
||||
if len(text_content) > 100:
|
||||
@@ -223,4 +220,4 @@ async def search_messages(
|
||||
|
||||
output.append(f"- [{create_time}] {sender} in '{space_name}': {text_content}")
|
||||
|
||||
return "\n".join(output)
|
||||
return "\n".join(output)
|
||||
|
||||
Reference in New Issue
Block a user