hd claim.
Set up the project
Use Python 3.12 or Node.js 24.17 or newer. Install in a new project, using a virtual environment for Python:- Python
- TypeScript
users.getProfile endpoint with me, which selects the authenticated mailbox. It reads the returned emailAddress. The endpoint requires a Gmail scope such as https://www.googleapis.com/auth/gmail.readonly; openid and email alone don’t grant this access. Ensure your Gmail auth config requests gmail.readonly if the agent will also read messages. See Controlling scopes to configure scopes and reconnect after changing them.
Connect and check the account
Replaceexample.com in the script with your allowed domain. Run the script, open the printed Connect Link, and complete authentication within five minutes. The script polls its own connection request, so it doesn’t need an HTTP callback handler.
- Python
- TypeScript
Save as
restrict_gmail.py:restrict_gmail.py
Connected as alice@example.com. Agent tools are ready for this account. Pass the returned tools to your agent framework using this approved session. The default SDK provider returns OpenAI-compatible tools; configure your framework’s provider when constructing Composio if needed.
For a rejected account, the script disables the connection and exits before creating the agent session. If disabling fails, the script stops with that error and still doesn’t create the agent session. Resolve the failure before retrying. Disabling a connection blocks Composio tool execution; it doesn’t revoke Google’s OAuth grant.
Keep the check in the execution path
The onboarding session never supplies tools to an agent. The second session selects the approved account explicitly and disables in-chat connection management, so the agent can’t connect another account through that session. Don’t expose the onboarding session to clients or reuse other sessions that can select unchecked accounts. Apply the same validation whenever you accept a new or replacement connection. The domain comparison ignores case and requires an exact match.alice@EXAMPLE.COM passes; alice@gmail.com, alice@sub.example.com, and alice@notexample.com don’t. Missing or malformed profile data and failed profile requests also prevent approval. An alias such as work-gmail or the account’s displayName isn’t the input to this decision.
To display identity without a domain policy, see Identify the connected account. For session account selection, see Managing multiple connected accounts.