-
Notifications
You must be signed in to change notification settings - Fork 54
fix(chat)!: hold mcp client sessions #140
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| from fastapi import Depends | ||
|
|
||
| from askui.chat.api.mcp_clients.manager import McpClientManagerManager | ||
| from askui.chat.api.mcp_configs.dependencies import McpConfigServiceDep | ||
| from askui.chat.api.mcp_configs.service import McpConfigService | ||
|
|
||
|
|
||
| def get_mcp_client_manager_manager( | ||
| mcp_config_service: McpConfigService = McpConfigServiceDep, | ||
| ) -> McpClientManagerManager: | ||
| return McpClientManagerManager(mcp_config_service) | ||
|
|
||
|
|
||
| McpClientManagerManagerDep = Depends(get_mcp_client_manager_manager) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import types | ||
| from datetime import timedelta | ||
| from typing import Any, Type | ||
|
|
||
| import anyio | ||
| import mcp | ||
| from fastmcp import Client | ||
| from fastmcp.client.client import CallToolResult, ProgressHandler | ||
| from fastmcp.exceptions import ToolError | ||
| from fastmcp.mcp_config import MCPConfig | ||
|
|
||
| from askui.chat.api.mcp_configs.service import McpConfigService | ||
| from askui.chat.api.models import WorkspaceId | ||
|
|
||
| McpServerName = str | ||
|
|
||
|
|
||
| class McpServerConnectionError(Exception): | ||
| """Exception raised when a MCP server connection fails.""" | ||
|
|
||
| def __init__(self, mcp_server_name: McpServerName, error: Exception): | ||
| super().__init__(f"Failed to connect to MCP server: {mcp_server_name}: {error}") | ||
| self.mcp_server_name = mcp_server_name | ||
| self.error = error | ||
|
|
||
|
|
||
| class McpClientManager: | ||
| def __init__( | ||
| self, mcp_clients: dict[McpServerName, Client[Any]] | None = None | ||
| ) -> None: | ||
| self._mcp_clients = mcp_clients or {} | ||
| self._tools: dict[McpServerName, list[mcp.types.Tool]] = {} | ||
|
|
||
| @classmethod | ||
| def from_config(cls, mcp_config: MCPConfig) -> "McpClientManager": | ||
| mcp_clients: dict[McpServerName, Client[Any]] = { | ||
| mcp_server_name: Client(mcp_server_config.to_transport()) | ||
| for mcp_server_name, mcp_server_config in mcp_config.mcpServers.items() | ||
| } | ||
| return cls(mcp_clients) | ||
|
|
||
| async def connect(self) -> "McpClientManager": | ||
| for mcp_server_name, mcp_client in self._mcp_clients.items(): | ||
| try: | ||
| await mcp_client._connect() # noqa: SLF001 | ||
| except Exception as e: # noqa: PERF203 | ||
| raise McpServerConnectionError(mcp_server_name, e) from e | ||
| return self | ||
|
|
||
| async def disconnect(self, force: bool = False) -> None: | ||
| for mcp_client in self._mcp_clients.values(): | ||
| if mcp_client.is_connected(): | ||
| await mcp_client._disconnect(force) # noqa: SLF001 | ||
|
|
||
| async def list_tools( | ||
| self, | ||
| ) -> list[mcp.types.Tool]: | ||
| tools: list[mcp.types.Tool] = [] | ||
| for mcp_server_name, mcp_client in self._mcp_clients.items(): | ||
| if mcp_server_name not in self._tools: | ||
| self._tools[mcp_server_name] = await mcp_client.list_tools() | ||
| tools.extend(self._tools[mcp_server_name]) | ||
| return tools | ||
|
|
||
| async def call_tool( | ||
| self, | ||
| name: str, | ||
| arguments: dict[str, Any] | None = None, | ||
| timeout: timedelta | float | None = None, # noqa: ASYNC109 | ||
| progress_handler: ProgressHandler | None = None, | ||
| raise_on_error: bool = True, | ||
| ) -> CallToolResult: | ||
| for mcp_server_name, tools in self._tools.items(): # Make lookup faster | ||
| for tool in tools: | ||
| if tool.name == name: | ||
| return await self._mcp_clients[mcp_server_name].call_tool( | ||
| name, | ||
| arguments, | ||
| timeout, | ||
| progress_handler, | ||
| raise_on_error, | ||
| ) | ||
| error_msg = f"Unknown tool: {name}" | ||
| if raise_on_error: | ||
| raise ToolError(error_msg) | ||
| return CallToolResult( | ||
| content=[mcp.types.TextContent(type="text", text=error_msg)], | ||
| structured_content=None, | ||
| data=None, | ||
| is_error=True, | ||
| ) | ||
|
|
||
| async def __aenter__(self) -> "McpClientManager": | ||
| return await self.connect() | ||
|
|
||
| async def __aexit__( | ||
| self, | ||
| exc_type: Type[BaseException] | None, | ||
| exc_value: BaseException | None, | ||
| traceback: types.TracebackType | None, | ||
| ) -> None: | ||
| await self.disconnect() | ||
|
|
||
|
|
||
| McpClientManagerKey = str | ||
|
|
||
|
|
||
| class McpClientManagerManager: | ||
| _mcp_client_managers: dict[McpClientManagerKey, McpClientManager | None] = {} | ||
| _lock: anyio.Lock = anyio.Lock() | ||
|
|
||
| def __init__(self, mcp_config_service: McpConfigService) -> None: | ||
| self._mcp_config_service = mcp_config_service | ||
|
|
||
| async def get_mcp_client_manager( | ||
| self, workspace_id: WorkspaceId | None | ||
| ) -> McpClientManager | None: | ||
| key: McpClientManagerKey = ( | ||
| f"workspace_{workspace_id}" if workspace_id else "global" | ||
| ) | ||
| if key in McpClientManagerManager._mcp_client_managers: | ||
| return McpClientManagerManager._mcp_client_managers[key] | ||
|
|
||
| fast_mcp_config = self._mcp_config_service.retrieve_fast_mcp_config( | ||
| workspace_id | ||
| ) | ||
| if not fast_mcp_config: | ||
| McpClientManagerManager._mcp_client_managers[key] = None | ||
| return None | ||
|
|
||
| async with McpClientManagerManager._lock: | ||
| if key not in McpClientManagerManager._mcp_client_managers: | ||
| try: | ||
| mcp_client_manager = McpClientManager.from_config(fast_mcp_config) | ||
| McpClientManagerManager._mcp_client_managers[key] = ( | ||
| mcp_client_manager | ||
| ) | ||
| await mcp_client_manager.connect() | ||
| except Exception: | ||
| if key in McpClientManagerManager._mcp_client_managers: | ||
| if ( | ||
| _mcp_client_manager | ||
| := McpClientManagerManager._mcp_client_managers[key] | ||
| ): | ||
| await _mcp_client_manager.disconnect(force=True) | ||
| del McpClientManagerManager._mcp_client_managers[key] | ||
| raise | ||
| return McpClientManagerManager._mcp_client_managers[key] | ||
|
|
||
| async def disconnect_all(self, force: bool = False) -> None: | ||
| async with McpClientManagerManager._lock: | ||
| for ( | ||
| mcp_client_manager | ||
| ) in McpClientManagerManager._mcp_client_managers.values(): | ||
| if mcp_client_manager: | ||
| await mcp_client_manager.disconnect(force) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.