自分だけの連携を作ろう

MCPサーバーを3ステップで作成し、VoiceOSに接続できます。

依存パッケージのインストール

pip install mcp
npm install @modelcontextprotocol/sdk zod

サーバーを作成する

MCPサーバーは、VoiceOSが音声で呼び出せる ツール を公開します。以下のサンプルから始めてみましょう:

my_mcp_server.py

from mcp.server.fastmcp import FastMCP

mcp = FastMCP("my-tools")

@mcp.tool()
def greet(name: str) -> str:
    """Say hello to someone by name."""
    return f"Hello, {name}!"

# Add more tools with the @mcp.tool() decorator

if __name__ == "__main__":
    mcp.run(transport="stdio")

my-mcp-server.ts

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";

const server = new McpServer({
  name: "my-tools",
  version: "1.0.0",
});

server.tool(
  "greet",
  "Say hello to someone by name",
  { name: z.string().describe("Person to greet") },
  async ({ name }) => ({
    content: [{ type: "text", text: `Hello, ${name}!` }],
  })
);

const transport = new StdioServerTransport();
await server.connect(transport);

home_control.py

import subprocess
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("home-control")

def run_shortcut(name: str, input_text: str = "") -> str:
    cmd = ["shortcuts", "run", name]
    if input_text:
        cmd += ["-i", input_text]
    result = subprocess.run(cmd, capture_output=True, text=True, timeout=15)
    return result.stdout.strip() or "Done"

@mcp.tool()
def lights_on() -> str:
    """Turn on the lights."""
    return run_shortcut("Lights On")

@mcp.tool()
def lights_off() -> str:
    """Turn off the lights."""
    return run_shortcut("Lights Off")

@mcp.tool()
def set_thermostat(temperature: int) -> str:
    """Set the thermostat to a specific temperature."""
    return run_shortcut("Set Thermostat", str(temperature))

if __name__ == "__main__":
    mcp.run(transport="stdio")

spotify_mcp.py

import subprocess
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("spotify")

def osascript(cmd: str) -> str:
    result = subprocess.run(
        ["osascript", "-e", f'tell application "Spotify" to {cmd}"] ,
        capture_output=True, text=True,
    )
    return result.stdout.strip() or "Done"

@mcp.tool()
def play() -> str:
    """Resume Spotify playback."""
    return osascript("play")

@mcp.tool()
def pause() -> str:
    """Pause Spotify playback."""
    return osascript("pause")

@mcp.tool()
def skip_track() -> str:
    """Skip to the next track."""
    return osascript("next track")

@mcp.tool()
def current_track() -> str:
    """Get the currently playing track name and artist."""
    name = osascript("name of current track")
    artist = osascript("artist of current track")
    return f"{name} by {artist}"

if __name__ == "__main__":
    mcp.run(transport="stdio")

VoiceOSに接続する

VoiceOSを開き、 設定 → 連携 → カスタム連携 に移動して 追加 をクリックし、名前を付けて起動コマンドを以下のように設定します:

python3 /path/to/my_mcp_server.py
npx tsx /path/to/my-mcp-server.ts
python3 /path/to/home_control.py
npx tsx /path/to/home-control.ts
python3 /path/to/spotify_mcp.py
npx tsx /path/to/spotify-mcp.ts
python3 /path/to/system_control.py
npx tsx /path/to/system-control.ts

/path/to/ を実際のファイルパスに置き換えてください。