Skip to main content
Glama
OtotaO

Unsloth MCP Server

by OtotaO

finetune_model

Optimize model fine-tuning using Unsloth's efficient training methods to reduce memory usage and accelerate the process with specific parameters.

Instructions

Fine-tune a model with Unsloth optimizations

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
model_nameYesName of the model to fine-tune
dataset_nameYesName of the dataset to use for fine-tuning
output_dirYesDirectory to save the fine-tuned model
max_seq_lengthNoMaximum sequence length for training
lora_rankNoRank for LoRA fine-tuning
lora_alphaNoAlpha for LoRA fine-tuning
batch_sizeNoBatch size for training
gradient_accumulation_stepsNoNumber of gradient accumulation steps
learning_rateNoLearning rate for training
max_stepsNoMaximum number of training steps
dataset_text_fieldNoField in the dataset containing the text
load_in_4bitNoWhether to use 4-bit quantization

Implementation Reference

  • The handler function for the 'finetune_model' tool. It extracts parameters from the input arguments, constructs a comprehensive Python script using Unsloth for loading a model, applying LoRA adapters, training with SFTTrainer on a Hugging Face dataset, and saving the fine-tuned model. The script is executed via executeUnslothScript, and the JSON result is parsed and returned.
              case 'finetune_model': {
                const {
                  model_name,
                  dataset_name,
                  output_dir,
                  max_seq_length = 2048,
                  lora_rank = 16,
                  lora_alpha = 16,
                  batch_size = 2,
                  gradient_accumulation_steps = 4,
                  learning_rate = 2e-4,
                  max_steps = 100,
                  dataset_text_field = 'text',
                  load_in_4bit = true,
                } = args as {
                  model_name: string;
                  dataset_name: string;
                  output_dir: string;
                  max_seq_length?: number;
                  lora_rank?: number;
                  lora_alpha?: number;
                  batch_size?: number;
                  gradient_accumulation_steps?: number;
                  learning_rate?: number;
                  max_steps?: number;
                  dataset_text_field?: string;
                  load_in_4bit?: boolean;
                };
    
                const script = `
    import json
    import os
    try:
        from unsloth import FastLanguageModel
        from datasets import load_dataset
        from trl import SFTTrainer, SFTConfig
        import torch
        
        # Create output directory if it doesn't exist
        os.makedirs("${output_dir}", exist_ok=True)
        
        # Load the model
        model, tokenizer = FastLanguageModel.from_pretrained(
            model_name="${model_name}",
            max_seq_length=${max_seq_length},
            load_in_4bit=${load_in_4bit ? 'True' : 'False'},
            use_gradient_checkpointing="unsloth"
        )
        
        # Load the dataset
        dataset = load_dataset("${dataset_name}")
        
        # Patch the model with LoRA
        model = FastLanguageModel.get_peft_model(
            model,
            r=${lora_rank},
            target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
            lora_alpha=${lora_alpha},
            use_gradient_checkpointing="unsloth",
            random_state=3407,
            max_seq_length=${max_seq_length},
            use_rslora=False,
            loftq_config=None
        )
        
        # Configure the trainer
        trainer = SFTTrainer(
            model=model,
            train_dataset=dataset["train"],
            tokenizer=tokenizer,
            args=SFTConfig(
                dataset_text_field="${dataset_text_field}",
                max_seq_length=${max_seq_length},
                per_device_train_batch_size=${batch_size},
                gradient_accumulation_steps=${gradient_accumulation_steps},
                warmup_steps=10,
                max_steps=${max_steps},
                learning_rate=${learning_rate},
                logging_steps=1,
                output_dir="${output_dir}",
                optim="adamw_8bit",
                seed=3407,
            ),
        )
        
        # Train the model
        trainer.train()
        
        # Save the model
        trainer.save_model()
        
        print(json.dumps({
            "success": True,
            "output_dir": "${output_dir}",
            "model_name": "${model_name}",
            "dataset_name": "${dataset_name}",
            "max_steps": ${max_steps}
        }))
    except Exception as e:
        print(json.dumps({"error": str(e), "success": False}))
    `;
                const result = await this.executeUnslothScript(script);
                
                try {
                  const trainingResult = JSON.parse(result);
                  if (!trainingResult.success) {
                    throw new Error(trainingResult.error);
                  }
                  
                  return {
                    content: [
                      {
                        type: 'text',
                        text: `Successfully fine-tuned model: ${model_name} with dataset: ${dataset_name}\n\n${JSON.stringify(trainingResult, null, 2)}`,
                      },
                    ],
                  };
                } catch (error: any) {
                  throw new Error(`Error fine-tuning model: ${error.message}`);
                }
              }
  • Input schema defining the parameters for the finetune_model tool, including required fields (model_name, dataset_name, output_dir) and optional hyperparameters for training.
    inputSchema: {
      type: 'object',
      properties: {
        model_name: {
          type: 'string',
          description: 'Name of the model to fine-tune',
        },
        dataset_name: {
          type: 'string',
          description: 'Name of the dataset to use for fine-tuning',
        },
        output_dir: {
          type: 'string',
          description: 'Directory to save the fine-tuned model',
        },
        max_seq_length: {
          type: 'number',
          description: 'Maximum sequence length for training',
        },
        lora_rank: {
          type: 'number',
          description: 'Rank for LoRA fine-tuning',
        },
        lora_alpha: {
          type: 'number',
          description: 'Alpha for LoRA fine-tuning',
        },
        batch_size: {
          type: 'number',
          description: 'Batch size for training',
        },
        gradient_accumulation_steps: {
          type: 'number',
          description: 'Number of gradient accumulation steps',
        },
        learning_rate: {
          type: 'number',
          description: 'Learning rate for training',
        },
        max_steps: {
          type: 'number',
          description: 'Maximum number of training steps',
        },
        dataset_text_field: {
          type: 'string',
          description: 'Field in the dataset containing the text',
        },
        load_in_4bit: {
          type: 'boolean',
          description: 'Whether to use 4-bit quantization',
        },
      },
      required: ['model_name', 'dataset_name', 'output_dir'],
    },
  • src/index.ts:112-169 (registration)
    Registration of the 'finetune_model' tool in the listTools response, including name, description, and inputSchema.
    {
      name: 'finetune_model',
      description: 'Fine-tune a model with Unsloth optimizations',
      inputSchema: {
        type: 'object',
        properties: {
          model_name: {
            type: 'string',
            description: 'Name of the model to fine-tune',
          },
          dataset_name: {
            type: 'string',
            description: 'Name of the dataset to use for fine-tuning',
          },
          output_dir: {
            type: 'string',
            description: 'Directory to save the fine-tuned model',
          },
          max_seq_length: {
            type: 'number',
            description: 'Maximum sequence length for training',
          },
          lora_rank: {
            type: 'number',
            description: 'Rank for LoRA fine-tuning',
          },
          lora_alpha: {
            type: 'number',
            description: 'Alpha for LoRA fine-tuning',
          },
          batch_size: {
            type: 'number',
            description: 'Batch size for training',
          },
          gradient_accumulation_steps: {
            type: 'number',
            description: 'Number of gradient accumulation steps',
          },
          learning_rate: {
            type: 'number',
            description: 'Learning rate for training',
          },
          max_steps: {
            type: 'number',
            description: 'Maximum number of training steps',
          },
          dataset_text_field: {
            type: 'string',
            description: 'Field in the dataset containing the text',
          },
          load_in_4bit: {
            type: 'boolean',
            description: 'Whether to use 4-bit quantization',
          },
        },
        required: ['model_name', 'dataset_name', 'output_dir'],
      },
    },
  • Helper method used by the finetune_model handler (and others) to execute dynamically generated Python scripts via child_process.exec.
    private async executeUnslothScript(script: string): Promise<string> {
      try {
        const { stdout, stderr } = await execPromise(`python -c "${script}"`);
        if (stderr && !stdout) {
          throw new Error(stderr);
        }
        return stdout;
      } catch (error: any) {
        throw new Error(`Error executing Unsloth script: ${error.message}`);
      }
    }
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 'Unsloth optimizations' but doesn't explain what this entails (e.g., performance improvements, memory efficiency). Critical behavioral traits like computational cost, time requirements, potential data destruction, or output format are omitted. The description adds minimal context beyond the basic action.

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

Conciseness5/5

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

The description is a single, efficient sentence with zero waste. It is front-loaded with the core action ('fine-tune a model') and includes a relevant detail ('with Unsloth optimizations') that adds value without verbosity. Every word earns its place.

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

Completeness2/5

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

Given the complexity of a fine-tuning operation (12 parameters, no annotations, no output schema), the description is incomplete. It lacks information on behavioral aspects (e.g., runtime, resource usage), output (what is returned), and usage guidelines. The high parameter count and absence of structured support fields make this inadequate for such a complex tool.

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?

Schema description coverage is 100%, so the schema already documents all 12 parameters thoroughly (e.g., 'model_name', 'dataset_name', 'lora_rank'). The description adds no additional meaning about parameters, such as typical values, constraints, or relationships between them. Baseline score of 3 is appropriate as the schema does the heavy lifting.

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 action ('fine-tune') and resource ('a model') with a specific optimization method ('with Unsloth optimizations'). It distinguishes from siblings like 'export_model' or 'generate_text' by focusing on training rather than inference or export. However, it doesn't explicitly differentiate from 'load_model' in terms of when to use each.

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 prerequisites (e.g., needing a pre-trained model and dataset), when not to use it (e.g., for inference tasks), or refer to sibling tools like 'list_supported_models' for model selection. Usage is implied but not explicitly stated.

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/OtotaO/unsloth-mcp-server'

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