fix(email): add OAuth XOAUTH2 support to MCP email server#5472
fix(email): add OAuth XOAUTH2 support to MCP email server#5472rudra496 wants to merge 3 commits into
Conversation
|
Thanks for jumping on this — I filed #4992 and had started on the same fix, so great to see it moving. The diagnosis matches what I found, and the IMAP/SMTP XOAUTH2 branches and Two things I'd suggest folding in before merge: 1. Refreshed tokens don't get back into the cached config. 2. The changed MCP code isn't covered by any test. I have a branch with the cache handling and these MCP tests already written — happy to share the patch or open them as a follow-up, whichever's easier. Thanks again for driving this. 🙏 |
|
Thanks for the thorough review, Alain — both points are spot on. |
Summary
The agent's MCP email tools couldn't access Gmail Workspace accounts set up via OAuth. The UI routes (
routes/email_helpers.py) handle OAuth correctly, but the MCP email server (mcp_servers/email_server.py) completely ignored the OAuth layer:_read_accounts_from_db()— the SQL query didn't select theoauth_provider,oauth_access_token,oauth_refresh_token, oroauth_token_expirycolumns, so OAuth data was never loaded._imap_connect()— always usedconn.login(user, password)with no XOAUTH2 path._smtp_connect()— same: alwaysconn.login(user, password), no XOAUTH2._smtp_ready()— requiredsmtp_password, rejecting OAuth accounts that don't have one.Fix (all in
mcp_servers/email_server.py):ownerandsmtp_securityhandling for older DBs).oauth_provider,oauth_access_token,oauth_refresh_token,oauth_token_expiryinto cfg in_load_config()(decrypted viasrc.secret_storage)._imap_connect(): whenoauth_provider == "google", useconn.authenticate("XOAUTH2", ...)with the token from_get_valid_google_token()— reusing the same refresh logic as the UI routes._smtp_connect(): same XOAUTH2 path viaconn.auth("XOAUTH2", ...)._smtp_ready(): OAuth accounts only needsmtp_host+smtp_user(no password required).The XOAUTH2 helpers (
_get_valid_google_token,_xoauth2_bytes,_xoauth2_raw) are imported fromroutes.email_helpersso the MCP server and UI share the same token-refresh and auth logic — no duplication.Target branch
dev, notmain.Linked Issue
Fixes #4992
Type of Change
Checklist
devpython -m pytest tests/test_email_oauth.py tests/test_email_smtp_security.py tests/test_email_send_only_no_inbox.py tests/test_email_imap_timeout.py -v— all 39 tests pass.How to Test
python -m pytest tests/test_email_oauth.py -v— 28 OAuth tests pass, includingtest_smtp_ready_true_for_oauth_account_without_password,test_imap_connect_uses_xoauth2_for_oauth_accounts, andtest_get_valid_google_token_refreshes_when_expired.python -m pytest tests/test_email_smtp_security.py tests/test_email_send_only_no_inbox.py tests/test_email_imap_timeout.py -v— 11 connection/security tests pass.python -m py_compile mcp_servers/email_server.py— syntax check passes.Visual / UI changes — REQUIRED if you touched anything that renders
Not applicable: backend MCP email server only.