for assorted things
1#!/usr/bin/env -S uv run --script --quiet 2# /// script 3# requires-python = ">=3.12" 4# dependencies = ["marvin>=3.1.0"] 5# /// 6""" 7Make some change to my phillips hue network of lights via agent + MCP server. 8 9Usage: 10 11```bash 12./update-lights -m "turn on sahara in the living room and nightlight in the kitchen" 13``` 14 15Details: 16- uses a [`marvin`](https://github.com/prefecthq/marvin) (built on [`pydantic-ai`](https://github.com/pydantic/pydantic-ai)) agent 17- the agent spins up a [`fastmcp`](https://github.com/jlowin/fastmcp) MCP server that talks to my [`phue`](https://github.com/studioimaginaire/phue) bridge 18- set `HUE_BRIDGE_IP` and `HUE_BRIDGE_USERNAME` in `.env` or otherwise in environment 19- uses `OPENAI_API_KEY` by default, but you can set `AI_MODEL` in `.env` or otherwise in environment to use a different model 20""" 21 22import marvin 23import argparse 24from pydantic_settings import BaseSettings, SettingsConfigDict 25from pydantic import Field 26from pydantic_ai.mcp import MCPServerStdio 27from pydantic_ai.models import KnownModelName 28from rich.console import Console 29from rich.panel import Panel 30from rich.prompt import Prompt 31 32 33class Settings(BaseSettings): 34 model_config = SettingsConfigDict(env_file=".env", extra="ignore") 35 36 hue_bridge_ip: str = Field(default=...) 37 hue_bridge_username: str = Field(default=...) 38 39 ai_model: KnownModelName = Field(default="gpt-4.1-mini") 40 41 42settings = Settings() 43console = Console() 44 45hub_mcp = MCPServerStdio( 46 command="uvx", 47 args=[ 48 "smart-home@git+https://github.com/jlowin/fastmcp.git#subdirectory=examples/smart_home" 49 ], 50 env={ 51 "HUE_BRIDGE_IP": settings.hue_bridge_ip, 52 "HUE_BRIDGE_USERNAME": settings.hue_bridge_username, 53 }, 54) 55 56 57if __name__ == "__main__": 58 parser = argparse.ArgumentParser(description="Send a command to the Marvin agent.") 59 parser.add_argument( 60 "--message", 61 "-m", 62 type=str, 63 default="soft and dim - Jessica Pratt energy, all areas", 64 help="The message to send to the agent (defaults to 'soft and dim - Jessica Pratt energy, all areas').", 65 ) 66 args = parser.parse_args() 67 68 agent = marvin.Agent( 69 model=settings.ai_model, 70 mcp_servers=[hub_mcp], 71 ) 72 73 console.print( 74 Panel.fit( 75 f"[bold cyan]🏠 lights agent[/bold cyan]\n" 76 f"[dim]model: {settings.ai_model}[/dim]", 77 border_style="blue", 78 ) 79 ) 80 81 with marvin.Thread(): 82 first = True 83 while True: 84 if first: 85 console.print(f"\n[bold yellow]→[/bold yellow] {args.message}") 86 agent.run(str(args.message)) 87 first = False 88 else: 89 try: 90 user_input = Prompt.ask( 91 "\n[bold green]enter a message[/bold green]" 92 ) 93 console.print(f"[bold yellow]→[/bold yellow] {user_input}") 94 agent.run(str(user_input)) 95 except KeyboardInterrupt: 96 console.print("\n[dim red]exiting...[/dim red]") 97 break