@@ -709,6 +709,59 @@ function normalizeOptionalToolString(value: unknown) {
709709 : undefined ;
710710}
711711
712+ /**
713+ * Build enhanced error message for envQuery tool errors
714+ * Provides actionable guidance based on error patterns
715+ */
716+ function buildEnvQueryErrorMessage ( error : unknown , action : string ) : string {
717+ const baseMessage = error instanceof Error ? error . message : String ( error ) ;
718+
719+ // Check for common error patterns and provide specific guidance
720+ const hasInvalidParameterError = / 4 0 0 | i n v a l i d p a r a m e t e r | i n v a l i d a r g u m e n t | p a r a m e t e r v a l u e / i. test ( baseMessage ) ;
721+ const hasAuthError = / 未 登 录 | a u t h r e q u i r e d | u n a u t h o r i z e d | a u t h e n t i c a t i o n | c r e d e n t i a l | t o k e n | s e c r e t / i. test ( baseMessage ) ;
722+ const hasNetworkError = / E C O N N R E S E T | s o c k e t h a n g u p | E T I M E D O U T | E N O T F O U N D | t i m e o u t / i. test ( baseMessage ) ;
723+ const hasPermissionError = / p e r m i s s i o n | d e n i e d | f o r b i d d e n | 无 权 | 拒 绝 / i. test ( baseMessage ) ;
724+ const hasEnvNotFoundError = / e n v | e n v i r o n m e n t | 环 境 .* 不 存 在 | n o t f o u n d / i. test ( baseMessage ) ;
725+
726+ const suggestions : string [ ] = [ ] ;
727+
728+ if ( hasInvalidParameterError ) {
729+ suggestions . push ( "参数错误:可能是认证信息无效或已过期,请尝试以下步骤:" ) ;
730+ suggestions . push ( "1. 先调用 auth(action=\"status\") 检查当前登录状态" ) ;
731+ suggestions . push ( "2. 如果未登录,调用 auth(action=\"start_auth\", authMode=\"device\") 完成登录" ) ;
732+ suggestions . push ( "3. 登录完成后再次调用 envQuery(action=\"list\")" ) ;
733+ }
734+
735+ if ( hasAuthError ) {
736+ suggestions . push ( "认证错误:当前未登录或认证已过期。" ) ;
737+ suggestions . push ( "建议先执行 auth(action=\"status\") 查看状态,然后按提示完成登录。" ) ;
738+ }
739+
740+ if ( hasPermissionError ) {
741+ suggestions . push ( "权限错误:当前账号可能没有访问该资源的权限。" ) ;
742+ suggestions . push ( "请确认:1) 已选择正确的环境 2) 账号有对应权限" ) ;
743+ }
744+
745+ if ( hasEnvNotFoundError ) {
746+ suggestions . push ( "环境错误:指定的环境不存在或无法访问。" ) ;
747+ suggestions . push ( "请使用 envQuery(action=\"list\") 查看可用的环境列表。" ) ;
748+ }
749+
750+ if ( hasNetworkError ) {
751+ suggestions . push ( "网络错误:请检查网络连接,稍后重试。" ) ;
752+ }
753+
754+ // If no specific pattern matched, provide general guidance
755+ if ( suggestions . length === 0 ) {
756+ suggestions . push ( "查询环境信息时出错,建议:" ) ;
757+ suggestions . push ( "1. 先调用 auth(action=\"status\") 确认登录状态" ) ;
758+ suggestions . push ( "2. 如未登录,执行 auth(action=\"start_auth\") 完成认证" ) ;
759+ suggestions . push ( "3. 确认环境 ID 正确且可访问" ) ;
760+ }
761+
762+ return `[envQuery/${ action } ] 调用失败: ${ baseMessage } \n\n解决建议:\n${ suggestions . join ( "\n" ) } ` ;
763+ }
764+
712765function normalizeOptionalToolBoolean ( value : unknown ) {
713766 return typeof value === "boolean" ? value : undefined ;
714767}
@@ -1335,15 +1388,12 @@ export function registerEnvTools(server: ExtendedMcpServer) {
13351388 return toolPayloadResult ;
13361389 }
13371390 debug ( "降级到 listEnvs() 也失败:" , fallbackError instanceof Error ? fallbackError : new Error ( String ( fallbackError ) ) ) ;
1391+ const enhancedMessage = buildEnvQueryErrorMessage ( fallbackError , "list" ) ;
13381392 return {
13391393 content : [
13401394 {
13411395 type : "text" ,
1342- text :
1343- "获取环境列表时出错: " +
1344- ( fallbackError instanceof Error
1345- ? fallbackError . message
1346- : String ( fallbackError ) ) ,
1396+ text : enhancedMessage ,
13471397 } ,
13481398 ] ,
13491399 } ;
@@ -1464,11 +1514,12 @@ export function registerEnvTools(server: ExtendedMcpServer) {
14641514 if ( toolPayloadResult ) {
14651515 return toolPayloadResult ;
14661516 }
1517+ const enhancedMessage = buildEnvQueryErrorMessage ( error , action ) ;
14671518 return {
14681519 content : [
14691520 {
14701521 type : "text" ,
1471- text : `环境查询失败: ${ error instanceof Error ? error . message : String ( error ) } ` ,
1522+ text : enhancedMessage ,
14721523 } ,
14731524 ] ,
14741525 } ;
0 commit comments