Skip to content

fix: respect account selection in MCP server and CLI --account flag#223

Merged
jpoehnelt merged 2 commits intogoogleworkspace:mainfrom
haunchen:fix/multi-account-selection
Mar 6, 2026
Merged

fix: respect account selection in MCP server and CLI --account flag#223
jpoehnelt merged 2 commits intogoogleworkspace:mainfrom
haunchen:fix/multi-account-selection

Conversation

@haunchen
Copy link
Contributor

@haunchen haunchen commented Mar 6, 2026

Summary

Test plan

  • Existing filter_args_for_subcommand tests updated and passing
  • New tests added: test_filter_args_account_before_service, test_filter_args_account_equals_before_service
  • cargo clippy -- -D warnings clean
  • cargo test passes (1 pre-existing failure in test_get_quota_project_reads_adc unrelated to this change)

…oogleworkspace#221, googleworkspace#181)

MCP server now reads GOOGLE_WORKSPACE_CLI_ACCOUNT env var and passes it
to get_token instead of always using None (default account).

CLI filter_args_for_subcommand now dynamically locates the service name
instead of hardcoding skip(2), fixing --account before service name
causing unrecognized subcommand errors.
@haunchen haunchen requested a review from jpoehnelt as a code owner March 6, 2026 00:24
@changeset-bot
Copy link

changeset-bot bot commented Mar 6, 2026

🦋 Changeset detected

Latest commit: b53c6d7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@googleworkspace/cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@googleworkspace-bot googleworkspace-bot added area: mcp area: core Core CLI parsing, commands, error handling, utilities labels Mar 6, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses critical issues related to multi-account handling within the Google Workspace CLI. It enhances the MCP server's ability to respect user-defined accounts via environment variables and refines the CLI's argument parsing to correctly interpret account flags regardless of their position relative to the service name. These changes significantly improve the flexibility and reliability of account selection across the tool.

Highlights

  • MCP Server Account Selection: The MCP server (gws mcp) now correctly reads the GOOGLE_WORKSPACE_CLI_ACCOUNT environment variable and passes it to the get_token function, ensuring that the specified account is used instead of always defaulting.
  • CLI Argument Parsing: The CLI's argument parsing logic has been improved to correctly handle the --account flag when it is placed before the service name (e.g., gws --account user@corp.com drive files list), preventing unrecognized subcommand errors. This was achieved by dynamically locating the service name instead of hardcoding argument skipping.
Changelog
  • .changeset/fix-multi-account-selection.md
    • Added a new changeset file to document the multi-account selection fixes.
  • src/main.rs
    • Modified the filter_args_for_subcommand function signature to accept the service name as an argument.
    • Updated the filter_args_for_subcommand implementation to dynamically skip the service name, resolving issues with --account flag placement.
    • Adjusted all existing calls to filter_args_for_subcommand to pass the first_arg (service name).
    • Introduced new test cases (test_filter_args_account_before_service, test_filter_args_account_equals_before_service) to validate the improved argument parsing for the --account flag.
  • src/mcp_server.rs
    • Updated the execute_mcp_method function to retrieve the GOOGLE_WORKSPACE_CLI_ACCOUNT environment variable.
    • Modified the call to crate::auth::get_token to pass the retrieved account from the environment variable, enabling account selection for the MCP server.
Activity
  • Existing filter_args_for_subcommand tests were updated and are passing.
  • New tests were added for test_filter_args_account_before_service and test_filter_args_account_equals_before_service.
  • cargo clippy -- -D warnings runs clean.
  • cargo test passes, with one pre-existing unrelated failure noted.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Generative AI Prohibited Use Policy, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces two important fixes for argument parsing. The MCP server now correctly respects the GOOGLE_WORKSPACE_CLI_ACCOUNT environment variable for multi-account support. Additionally, the CLI is now more robust in handling the --account flag when it's placed before the service name, preventing parsing errors.

The changes are well-implemented and include corresponding test updates. I've identified one high-severity issue where the argument parsing logic for finding the service name is still incomplete, as it doesn't handle the --api-version flag appearing before the service name. Addressing this would make the global flag handling fully consistent and robust.

The first_arg loop only skipped --account but not --api-version,
so `gws --api-version v3 drive ...` would misidentify --api-version
as the service name. Now both global flags are consistently skipped.
@googleworkspace-bot
Copy link
Collaborator

/gemini review

@jpoehnelt jpoehnelt merged commit c86b964 into googleworkspace:main Mar 6, 2026
30 checks passed
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces two important fixes for account selection. First, it allows the --account and --api-version global flags to be placed before the service name, which improves the CLI's usability. The argument filtering logic in src/main.rs has been correctly updated to support this. Second, the MCP server now respects the GOOGLE_WORKSPACE_CLI_ACCOUNT environment variable, which fixes an issue where it would always use the default account.

However, I've found a critical issue with the implementation. While the MCP server now respects the environment variable, it does not handle the global --account flag. This leads to an inconsistent user experience, as the flag does not work for the mcp subcommand. I've left a detailed comment on how this could be addressed.

Comment on lines +817 to +818
let account = std::env::var("GOOGLE_WORKSPACE_CLI_ACCOUNT").ok();
let (token, auth_method) = match crate::auth::get_token(&scopes, account.as_deref()).await {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This change correctly makes the MCP server respect the GOOGLE_WORKSPACE_CLI_ACCOUNT environment variable. However, it overlooks the global --account flag, leading to inconsistent behavior.

Currently, if a user runs gws --account user@corp.com mcp, the command will fail with an unrecognized subcommand error because the mcp command's argument parser does not know about the --account flag.

For --account to work as a truly global flag, the mcp subcommand should also handle it. The account value is already extracted in main.rs, but it's not made available to the mcp_server logic when invoked via the CLI flag.

A potential solution would involve:

  1. In main.rs, setting the GOOGLE_WORKSPACE_CLI_ACCOUNT environment variable from the account variable that's already determined. This would make the --account flag's value available to any part of the code that reads this environment variable.
  2. Updating the argument parser for the mcp command (in build_mcp_cli) to accept and ignore the --account flag to prevent parsing errors.

Without this, the behavior of the --account flag is inconsistent across the CLI, which can be very confusing for users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core Core CLI parsing, commands, error handling, utilities area: mcp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP server ignores GOOGLE_WORKSPACE_CLI_ACCOUNT env var Bug: multiple accounts don't work as described

3 participants