1010from rich .panel import Panel
1111
1212from mcp_agent .cli .exceptions import CLIError
13+ from mcp_agent .cli .utils .ux import print_error
1314
1415console = Console ()
1516
@@ -32,31 +33,29 @@ def configure_logger(
3233 ),
3334) -> None :
3435 """Configure OTEL endpoint and headers for log collection.
35-
36+
3637 This command allows you to configure the OpenTelemetry endpoint and headers
3738 that will be used for collecting logs from your deployed MCP apps.
38-
39+
3940 Examples:
4041 mcp-agent cloud logger configure https://otel.example.com:4318/v1/logs
4142 mcp-agent cloud logger configure https://otel.example.com --headers "Authorization=Bearer token,X-Custom=value"
4243 mcp-agent cloud logger configure --test # Test current configuration
4344 """
4445 if not endpoint and not test :
45- console . print ( "[red]Error: Must specify endpoint or use --test[/red] " )
46+ print_error ( " Must specify endpoint or use --test" )
4647 raise typer .Exit (1 )
47-
48+
4849 config_path = _find_config_file ()
49-
50+
5051 if test :
5152 if config_path and config_path .exists ():
5253 config = _load_config (config_path )
5354 otel_config = config .get ("otel" , {})
5455 endpoint = otel_config .get ("endpoint" )
5556 headers_dict = otel_config .get ("headers" , {})
5657 else :
57- console .print (
58- "[yellow]No configuration file found. Use --endpoint to set up OTEL configuration.[/yellow]"
59- )
58+ console .print ("[yellow]No configuration file found. Use --endpoint to set up OTEL configuration.[/yellow]" )
6059 raise typer .Exit (1 )
6160 else :
6261 headers_dict = {}
@@ -66,71 +65,56 @@ def configure_logger(
6665 key , value = header_pair .strip ().split ("=" , 1 )
6766 headers_dict [key .strip ()] = value .strip ()
6867 except ValueError :
69- console .print (
70- "[red]Error: Headers must be in format 'key=value,key2=value2'[/red]"
71- )
68+ print_error ("Headers must be in format 'key=value,key2=value2'" )
7269 raise typer .Exit (1 )
73-
70+
7471 if endpoint :
7572 console .print (f"[blue]Testing connection to { endpoint } ...[/blue]" )
76-
73+
7774 try :
7875 with httpx .Client (timeout = 10.0 ) as client :
7976 response = client .get (
80- endpoint .replace ("/v1/logs" , "/health" )
81- if "/v1/logs" in endpoint
82- else f"{ endpoint } /health" ,
83- headers = headers_dict ,
77+ endpoint .replace ("/v1/logs" , "/health" ) if "/v1/logs" in endpoint else f"{ endpoint } /health" ,
78+ headers = headers_dict
8479 )
85-
86- if response .status_code in [
87- 200 ,
88- 404 ,
89- ]: # 404 is fine, means endpoint exists
80+
81+ if response .status_code in [200 , 404 ]: # 404 is fine, means endpoint exists
9082 console .print ("[green]✓ Connection successful[/green]" )
9183 else :
92- console .print (
93- f"[yellow]⚠ Got status { response .status_code } , but endpoint is reachable[/yellow]"
94- )
95-
84+ console .print (f"[yellow]⚠ Got status { response .status_code } , but endpoint is reachable[/yellow]" )
85+
9686 except httpx .RequestError as e :
97- console . print (f"[red] ✗ Connection failed: { e } [/red] " )
87+ print_error (f"✗ Connection failed: { e } " )
9888 if not test :
99- console .print (
100- "[yellow]Configuration will be saved anyway. Check your endpoint URL and network connection.[/yellow]"
101- )
102-
89+ console .print ("[yellow]Configuration will be saved anyway. Check your endpoint URL and network connection.[/yellow]" )
90+
10391 if not test :
10492 if not config_path :
10593 config_path = Path .cwd () / "mcp_agent.config.yaml"
106-
94+
10795 config = _load_config (config_path ) if config_path .exists () else {}
108-
96+
10997 if "otel" not in config :
11098 config ["otel" ] = {}
111-
99+
112100 config ["otel" ]["endpoint" ] = endpoint
113101 config ["otel" ]["headers" ] = headers_dict
114-
102+
115103 try :
116104 config_path .parent .mkdir (parents = True , exist_ok = True )
117105 with open (config_path , "w" ) as f :
118106 yaml .dump (config , f , default_flow_style = False , sort_keys = False )
119-
120- console .print (
121- Panel (
122- f"[green]✓ OTEL configuration saved to { config_path } [/green]\n \n "
123- f"Endpoint: { endpoint } \n "
124- f"Headers: { len (headers_dict )} configured"
125- + (f" ({ ', ' .join (headers_dict .keys ())} )" if headers_dict else "" ),
126- title = "Configuration Saved" ,
127- border_style = "green" ,
128- )
129- )
130-
107+
108+ console .print (Panel (
109+ f"[green]✓ OTEL configuration saved to { config_path } [/green]\n \n "
110+ f"Endpoint: { endpoint } \n "
111+ f"Headers: { len (headers_dict )} configured" + (f" ({ ', ' .join (headers_dict .keys ())} )" if headers_dict else "" ),
112+ title = "Configuration Saved" ,
113+ border_style = "green"
114+ ))
115+
131116 except Exception as e :
132- console .print (f"[red]Error saving configuration: { e } [/red]" )
133- raise typer .Exit (1 )
117+ raise CLIError (f"Error saving configuration: { e } " )
134118
135119
136120def _find_config_file () -> Optional [Path ]:
@@ -150,4 +134,4 @@ def _load_config(config_path: Path) -> dict:
150134 with open (config_path , "r" ) as f :
151135 return yaml .safe_load (f ) or {}
152136 except Exception as e :
153- raise CLIError (f"Failed to load config from { config_path } : { e } " )
137+ raise CLIError (f"Failed to load config from { config_path } : { e } " )
0 commit comments