|
| 1 | +""" |
| 2 | +MCP voice agent that routes queries either to Firecrawl web search or to Supabase via MCP. |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import asyncio |
| 8 | +import copy |
| 9 | +import json |
| 10 | +import logging |
| 11 | +import os |
| 12 | +from typing import Any, Callable, List, Optional |
| 13 | + |
| 14 | +import inspect |
| 15 | +from dotenv import load_dotenv |
| 16 | +from firecrawl import FirecrawlApp, ScrapeOptions |
| 17 | +from pydantic_ai.mcp import MCPServerStdio |
| 18 | + |
| 19 | +from livekit.agents import ( |
| 20 | + Agent, |
| 21 | + AgentSession, |
| 22 | + JobContext, |
| 23 | + RunContext, |
| 24 | + WorkerOptions, |
| 25 | + cli, |
| 26 | + function_tool, |
| 27 | +) |
| 28 | +from livekit.plugins import assemblyai, openai, silero |
| 29 | + |
| 30 | +# ------------------------------------------------------------------------------ |
| 31 | +# Configuration & Logging |
| 32 | +# ------------------------------------------------------------------------------ |
| 33 | +load_dotenv() |
| 34 | +logging.basicConfig(level=logging.INFO) |
| 35 | +logger = logging.getLogger(__name__) |
| 36 | + |
| 37 | +FIRECRAWL_API_KEY = os.getenv("FIRECRAWL_API_KEY") |
| 38 | +SUPABASE_TOKEN = os.getenv("SUPABASE_ACCESS_TOKEN") |
| 39 | + |
| 40 | +if not FIRECRAWL_API_KEY: |
| 41 | + logger.error("FIRECRAWL_API_KEY is not set in environment.") |
| 42 | + raise EnvironmentError("Please set FIRECRAWL_API_KEY env var.") |
| 43 | + |
| 44 | +if not SUPABASE_TOKEN: |
| 45 | + logger.error("SUPABASE_ACCESS_TOKEN is not set in environment.") |
| 46 | + raise EnvironmentError("Please set SUPABASE_ACCESS_TOKEN env var.") |
| 47 | + |
| 48 | +firecrawl_app = FirecrawlApp(api_key=FIRECRAWL_API_KEY) |
| 49 | + |
| 50 | + |
| 51 | +def _py_type(schema: dict) -> Any: |
| 52 | + """Convert JSON schema types into Python typing annotations.""" |
| 53 | + t = schema.get("type") |
| 54 | + mapping = { |
| 55 | + "string": str, |
| 56 | + "integer": int, |
| 57 | + "number": float, |
| 58 | + "boolean": bool, |
| 59 | + "object": dict, |
| 60 | + } |
| 61 | + |
| 62 | + if isinstance(t, list): |
| 63 | + if "array" in t: |
| 64 | + return List[_py_type(schema.get("items", {}))] |
| 65 | + t = t[0] |
| 66 | + |
| 67 | + if isinstance(t, str) and t in mapping: |
| 68 | + return mapping[t] |
| 69 | + if t == "array": |
| 70 | + return List[_py_type(schema.get("items", {}))] |
| 71 | + |
| 72 | + return Any |
| 73 | + |
| 74 | + |
| 75 | +def schema_to_google_docstring(description: str, schema: dict) -> str: |
| 76 | + """ |
| 77 | + Generate a Google-style docstring section from a JSON schema. |
| 78 | + """ |
| 79 | + props = schema.get("properties", {}) |
| 80 | + required = set(schema.get("required", [])) |
| 81 | + lines = [description or "", "Args:"] |
| 82 | + |
| 83 | + for name, prop in props.items(): |
| 84 | + t = prop.get("type", "Any") |
| 85 | + if isinstance(t, list): |
| 86 | + if "array" in t: |
| 87 | + subtype = prop.get("items", {}).get("type", "Any") |
| 88 | + py_type = f"List[{subtype.capitalize()}]" |
| 89 | + else: |
| 90 | + py_type = t[0].capitalize() |
| 91 | + elif t == "array": |
| 92 | + subtype = prop.get("items", {}).get("type", "Any") |
| 93 | + py_type = f"List[{subtype.capitalize()}]" |
| 94 | + else: |
| 95 | + py_type = t.capitalize() |
| 96 | + |
| 97 | + if name not in required: |
| 98 | + py_type = f"Optional[{py_type}]" |
| 99 | + |
| 100 | + desc = prop.get("description", "") |
| 101 | + lines.append(f" {name} ({py_type}): {desc}") |
| 102 | + |
| 103 | + return "\n".join(lines) |
| 104 | + |
| 105 | + |
| 106 | +@function_tool |
| 107 | +async def firecrawl_search( |
| 108 | + context: RunContext, |
| 109 | + query: str, |
| 110 | + limit: int = 5 |
| 111 | +) -> List[str]: |
| 112 | + """ |
| 113 | + Search the web via Firecrawl. |
| 114 | +
|
| 115 | + Args: |
| 116 | + context (RunContext): LiveKit runtime context. |
| 117 | + query (str): Search query string. |
| 118 | + limit (int): Maximum pages to crawl. |
| 119 | +
|
| 120 | + Returns: |
| 121 | + List[str]: Raw page contents. |
| 122 | + """ |
| 123 | + url = f"https://www.google.com/search?q={query}" |
| 124 | + logger.debug("Starting Firecrawl for URL: %s (limit=%d)", url, limit) |
| 125 | + |
| 126 | + loop = asyncio.get_event_loop() |
| 127 | + try: |
| 128 | + result = await loop.run_in_executor( |
| 129 | + None, |
| 130 | + lambda: firecrawl_app.crawl_url( |
| 131 | + url, |
| 132 | + limit=limit, |
| 133 | + scrape_options=ScrapeOptions(formats=["text", "markdown"]) |
| 134 | + ) |
| 135 | + ) |
| 136 | + logger.info("Firecrawl returned %d pages", len(result)) |
| 137 | + return result |
| 138 | + except Exception as e: |
| 139 | + logger.error("Firecrawl search failed: %s", e, exc_info=True) |
| 140 | + return [] |
| 141 | + |
| 142 | + |
| 143 | +async def build_livekit_tools(server: MCPServerStdio) -> List[Callable]: |
| 144 | + """ |
| 145 | + Build LiveKit tools from a Supabase MCP server. |
| 146 | + """ |
| 147 | + tools: List[Callable] = [] |
| 148 | + all_tools = await server.list_tools() |
| 149 | + logger.info("Found %d MCP tools", len(all_tools)) |
| 150 | + |
| 151 | + for td in all_tools: |
| 152 | + if td.name == "deploy_edge_function": |
| 153 | + logger.warning("Skipping tool %s", td.name) |
| 154 | + continue |
| 155 | + |
| 156 | + schema = copy.deepcopy(td.parameters_json_schema) |
| 157 | + if td.name == "list_tables": |
| 158 | + props = schema.setdefault("properties", {}) |
| 159 | + props["schemas"] = { |
| 160 | + "type": ["array", "null"], |
| 161 | + "items": {"type": "string"}, |
| 162 | + "default": [] |
| 163 | + } |
| 164 | + schema["required"] = [r for r in schema.get("required", []) if r != "schemas"] |
| 165 | + |
| 166 | + props = schema.get("properties", {}) |
| 167 | + required = set(schema.get("required", [])) |
| 168 | + |
| 169 | + def make_proxy( |
| 170 | + tool_def=td, |
| 171 | + _props=props, |
| 172 | + _required=required, |
| 173 | + _schema=schema |
| 174 | + ) -> Callable: |
| 175 | + async def proxy(context: RunContext, **kwargs): |
| 176 | + # Convert None → [] for array params |
| 177 | + for k, v in list(kwargs.items()): |
| 178 | + if ((_props[k].get("type") == "array" |
| 179 | + or "array" in (_props[k].get("type") or [])) |
| 180 | + and v is None): |
| 181 | + kwargs[k] = [] |
| 182 | + |
| 183 | + response = await server.call_tool(tool_def.name, arguments=kwargs or None) |
| 184 | + if isinstance(response, list): |
| 185 | + return response |
| 186 | + if hasattr(response, "content") and response.content: |
| 187 | + text = response.content[0].text |
| 188 | + try: |
| 189 | + return json.loads(text) |
| 190 | + except json.JSONDecodeError: |
| 191 | + return text |
| 192 | + return response |
| 193 | + |
| 194 | + # Build signature from schema |
| 195 | + params = [ |
| 196 | + inspect.Parameter("context", inspect.Parameter.POSITIONAL_OR_KEYWORD, annotation=RunContext) |
| 197 | + ] |
| 198 | + ann = {"context": RunContext} |
| 199 | + |
| 200 | + for name, ps in _props.items(): |
| 201 | + default = ps.get("default", inspect._empty if name in required else None) |
| 202 | + params.append( |
| 203 | + inspect.Parameter( |
| 204 | + name, |
| 205 | + inspect.Parameter.KEYWORD_ONLY, |
| 206 | + annotation=_py_type(ps), |
| 207 | + default=default, |
| 208 | + ) |
| 209 | + ) |
| 210 | + ann[name] = _py_type(ps) |
| 211 | + |
| 212 | + proxy.__signature__ = inspect.Signature(params) |
| 213 | + proxy.__annotations__ = ann |
| 214 | + proxy.__name__ = tool_def.name |
| 215 | + proxy.__doc__ = schema_to_google_docstring(tool_def.description or "", _schema) |
| 216 | + return function_tool(proxy) |
| 217 | + |
| 218 | + tools.append(make_proxy()) |
| 219 | + |
| 220 | + return tools |
| 221 | + |
| 222 | + |
| 223 | +async def entrypoint(ctx: JobContext) -> None: |
| 224 | + """ |
| 225 | + Main entrypoint for the LiveKit agent. |
| 226 | + """ |
| 227 | + await ctx.connect() |
| 228 | + server = MCPServerStdio( |
| 229 | + "npx", |
| 230 | + args=["-y", "@supabase/mcp-server-supabase@latest", "--access-token", SUPABASE_TOKEN], |
| 231 | + ) |
| 232 | + await server.__aenter__() |
| 233 | + |
| 234 | + try: |
| 235 | + supabase_tools = await build_livekit_tools(server) |
| 236 | + tools = [firecrawl_search] + supabase_tools |
| 237 | + |
| 238 | + agent = Agent( |
| 239 | + instructions=( |
| 240 | + "You can either perform live web searches via `firecrawl_search` or " |
| 241 | + "database queries via Supabase MCP tools. " |
| 242 | + "Choose the appropriate tool based on whether the user needs fresh web data " |
| 243 | + "(news, external facts) or internal Supabase data." |
| 244 | + ), |
| 245 | + tools=tools, |
| 246 | + ) |
| 247 | + |
| 248 | + session = AgentSession( |
| 249 | + vad=silero.VAD.load(min_silence_duration=0.1), |
| 250 | + stt=assemblyai.STT(word_boost=["Supabase"]), |
| 251 | + llm=openai.LLM(model="gpt-4o"), |
| 252 | + tts=openai.TTS(voice="ash"), |
| 253 | + ) |
| 254 | + |
| 255 | + await session.start(agent=agent, room=ctx.room) |
| 256 | + await session.generate_reply(instructions="Hello! How can I assist you today?") |
| 257 | + |
| 258 | + # Keep the session alive until cancelled |
| 259 | + try: |
| 260 | + while True: |
| 261 | + await asyncio.sleep(1) |
| 262 | + except asyncio.CancelledError: |
| 263 | + logger.info("Session cancelled, shutting down.") |
| 264 | + |
| 265 | + finally: |
| 266 | + await server.__aexit__(None, None, None) |
| 267 | + |
| 268 | + |
| 269 | +if __name__ == "__main__": |
| 270 | + cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint)) |
0 commit comments