Skip to main content
Glama
XD3an
by XD3an

install_repo_mcp_server

Install MCP servers via pip or npm by specifying package names, arguments, and environment variables for configuration.

Instructions

Install an MCP server via pip or npm.

Args:
    name: The package name of the MCP server
    args: The arguments to pass along
    env: The environment variables to set, delimited by =

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
nameYes
argsNo
envNo

Implementation Reference

  • The handler function for the 'install_repo_mcp_server' tool, decorated with @mcp.tool() for registration. It detects if the package is npm or pip, checks prerequisites, and installs by configuring Claude Desktop's mcpServers JSON.
    @mcp.tool()
    def install_repo_mcp_server(
        name: str, 
        args: Optional[List[str]] = None,
        env: Optional[List[str]] = None,
    ) -> str:
        """
        Install an MCP server via pip or npm.
        
        Args:
            name: The package name of the MCP server
            args: The arguments to pass along
            env: The environment variables to set, delimited by =
        """
        if args is None:
            args = []
            
        # Check if Node.js is installed (for npm packages)
        has_node = check_command_exists("node")
        has_npm = check_command_exists("npm")
        has_npx = check_command_exists("npx")
        has_pip = check_command_exists("pip")
        has_python = check_command_exists("python")
        
        if not has_node and not has_python:
            return "Neither Node.js nor Python is installed. Please install one of them."
        
        # Determine if this is likely an npm package (starts with @ or doesn't have dots)
        is_likely_npm_package = name.startswith('@') or '.' not in name
        
        # Handle npm packages
        if has_npm and has_npx and is_likely_npm_package:
            # Extract server name from package name (handle scoped packages)
            if name.startswith("@") and "/" in name:
                # For @scope/package, use just "package" as the server name
                server_name = name.split("/")[1]
            else:
                server_name = name
                
            # Install to Claude Desktop using npx directly
            install_to_claude_desktop(
                server_name,
                "npx",
                [name] + args,
                env,
            )
            
            return f"Successfully installed MCP server '{server_name}' via npx! Please tell the user to restart the application."
            
        # Handle Python packages (likely has dots in the name)
        if has_pip and has_python and '.' in name:
            # For Python packages, use the package name directly
            server_name = name
            
            # Install to Claude Desktop
            install_to_claude_desktop(
                server_name,
                "python",
                ["-m", name] + args,
                env,
            )
            
            return f"Successfully installed MCP server '{server_name}' via Python! Please tell the user to restart the application."
        
        # If we can't determine the type, try npm first if available, then Python
        if has_npm and has_npx:
            # Use the last part of the name as the server name
            if "/" in name:
                server_name = name.split("/")[-1]
            else:
                server_name = name
                
            install_to_claude_desktop(
                server_name,
                "npx",
                [name] + args,
                env,
            )
            
            return f"Successfully installed MCP server '{server_name}' via npx! Please tell the user to restart the application."
        elif has_pip and has_python:
            server_name = name.split(".")[-1] if "." in name else name
            
            install_to_claude_desktop(
                server_name,
                "python",
                ["-m", name] + args,
                env,
            )
            
            return f"Successfully installed MCP server '{server_name}' via Python! Please tell the user to restart the application."
            
        return f"Could not determine how to install '{name}'"
  • Key helper function called by install_repo_mcp_server to update the Claude Desktop configuration file with the new MCP server entry.
    def install_to_claude_desktop(
        server_name: str,
        command: str,
        args: List[str],
        env: Optional[List[str]] = None,
        cwd: Optional[str] = None,
    ) -> None:
        """
        Install an MCP server to Claude Desktop.
        
        Args:
            server_name: The name of the MCP server
            command: The command to run the MCP server
            args: The arguments to pass to the command
            env: The environment variables to set, delimited by =
            cwd: The working directory for the command
        """
        # Normalize server name to be a valid identifier
        # For npm packages, make sure we use a simple name without @ or /
        if server_name.startswith("@"):
            # For @scope/package, use just "package" as the server name
            if "/" in server_name:
                server_name = server_name.split("/")[1]
        
        # Remove any invalid characters from server name
        server_name = re.sub(r'[^a-zA-Z0-9_-]', '-', server_name)
        
        # Get the Claude Desktop config file path
        config_path = get_claude_desktop_config_path()
        
        if not config_path:
            raise ValueError("Could not find Claude Desktop config file")
            
        # Read the existing config
        try:
            with open(config_path, "r", encoding="utf-8") as f:
                config = json.load(f)
        except (FileNotFoundError, json.JSONDecodeError):
            config = {}
            
        # Initialize mcpServers if it doesn't exist
        if "mcpServers" not in config:
            config["mcpServers"] = {}
            
        # Prepare the server config
        server_config = {
            "command": command,
            "args": args,
        }
        
        # Add environment variables if provided
        if env and len(env) > 0:
            server_config["env"] = parse_env_vars(env)
                    
        # Add working directory if provided
        if cwd:
            server_config["cwd"] = cwd
            
        # Add the server to the config
        config["mcpServers"][server_name] = server_config
        
        # Write the config back to the file
        with open(config_path, "w", encoding="utf-8") as f:
            json.dump(config, f, indent=2)

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/XD3an/mcp-builder'

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