Skip to main content
Glama

translate_text

Translate text between languages using specified source and target language codes to convert content accurately.

Instructions

Translate text from one language to another.

⚠️ COST WARNING: This tool makes an API call to Whissle which may incur costs. Only use when explicitly requested by the user.

Args:
    text (str): The text to translate
    source_language (str): Source language code (e.g., "en" for English)
    target_language (str): Target language code (e.g., "es" for Spanish)

Returns:
    TextContent with the translated text.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
textYes
source_languageYes
target_languageYes

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
textYes
typeYes
_metaNo
annotationsNo

Implementation Reference

  • The handler function implementing the translate_text tool. It performs text translation using the Whissle API's machine_translation method, includes logging, error handling with retries using handle_api_error, and returns formatted TextContent.
    def translate_text(
        text: str,
        source_language: str,
        target_language: str,
    ) -> TextContent:
        try:
            if not text:
                logger.error("Empty text provided for translation")
                return make_error("Text is required")
            
            # Log the request details
            logger.info(f"Translating text from {source_language} to {target_language}")
            logger.info(f"Text length: {len(text)} characters")
            
            retry_count = 0
            max_retries = 2  # Increased from 1 to 2
            
            while retry_count <= max_retries:
                try:
                    logger.info(f"Attempting translation (Attempt {retry_count+1}/{max_retries+1})")
                    response = client.machine_translation(
                        text=text,
                        source_language=source_language,
                        target_language=target_language,
                    )
                    
                    if response and response.translated_text:
                        logger.info("Translation successful")
                        return TextContent(
                            type="text",
                            text=f"Translation:\n{response.translated_text}",
                        )
                    else:
                        logger.error("No translation was returned from the API")
                        return make_error("No translation was returned from the API")
                except Exception as api_error:
                    error_msg = str(api_error)
                    logger.error(f"Translation error: {error_msg}")
                    
                    # Handle API errors with retries
                    error_result = handle_api_error(error_msg, "translation", retry_count, max_retries)
                    if error_result is not None:  # If we should not retry
                        return error_result  # Return the error message
                    
                    retry_count += 1
            
            # If we get here, all retries failed
            logger.error(f"All translation attempts failed after {max_retries+1} attempts")
            return make_error(f"Failed to translate text after {max_retries+1} attempts")
        except Exception as e:
            logger.error(f"Unexpected error during translation: {str(e)}")
            return make_error(f"Failed to translate text: {str(e)}")
  • Registration of the 'translate_text' tool using the @mcp.tool decorator. Includes the tool description, input schema (args: text, source_language, target_language), cost warning, and return type.
    @mcp.tool(
        description="""Translate text from one language to another.
    
        ⚠️ COST WARNING: This tool makes an API call to Whissle which may incur costs. Only use when explicitly requested by the user.
    
        Args:
            text (str): The text to translate
            source_language (str): Source language code (e.g., "en" for English)
            target_language (str): Target language code (e.g., "es" for Spanish)
    
        Returns:
            TextContent with the translated text.
        """
    )

Tool Definition Quality

Score is being calculated. Check back soon.

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/WhissleAI/whissle-mcp'

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