3131 get_config ,
3232 get_keyring_name ,
3333)
34- from linkedin_mcp_server .drivers .chrome import close_all_drivers , get_or_create_driver
34+
35+ # Chrome driver imports are now handled by the scraper factory
3536from linkedin_mcp_server .exceptions import CredentialsNotFoundError , LinkedInMCPError
3637from linkedin_mcp_server .logging_config import configure_logging
3738from linkedin_mcp_server .server import create_mcp_server , shutdown_handler
39+ from linkedin_mcp_server .scraper_factory import (
40+ cleanup_scraper_backend ,
41+ get_backend_capabilities ,
42+ initialize_scraper_backend ,
43+ )
3844from linkedin_mcp_server .setup import run_cookie_extraction_setup , run_interactive_setup
3945
4046sys .stdout = io .TextIOWrapper (sys .stdout .buffer , encoding = "utf-8" )
@@ -242,33 +248,38 @@ def ensure_authentication_ready() -> str:
242248 return run_interactive_setup ()
243249
244250
245- def initialize_driver_with_auth (authentication : str ) -> None :
251+ def initialize_backend_with_auth (authentication : str ) -> None :
246252 """
247- Phase 2: Initialize driver using existing authentication.
253+ Phase 2: Initialize scraper backend using existing authentication.
248254
249255 Args:
250- authentication: LinkedIn session cookie
256+ authentication: LinkedIn session cookie (not used directly, backends get auth via ensure_authentication())
251257
252258 Raises:
253- Various exceptions if driver creation or login fails
259+ Various exceptions if backend initialization fails
254260 """
255261 config = get_config ()
256262
257263 if config .server .lazy_init :
264+ backend_info = get_backend_capabilities ()
258265 logger .info (
259- "Using lazy initialization - driver will be created on first tool call"
266+ f "Using lazy initialization - { backend_info [ 'backend' ] } will be created on first tool call"
260267 )
261268 return
262269
263- logger .info ("Initializing Chrome WebDriver and logging in..." )
270+ backend_info = get_backend_capabilities ()
271+ logger .info (f"Initializing { backend_info ['backend' ]} backend..." )
264272
265273 try :
266- # Create driver and login with provided authentication
267- get_or_create_driver (authentication )
268- logger .info ("✅ Web driver initialized and authenticated successfully" )
274+ # Initialize the appropriate backend (authentication is handled internally)
275+ success = initialize_scraper_backend ()
276+ if success :
277+ logger .info ("✅ Scraper backend initialized and authenticated successfully" )
278+ else :
279+ raise Exception ("Backend initialization returned False" )
269280
270281 except Exception as e :
271- logger .error (f"Failed to initialize driver : { e } " )
282+ logger .error (f"Failed to initialize scraper backend : { e } " )
272283 raise e
273284
274285
@@ -345,21 +356,21 @@ def main() -> None:
345356 print ("\n ❌ Setup failed - please try again" )
346357 sys .exit (1 )
347358
348- # Phase 2: Initialize Driver (if not lazy)
359+ # Phase 2: Initialize Backend (if not lazy)
349360 try :
350- initialize_driver_with_auth (authentication )
361+ initialize_backend_with_auth (authentication )
351362 except InvalidCredentialsError as e :
352- logger .error (f"Driver initialization failed with invalid credentials: { e } " )
363+ logger .error (f"Backend initialization failed with invalid credentials: { e } " )
353364
354- # Cookie was already cleared in driver layer
365+ # Cookie was already cleared in authentication layer
355366 # In interactive mode, try setup again
356367 if config .is_interactive :
357368 print (f"\n ❌ { str (e )} " )
358369 print ("🔄 Starting interactive setup for new authentication..." )
359370 try :
360371 new_authentication = run_interactive_setup ()
361372 # Try again with new authentication
362- initialize_driver_with_auth (new_authentication )
373+ initialize_backend_with_auth (new_authentication )
363374 logger .info ("✅ Successfully authenticated with new credentials" )
364375 except Exception as setup_error :
365376 logger .error (f"Setup failed: { setup_error } " )
@@ -377,13 +388,13 @@ def main() -> None:
377388 RateLimitError ,
378389 LoginTimeoutError ,
379390 ) as e :
380- logger .error (f"Driver initialization failed: { e } " )
391+ logger .error (f"Backend initialization failed: { e } " )
381392 print (f"\n ❌ { str (e )} " )
382393 if not config .server .lazy_init :
383394 sys .exit (1 )
384395 except Exception as e :
385- logger .error (f"Unexpected error during driver initialization: { e } " )
386- print (f"\n ❌ Driver initialization failed: { e } " )
396+ logger .error (f"Unexpected error during backend initialization: { e } " )
397+ print (f"\n ❌ Backend initialization failed: { e } " )
387398 if not config .server .lazy_init :
388399 sys .exit (1 )
389400
@@ -437,8 +448,8 @@ def exit_gracefully(exit_code: int = 0) -> None:
437448 """Exit the application gracefully, cleaning up resources."""
438449 print ("👋 Shutting down LinkedIn MCP server..." )
439450
440- # Clean up drivers
441- close_all_drivers ()
451+ # Clean up scraper backend
452+ cleanup_scraper_backend ()
442453
443454 # Clean up server
444455 shutdown_handler ()
0 commit comments