-
Notifications
You must be signed in to change notification settings - Fork 0
Add App Management tools and user-permission tests #32
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 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ca5cb11
Add App Management tools and user-permission integration tests
bigcat88 4304a2f
Fix pyright errors, expand user-permission tests to cover files/share…
bigcat88 c1746b0
Fix EXPECTED_TOOLS sort order, remove list_conversations from user te…
bigcat88 526a1fa
Install spreed in user-permissions CI job, restore list_conversations…
bigcat88 848856f
Fix get_app_info returning null fields, tighten test assertions
bigcat88 0e1c762
Fix isort ordering in test_user_permissions.py
bigcat88 f6664da
Scope CI isort check to src/ to match pre-commit config
bigcat88 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| """App management tools — list, enable, and disable Nextcloud apps via OCS API.""" | ||
|
|
||
| import json | ||
| from typing import Any | ||
|
|
||
| from mcp.server.fastmcp import FastMCP | ||
|
|
||
| from ..annotations import ADDITIVE_IDEMPOTENT, DESTRUCTIVE, READONLY | ||
| from ..permissions import PermissionLevel, require_permission | ||
| from ..state import get_client | ||
|
|
||
| APPS_API = "cloud/apps" | ||
|
|
||
|
|
||
| def _format_app(a: dict[str, Any]) -> dict[str, Any]: | ||
| return { | ||
| "id": a.get("id"), | ||
| "name": a.get("name"), | ||
| "summary": a.get("summary"), | ||
| "version": a.get("version"), | ||
| "author": a.get("author"), | ||
| } | ||
|
|
||
|
|
||
| def _register_read_tools(mcp: FastMCP) -> None: | ||
| @mcp.tool(annotations=READONLY) | ||
| @require_permission(PermissionLevel.READ) | ||
| async def list_apps(app_filter: str = "enabled") -> str: | ||
| """List installed Nextcloud apps. Requires admin privileges. | ||
|
|
||
| Returns the list of app IDs matching the filter criteria. | ||
|
|
||
| Args: | ||
| app_filter: Filter apps by status. Options: | ||
| "enabled" (default) — only enabled apps | ||
| "disabled" — only disabled apps | ||
| "all" — all installed apps | ||
|
|
||
| Returns: | ||
| JSON list of app ID strings. | ||
| """ | ||
| valid = {"enabled", "disabled", "all"} | ||
| if app_filter not in valid: | ||
| raise ValueError(f"Invalid filter '{app_filter}'. Must be one of: {', '.join(sorted(valid))}") | ||
| client = get_client() | ||
| params = {} if app_filter == "all" else {"filter": app_filter} | ||
| data = await client.ocs_get(APPS_API, params=params) | ||
| return json.dumps(data["apps"], indent=2) | ||
|
|
||
| @mcp.tool(annotations=READONLY) | ||
| @require_permission(PermissionLevel.READ) | ||
| async def get_app_info(app_id: str) -> str: | ||
| """Get detailed information about an installed Nextcloud app. Requires admin privileges. | ||
|
|
||
| Returns app metadata including name, version, description, and author. | ||
|
|
||
| Args: | ||
| app_id: The app identifier (e.g. "spreed", "mail", "collectives"). | ||
| Use list_apps to find available app IDs. | ||
|
|
||
| Returns: | ||
| JSON object with app details: id, name, summary, version, author. | ||
| """ | ||
| client = get_client() | ||
| data = await client.ocs_get(f"{APPS_API}/{app_id}") | ||
| return json.dumps(_format_app(data), indent=2, default=str) | ||
|
|
||
|
|
||
| def _register_write_tools(mcp: FastMCP) -> None: | ||
| @mcp.tool(annotations=ADDITIVE_IDEMPOTENT) | ||
| @require_permission(PermissionLevel.WRITE) | ||
| async def enable_app(app_id: str) -> str: | ||
| """Enable a Nextcloud app. Requires admin privileges. | ||
|
|
||
| Activates a previously disabled or newly installed app. This makes | ||
| the app's features available to users. | ||
|
|
||
| Args: | ||
| app_id: The app identifier to enable (e.g. "spreed", "mail"). | ||
|
|
||
| Returns: | ||
| Confirmation message. | ||
| """ | ||
| client = get_client() | ||
| await client.ocs_post(f"{APPS_API}/{app_id}") | ||
| return f"App '{app_id}' enabled." | ||
|
|
||
|
|
||
| def _register_destructive_tools(mcp: FastMCP) -> None: | ||
| @mcp.tool(annotations=DESTRUCTIVE) | ||
| @require_permission(PermissionLevel.DESTRUCTIVE) | ||
| async def disable_app(app_id: str) -> str: | ||
| """Disable a Nextcloud app. Requires admin privileges. | ||
|
|
||
| Deactivates the app, making its features unavailable. The app data | ||
| is preserved and can be re-enabled later. | ||
|
|
||
| Be careful: disabling core apps (like files, dav) can break | ||
| Nextcloud functionality. | ||
|
|
||
| Args: | ||
| app_id: The app identifier to disable (e.g. "weather_status"). | ||
|
|
||
| Returns: | ||
| Confirmation message. | ||
| """ | ||
| client = get_client() | ||
| await client.ocs_delete(f"{APPS_API}/{app_id}") | ||
| return f"App '{app_id}' disabled." | ||
|
|
||
|
|
||
| def register(mcp: FastMCP) -> None: | ||
| """Register app management tools with the MCP server.""" | ||
| _register_read_tools(mcp) | ||
| _register_write_tools(mcp) | ||
| _register_destructive_tools(mcp) |
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,112 @@ | ||
| """Integration tests for App Management tools against a real Nextcloud instance.""" | ||
|
|
||
| import json | ||
|
|
||
| import pytest | ||
| from mcp.server.fastmcp.exceptions import ToolError | ||
|
|
||
| from .conftest import McpTestHelper | ||
|
|
||
| pytestmark = pytest.mark.integration | ||
|
|
||
| SAFE_APP = "weather_status" | ||
|
|
||
|
|
||
| class TestListApps: | ||
| @pytest.mark.asyncio | ||
| async def test_list_enabled_returns_list(self, nc_mcp: McpTestHelper) -> None: | ||
| result = await nc_mcp.call("list_apps") | ||
| apps: list[str] = json.loads(result) | ||
| assert isinstance(apps, list) | ||
| assert len(apps) > 0 | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_list_enabled_contains_core_apps(self, nc_mcp: McpTestHelper) -> None: | ||
| result = await nc_mcp.call("list_apps", app_filter="enabled") | ||
| apps = json.loads(result) | ||
| assert "files" in apps | ||
| assert "dav" in apps | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_list_all(self, nc_mcp: McpTestHelper) -> None: | ||
| result = await nc_mcp.call("list_apps", app_filter="all") | ||
| apps = json.loads(result) | ||
| assert len(apps) > 0 | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_list_disabled(self, nc_mcp: McpTestHelper) -> None: | ||
| result = await nc_mcp.call("list_apps", app_filter="disabled") | ||
| apps = json.loads(result) | ||
| assert isinstance(apps, list) | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_invalid_filter_raises(self, nc_mcp: McpTestHelper) -> None: | ||
| with pytest.raises((ToolError, ValueError)): | ||
| await nc_mcp.call("list_apps", app_filter="invalid") | ||
|
|
||
|
|
||
| class TestGetAppInfo: | ||
| @pytest.mark.asyncio | ||
| async def test_get_known_app(self, nc_mcp: McpTestHelper) -> None: | ||
| result = await nc_mcp.call("get_app_info", app_id="files") | ||
| app = json.loads(result) | ||
| assert app["id"] == "files" | ||
| assert app["name"] is not None | ||
| assert app["version"] is not None | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_app_has_fields(self, nc_mcp: McpTestHelper) -> None: | ||
| result = await nc_mcp.call("get_app_info", app_id="dav") | ||
| app = json.loads(result) | ||
| for field in ["id", "name", "version", "author"]: | ||
| assert field in app, f"Missing field: {field}" | ||
| assert app[field] is not None, f"Field '{field}' should not be None" | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_nonexistent_app_raises(self, nc_mcp: McpTestHelper) -> None: | ||
| with pytest.raises(ToolError): | ||
| await nc_mcp.call("get_app_info", app_id="nonexistent_app_xyz") | ||
|
|
||
|
|
||
| class TestEnableDisableApp: | ||
| @pytest.mark.asyncio | ||
| async def test_disable_and_enable(self, nc_mcp: McpTestHelper) -> None: | ||
| await nc_mcp.call("disable_app", app_id=SAFE_APP) | ||
| try: | ||
| disabled = json.loads(await nc_mcp.call("list_apps", app_filter="disabled")) | ||
| assert SAFE_APP in disabled | ||
| await nc_mcp.call("enable_app", app_id=SAFE_APP) | ||
| enabled = json.loads(await nc_mcp.call("list_apps", app_filter="enabled")) | ||
| assert SAFE_APP in enabled | ||
| finally: | ||
| await nc_mcp.call("enable_app", app_id=SAFE_APP) | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_enable_already_enabled(self, nc_mcp: McpTestHelper) -> None: | ||
| result = await nc_mcp.call("enable_app", app_id=SAFE_APP) | ||
| assert "enabled" in result.lower() | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_disable_returns_confirmation(self, nc_mcp: McpTestHelper) -> None: | ||
| try: | ||
| result = await nc_mcp.call("disable_app", app_id=SAFE_APP) | ||
| assert "disabled" in result.lower() | ||
| finally: | ||
| await nc_mcp.call("enable_app", app_id=SAFE_APP) | ||
|
|
||
|
|
||
| class TestAppManagementPermissions: | ||
| @pytest.mark.asyncio | ||
| async def test_read_only_allows_list(self, nc_mcp_read_only: McpTestHelper) -> None: | ||
| result = await nc_mcp_read_only.call("list_apps") | ||
| assert isinstance(json.loads(result), list) | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_read_only_blocks_enable(self, nc_mcp_read_only: McpTestHelper) -> None: | ||
| with pytest.raises(ToolError, match=r"requires 'write' permission"): | ||
| await nc_mcp_read_only.call("enable_app", app_id=SAFE_APP) | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_read_only_blocks_disable(self, nc_mcp_read_only: McpTestHelper) -> None: | ||
| with pytest.raises(ToolError, match=r"requires 'destructive' permission"): | ||
| await nc_mcp_read_only.call("disable_app", app_id=SAFE_APP) | ||
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.
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.