-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: add SubAgent-scoped MCP tool isolation #5246
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
whatevertogo
wants to merge
2
commits into
AstrBotDevs:master
from
whatevertogo:feat/mcp-subagent-scope
Closed
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,88 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Iterable, Mapping | ||
| from typing import Any | ||
|
|
||
| MCP_SCOPE_FIELDS = ("scopes", "agent_scope") | ||
| MCP_SCOPE_WILDCARDS = {"*", "all"} | ||
|
|
||
|
|
||
| def _normalize_scope_token(value: Any) -> str | None: | ||
| token = str(value).strip().lower() | ||
| return token or None | ||
|
|
||
|
|
||
| def normalize_mcp_scope_value(raw_scope: Any) -> tuple[str, ...] | None: | ||
| """Normalize a raw MCP scope value. | ||
|
|
||
| Returns: | ||
| - None: no scope restriction (visible to all agents). | ||
| - tuple[str, ...]: explicit allow list; empty tuple means visible to none. | ||
| """ | ||
| if raw_scope is None: | ||
| return None | ||
|
|
||
| if isinstance(raw_scope, str): | ||
| values: Iterable[Any] = [raw_scope] | ||
| elif isinstance(raw_scope, Mapping): | ||
| return () | ||
| elif isinstance(raw_scope, Iterable): | ||
| values = raw_scope | ||
| else: | ||
| return () | ||
|
|
||
| normalized: list[str] = [] | ||
| for value in values: | ||
| token = _normalize_scope_token(value) | ||
| if not token: | ||
| continue | ||
| if token in MCP_SCOPE_WILDCARDS: | ||
| return ("*",) | ||
| if token not in normalized: | ||
| normalized.append(token) | ||
| return tuple(normalized) | ||
|
|
||
|
|
||
| def get_mcp_scopes_from_config( | ||
| config: Mapping[str, Any] | None, | ||
| ) -> tuple[str, ...] | None: | ||
| """Extract and normalize MCP scope config from server config.""" | ||
| if not isinstance(config, Mapping): | ||
| return None | ||
|
|
||
| raw_scope = config.get("scopes", None) | ||
| if raw_scope is None: | ||
| raw_scope = config.get("agent_scope", None) | ||
|
|
||
| return normalize_mcp_scope_value(raw_scope) | ||
|
|
||
|
|
||
| def strip_mcp_scope_fields(config: dict[str, Any]) -> None: | ||
| """Remove MCP scope-only fields before passing config to MCP transport layer.""" | ||
| for key in MCP_SCOPE_FIELDS: | ||
| config.pop(key, None) | ||
|
|
||
|
|
||
| def is_scope_allowed_for_agent( | ||
| scopes: tuple[str, ...] | None, | ||
| agent_name: str | None, | ||
| ) -> bool: | ||
| """Return whether an agent can see a tool with the given scopes.""" | ||
| if scopes is None: | ||
| return True | ||
|
|
||
| if "*" in scopes: | ||
| return True | ||
|
|
||
| normalized_agent = _normalize_scope_token(agent_name) | ||
| if not normalized_agent: | ||
| return False | ||
| return normalized_agent in scopes | ||
|
|
||
|
|
||
| def is_mcp_tool_visible_to_agent(tool: Any, agent_name: str | None) -> bool: | ||
| """Return whether an MCP tool is visible to the target agent.""" | ||
| return is_scope_allowed_for_agent( | ||
| normalize_mcp_scope_value(getattr(tool, "mcp_server_scopes", None)), | ||
| agent_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
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.