Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
docs: fix gmail labeler code quality issues
Sushmitha Mallesh9f8df7a42ae9
diff --git a/docs/content/cookbooks/gmail-labeler.mdx b/docs/content/cookbooks/gmail-labeler.mdxindex ee4ffe549..aa647df6f 100644--- a/docs/content/cookbooks/gmail-labeler.mdx+++ b/docs/content/cookbooks/gmail-labeler.mdx@@ -42,16 +42,6 @@ Before labeling, we need to make sure the user has connected their Gmail account<include>../../examples/gmail-labeler/main.py#connect</include>-## Creating a trigger--The `create_trigger` function sets up a `GMAIL_NEW_GMAIL_MESSAGE` trigger that fires whenever a new email arrives. It prints the trigger ID you'll need for the listener.--<include>../../examples/gmail-labeler/main.py#trigger</include>--<Callout>-When you pass a `user_id`, the SDK automatically resolves the user's connected account for Gmail. You can also pass a `connected_account_id` directly for more control.-</Callout>-## Labeling with ClaudeFor each incoming email, `label_email` gets Gmail tools from `session.tools()`, wraps them in an MCP server with `create_sdk_mcp_server()`, and hands them to a Claude agent. The agent lists existing labels, picks the best fit (or creates a new one), and applies it.@@ -60,7 +50,7 @@ For each incoming email, `label_email` gets Gmail tools from `session.tools()`,## Listening for emails-`composio.triggers.subscribe()` opens a WebSocket connection. When a new email arrives, the handler calls `label_email` to classify and label it.+The `listen` function creates a trigger for new Gmail messages and subscribes to events via WebSocket. When a new email arrives, the handler calls `label_email` to classify and label it.<include>../../examples/gmail-labeler/main.py#listen</include>@@ -84,16 +74,10 @@ uv run --env-file .env python main.py connect defaultIf Gmail is not connected yet, you'll get an OAuth URL. Open it in your browser and authorize the app. If you've already connected, the script will print "Gmail is already connected."-Next, create a trigger:--```bash-uv run --env-file .env python main.py trigger default-```--Then start the listener with the trigger ID from the previous step:+Then start the listener:```bash-uv run --env-file .env python main.py listen default <trigger_id>+uv run --env-file .env python main.py listen default```Send yourself an email and watch the terminal — the agent will receive the event, inspect the email, and apply an appropriate label.diff --git a/docs/examples/gmail-labeler/main.py b/docs/examples/gmail-labeler/main.pyindex 4f9c48bd8..6dd976809 100644--- a/docs/examples/gmail-labeler/main.py+++ b/docs/examples/gmail-labeler/main.py@@ -29,19 +29,6 @@ def connect(user_id: str):# endregion connect-# region trigger-def create_trigger(user_id: str):- """Create a trigger for new Gmail messages."""- trigger = composio.triggers.create(- slug="GMAIL_NEW_GMAIL_MESSAGE",- user_id=user_id,- trigger_config={},- )- print(f"Trigger created: {trigger.trigger_id}")- return trigger.trigger_id-# endregion trigger--# region labelasync def label_email(session, message_id: str, subject: str, body: str):"""Use Claude to label an incoming email."""@@ -73,22 +60,34 @@ Steps:# region listen-def listen(user_id: str, trigger_id: str):- """Subscribe to trigger events and label incoming emails."""+def listen(user_id: str):+ """Create a trigger, subscribe to events, and label incoming emails."""session = composio.create(user_id=user_id, toolkits=["gmail"])++ trigger = composio.triggers.create(+ slug="GMAIL_NEW_GMAIL_MESSAGE",+ user_id=user_id,+ trigger_config={},+ )+ print(f"Trigger created: {trigger.trigger_id}")++ loop = asyncio.new_event_loop()subscription = composio.triggers.subscribe()- @subscription.handle(trigger_id=trigger_id)+ @subscription.handle(trigger_id=trigger.trigger_id)def handle_event(data):print(f"New email: {data.get('subject', 'No subject')}")- asyncio.run(- label_email(- session,- message_id=data.get("id", ""),- subject=data.get("subject", ""),- body=data.get("message_text", ""),+ try:+ loop.run_until_complete(+ label_email(+ session,+ message_id=data.get("id", ""),+ subject=data.get("subject", ""),+ body=data.get("message_text", ""),+ ))- )+ except Exception as e:+ print(f"Error labeling email: {e}")print("Listening for new emails...")subscription.wait_forever()@@ -99,8 +98,7 @@ if __name__ == "__main__":if len(sys.argv) < 2:print("Usage:")print(" python main.py connect <user_id>")- print(" python main.py trigger <user_id>")- print(" python main.py listen <user_id> <trigger_id>")+ print(" python main.py listen <user_id>")sys.exit(1)command = sys.argv[1]@@ -108,17 +106,10 @@ if __name__ == "__main__":if command == "connect":uid = sys.argv[2] if len(sys.argv) > 2 else "default"connect(uid)- elif command == "trigger":- uid = sys.argv[2] if len(sys.argv) > 2 else "default"- create_trigger(uid)elif command == "listen":uid = sys.argv[2] if len(sys.argv) > 2 else "default"- tid = sys.argv[3] if len(sys.argv) > 3 else None- if not tid:- print("Error: trigger_id is required. Run 'python main.py trigger <user_id>' first.")- sys.exit(1)- listen(uid, tid)+ listen(uid)else:print(f"Unknown command: {command}")- print("Use 'connect', 'trigger', or 'listen'.")+ print("Use 'connect' or 'listen'.")sys.exit(1)