fix: resolve asyncio event loop conflicts in MCP server registration#134
Merged
Conversation
6aee638 to
e808906
Compare
Add async/await support for LlamaStackAsLibraryClient to prevent "Cannot run event loop while another loop is running" RuntimeError. Changes: - Introduce register_mcp_servers_async() for async MCP server registration - Refactor MCP registration logic into separate sync/async helper functions - Use async client methods (client.async_client) for library clients to avoid "Cannot run event loop while another loop is running" errors - Add async tests for library client configuration The LlamaStackAsLibraryClient requires async initialization, but the previous sync implementation caused event loop conflicts. This change provides dual support for both service clients (sync) and library clients (async). Fixes RuntimeError when configuration.llama_stack.use_as_library_client = true
e808906 to
ea0c1dd
Compare
manstis
approved these changes
Jun 26, 2025
Contributor
manstis
left a comment
There was a problem hiding this comment.
LGTM 👍
Minor comment about consistency of the code-flow.
| ) | ||
| else: | ||
| # Service client - use sync interface | ||
| register_mcp_servers(logger, configuration) |
Contributor
There was a problem hiding this comment.
IMO it'd be better to get the client here too; to be consistent with the async approach.
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
client = get_llama_stack_client(configuration.llama_stack)
_register_mcp_toolgroups_sync(client, configuration.mcp_servers, logger)
| register_mcp_servers(logger, configuration) | ||
|
|
||
|
|
||
| def register_mcp_servers(logger: Logger, configuration: Configuration) -> None: |
Contributor
There was a problem hiding this comment.
Delete this if doing the above proposal.
Contributor
Author
There was a problem hiding this comment.
I added it to make the tests a bit clearer (async when not using as a library).
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Add async/await support for LlamaStackAsLibraryClient to prevent
"Cannot run event loop while another loop is running" RuntimeError.
Changes:
- Introduce register_mcp_servers_async() for async MCP server registration
- Refactor MCP registration logic into separate sync/async helper functions
- Use async client methods (client.async_client) for library clients
to avoid "Cannot run event loop while another loop is running" errors
- Add async tests for library client configuration
The LlamaStackAsLibraryClient requires async initialization, but the previous
sync implementation caused event loop conflicts. This change provides dual
support for both service clients (sync) and library clients (async).
Type of change
Related Tickets & Documents
Related Issue #
Fixes RuntimeError when configuration.llama_stack.use_as_library_client = true
Closes #
Checklist before requesting a review
Testing