@@ -8,6 +8,15 @@ use super::json::{build_mcp_diagnose_json, normalize_json_string};
88use super :: tools:: list_remote_tool_names;
99use crate :: runtime_env;
1010
11+ pub ( super ) struct McpDiagnoseData {
12+ pub ( super ) sanitized_config : Value ,
13+ pub ( super ) servers_raw : String ,
14+ pub ( super ) servers_json : Value ,
15+ pub ( super ) tool_names : Vec < String > ,
16+ pub ( super ) resources_json : Option < Value > ,
17+ pub ( super ) prompts_json : Option < Value > ,
18+ }
19+
1120pub ( super ) async fn run_diagnose (
1221 workspace : PathBuf ,
1322 session : Option < String > ,
@@ -43,45 +52,24 @@ pub(super) async fn run_diagnose(
4352 }
4453 } ;
4554
46- let parsed_config: Value =
47- serde_json:: from_str ( & raw_config) . context ( "parse mcp config JSON" ) ?;
48- let sanitized_config = sanitize_mcp_config ( & parsed_config) ;
49-
50- let mut tools =
51- rexos:: tools:: Toolset :: new_with_security_config ( workspace. clone ( ) , cfg. security . clone ( ) ) ?;
52- tools
53- . enable_mcp_from_json ( & raw_config)
54- . await
55- . context ( "connect mcp servers" ) ?;
56-
57- let servers_raw = tools. call ( "mcp_servers_list" , r#"{}"# ) . await ?;
58- let servers_json: Value =
59- serde_json:: from_str ( & servers_raw) . context ( "decode mcp_servers_list output" ) ?;
60- let tool_names = list_remote_tool_names ( & tools) ;
61-
62- let resources_json = if resources {
63- let out = tools. call ( "mcp_resources_list" , r#"{}"# ) . await ?;
64- Some ( serde_json:: from_str ( & out) . context ( "decode mcp_resources_list output" ) ?)
65- } else {
66- None
67- } ;
68-
69- let prompts_json = if prompts {
70- let out = tools. call ( "mcp_prompts_list" , r#"{}"# ) . await ?;
71- Some ( serde_json:: from_str ( & out) . context ( "decode mcp_prompts_list output" ) ?)
72- } else {
73- None
74- } ;
55+ let data = collect_mcp_diagnose_data (
56+ workspace. clone ( ) ,
57+ & raw_config,
58+ cfg. security . clone ( ) ,
59+ resources,
60+ prompts,
61+ )
62+ . await ?;
7563
7664 if json {
7765 let out = build_mcp_diagnose_json (
7866 & workspace,
7967 & session_id,
80- sanitized_config,
81- servers_json,
82- tool_names,
83- resources_json,
84- prompts_json,
68+ data . sanitized_config ,
69+ data . servers_json ,
70+ data . tool_names ,
71+ data . resources_json ,
72+ data . prompts_json ,
8573 ) ?;
8674 println ! ( "{}" , serde_json:: to_string_pretty( & out) ?) ;
8775 return Ok ( ( ) ) ;
@@ -96,7 +84,8 @@ pub(super) async fn run_diagnose(
9684 } else {
9785 println ! ( "config_source: session" ) ;
9886 }
99- if let Some ( servers) = sanitized_config
87+ if let Some ( servers) = data
88+ . sanitized_config
10089 . get ( "servers" )
10190 . and_then ( |v| v. as_object ( ) )
10291 . map ( |obj| obj. keys ( ) . cloned ( ) . collect :: < Vec < _ > > ( ) )
@@ -106,18 +95,63 @@ pub(super) async fn run_diagnose(
10695 }
10796 }
10897 println ! ( ) ;
109- println ! ( "servers: {}" , servers_raw. trim( ) ) ;
110- println ! ( "remote_tools: {} tool(s)" , tool_names. len( ) ) ;
111- for name in tool_names {
98+ println ! ( "servers: {}" , data . servers_raw. trim( ) ) ;
99+ println ! ( "remote_tools: {} tool(s)" , data . tool_names. len( ) ) ;
100+ for name in data . tool_names {
112101 println ! ( "- {name}" ) ;
113102 }
114- if let Some ( v) = resources_json {
103+ if let Some ( v) = data . resources_json {
115104 println ! ( ) ;
116105 println ! ( "resources_list: {}" , serde_json:: to_string_pretty( & v) ?) ;
117106 }
118- if let Some ( v) = prompts_json {
107+ if let Some ( v) = data . prompts_json {
119108 println ! ( ) ;
120109 println ! ( "prompts_list: {}" , serde_json:: to_string_pretty( & v) ?) ;
121110 }
122111 Ok ( ( ) )
123112}
113+
114+ pub ( super ) async fn collect_mcp_diagnose_data (
115+ workspace : PathBuf ,
116+ raw_config : & str ,
117+ security : rexos:: security:: SecurityConfig ,
118+ resources : bool ,
119+ prompts : bool ,
120+ ) -> anyhow:: Result < McpDiagnoseData > {
121+ let parsed_config: Value = serde_json:: from_str ( raw_config) . context ( "parse mcp config JSON" ) ?;
122+ let sanitized_config = sanitize_mcp_config ( & parsed_config) ;
123+
124+ let mut tools = rexos:: tools:: Toolset :: new_with_security_config ( workspace, security) ?;
125+ tools
126+ . enable_mcp_from_json ( raw_config)
127+ . await
128+ . context ( "connect mcp servers" ) ?;
129+
130+ let servers_raw = tools. call ( "mcp_servers_list" , r#"{}"# ) . await ?;
131+ let servers_json: Value =
132+ serde_json:: from_str ( & servers_raw) . context ( "decode mcp_servers_list output" ) ?;
133+ let tool_names = list_remote_tool_names ( & tools) ;
134+
135+ let resources_json = if resources {
136+ let out = tools. call ( "mcp_resources_list" , r#"{}"# ) . await ?;
137+ Some ( serde_json:: from_str ( & out) . context ( "decode mcp_resources_list output" ) ?)
138+ } else {
139+ None
140+ } ;
141+
142+ let prompts_json = if prompts {
143+ let out = tools. call ( "mcp_prompts_list" , r#"{}"# ) . await ?;
144+ Some ( serde_json:: from_str ( & out) . context ( "decode mcp_prompts_list output" ) ?)
145+ } else {
146+ None
147+ } ;
148+
149+ Ok ( McpDiagnoseData {
150+ sanitized_config,
151+ servers_raw,
152+ servers_json,
153+ tool_names,
154+ resources_json,
155+ prompts_json,
156+ } )
157+ }
0 commit comments