Skip to main content
Glama
nwnusun-cool

MCP SSH Tools Server

by nwnusun-cool

upload_file

Transfer files or directories from a local system to a remote server via SSH. Specify server name, remote path, and local source path to complete the upload.

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:369-453 (handler)
    The core handler function for the 'upload_file' tool. It is registered via the @mcp.tool() decorator. Handles uploading local files or directories (recursively using the helper function) to a remote SSH server via SFTP.
    @mcp.tool()
    def upload_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:
            # 检查本地路径是否存在
            if not os.path.exists(local_src):
                return {
                    "success": False,
                    "error": f"本地路径不存在: {local_src}",
                    "server": server_name
                }
            
            sftp = client.open_sftp()
            
            if os.path.isdir(local_src):
                # 是目录,递归上传
                result = upload_directory_recursive(sftp, local_src, remote_path)
                sftp.close()
                
                if result["success"]:
                    return {
                        "success": True,
                        "server": server_name,
                        "ip": mcp_manager.server_configs[server_name].ssh_ip,
                        "type": "directory",
                        "message": f"目录上传完成: {local_src} -> {remote_path}",
                        "uploaded_files": result["uploaded_files"],
                        "failed_files": result["failed_files"],
                        "summary": {
                            "total_uploaded": len(result["uploaded_files"]),
                            "total_failed": len(result["failed_files"])
                        }
                    }
                else:
                    return {
                        "success": False,
                        "error": result["error"],
                        "server": server_name
                    }
            else:
                # 是文件,直接上传
                # 创建远程目录
                remote_dir = os.path.dirname(remote_path)
                if remote_dir:
                    try:
                        sftp.mkdir(remote_dir)
                    except IOError:
                        # 目录可能已存在,忽略错误
                        pass
                
                sftp.put(local_src, remote_path)
                sftp.close()
                
                return {
                    "success": True,
                    "server": server_name,
                    "ip": mcp_manager.server_configs[server_name].ssh_ip,
                    "type": "file",
                    "message": f"文件上传成功: {local_src} -> {remote_path}",
                    "local_path": local_src,
                    "remote_path": remote_path,
                    "size": os.path.getsize(local_src)
                }
                
        except Exception as e:
            return {
                "success": False,
                "error": str(e),
                "server": server_name
            }
  • Supporting helper function that performs recursive directory upload via SFTP, used by the upload_file handler when uploading directories.
    def upload_directory_recursive(sftp, local_path: str, remote_path: str) -> Dict[str, Any]:
        """递归上传目录"""
        try:
            # 创建远程目录
            try:
                sftp.mkdir(remote_path)
            except IOError:
                # 目录可能已存在,忽略错误
                pass
            
            uploaded_files = []
            failed_files = []
            
            for root, dirs, files in os.walk(local_path):
                # 计算相对路径
                rel_path = os.path.relpath(root, local_path)
                if rel_path == '.':
                    remote_root = remote_path
                else:
                    remote_root = f"{remote_path}/{rel_path.replace(os.sep, '/')}"
                
                # 创建远程目录
                for dir_name in dirs:
                    remote_dir = f"{remote_root}/{dir_name}"
                    try:
                        sftp.mkdir(remote_dir)
                    except IOError:
                        # 目录可能已存在,忽略错误
                        pass
                
                # 上传文件
                for file_name in files:
                    local_file_path = os.path.join(root, file_name)
                    remote_file_path = f"{remote_root}/{file_name}"
                    
                    try:
                        sftp.put(local_file_path, remote_file_path)
                        uploaded_files.append({
                            "local": local_file_path,
                            "remote": remote_file_path,
                            "size": os.path.getsize(local_file_path)
                        })
                        logger.info(f"已上传文件: {local_file_path} -> {remote_file_path}")
                    except Exception as e:
                        failed_files.append({
                            "local": local_file_path,
                            "remote": remote_file_path,
                            "error": str(e)
                        })
                        logger.error(f"上传失败: {local_file_path} - {str(e)}")
            
            return {
                "success": True,
                "uploaded_files": uploaded_files,
                "failed_files": failed_files
            }
        except Exception as e:
            return {
                "success": False,
                "error": str(e)
            }
Behavior2/5

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

No annotations are provided, so the description carries full burden. It states the upload action but doesn't disclose behavioral traits like whether it overwrites existing files, requires authentication, handles errors, supports recursive directory uploads, or has rate limits. The description is minimal and lacks crucial operational context for a file transfer tool.

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 appropriately concise with two sentences: one stating the purpose and another listing parameters. It's front-loaded with the core functionality. However, the parameter list uses minimal labels without elaboration, which could be seen as slightly under-specified rather than optimally concise.

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 has an output schema (which handles return values), no annotations, and 3 parameters with 0% schema coverage, the description is incomplete. It covers the basic purpose and parameters but lacks behavioral details, error handling, and usage context. For a file upload operation with potential complexity (e.g., overwrites, permissions), this leaves significant gaps.

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

Parameters3/5

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

The description lists all three parameters with brief Chinese labels, but schema description coverage is 0%, so the schema provides no additional parameter documentation. The description adds basic semantic meaning (e.g., 'server_name' identifies the target server), but doesn't explain format expectations (e.g., path syntax, server name conventions) or constraints beyond what's implied by parameter names.

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: '将本地的文件或者目录上传到远程服务器' (upload local files or directories to a remote server). It specifies both the verb (upload) and resources (local files/directories to remote server). However, it doesn't explicitly differentiate from sibling tools like 'download_file' or 'list_directory' beyond the obvious directional difference.

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. There are no explicit instructions about prerequisites (e.g., server must be configured first), when not to use it, or comparisons to sibling tools like 'download_file' for reverse operations or 'list_directory' for checking remote contents before upload.

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