Skip to content

refactor: separate file watcher from router #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

calmini
Copy link
Contributor

@calmini calmini commented May 7, 2025

continue from #141

Separate the file watcher from router, in the later refactor pr, the interaction will act as below:

class MCPRouter:

    def __init__(self):
        self.client_session_manager: MCPClientSessionManager = session_manager or MCPClientSessionManager.get_instance()
        self.notifier = ConfigUpdateNotifier.get_instance()
        self._unsubscribe = None

    async def initialize_router(self):
        ...
        # subscribe config update
        self._unsubscribe = self.notifier.subscribe(self._handle_config_update)
        ...

    async def _handle_config_update(self, config_type: ConfigType):
        """handles configuration update"""
        logger.info(f"Received configuration update from {config_type}")
        if config_type == ConfigType.FILE:
            self.profile_manager.reload()
            servers_wait_for_update = self.get_unique_servers()
            servers_add, servers_remove = await self.client_session_manager.update_sessions(servers_wait_for_update)
            logger.info(f"Updated servers: {','.join(servers_add)} added, {','.join(servers_remove)} removed")
        elif config_type == ConfigType.CLOUD:
            logger.warning("Cloud config update is not supported yet")

and inside of app scope

@asynccontextmanager
async def lifespan(app):
    ...
    global router
    router = MCPRouter(session_manager=client_session_manager)
    if RELOAD:
        global watcher
        watcher = FileConfigWatcher(router.profile_manager.profile_path)
        await watcher.start()
    ...

@calmini calmini requested a review from GabrielDrapor May 7, 2025 09:24
def subscribe(self, callback: CallableT):
if callback not in self._subscribers:
self._subscribers.append(callback)
return lambda: self.unsubscribe(callback)
Copy link
Contributor

Choose a reason for hiding this comment

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

looks a little strange to return the unsubscribe function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

just for convenience to call when router shutdown

unsubscribe = notifier.subscribe(callback)
# when shutdown
unsubscribe()

@calmini calmini force-pushed the calmini/ref-router-core-step-2 branch from 9b6c959 to a43e28b Compare May 8, 2025 10:54
@calmini calmini closed this May 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants