-
Notifications
You must be signed in to change notification settings - Fork 88
fix: resolve asyncio event loop conflicts in MCP server registration #134
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
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
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 |
|---|---|---|
| @@ -1,11 +1,17 @@ | ||
| """Common utilities for the project.""" | ||
|
|
||
| from typing import Any | ||
| from typing import Any, List, cast | ||
| from logging import Logger | ||
|
|
||
| from llama_stack_client import LlamaStackClient | ||
|
|
||
| from llama_stack.distribution.library_client import ( | ||
| LlamaStackAsLibraryClient, | ||
| AsyncLlamaStackAsLibraryClient, | ||
| ) | ||
|
|
||
| from client import get_llama_stack_client | ||
| from models.config import Configuration | ||
| from models.config import Configuration, ModelContextProtocolServer | ||
|
|
||
|
|
||
| # TODO(lucasagomes): implement this function to retrieve user ID from auth | ||
|
|
@@ -22,20 +28,81 @@ def retrieve_user_id(auth: Any) -> str: # pylint: disable=unused-argument | |
| return "user_id_placeholder" | ||
|
|
||
|
|
||
| async def register_mcp_servers_async( | ||
| logger: Logger, configuration: Configuration | ||
| ) -> None: | ||
| """Register Model Context Protocol (MCP) servers with the LlamaStack client (async).""" | ||
| if configuration.llama_stack.use_as_library_client: | ||
| # Library client - use async interface | ||
| # config.py validation ensures library_client_config_path is not None | ||
| # when use_as_library_client is True | ||
| config_path = cast(str, configuration.llama_stack.library_client_config_path) | ||
| client = LlamaStackAsLibraryClient(config_path) | ||
| await client.async_client.initialize() | ||
|
|
||
| await _register_mcp_toolgroups_async( | ||
| client.async_client, configuration.mcp_servers, logger | ||
| ) | ||
| else: | ||
| # Service client - use sync interface | ||
| register_mcp_servers(logger, configuration) | ||
|
|
||
|
|
||
| def register_mcp_servers(logger: Logger, configuration: Configuration) -> None: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete this if doing the above proposal.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added it to make the tests a bit clearer (async when not using as a library). |
||
| """Register Model Context Protocol (MCP) servers with the LlamaStack client.""" | ||
| # Get list of registered tools and extract their toolgroup IDs | ||
| """Register Model Context Protocol (MCP) servers with the LlamaStack client (sync).""" | ||
| # Service client - use sync interface | ||
| client = get_llama_stack_client(configuration.llama_stack) | ||
|
|
||
| _register_mcp_toolgroups_sync(client, configuration.mcp_servers, logger) | ||
|
|
||
|
|
||
| async def _register_mcp_toolgroups_async( | ||
| client: AsyncLlamaStackAsLibraryClient, | ||
| mcp_servers: List[ModelContextProtocolServer], | ||
| logger: Logger, | ||
| ) -> None: | ||
| """Async logic for registering MCP toolgroups.""" | ||
| # Get registered tools | ||
| registered_tools = await client.tools.list() | ||
| registered_toolgroups = [tool.toolgroup_id for tool in registered_tools] | ||
| logger.debug("Registered toolgroups: %s", set(registered_toolgroups)) | ||
|
|
||
| # Register toolgroups for MCP servers if not already registered | ||
| for mcp in mcp_servers: | ||
| if mcp.name not in registered_toolgroups: | ||
| logger.debug("Registering MCP server: %s, %s", mcp.name, mcp.url) | ||
|
|
||
| registration_params = { | ||
| "toolgroup_id": mcp.name, | ||
| "provider_id": mcp.provider_id, | ||
| "mcp_endpoint": {"uri": mcp.url}, | ||
| } | ||
|
|
||
| await client.toolgroups.register(**registration_params) | ||
| logger.debug("MCP server %s registered successfully", mcp.name) | ||
|
|
||
|
|
||
| def _register_mcp_toolgroups_sync( | ||
| client: LlamaStackClient, | ||
| mcp_servers: List[ModelContextProtocolServer], | ||
| logger: Logger, | ||
| ) -> None: | ||
| """Sync logic for registering MCP toolgroups.""" | ||
| # Get registered tools | ||
| registered_tools = client.tools.list() | ||
| registered_toolgroups = [tool.toolgroup_id for tool in registered_tools] | ||
| logger.debug("Registered toolgroups: %s", set(registered_toolgroups)) | ||
|
|
||
| # Register toolgroups for MCP servers if not already registered | ||
| for mcp in configuration.mcp_servers: | ||
| if mcp.name not in registered_toolgroups: # required | ||
| for mcp in mcp_servers: | ||
| if mcp.name not in registered_toolgroups: | ||
| logger.debug("Registering MCP server: %s, %s", mcp.name, mcp.url) | ||
| client.toolgroups.register( | ||
| toolgroup_id=mcp.name, | ||
| provider_id=mcp.provider_id, | ||
| mcp_endpoint={"uri": mcp.url}, | ||
| ) | ||
|
|
||
| registration_params = { | ||
| "toolgroup_id": mcp.name, | ||
| "provider_id": mcp.provider_id, | ||
| "mcp_endpoint": {"uri": mcp.url}, | ||
| } | ||
|
|
||
| client.toolgroups.register(**registration_params) | ||
| logger.debug("MCP server %s registered successfully", mcp.name) | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO it'd be better to get the
clienthere too; to be consistent with the async approach.