1- """CLI command for launching a web chat UI for agents."""
2-
31from __future__ import annotations
42
5- import sys
6- from pathlib import Path
7- from typing import Any
8-
9- from pydantic import ImportString , TypeAdapter , ValidationError
3+ from pydantic import ValidationError
104from rich .console import Console
115
126from pydantic_ai import Agent
13- from pydantic_ai .builtin_tools import AbstractBuiltinTool , get_builtin_tool_cls
7+ from pydantic_ai .builtin_tools import (
8+ BUILTIN_TOOL_TYPES ,
9+ DEPRECATED_BUILTIN_TOOL_TYPES ,
10+ AbstractBuiltinTool ,
11+ )
1412from pydantic_ai .mcp import MCPServerSSE , MCPServerStdio , MCPServerStreamableHTTP , load_mcp_servers
1513from pydantic_ai .models import infer_model
1614from pydantic_ai .ui ._web import create_web_app
1715
18- __all__ = ['_run_web_command' ]
19-
20- _import_string_adapter : TypeAdapter [Any ] = TypeAdapter (ImportString )
16+ from . import load_agent
2117
18+ # Tools that require configuration and cannot be enabled via CLI
19+ # (includes deprecated tools plus tools needing config like mcp_server and memory)
20+ UNSUPPORTED_CLI_TOOLS = DEPRECATED_BUILTIN_TOOL_TYPES | frozenset ({'mcp_server' , 'memory' })
2221
23- def _load_agent (agent_path : str ) -> Agent | None :
24- """Load an agent from module path in uvicorn style.
2522
26- Args:
27- agent_path: Path in format 'module:variable', e.g. 'test_agent:my_agent'
28-
29- Returns:
30- Agent instance or None if loading fails
31- """
32- sys .path .insert (0 , str (Path .cwd ()))
33- try :
34- obj = _import_string_adapter .validate_python (agent_path )
35- if not isinstance (obj , Agent ):
36- return None
37- return obj # pyright: ignore[reportUnknownVariableType]
38- except ValidationError :
39- return None
40-
41-
42- def _run_web_command ( # noqa: C901
23+ def run_web_command ( # noqa: C901
4324 agent_path : str | None = None ,
4425 host : str = '127.0.0.1' ,
4526 port : int = 7932 ,
@@ -66,7 +47,7 @@ def _run_web_command( # noqa: C901
6647 console = Console ()
6748
6849 if agent_path :
69- agent = _load_agent (agent_path )
50+ agent = load_agent (agent_path )
7051 if agent is None :
7152 console .print (f'[red]Error: Could not load agent from { agent_path } [/red]' )
7253 return 1
@@ -92,12 +73,14 @@ def _run_web_command( # noqa: C901
9273 # then CLI tools
9374 if tools :
9475 for tool_id in tools :
95- tool_cls = get_builtin_tool_cls (tool_id )
96- if tool_cls is None or tool_id in ('url_context' , 'mcp_server' ):
97- console .print (f'[yellow]Warning: Unknown tool "{ tool_id } ", skipping[/yellow]' )
76+ if tool_id in UNSUPPORTED_CLI_TOOLS :
77+ console .print (
78+ f'[yellow]Warning: "{ tool_id } " requires configuration and cannot be enabled via CLI, skipping[/yellow]'
79+ )
9880 continue
99- if tool_id == 'memory' :
100- console .print ('[yellow]Warning: MemoryTool requires agent to have memory configured, skipping[/yellow]' )
81+ tool_cls = BUILTIN_TOOL_TYPES .get (tool_id )
82+ if tool_cls is None :
83+ console .print (f'[yellow]Warning: Unknown tool "{ tool_id } ", skipping[/yellow]' )
10184 continue
10285 all_tool_instances .append (tool_cls ())
10386
@@ -120,8 +103,8 @@ def _run_web_command( # noqa: C901
120103 app = create_web_app (
121104 agent ,
122105 models = models ,
123- builtin_tools = all_tool_instances if all_tool_instances else None ,
124- toolsets = mcp_servers if mcp_servers else None ,
106+ builtin_tools = all_tool_instances or None ,
107+ toolsets = mcp_servers or None ,
125108 )
126109
127110 agent_desc = agent_path if agent_path else 'generic agent'
0 commit comments