MCP loginAsUser tool consistently times out with "invalid credentials" after server restarts
Bug Report: loginAsUser Tool Authentication Failures
Summary
The loginAsUser MCP tool consistently fails with authentication timeouts after MCP server restarts, while admin login works correctly. The credentials are verified to work via direct PHP authentication, indicating this is a browser automation issue rather than actual credential problems.
Environment
- Claude Code version: Latest
- MCP Server: Custom Moodle integration server
- Platform: macOS (Darwin 24.5.0)
- Browser automation: Playwright with Chromium
Bug Details
Symptoms
mcp__Kai_the_AI__loginAsUserconsistently times out with "invalid credentials"mcp__Kai_the_AI__login(admin login) works perfectly- Same student credentials work when tested via PHP CLI
- Issue occurs consistently after MCP server restarts
- Manual browser login with same credentials works fine
Expected Behavior
Student users should be able to log in via loginAsUser tool just like admin login works.
Actual Behavior
Tool execution failed: Failed to login user brown2: TimeoutError: page.waitForURL: Timeout 30000ms exceeded.
=========================== logs ===========================
waiting for navigation until "load"
navigated to "http://localhost:8000/login/index.php"
============================================================
Steps to Reproduce
- Set up MCP server with Moodle integration
- Create a test user:
```
mcp__Kai_the_AI__create_user:
- username: testuser
- firstname: Test
- lastname: User
- email: testuser@test.com
```
- Restart MCP server (this seems to trigger the issue)
- Test admin login (this works):
````
mcp__Kai_the_AI__login
- Test student login (this fails):
```
mcp__Kai_the_AI__loginAsUser:
- username: testuser
- password: TestUser@123
```
Verification Steps
The credentials are valid - this can be verified via PHP:
docker exec moodle-webserver php -r 'define("CLI_SCRIPT", true); require_once("/var/www/html/config.php"); global $DB; $auth = get_auth_plugin("manual"); $result = $auth->user_login("testuser", "TestUser@123"); echo "Login test: " . ($result ? "SUCCESS" : "FAILED") . "\n";'
Result: Login test: SUCCESS
Root Cause Analysis
Based on code investigation, the issue stems from differences between the login tools:
Admin Login (login tool)
- Uses
SharedBrowserManagerwith robust session recovery - Reuses persistent browser context
- Has better error handling for browser disconnections
Student Login (loginAsUser tool)
- Uses
MultiUserBrowserManagerwith separate contexts per user - Creates fresh browser context each time
- Less robust session recovery after browser disconnections
- No retry logic for failed logins
Technical Details
The authentication timeout suggests the MultiUserBrowserManager is not properly handling:
- Browser context reinitialization after server restarts
- Session recovery when browser connections are lost
- Timing issues in the login flow navigation
Workaround
For immediate functionality, admin login can be used to perform actions on behalf of students, but this is not ideal for user simulation scenarios.
Impact
This prevents realistic student simulation workflows and multi-user testing scenarios in Moodle integrations.
Additional Context
- Issue appeared after implementing enhanced forum posting with section-based navigation
- Container recreation during time changes also triggers this issue
- The problem is reproducible across different student accounts
- Manual browser testing with the same credentials works fine
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗