-
Notifications
You must be signed in to change notification settings - Fork 53.1k
fix(cli): accept configured MCP prefixed toolsets before discovery #44044
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
Open
thedavidweng
wants to merge
1
commit into
NousResearch:main
Choose a base branch
from
thedavidweng:fix/mcp-prefixed-toolset-warning
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| """Helpers for configured MCP server toolset names. | ||
|
|
||
| MCP discovery registers runtime toolsets as ``mcp-<server>`` and also exposes | ||
| bare server-name aliases. Several startup paths validate explicit toolset lists | ||
| before discovery has run, so they need a config-only view of both spellings. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Mapping | ||
|
|
||
|
|
||
| def _parse_enabled_flag(value: object, default: bool = True) -> bool: | ||
| """Parse bool-like config values used by MCP server settings.""" | ||
| if value is None: | ||
| return default | ||
| if isinstance(value, bool): | ||
| return value | ||
| if isinstance(value, int): | ||
| return value != 0 | ||
| if isinstance(value, str): | ||
| lowered = value.strip().lower() | ||
| if lowered in {"true", "1", "yes", "on"}: | ||
| return True | ||
| if lowered in {"false", "0", "no", "off"}: | ||
| return False | ||
| return default | ||
|
|
||
|
|
||
| def _server_enabled(server_cfg: object) -> bool: | ||
| """Return whether an MCP server config is enabled by config semantics.""" | ||
| if isinstance(server_cfg, Mapping): | ||
| return _parse_enabled_flag(server_cfg.get("enabled", True), default=True) | ||
| return True | ||
|
|
||
|
|
||
| def mcp_toolset_aliases_for_servers( | ||
| mcp_servers: object, | ||
| *, | ||
| enabled: bool | None = None, | ||
| ) -> set[str]: | ||
| """Return configured MCP toolset spellings for startup validation. | ||
|
|
||
| Args: | ||
| mcp_servers: The ``config.yaml`` ``mcp_servers`` mapping. | ||
| enabled: ``True`` for only enabled servers, ``False`` for only disabled | ||
| servers, ``None`` for every configured server. | ||
|
|
||
| Each configured server contributes both accepted toolset spellings: | ||
| ``<server>`` and ``mcp-<server>``. The latter is the runtime toolset name | ||
| registered by MCP discovery; the former is the historical alias supported | ||
| by earlier startup validators. | ||
| """ | ||
| if not isinstance(mcp_servers, Mapping): | ||
| return set() | ||
|
|
||
| aliases: set[str] = set() | ||
| for name, server_cfg in mcp_servers.items(): | ||
| if not isinstance(server_cfg, Mapping): | ||
| continue | ||
| name = str(name) | ||
| is_enabled = _server_enabled(server_cfg) | ||
| if enabled is not None and is_enabled is not enabled: | ||
| continue | ||
| aliases.add(name) | ||
| aliases.add(f"mcp-{name}") | ||
| return aliases | ||
|
|
||
|
|
||
| def split_configured_mcp_toolset_aliases( | ||
| mcp_servers: object, | ||
| ) -> tuple[set[str], set[str]]: | ||
| """Return ``(enabled_aliases, disabled_aliases)`` for configured MCP servers.""" | ||
| return ( | ||
| mcp_toolset_aliases_for_servers(mcp_servers, enabled=True), | ||
| mcp_toolset_aliases_for_servers(mcp_servers, enabled=False), | ||
| ) | ||
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 |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| from copy import deepcopy | ||
|
|
||
| import cli | ||
|
|
||
|
|
||
| def _init_cli_and_capture_warnings(monkeypatch, toolsets): | ||
| cfg = deepcopy(cli.CLI_CONFIG) | ||
| cfg["model"] = {"default": "stub-model", "provider": "auto", "base_url": ""} | ||
| cfg.setdefault("agent", {})["disabled_toolsets"] = [] | ||
| cfg.setdefault("display", {}) | ||
| cfg["mcp_servers"] = { | ||
| "agentmail": {"enabled": True}, | ||
| "notion-findit": {"enabled": True}, | ||
| } | ||
|
|
||
| warnings = [] | ||
| monkeypatch.setattr(cli, "CLI_CONFIG", cfg) | ||
| monkeypatch.setattr(cli, "validate_toolset", lambda name: name == "terminal") | ||
| monkeypatch.setattr( | ||
| cli.HermesCLI, | ||
| "_console_print", | ||
| lambda self, message, *args, **kwargs: warnings.append(str(message)), | ||
| ) | ||
|
|
||
| cli.HermesCLI( | ||
| model="stub-model", | ||
| provider="auto", | ||
| toolsets=toolsets, | ||
| compact=True, | ||
| ) | ||
| return warnings | ||
|
|
||
|
|
||
| def test_cli_startup_accepts_mcp_prefixed_toolsets_before_discovery(monkeypatch): | ||
| warnings = _init_cli_and_capture_warnings( | ||
| monkeypatch, | ||
| ["terminal", "mcp-agentmail", "mcp-notion-findit"], | ||
| ) | ||
|
|
||
| assert warnings == [] | ||
|
|
||
|
|
||
| def test_cli_startup_still_warns_for_non_mcp_unknown_toolsets(monkeypatch): | ||
| warnings = _init_cli_and_capture_warnings( | ||
| monkeypatch, | ||
| ["terminal", "mcp-agentmail", "not-real"], | ||
| ) | ||
|
|
||
| assert warnings == ["[bold red]Warning: Unknown toolsets: not-real[/]"] | ||
|
|
||
|
|
||
| def test_mcp_toolset_aliases_skips_non_mapping_entries(): | ||
| """Non-mapping server entries are skipped to avoid validating toolsets | ||
| that discovery cannot register.""" | ||
| from hermes_cli.mcp_toolsets import mcp_toolset_aliases_for_servers | ||
|
|
||
| result = mcp_toolset_aliases_for_servers({ | ||
| "good": {"enabled": True}, | ||
| "bad": "just-a-string", | ||
| "also-bad": 42, | ||
| }) | ||
| assert result == {"good", "mcp-good"} | ||
|
|
||
|
|
||
| def test_split_configured_mcp_toolset_aliases_skips_non_mapping(): | ||
| from hermes_cli.mcp_toolsets import split_configured_mcp_toolset_aliases | ||
|
|
||
| enabled, disabled = split_configured_mcp_toolset_aliases({ | ||
| "active": {"enabled": True}, | ||
| "inactive": {"enabled": False}, | ||
| "malformed": "not-a-dict", | ||
| }) | ||
| assert enabled == {"active", "mcp-active"} | ||
| assert disabled == {"inactive", "mcp-inactive"} |
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.