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.
        """
    )
Behavior5/5

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

With no annotations provided, the description carries full burden and does so effectively. It discloses critical behavioral traits including cost implications ('makes an API call to Whissle which may incur costs') and the return format ('TextContent with the translated text'), which goes beyond basic functionality.

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?

Well-structured with clear sections (purpose, warning, args, returns). Every sentence earns its place - the warning is crucial, parameter explanations are necessary given 0% schema coverage, and return statement is valuable. No wasted words.

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

Completeness5/5

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

Given 3 parameters with 0% schema coverage and no annotations, the description provides complete context. It explains purpose, usage constraints, parameter meanings with examples, and return format. The output schema exists but the description still adds value by specifying the return type.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 0%, so the description must compensate. It adds meaningful context for all three parameters with examples ('e.g., "en" for English'), explaining what each parameter represents beyond just their names. However, it doesn't specify format constraints or valid language codes beyond examples.

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

Purpose5/5

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

The description clearly states the specific action ('translate text from one language to another') and identifies the resource (text). It distinguishes from sibling tools like 'summarize_text' by focusing on translation rather than summarization or speech processing.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Explicitly states when to use ('only use when explicitly requested by the user') and provides a cost warning that helps determine when NOT to use it. This gives clear alternative scenarios where other tools might be more appropriate.

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

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