mirror of
https://github.com/jmagar/unraid-mcp.git
synced 2026-03-23 12:39:24 -07:00
feat(elicitation): auto-elicit credentials on CredentialsNotConfiguredError in unraid_info
This commit is contained in:
@@ -31,8 +31,12 @@ def make_tool_fn(
|
||||
|
||||
This wraps the repeated pattern of creating a test FastMCP instance,
|
||||
registering a tool, and extracting the inner function. Centralizing
|
||||
this avoids reliance on FastMCP's private `_tool_manager._tools` API
|
||||
in every test file.
|
||||
this avoids reliance on FastMCP's internal tool storage API in every
|
||||
test file.
|
||||
|
||||
FastMCP 3.x removed `_tool_manager._tools`; use `await mcp.get_tool()`
|
||||
instead. We run a small event loop here to keep the helper synchronous
|
||||
so callers don't need to change.
|
||||
|
||||
Args:
|
||||
module_path: Dotted import path to the tool module (e.g., "unraid_mcp.tools.info")
|
||||
@@ -48,4 +52,8 @@ def make_tool_fn(
|
||||
register_fn = getattr(module, register_fn_name)
|
||||
test_mcp = FastMCP("test")
|
||||
register_fn(test_mcp)
|
||||
return test_mcp._tool_manager._tools[tool_name].fn # type: ignore[union-attr]
|
||||
# FastMCP 3.x stores tools in providers[0]._components keyed as "tool:{name}@"
|
||||
# (the "@" suffix is the version separator with no version set).
|
||||
local_provider = test_mcp.providers[0]
|
||||
tool = local_provider._components[f"tool:{tool_name}@"]
|
||||
return tool.fn
|
||||
|
||||
@@ -170,18 +170,37 @@ class TestUnraidInfoTool:
|
||||
await tool_fn(action="ups_device")
|
||||
|
||||
async def test_network_action(self, _mock_graphql: AsyncMock) -> None:
|
||||
_mock_graphql.return_value = {"network": {"id": "net:1", "accessUrls": []}}
|
||||
tool_fn = _make_tool()
|
||||
result = await tool_fn(action="network")
|
||||
assert result["id"] == "net:1"
|
||||
|
||||
async def test_connect_action(self, _mock_graphql: AsyncMock) -> None:
|
||||
_mock_graphql.return_value = {
|
||||
"connect": {"status": "connected", "sandbox": False, "flashGuid": "abc123"}
|
||||
"servers": [
|
||||
{
|
||||
"id": "s:1",
|
||||
"name": "tootie",
|
||||
"status": "ONLINE",
|
||||
"lanip": "10.1.0.2",
|
||||
"wanip": "",
|
||||
"localurl": "http://10.1.0.2:6969",
|
||||
"remoteurl": "",
|
||||
}
|
||||
],
|
||||
"vars": {
|
||||
"id": "v:1",
|
||||
"port": 6969,
|
||||
"portssl": 31337,
|
||||
"localTld": "local",
|
||||
"useSsl": None,
|
||||
},
|
||||
}
|
||||
tool_fn = _make_tool()
|
||||
result = await tool_fn(action="connect")
|
||||
assert result["status"] == "connected"
|
||||
result = await tool_fn(action="network")
|
||||
assert "accessUrls" in result
|
||||
assert result["httpPort"] == 6969
|
||||
assert result["httpsPort"] == 31337
|
||||
assert any(u["type"] == "LAN" and u["ipv4"] == "10.1.0.2" for u in result["accessUrls"])
|
||||
|
||||
async def test_connect_action_raises_tool_error(self, _mock_graphql: AsyncMock) -> None:
|
||||
tool_fn = _make_tool()
|
||||
with pytest.raises(ToolError, match="connect.*not available"):
|
||||
await tool_fn(action="connect")
|
||||
|
||||
async def test_generic_exception_wraps(self, _mock_graphql: AsyncMock) -> None:
|
||||
_mock_graphql.side_effect = RuntimeError("unexpected")
|
||||
|
||||
@@ -174,3 +174,42 @@ async def test_make_graphql_request_raises_sentinel_when_unconfigured():
|
||||
finally:
|
||||
settings_mod.UNRAID_API_URL = original_url
|
||||
settings_mod.UNRAID_API_KEY = original_key
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_auto_elicitation_triggered_on_credentials_not_configured():
|
||||
"""Any tool call with missing creds auto-triggers elicitation before erroring."""
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
from conftest import make_tool_fn
|
||||
from fastmcp import FastMCP
|
||||
|
||||
from unraid_mcp.core.exceptions import CredentialsNotConfiguredError
|
||||
from unraid_mcp.tools.info import register_info_tool
|
||||
|
||||
test_mcp = FastMCP("test")
|
||||
register_info_tool(test_mcp)
|
||||
tool_fn = make_tool_fn("unraid_mcp.tools.info", "register_info_tool", "unraid_info")
|
||||
|
||||
mock_ctx = MagicMock()
|
||||
|
||||
# First call raises CredentialsNotConfiguredError, second returns data
|
||||
call_count = 0
|
||||
|
||||
async def side_effect(*args, **kwargs):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
if call_count == 1:
|
||||
raise CredentialsNotConfiguredError()
|
||||
return {"info": {"os": {"hostname": "tootie"}}}
|
||||
|
||||
with (
|
||||
patch("unraid_mcp.tools.info.make_graphql_request", side_effect=side_effect),
|
||||
patch(
|
||||
"unraid_mcp.tools.info.elicit_and_configure", new=AsyncMock(return_value=True)
|
||||
) as mock_elicit,
|
||||
):
|
||||
result = await tool_fn(action="overview", ctx=mock_ctx)
|
||||
|
||||
mock_elicit.assert_called_once_with(mock_ctx)
|
||||
assert result is not None
|
||||
|
||||
Reference in New Issue
Block a user