From 125c32a0b51567f00c60d520a7a51280a9bb87aa Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 28 Nov 2025 19:56:16 +0800 Subject: [PATCH] Improve MCP loading process by verifying directory existence before file operations - Added a check to confirm the existence of the 'mcps' directory prior to loading MCP files, enhancing error handling and preventing unnecessary attempts to load from a non-existent directory. - Updated error handling to ensure that any issues encountered during the loading process are logged appropriately. --- mcp/mcp.go | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/mcp/mcp.go b/mcp/mcp.go index 6d83c570..e7455586 100644 --- a/mcp/mcp.go +++ b/mcp/mcp.go @@ -19,24 +19,29 @@ import ( func Load(cfg config.Config) error { messages := []string{} - // Load filesystem MCP clients - exts := []string{"*.mcp.yao", "*.mcp.json", "*.mcp.jsonc"} - err := application.App.Walk("mcps", func(root, file string, isdir bool) error { - if isdir { - return nil - } - _, err := mcp.LoadClient(file, share.ID(root, file)) - if err != nil { - messages = append(messages, err.Error()) - } - return err - }, exts...) + // Check if mcps directory exists + exists, err := application.App.Exists("mcps") - if len(messages) > 0 { - for _, message := range messages { - log.Error("Load filesystem MCP clients error: %s", message) + // Load filesystem MCP clients if directory exists + if err == nil && exists { + exts := []string{"*.mcp.yao", "*.mcp.json", "*.mcp.jsonc"} + err = application.App.Walk("mcps", func(root, file string, isdir bool) error { + if isdir { + return nil + } + _, err := mcp.LoadClient(file, share.ID(root, file)) + if err != nil { + messages = append(messages, err.Error()) + } + return err + }, exts...) + + if len(messages) > 0 { + for _, message := range messages { + log.Error("Load filesystem MCP clients error: %s", message) + } + return fmt.Errorf("%s", strings.Join(messages, ";\n")) } - return fmt.Errorf("%s", strings.Join(messages, ";\n")) } // Load database MCP clients (ignore error)