#!/usr/bin/env python3
import os
from fastmcp import FastMCP
from dotenv import load_dotenv
from smartthings import get_client_from_env
load_dotenv()
mcp = FastMCP("TV Control MCP Server")
@mcp.tool(description="Turn on the TV via SmartThings")
def tv_turn_on() -> dict:
client = get_client_from_env()
return client.turn_on()
@mcp.tool(description="Turn off the TV via SmartThings")
def tv_turn_off() -> dict:
client = get_client_from_env()
return client.turn_off()
@mcp.tool(description="Set the TV volume (0-100) via SmartThings")
def tv_set_volume(level: int) -> dict:
client = get_client_from_env()
return client.set_volume(level)
@mcp.tool(description="Open a TV app by name or appId (e.g., 'youtube' or '111299001912')")
def tv_open_app(app: str) -> dict:
client = get_client_from_env()
messages = []
if not client.is_on():
messages.append("TV is off; turning on first…")
try:
client.turn_on()
except Exception as e:
return {"ok": False, "messages": messages + [f"Failed to turn on TV: {e}"]}
result = client.open_app(app)
return {"ok": True, "messages": messages, "result": result}
@mcp.tool(description="Set the TV input source (e.g., hdmi1) via SmartThings")
def tv_set_input(source: str) -> dict:
client = get_client_from_env()
return client.set_input(source)
if __name__ == "__main__":
port = int(os.environ.get("PORT", 8000))
host = "0.0.0.0"
print(f"Starting FastMCP server on {host}:{port}")
mcp.run(
transport="http",
host=host,
port=port,
stateless_http=True
)