@@ -702,8 +702,78 @@ impl CodeGraphMCPServer {
702702 request. target_name
703703 ) ;
704704
705- // Execute intelligent documentation generation
706- match rag_engine. answer ( & doc_query) . await {
705+ // Prepare shared server state (graph + optional Qwen client)
706+ let server_state = crate :: server:: ServerState {
707+ graph : self . graph . clone ( ) ,
708+ qwen_client : self . get_qwen_client ( ) . await ,
709+ } ;
710+
711+ // First, gather RAG insights so we always have citations/fallback ready
712+ let rag_result = rag_engine. answer ( & doc_query) . await ;
713+ let citations: Vec < serde_json:: Value > = rag_result
714+ . as_ref ( )
715+ . ok ( )
716+ . map ( |answer| {
717+ answer
718+ . citations
719+ . iter ( )
720+ . map ( |c| {
721+ serde_json:: json!( {
722+ "file" : c. file_path,
723+ "line" : c. line,
724+ "relevance" : c. relevance,
725+ "context" : c. name
726+ } )
727+ } )
728+ . collect ( )
729+ } )
730+ . unwrap_or_else ( Vec :: new) ;
731+
732+ // If Qwen is available, use it for richer documentation synthesis
733+ if let Some ( ref qwen_client) = server_state. qwen_client {
734+ let context_limit = ( ( qwen_client. config . context_window as f32 ) * 0.6 ) as usize ;
735+ if let Ok ( context) = crate :: server:: build_comprehensive_context (
736+ & server_state,
737+ & doc_query,
738+ context_limit. max ( 1024 ) ,
739+ )
740+ . await
741+ {
742+ match qwen_client. analyze_codebase ( & doc_query, & context) . await {
743+ Ok ( doc_result) => {
744+ let response = serde_json:: json!( {
745+ "target_name" : request. target_name,
746+ "documentation" : doc_result. text,
747+ "confidence" : doc_result. confidence_score,
748+ "style" : request. style,
749+ "sources" : citations,
750+ "processing_time_ms" : doc_result. processing_time. as_millis( ) as u64 ,
751+ "generation_method" : "qwen_documentation" ,
752+ "graph_context_used" : true ,
753+ "tool_type" : "revolutionary_documentation" ,
754+ "model_performance" : {
755+ "model_used" : doc_result. model_used,
756+ "context_tokens" : doc_result. context_tokens,
757+ "completion_tokens" : doc_result. completion_tokens,
758+ "processing_time_ms" : doc_result. processing_time. as_millis( ) ,
759+ }
760+ } ) ;
761+
762+ return Ok ( CallToolResult :: success ( vec ! [ Content :: text(
763+ serde_json:: to_string_pretty( & response) . unwrap_or_else( |_| {
764+ "Error formatting documentation response" . to_string( )
765+ } ) ,
766+ ) ] ) ) ;
767+ }
768+ Err ( e) => {
769+ eprintln ! ( "⚠️ Qwen documentation generation failed: {}" , e) ;
770+ }
771+ }
772+ }
773+ }
774+
775+ // Fallback to heuristic RAG output if Qwen is unavailable or failed
776+ match rag_result {
707777 Ok ( answer) => {
708778 let response = serde_json:: json!( {
709779 "target_name" : request. target_name,
0 commit comments