|
| 1 | +import asyncio |
| 2 | +from typing import Union |
| 3 | +from urllib.parse import quote |
| 4 | + |
| 5 | +from agno.agent import Agent |
| 6 | +from agno.api.playground import PlaygroundEndpointCreate, create_playground_endpoint |
| 7 | +from agno.cli.console import console |
| 8 | +from agno.cli.settings import agno_cli_settings |
| 9 | +from agno.models.anthropic import Claude |
| 10 | +from agno.playground import Playground |
| 11 | +from agno.tools.mcp import MCPTools |
| 12 | +from agno.utils.log import logger |
| 13 | +from fastapi import FastAPI |
| 14 | +from rich import box |
| 15 | +from rich.panel import Panel |
| 16 | + |
| 17 | + |
| 18 | +async def serve_playground_app_async( |
| 19 | + app: Union[str, FastAPI], |
| 20 | + *, |
| 21 | + scheme: str = "http", |
| 22 | + host: str = "localhost", |
| 23 | + port: int = 7777, |
| 24 | + reload: bool = False, |
| 25 | + prefix="/v1", |
| 26 | + **kwargs, |
| 27 | +): |
| 28 | + import uvicorn |
| 29 | + |
| 30 | + try: |
| 31 | + create_playground_endpoint( |
| 32 | + playground=PlaygroundEndpointCreate( |
| 33 | + endpoint=f"{scheme}://{host}:{port}", playground_data={"prefix": prefix} |
| 34 | + ), |
| 35 | + ) |
| 36 | + except Exception as e: |
| 37 | + logger.error(f"Could not create playground endpoint: {e}") |
| 38 | + logger.error("Please try again.") |
| 39 | + return |
| 40 | + |
| 41 | + logger.info(f"Starting playground on {scheme}://{host}:{port}") |
| 42 | + # Encode the full endpoint (host:port) |
| 43 | + encoded_endpoint = quote(f"{host}:{port}") |
| 44 | + |
| 45 | + # Create a panel with the playground URL |
| 46 | + url = f"{agno_cli_settings.playground_url}?endpoint={encoded_endpoint}" |
| 47 | + panel = Panel( |
| 48 | + f"[bold green]Playground URL:[/bold green] [link={url}]{url}[/link]", |
| 49 | + title="Agent Playground", |
| 50 | + expand=False, |
| 51 | + border_style="cyan", |
| 52 | + box=box.HEAVY, |
| 53 | + padding=(2, 2), |
| 54 | + ) |
| 55 | + |
| 56 | + # Print the panel |
| 57 | + console.print(panel) |
| 58 | + |
| 59 | + config = uvicorn.Config(app=app, host=host, port=port, reload=reload, **kwargs) |
| 60 | + server = uvicorn.Server(config) |
| 61 | + await server.serve() |
| 62 | + |
| 63 | + |
| 64 | +async def main(): |
| 65 | + async with MCPTools( |
| 66 | + f"/Users/ezyang/Dev/codemcp-prod/.venv/bin/python -m codemcp.hot_reload_entry" |
| 67 | + ) as codemcp: |
| 68 | + agent = Agent( |
| 69 | + model=Claude(id="claude-3-7-sonnet-20250219"), |
| 70 | + tools=[codemcp], |
| 71 | + instructions="init codemcp /Users/ezyang/Dev/refined-claude", |
| 72 | + markdown=True, |
| 73 | + show_tool_calls=True, |
| 74 | + ) |
| 75 | + playground = Playground(agents=[agent]).get_app() |
| 76 | + await serve_playground_app_async(playground) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + from agno.debug import enable_debug_mode |
| 81 | + |
| 82 | + enable_debug_mode() |
| 83 | + import logging |
| 84 | + |
| 85 | + logging.basicConfig(level=logging.DEBUG) |
| 86 | + logging.getLogger("httpx").setLevel(logging.DEBUG) # For HTTP logging |
| 87 | + logging.getLogger("anthropic").setLevel(logging.DEBUG) |
| 88 | + |
| 89 | + asyncio.run(main()) |
0 commit comments