π The fast, Pythonic way to build MCP servers and clients.
Add app restart to the smart home Fire TV example (#6356)
nate nowack23231796e134
diff --git a/examples/smart_home/docs/usage.md b/examples/smart_home/docs/usage.mdindex 62bf598df..867bdd541 100644--- a/examples/smart_home/docs/usage.md+++ b/examples/smart_home/docs/usage.md@@ -83,6 +83,23 @@ installed package ID; YouTube playback requires an 11-character video ID. Commanreturn acceptance receipts, not proof that navigation completed. The constrainedsurface deliberately exposes neither arbitrary URLs nor raw ADB shell commands.+If an app is stuck on a blank screen, explicitly call `fire_tv_restart_app`+with its installed package ID. For example, YouTube TV is+`com.amazon.firetv.youtube.tv`, distinct from regular YouTube. Restarting+interrupts playback by force-stopping and relaunching that app, without clearing+data or sign-in. Read status afterward and confirm the picture recovered;+foreground state alone cannot establish that the app rendered successfully.++An opt-in device test verifies rejection of an invalid target, a changed process+ID, and return to the foreground. With `FIRE_TV_HOST` and `FIRE_TV_ADB_KEY`+configured and the selected app already open, run from this example directory:++```bash+FIRE_TV_TEST_RESTART_PACKAGE=com.amazon.firetv.youtube.tv uv run pytest tests/test_fire_tv_device.py+```++This test interrupts the selected app; ordinary test runs skip it.+For a request such as "play a live bird camera where it is daylight": find acurrent feed, check the camera's location and local time, verify that the videois live and playable, then call `fire_tv_play_youtube_video` with its video ID.diff --git a/examples/smart_home/src/smart_home/fire_tv/client.py b/examples/smart_home/src/smart_home/fire_tv/client.pyindex a34fcf9d0..cb02bb31b 100644--- a/examples/smart_home/src/smart_home/fire_tv/client.py+++ b/examples/smart_home/src/smart_home/fire_tv/client.py@@ -28,6 +28,8 @@ class FireTVClient(Protocol):async def launch_app(self, app: str) -> None: ...+ async def stop_app(self, app: str) -> str | None: ...+async def adb_shell(self, command: str) -> str | None: ...async def adb_connect(self, log_errors: bool = True) -> bool: ...diff --git a/examples/smart_home/src/smart_home/fire_tv/server.py b/examples/smart_home/src/smart_home/fire_tv/server.pyindex c8cf3284a..bb4497b8e 100644--- a/examples/smart_home/src/smart_home/fire_tv/server.py+++ b/examples/smart_home/src/smart_home/fire_tv/server.py@@ -75,6 +75,25 @@ async def launch_app(return CommandReceipt(command=f"launch_app:{package_id}")+@fire_tv_mcp.tool(annotations=WRITE)+async def restart_app(+ package_id: str,+ fire_tv: FireTVClient = Depends(get_fire_tv),+) -> CommandReceipt:+ """Force-stop and relaunch an installed app without clearing data or sign-in.++ This interrupts playback. Read status afterward; acceptance does not prove+ the picture recovered.+ """+ if package_id not in (fire_tv.installed_apps or []):+ raise ToolError(f"Package {package_id!r} is not installed")+ result = await fire_tv.stop_app(package_id)+ if result is None or result.strip():+ raise ToolError("Could not confirm app stop; relaunch was not attempted")+ await fire_tv.launch_app(package_id)+ return CommandReceipt(command=f"restart_app:{package_id}")++@fire_tv_mcp.tool(annotations=WRITE)async def play_youtube_video(video_id: str,diff --git a/examples/smart_home/tests/test_fire_tv.py b/examples/smart_home/tests/test_fire_tv.pyindex 400cc259b..239169bea 100644--- a/examples/smart_home/tests/test_fire_tv.py+++ b/examples/smart_home/tests/test_fire_tv.py@@ -65,6 +65,7 @@ async def test_status_and_commands_share_one_connection(configured_fire_tv):"read_status","press_home","launch_app",+ "restart_app","play_media","play_youtube_video",}@@ -194,6 +195,7 @@ async def test_concurrent_calls_reconnect_once(monkeypatch: pytest.MonkeyPatch)("tool", "arguments", "message"),[("launch_app", {"package_id": "not.installed"}, "not installed"),+ ("restart_app", {"package_id": "not.installed; reboot"}, "not installed"),("play_youtube_video",{"video_id": "invalid; reboot"},diff --git a/examples/smart_home/tests/test_fire_tv_device.py b/examples/smart_home/tests/test_fire_tv_device.pynew file mode 100644index 000000000..682504771--- /dev/null+++ b/examples/smart_home/tests/test_fire_tv_device.py@@ -0,0 +1,46 @@+import asyncio+import os++import pytest+from smart_home.fire_tv.client import FireTVConnection, FireTVSettings+from smart_home.fire_tv.server import fire_tv_mcp++from fastmcp import Client+++ not os.getenv("FIRE_TV_TEST_RESTART_PACKAGE"),+ reason="Opt-in device test interrupts the selected app",+)+async def test_restart_installed_app_on_device() -> None:+ package = os.environ["FIRE_TV_TEST_RESTART_PACKAGE"]+ observer = FireTVConnection(FireTVSettings())++ async def process_id() -> str:+ try:+ device = await observer.get()+ assert package in (device.installed_apps or [])+ return (await device.adb_shell(f"pidof {package}") or "").strip()+ finally:+ await observer.close()++ async with Client(fire_tv_mcp) as client:+ before = (await client.call_tool("read_status")).data+ assert before.available+ rejected = await client.call_tool(+ "restart_app", {"package_id": "not.installed; reboot"}, raise_on_error=False+ )+ assert rejected.is_error+ old_pid = await process_id()+ assert old_pid, "Launch the selected app before running this test"+ receipt = (await client.call_tool("restart_app", {"package_id": package})).data+ assert receipt.accepted and not receipt.state_verified+ for _ in range(10):+ after = (await client.call_tool("read_status")).data+ if after.available and after.current_app == package:+ break+ await asyncio.sleep(1)+ else:+ pytest.fail(f"App did not return to foreground: {after}")+ new_pid = await process_id()+ assert new_pid and new_pid != old_pid