alist/cmd/mcp.go
okatu-loli 69464a2825 fix(mcp): initialize task manager so async fs operations don't panic
cmd/mcp.go (added in v3.60.0) calls Init() and LoadStorages() but skips
bootstrap.InitTaskManager(). As a result fs.CopyTaskManager,
fs.UploadTaskManager, fs.MoveTaskManager and friends are nil, and any
MCP fs_copy / fs_move on a cross-storage target panics at
internal/fs/copy.go (CopyTaskManager.Add) with a nil-pointer dereference
that the MCP handler surfaces as:

    panic recovered in fs_copy tool handler:
    runtime error: invalid memory address or nil pointer dereference

Mirror the cmd/server.go bootstrap order so the MCP command initializes
the task manager too.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-29 09:37:25 +08:00

29 lines
835 B
Go

package cmd
import (
"github.com/alist-org/alist/v3/internal/bootstrap"
mcpserver "github.com/alist-org/alist/v3/server/mcp"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/spf13/cobra"
)
var MCPCmd = &cobra.Command{
Use: "mcp",
Short: "Start MCP server in STDIO mode",
Long: `Start an MCP (Model Context Protocol) server that communicates via STDIO, suitable for integration with AI assistants like Claude Desktop.`,
Run: func(cmd *cobra.Command, args []string) {
Init()
bootstrap.LoadStorages()
bootstrap.InitTaskManager()
username, _ := cmd.Flags().GetString("user")
if err := mcpserver.ServeStdio(username); err != nil {
utils.Log.Fatalf("MCP STDIO server error: %v", err)
}
},
}
func init() {
MCPCmd.Flags().String("user", "admin", "Username for MCP operations")
RootCmd.AddCommand(MCPCmd)
}