-
Notifications
You must be signed in to change notification settings - Fork 357
feat: implement --read-only mode with tool filtering #334
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -90,16 +90,42 @@ def filter_server_tools(server): | |||||||||||||||||||||||||||||||||||||||||||||
| if hasattr(tool_manager, "_tools"): | ||||||||||||||||||||||||||||||||||||||||||||||
| tool_registry = tool_manager._tools | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| from auth.scopes import is_read_only_mode, get_all_read_only_scopes | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| read_only_mode = is_read_only_mode() | ||||||||||||||||||||||||||||||||||||||||||||||
| allowed_scopes = set(get_all_read_only_scopes()) if read_only_mode else None | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| tools_to_remove = [] | ||||||||||||||||||||||||||||||||||||||||||||||
| for tool_name in list(tool_registry.keys()): | ||||||||||||||||||||||||||||||||||||||||||||||
| for tool_name, tool_func in tool_registry.items(): | ||||||||||||||||||||||||||||||||||||||||||||||
| # 1. Tier filtering | ||||||||||||||||||||||||||||||||||||||||||||||
| if not is_tool_enabled(tool_name): | ||||||||||||||||||||||||||||||||||||||||||||||
| tools_to_remove.append(tool_name) | ||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| # 2. Read-only filtering | ||||||||||||||||||||||||||||||||||||||||||||||
| if read_only_mode: | ||||||||||||||||||||||||||||||||||||||||||||||
| # Check if tool has required scopes attached (from @require_google_service) | ||||||||||||||||||||||||||||||||||||||||||||||
| # Note: FastMCP wraps functions in Tool objects, so we need to check .fn if available | ||||||||||||||||||||||||||||||||||||||||||||||
| func_to_check = tool_func | ||||||||||||||||||||||||||||||||||||||||||||||
| if hasattr(tool_func, "fn"): | ||||||||||||||||||||||||||||||||||||||||||||||
| func_to_check = tool_func.fn | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| required_scopes = getattr(func_to_check, "_required_google_scopes", []) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if required_scopes: | ||||||||||||||||||||||||||||||||||||||||||||||
| # If ANY required scope is not in the allowed read-only scopes, disable the tool | ||||||||||||||||||||||||||||||||||||||||||||||
| if not all(scope in allowed_scopes for scope in required_scopes): | ||||||||||||||||||||||||||||||||||||||||||||||
| logger.info( | ||||||||||||||||||||||||||||||||||||||||||||||
| f"Read-only mode: Disabling tool '{tool_name}' (requires write scopes: {required_scopes})" | ||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||
| tools_to_remove.append(tool_name) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+112
to
122
|
||||||||||||||||||||||||||||||||||||||||||||||
| required_scopes = getattr(func_to_check, "_required_google_scopes", []) | |
| if required_scopes: | |
| # If ANY required scope is not in the allowed read-only scopes, disable the tool | |
| if not all(scope in allowed_scopes for scope in required_scopes): | |
| logger.info( | |
| f"Read-only mode: Disabling tool '{tool_name}' (requires write scopes: {required_scopes})" | |
| ) | |
| tools_to_remove.append(tool_name) | |
| required_scopes = getattr(func_to_check, "_required_google_scopes", []) | |
| if not required_scopes: | |
| # Tools without required scopes are allowed in read-only mode | |
| continue | |
| # If ANY required scope is not in the allowed read-only scopes, disable the tool | |
| if not all(scope in allowed_scopes for scope in required_scopes): | |
| logger.info( | |
| f"Read-only mode: Disabling tool '{tool_name}' (requires write scopes: {required_scopes})" | |
| ) | |
| tools_to_remove.append(tool_name) |
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.
The function returns a list conversion of a set, which doesn't guarantee consistent ordering across calls. If scope ordering matters for OAuth or comparison operations elsewhere in the codebase, consider returning
sorted(all_scopes)instead to ensure deterministic behavior.