[Overview]
This plan outlines the process to refactor the Shufersal grocery provider's cart management logic in mcp_groceries_server/server/providers/shufersal/_service.py to leverage Playwright for update_cart and clear_cart operations, triggering AJAX calls on shufersal.co.il. This approach involves directly integrating Playwright into the Python codebase, managing browser instances within Python, and executing JavaScript code in the browser context to interact with the Shufersal website's AJAX functionalities. This is a revised plan based on the constraint that the shufersal-mcp TypeScript project cannot be modified.
[Types]
No new complex types are introduced. Existing CartItemSchema will continue to be used as input for update_cart.
[Files] This plan involves modifications to Python files:
- Modified file:
pyproject.toml- Addplaywrightas a dependency. - Modified file:
mcp_groceries_server/server/providers/shufersal/_service.py- This file will be significantly refactored to:- Import
playwrightmodules. - Implement a mechanism to launch, manage, and close a Playwright browser instance.
- Update
clear_cartto use Playwright'spage.evaluateto execute afetchPOST request to/cart/remove. - Update
update_cartto use Playwright'spage.evaluateto execute thewindow.ajaxCallfor adding/updating items. - The existing
_requestfunction will be kept forsearchandget_cart(if uncommented), as those do not require browser interaction.
- Import
- Modified file:
mcp_groceries_server/server/providers/shufersal/tools.py- Will be updated to call the modified_service.pyfunctions, ensuringremove_items_from_cartusesclear_cartorupdate_cartwithqty: 0as appropriate.
[Functions]
- New functions (Python):
launch_browser(): An asynchronous function to launch a Playwright browser instance and return aPageobject. This will handle browser setup and potentially reuse an existing browser instance.close_browser(): An asynchronous function to close the Playwright browser instance._execute_browser_script(page: Page, script: str, args: Any): A utility function to execute JavaScript within the browser page and return the result.
- Modified function (Python):
clear_cartinmcp_groceries_server/server/providers/shufersal/_service.py. This function will be updated to:- Get a Playwright
Pageinstance. - Call
page.evaluateto perform afetchPOST request to/cart/removewith appropriate headers (e.g.,Content-Type).
- Get a Playwright
- Modified function (Python):
update_cartinmcp_groceries_server/server/providers/shufersal/_service.py. This function will be updated to:- Get a Playwright
Pageinstance. - For each item in the
itemslist, callpage.evaluateto execute thewindow.ajaxCallwith the product details.
- Get a Playwright
- Modified function (Python):
remove_items_from_cartinmcp_groceries_server/server/providers/shufersal/tools.py. This function will be updated to utilize the modifiedclear_cartor `<write_to_file> implementation_plan.md
[Overview]
This plan outlines the process to refactor the Shufersal grocery provider's cart management logic in mcp_groceries_server/server/providers/shufersal/_service.py to leverage Playwright for update_cart and clear_cart operations, triggering AJAX calls on shufersal.co.il. This approach involves directly integrating Playwright into the Python codebase, managing browser instances within Python, and executing JavaScript code in the browser context to interact with the Shufersal website's AJAX functionalities. This is a revised plan based on the constraint that the shufersal-mcp TypeScript project cannot be modified.
[Types]
No new complex types are introduced. Existing CartItemSchema will continue to be used as input for update_cart.
[Files] This plan involves modifications to Python files:
- Modified file:
pyproject.toml- Addplaywrightas a dependency. - Modified file:
mcp_groceries_server/server/providers/shufersal/_service.py- This file will be significantly refactored to:- Import
playwrightmodules. - Implement a mechanism to launch, manage, and close a Playwright browser instance.
- Update
clear_cartto use Playwright'spage.evaluateto execute afetchPOST request to/cart/remove. - Update
update_cartto use Playwright'spage.evaluateto execute thewindow.ajaxCallfor adding/updating items. - The existing
_requestfunction will be kept forsearchandget_cart(if uncommented), as those do not require browser interaction.
- Import
- Modified file:
mcp_groceries_server/server/providers/shufersal/tools.py- Will be updated to call the modified_service.pyfunctions, ensuringremove_items_from_cartusesclear_cartorupdate_cartwithqty: 0as appropriate.
[Functions]
- New functions (Python):
launch_browser(): An asynchronous function to launch a Playwright browser instance and return aPageobject. This will handle browser setup and potentially reuse an existing browser instance.close_browser(): An asynchronous function to close the Playwright browser instance._execute_browser_script(page: Page, script: str, args: Any): A utility function to execute JavaScript within the browser page and return the result.
- Modified function (Python):
clear_cartinmcp_groceries_server/server/providers/shufersal/_service.py. This function will be updated to:- Get a Playwright
Pageinstance. - Call
page.evaluateto perform afetchPOST request to/cart/removewith appropriate headers (e.g.,Content-Type).
- Get a Playwright
- Modified function (Python):
update_cartinmcp_groceries_server/server/providers/shufersal/_service.py. This function will be updated to:- Get a Playwright
Pageinstance. - For each item in the
itemslist, callpage.evaluateto execute thewindow.ajaxCallwith the product details.
- Get a Playwright
- Modified function (Python):
remove_items_from_cartinmcp_groceries_server/server/providers/shufersal/tools.py. This function will be updated to utilize the modifiedclear_cartorupdate_cartmethods from_service.py. Ifitemsis empty, it should callclear_cart. Otherwise, it should callupdate_cartwithqty: 0for each item.
[Classes]
No new classes are introduced. The ShufersalProvider class methods will call the modified service functions.
[Dependencies]
- New Python Dependency:
playwright. This will be added topyproject.toml. - After adding
playwright,uv.lockwill need to be updated by runninguv lock. - Browser binaries for Playwright will need to be installed by running
playwright install.
[Testing]
- New unit tests will be required for
clear_cartandupdate_cartinmcp_groceries_server/server/providers/shufersal/_service.pyto verify their Playwright-based implementation. These tests will need to mock Playwright browser and page interactions. - Existing tests for
ShufersalProviderinmcp_groceries_server/server/providers/shufersal/tools.pywill be updated to reflect the new underlying implementation, potentially mocking the_service.pycalls.
[Implementation Order]
- Add
playwrighttopyproject.tomland runuv lockandplaywright install. - Refactor
mcp_groceries_server/server/providers/shufersal/_service.py: a. Importplaywrightmodules. b. Implementlaunch_browser()andclose_browser()for browser management. c. Implement_execute_browser_script(). d. Updateclear_cartto use Playwright with afetchcall. e. Updateupdate_cartto use Playwright withwindow.ajaxCall. f. Update the existing_requestfunction to only be used forsearchmethod. - Update
mcp_groceries_server/server/providers/shufersal/tools.pyto use the refactored_service.pymethods correctly, especially forremove_items_from_cart. - Add new unit tests and update existing ones for the Playwright integration.