Skip to main content
Glama
nwnusun-cool

MCP SSH Tools Server

by nwnusun-cool

download_file

Download files or directories from remote SSH servers to your local system. Specify server name, remote path, and local destination for secure file transfers.

Instructions

下载远程服务器的文件或者目录,保存到指定目录下,如果是目录,则递归下载 参数:

  • server_name: 服务器名称

  • remote_path: 远程文件/目录路径

  • local_src: 本地文件/目录路径

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
server_nameYes
remote_pathYes
local_srcYes

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • main.py:281-367 (handler)
    Main handler function for the download_file tool, decorated with @mcp.tool() for registration in MCP. Handles single file and directory (recursive) downloads via SFTP.
    @mcp.tool()
    def download_file(server_name: str, remote_path: str, local_src: str) -> Dict[str, Any]:
        """
        下载远程服务器的文件或者目录,保存到指定目录下,如果是目录,则递归下载
        参数:
        - server_name: 服务器名称
        - remote_path: 远程文件/目录路径
        - local_src: 本地文件/目录路径
        """
        client = mcp_manager.get_connection(server_name)
        if not client:
            return {
                "success": False,
                "error": "SSH连接失败",
                "server": server_name
            }
        
        try:
            sftp = client.open_sftp()
            
            # 检查远程路径是否存在
            try:
                file_attr = sftp.stat(remote_path)
            except IOError:
                sftp.close()
                return {
                    "success": False,
                    "error": f"远程路径不存在: {remote_path}",
                    "server": server_name
                }
            
            # 判断是文件还是目录
            if stat.S_ISDIR(file_attr.st_mode):
                # 是目录,递归下载
                result = download_directory_recursive(sftp, remote_path, local_src)
                sftp.close()
                
                if result["success"]:
                    return {
                        "success": True,
                        "server": server_name,
                        "ip": mcp_manager.server_configs[server_name].ssh_ip,
                        "type": "directory",
                        "message": f"目录下载完成: {remote_path} -> {local_src}",
                        "downloaded_files": result["downloaded_files"],
                        "failed_files": result["failed_files"],
                        "summary": {
                            "total_downloaded": len(result["downloaded_files"]),
                            "total_failed": len(result["failed_files"])
                        }
                    }
                else:
                    return {
                        "success": False,
                        "error": result["error"],
                        "server": server_name
                    }
            else:
                # 是文件,直接下载
                # 如果本地路径是目录,则在该目录下创建同名文件
                if os.path.isdir(local_src):
                    local_file_path = os.path.join(local_src, os.path.basename(remote_path))
                else:
                    local_file_path = local_src
                    # 创建本地目录
                    os.makedirs(os.path.dirname(local_file_path), exist_ok=True)
                
                sftp.get(remote_path, local_file_path)
                sftp.close()
                
                return {
                    "success": True,
                    "server": server_name,
                    "ip": mcp_manager.server_configs[server_name].ssh_ip,
                    "type": "file",
                    "message": f"文件下载成功: {remote_path} -> {local_file_path}",
                    "remote_path": remote_path,
                    "local_path": local_file_path,
                    "size": file_attr.st_size
                }
                
        except Exception as e:
            return {
                "success": False,
                "error": str(e),
                "server": server_name
            }
  • Supporting recursive function for downloading entire directories, invoked by the main handler when the remote path is a directory.
    def download_directory_recursive(sftp, remote_path: str, local_path: str) -> Dict[str, Any]:
        """递归下载目录"""
        try:
            # 创建本地目录
            os.makedirs(local_path, exist_ok=True)
            
            # 获取远程目录内容
            files = sftp.listdir_attr(remote_path)
            downloaded_files = []
            failed_files = []
            
            for file_attr in files:
                remote_file_path = f"{remote_path}/{file_attr.filename}"
                local_file_path = os.path.join(local_path, file_attr.filename)
                
                try:
                    if stat.S_ISDIR(file_attr.st_mode):
                        # 如果是目录,递归下载
                        result = download_directory_recursive(sftp, remote_file_path, local_file_path)
                        downloaded_files.extend(result.get("downloaded_files", []))
                        failed_files.extend(result.get("failed_files", []))
                    else:
                        # 如果是文件,直接下载
                        sftp.get(remote_file_path, local_file_path)
                        downloaded_files.append({
                            "remote": remote_file_path,
                            "local": local_file_path,
                            "size": file_attr.st_size
                        })
                        logger.info(f"已下载文件: {remote_file_path} -> {local_file_path}")
                except Exception as e:
                    failed_files.append({
                        "remote": remote_file_path,
                        "local": local_file_path,
                        "error": str(e)
                    })
                    logger.error(f"下载失败: {remote_file_path} - {str(e)}")
            
            return {
                "success": True,
                "downloaded_files": downloaded_files,
                "failed_files": failed_files
            }
        except Exception as e:
            return {
                "success": False,
                "error": str(e)
            }
  • Input schema defined by function parameters (server_name: str, remote_path: str, local_src: str) and descriptive docstring.
    def download_file(server_name: str, remote_path: str, local_src: str) -> Dict[str, Any]:
        """
        下载远程服务器的文件或者目录,保存到指定目录下,如果是目录,则递归下载
        参数:
        - server_name: 服务器名称
        - remote_path: 远程文件/目录路径
        - local_src: 本地文件/目录路径
        """
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It mentions recursive downloading for directories, which adds some context beyond basic functionality. However, it lacks critical details: authentication requirements (implied by 'server_name'), potential destructive behavior (overwriting local files), error handling, or rate limits. For a tool with no annotations, this is insufficient.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is concise and well-structured: a clear purpose statement followed by a bulleted list of parameters. Every sentence earns its place, with no redundant information. However, it could be more front-loaded by integrating parameter hints into the main statement, and the use of Chinese might reduce accessibility in some contexts.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (file operations with potential recursion) and no annotations, the description is minimally adequate. It covers the basic action and parameters but lacks details on permissions, errors, or output (though an output schema exists, reducing the need to explain returns). For a tool with no annotations and 0% schema coverage, it should do more to be complete.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 0%, so the description must compensate. It lists all three parameters ('server_name', 'remote_path', 'local_src') with brief explanations in Chinese, adding meaning beyond the schema's generic titles. For example, it clarifies 'local_src' as the local destination path. This adequately covers the parameters, though more detail (e.g., path formats) would be needed for a 5.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: '下载远程服务器的文件或者目录,保存到指定目录下,如果是目录,则递归下载' (Download files or directories from a remote server to a specified local directory, recursively for directories). It specifies the verb (download), resource (files/directories), and scope (recursive for directories). However, it doesn't explicitly differentiate from sibling tools like 'upload_file' or 'list_directory', which would require a 5.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention sibling tools like 'upload_file' (for uploading), 'list_directory' (for browsing), or 'execute' (for running commands). There's no context about prerequisites (e.g., server configuration via 'add_server_config') or exclusions, leaving usage unclear.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/nwnusun-cool/mcp-server-ssh-tools'

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