Skip to main content
Glama
freefish1218

MCP HuggingFetch

by freefish1218

download_huggingface_model

Download Hugging Face models to your local machine with options for file filtering, revision selection, and custom directories.

Instructions

⚡ 高速下载 HuggingFace 模型到本地

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
repo_idYesHuggingFace 仓库 ID(格式:owner/repo)
download_dirNo下载目录(默认:~/Downloads/huggingface_models)
revisionNoGit 分支或标签main
patternNo下载文件模式(例:*.safetensors)
excludeNo排除文件模式(例:*.bin)
filesNo指定下载的文件列表
max_filesNo最大下载文件数
forceNo强制重新下载
max_concurrentNo最大并发下载数

Implementation Reference

  • Primary execution logic for the download_huggingface_model tool. Validates arguments, prepares download options, invokes the downloader, and returns formatted success/error results.
    async callDownloadTool(args) {
      try {
        // 基础验证
        if (!args.repo_id) {
          return CallToolResult.error(
            ToolContent.text('缺少必需参数: repo_id')
          );
        }
    
        logger.info(`开始下载: ${args.repo_id}`);
    
        // 获取配置
        const config = getConfig();
        const downloadDir = args.download_dir || config.download_dir;
    
        // 构建目标目录
        const targetDir = require('path').join(
          downloadDir,
          args.repo_id.replace('/', '_')
        );
    
        // 构建选项
        const options = {
          revision: args.revision,
          pattern: args.pattern,
          exclude: args.exclude,
          files: args.files,
          maxFiles: args.max_files,
          force: args.force,
          maxConcurrent: args.max_concurrent,
          token: args.token || process.env.HF_TOKEN
        };
    
        // 执行下载
        const result = await this.downloader.download(
          args.repo_id,
          targetDir,
          options
        );
    
        if (result.success) {
          logger.info(`下载完成: ${result.files} 个文件`);
    
          const response = {
            success: true,
            repo_id: args.repo_id,
            path: result.path,
            files: result.files,
            size: result.size,
            duration: result.duration,
            suggestions: result.suggestions
          };
    
          return CallToolResult.success(
            ToolContent.text(JSON.stringify(response, null, 2))
          );
        } else {
          logger.error('下载失败:', result.error);
          return CallToolResult.error(
            ToolContent.text(JSON.stringify({
              success: false,
              error: result.error,
              suggestions: result.suggestions
            }, null, 2))
          );
        }
      } catch (error) {
        logger.error('工具调用失败:', error);
        return CallToolResult.error(
          ToolContent.text(`工具调用失败: ${error.message}`)
        );
      }
    }
  • Tool schema definition for 'download_huggingface_model', specifying input parameters, descriptions, defaults, and required fields.
    getDownloadTool() {
      return new Tool(
        'download_huggingface_model',
        '⚡ 高速下载 HuggingFace 模型到本地',
        {
          type: 'object',
          properties: {
            repo_id: {
              type: 'string',
              description: 'HuggingFace 仓库 ID(格式:owner/repo)',
              examples: ['2Noise/ChatTTS', 'microsoft/DialoGPT-medium']
            },
            download_dir: {
              type: 'string',
              description: '下载目录(默认:~/Downloads/huggingface_models)'
            },
            revision: {
              type: 'string',
              description: 'Git 分支或标签',
              default: 'main'
            },
            pattern: {
              type: 'string',
              description: '下载文件模式(例:*.safetensors)'
            },
            exclude: {
              type: 'string',
              description: '排除文件模式(例:*.bin)'
            },
            files: {
              type: 'array',
              items: { type: 'string' },
              description: '指定下载的文件列表'
            },
            max_files: {
              type: 'integer',
              description: '最大下载文件数',
              default: 100
            },
            force: {
              type: 'boolean',
              description: '强制重新下载',
              default: false
            },
            max_concurrent: {
              type: 'integer',
              description: '最大并发下载数',
              default: 5
            }
          },
          required: ['repo_id']
        }
      );
    }
  • Tool dispatch registration in callTool method, mapping 'download_huggingface_model' to its handler function.
    async callTool(name, args) {
      switch (name) {
      case 'download_huggingface_model':
        return await this.callDownloadTool(args);
      case 'list_huggingface_files':
        return await this.callListTool(args);
      case 'explore_huggingface_repo':
        return await this.callExploreTool(args);
      case 'search_huggingface_files':
        return await this.callSearchTool(args);
      default:
        return CallToolResult.error(
          ToolContent.text(`未知工具: ${name}`)
        );
      }
    }
  • src/mcp/tools.js:24-31 (registration)
    Registers the download tool by including getDownloadTool() in the list returned by getTools(), used by the MCP server.
    getTools() {
      return [
        this.getDownloadTool(),
        this.getListTool(),
        this.getExploreTool(),
        this.getSearchTool()
      ];
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/freefish1218/mcp-huggingfetch'

If you have feedback or need assistance with the MCP directory API, please join our Discord server