Skip to main content
Glama

homeassistant_call_service

Execute specific actions on Home Assistant entities by calling services. Define the entity, domain, and service to automate smart home operations efficiently.

Instructions

Call a service of a Home Assistant entity

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
domainYesThe domain of the service
entity_idYesThe entity ID of the Home Assistant entity
serviceYesThe service to call

Implementation Reference

  • Handler function for the 'homeassistant_call_service' tool. It invokes the callHomeAssistantService helper, handles errors, and formats the MCP response.
    async ({ entity_id, domain, service }) => {  
      console.error(`Verificando Home Assistant`);
      
      const result = await callHomeAssistantService(entity_id, domain, service);
      
      if (!result.success) {
        return formatErrorResponse(`Failed to call service: ${result.message}`);
      }
      
      // Formata os dados da resposta
      const response = result.data;
      
      if (!response.results || response.results === 0) {
        return {
          content: [{ 
            type: "text",
             text: response == "" ? "Call service successfully" : `Name: ${response[0].attributes.friendly_name || "Unknown"}\nEntity: ${entity_id}\nState: ${response[0].state || "Unknown"}\nLast Changed: ${response[0].last_changed || "Unknown"}\n\nAttributes: ${JSON.stringify(response[0].attributes)}`
          }]
        };
      }
      
      const responseMessage = response.message;
      
      return {
        content: [{ 
          type: "text" as const, 
          text: responseMessage
        }]
      };
    }
  • Input schema for the tool using Zod, defining entity_id, domain, and service parameters.
      entity_id: z.string().describe("The entity ID of the Home Assistant entity"),
      domain: z.string().describe("The domain of the service"),
      service: z.string().describe("The service to call")
    },
  • Registration of the 'homeassistant_call_service' tool on the MCP server.
    server.tool(
      "homeassistant_call_service",  
      "Call a service of a Home Assistant entity",
      {
        entity_id: z.string().describe("The entity ID of the Home Assistant entity"),
        domain: z.string().describe("The domain of the service"),
        service: z.string().describe("The service to call")
      },
      async ({ entity_id, domain, service }) => {  
        console.error(`Verificando Home Assistant`);
        
        const result = await callHomeAssistantService(entity_id, domain, service);
        
        if (!result.success) {
          return formatErrorResponse(`Failed to call service: ${result.message}`);
        }
        
        // Formata os dados da resposta
        const response = result.data;
        
        if (!response.results || response.results === 0) {
          return {
            content: [{ 
              type: "text",
               text: response == "" ? "Call service successfully" : `Name: ${response[0].attributes.friendly_name || "Unknown"}\nEntity: ${entity_id}\nState: ${response[0].state || "Unknown"}\nLast Changed: ${response[0].last_changed || "Unknown"}\n\nAttributes: ${JSON.stringify(response[0].attributes)}`
            }]
          };
        }
        
        const responseMessage = response.message;
        
        return {
          content: [{ 
            type: "text" as const, 
            text: responseMessage
          }]
        };
      }
    );
  • Supporting utility function that makes the POST request to Home Assistant API to call the specified service on the entity.
    async function callHomeAssistantService(entity_id: string, domain: string, service: string) {
      try {
        const url = `${process.env.HOME_ASSISTANT_URL}/api/services/${domain}/${service}`;
        console.error(`Making request to: ${url}`);
        
        // O API key deve ser configurado como variável de ambiente
        const bearerToken = process.env.HOME_ASSISTANT_TOKEN;
    
        if (!bearerToken) {
          console.error('HOME_ASSISTANT_TOKEN environment variable is not set')
          process.exit(1)
        }    
        
        const response = await axios.post(url, 
          {
            entity_id: entity_id
          }, 
          {
            headers: {
              'Authorization': `Bearer ${bearerToken}`
            }
          }
        );
        
        return { 
          data: response.data,
          success: true
        };
      } catch (error: any) {
        console.error(`Failed to call service: ${error.message}`);
        
        // Tratamento de erros da API de forma estruturada
        if (error.response) {
          return {
            success: false,
            statusCode: error.response.status,
            message: error.response.data?.message || error.message,
            error: error.response.data
          };
        }
        
        return {
          success: false,
          message: error.message,
          error
        };
      }
    } 
  • src/index.ts:23-23 (registration)
    Top-level call to register Home Assistant tools, including 'homeassistant_call_service', on the MCP server.
    registerHomeAssistantApiTools(server);
Behavior2/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It states the tool calls a service but doesn't explain what that entails—whether it's a read, write, or destructive operation, what permissions are needed, or how errors are handled. This leaves significant gaps for a tool that likely performs mutations.

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's appropriately sized and front-loaded, directly stating the tool's purpose without unnecessary elaboration.

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 calling a service (likely a mutation), the lack of annotations and output schema, and the description's minimal detail, this is incomplete. The agent lacks crucial information about behavior, return values, and error handling, making it inadequate for safe and effective use.

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 schema description coverage is 100%, so the schema already documents all three parameters (domain, entity_id, service). The description adds no additional meaning beyond what the schema provides, such as examples or contextual usage, but meets the baseline for adequate coverage.

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 ('call a service') and the target ('Home Assistant entity'), providing a specific verb+resource combination. However, it doesn't distinguish this tool from its siblings (homeassistant_api and homeassistant_get_state), which would require explicit differentiation to earn 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 like homeassistant_api or homeassistant_get_state. There's no mention of prerequisites, use cases, or exclusions, leaving the agent with minimal context for tool selection.

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

Related 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/guilhermelirio/homeassistant-mpc'

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