Skip to main content
Glama

ensure_service

Control service states on remote servers via SSH by starting, stopping, enabling, or disabling services to maintain desired configurations.

Instructions

Ensures a service is in the desired state

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sessionIdYesSSH session ID
nameYesService name
stateYesDesired state

Implementation Reference

  • The implementation of the ensureService tool handler.
    export async function ensureService(
      sessionId: string,
      serviceName: string,
      state: 'started' | 'stopped' | 'restarted' | 'enabled' | 'disabled',
      sudoPassword?: string
    ): Promise<ServiceResult> {
      logger.debug('Ensuring service state', { sessionId, serviceName, state });
    
      const session = sessionManager.getSession(sessionId);
      if (!session) {
        throw new Error(`Session ${sessionId} not found or expired`);
      }
    
      try {
        // Detect init system
        const osInfo = await sessionManager.getOSInfo(sessionId);
        const initSystem = osInfo.init;
    
        if (initSystem === 'launchd') {
          throw createSudoError(
            'launchd services are not managed by this tool',
            'Use launchctl directly on macOS hosts'
          );
        }
    
        if (initSystem === 'windows-service') {
          throw createSudoError(
            'Windows services are not managed by this tool',
            'Use sc.exe or PowerShell to manage Windows services'
          );
        }
    
        if (initSystem === 'unknown') {
          throw createSudoError(
            'No supported init system found',
            'Supported init systems: systemd, service'
          );
        }
    
        logger.debug('Detected init system', { sessionId, initSystem });
    
        let command: string;
    
        if (initSystem === 'systemd') {
          switch (state) {
            case 'started':
              command = `systemctl start ${serviceName}`;
              break;
            case 'stopped':
              command = `systemctl stop ${serviceName}`;
              break;
            case 'restarted':
              command = `systemctl restart ${serviceName}`;
              break;
            case 'enabled':
              command = `systemctl enable ${serviceName}`;
              break;
            case 'disabled':
              command = `systemctl disable ${serviceName}`;
              break;
          }
        } else {
          // Traditional service command
          switch (state) {
            case 'started':
              command = `service ${serviceName} start`;
              break;
            case 'stopped':
              command = `service ${serviceName} stop`;
              break;
            case 'restarted':
              command = `service ${serviceName} restart`;
              break;
            case 'enabled':
              command = `chkconfig ${serviceName} on || update-rc.d ${serviceName} enable`;
              break;
            case 'disabled':
              command = `chkconfig ${serviceName} off || update-rc.d ${serviceName} disable`;
              break;
          }
        }
    
        logger.debug('Executing service command', { sessionId, serviceName, command });
    
        const result = await execSudo(sessionId, command, sudoPassword);
    
        const serviceResult: ServiceResult = {
          ok: result.code === 0
        };
    
        if (result.code === 0) {
          logger.info('Service state changed successfully', { sessionId, serviceName, state });
        } else {
          logger.error('Service state change failed', {
            sessionId,
            serviceName,
            state,
            code: result.code,
            stderr: result.stderr
          });
        }
    
        return serviceResult;
    
      } catch (error) {
        logger.error('Failed to ensure service state', { sessionId, serviceName, state, error });
        throw error;
      }
    }
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. 'Ensures' implies idempotent state management, but the description doesn't disclose whether this requires specific permissions, what happens if the service is already in the desired state, whether it performs restart/enable/disable actions, or any error handling. For a mutation tool with zero annotation coverage, this leaves significant behavioral gaps.

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 a single, efficient sentence that states the core purpose without unnecessary words. It's appropriately sized for a tool with three parameters and no complex behavior described. However, it could be more front-loaded with additional context about the tool's domain or scope.

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?

For a service state management tool with no annotations and no output schema, the description is incomplete. It doesn't explain what 'ensuring' means operationally, what system it applies to (implied Linux/systemd via SSH), what happens on success/failure, or return values. Given the mutation nature and lack of structured behavioral data, the description should provide more context.

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 three parameters (sessionId, name, state with enum). The description doesn't add any parameter meaning beyond what's in the schema - it doesn't explain service naming conventions, what 'ensuring' entails for each state, or SSH session requirements. Baseline 3 is appropriate when schema does the heavy lifting.

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

Purpose3/5

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

The description states the tool's purpose as ensuring a service is in a desired state, which is clear but vague. It specifies the verb ('ensures') and resource ('service'), but doesn't distinguish from siblings like ensure_package or ensure_lines_in_file beyond the resource type. The purpose is understandable but lacks specificity about what kind of service (e.g., system service) or how it operates.

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 like needing an SSH session (implied by sessionId parameter) or differentiate from other service management tools that might exist. There's no explicit when/when-not usage context or named alternatives provided.

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/oaslananka/mcp-ssh-tool'

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