Skip to main content
Glama

seedream_text_to_image

Generate images from text descriptions using AI. Provide clear text instructions to create single high-quality images matching your specifications.

Instructions

文生图:

通过给模型提供清晰准确的文字指令,即可快速获得符合描述的高质量单张图片。

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
paramsYes

Implementation Reference

  • Tool registration decorator @mcp.tool with name 'seedream_text_to_image' and thin wrapper function delegating to run_text_to_image.
    @mcp.tool(
        name="seedream_text_to_image",
        annotations={"title": "Seedream 文生图", **GENERATION_TOOL_ANNOTATIONS},
    )
    async def seedream_text_to_image(params: TextToImageInput) -> list[TextContent]:
        """
        文生图:
    
        通过给模型提供清晰准确的文字指令,即可快速获得符合描述的高质量单张图片。
        """
        return await run_text_to_image(params)
  • Intermediate runner that converts validated Pydantic input to dict and delegates to core handle_text_to_image.
    async def run_text_to_image(params: TextToImageInput) -> List[TextContent]:
        """
        执行文本到图像生成工具。
    
        Args:
            params: 文本到图像生成的已验证参数对象。
    
        Returns:
            包含生成结果的文本内容列表。
        """
        return await handle_text_to_image(params.model_dump(exclude_none=True))
  • Core handler: validates parameters, calls SeedreamClient.text_to_image API, handles auto-save from URL/base64, formats MCP TextContent response, and error handling.
    async def handle_text_to_image(arguments: Dict[str, Any]) -> List[TextContent]:
        """
        处理文生图请求并返回 MCP 响应。
    
        根据用户提供的文本提示词生成图片,支持尺寸配置、水印添加、提示词优化及自动保存等功能。
        调用 API 生成图片后,可选择性地将结果保存至本地并返回统一格式的响应。
    
        Args:
            arguments: 请求参数字典,包含以下键值:
                - prompt (str): 生成图片的文本提示词
                - size (str, optional): 图片尺寸规格
                - watermark (bool, optional): 是否添加水印
                - response_format (str, optional): 响应格式,"url" 或 "b64_json",默认为 "url"
                - stream (bool, optional): 是否启用流式输出,默认为 False
                - optimize_prompt_options (dict, optional): 提示词优化选项配置
                - auto_save (bool, optional): 是否自动保存生成的图片
                - save_path (str, optional): 自定义图片保存路径
                - custom_name (str, optional): 自定义文件名前缀
    
        Returns:
            包含文本内容的列表,通常只有一个元素,描述生成任务的执行结果、图片 URL 或 Base64 数据及保存状态。
    
        Raises:
            Exception: 当生成过程中发生错误时,捕获异常并返回格式化的错误提示信息。
        """
        try:
            # 加载全局配置
            config = get_global_config()
    
            # 提取并验证请求参数
            prompt = arguments.get("prompt", "")
            size = validate_size_for_model(
                arguments.get("size") or config.default_size, config.model_id
            )
            watermark_value = arguments.get("watermark")
            watermark = (
                validate_watermark(watermark_value)
                if watermark_value is not None
                else config.default_watermark
            )
            response_format = validate_response_format(arguments.get("response_format", "url"))
            stream = bool(arguments.get("stream", False))
            optimize_prompt_options = validate_optimize_prompt_options(
                arguments.get("optimize_prompt_options"), config.model_id
            )
            auto_save = arguments.get("auto_save")
            save_path = arguments.get("save_path")
            custom_name = arguments.get("custom_name")
    
            # 确定自动保存配置
            enable_auto_save = auto_save if auto_save is not None else config.auto_save_enabled
    
            # 记录任务开始信息
            logger.info(
                "文生图开始: prompt='{}...', size={}, stream={}",
                (prompt or "")[:50],
                size,
                stream,
            )
    
            # 执行文生图生成请求
            async with SeedreamClient(config) as client:
                result = await client.text_to_image(
                    prompt=prompt,
                    size=size,
                    watermark=watermark,
                    response_format=response_format,
                    stream=stream,
                    optimize_prompt_options=optimize_prompt_options,
                )
    
            # 处理自动保存逻辑
            auto_save_results: List[Any] = []
            if enable_auto_save and result.get("success"):
                # 根据响应格式选择对应的保存方法
                if response_format == "url":
                    auto_save_results = await auto_save_from_urls(
                        result, prompt, config, save_path, custom_name, "text_to_image"
                    )
                else:
                    auto_save_results = await auto_save_from_base64(
                        result, prompt, config, save_path, custom_name, "text_to_image"
                    )
    
                # 将保存结果合并到响应数据中
                if auto_save_results:
                    result = update_result_with_auto_save(result, auto_save_results)
    
            # 格式化最终响应文本
            response_text = format_generation_response(
                "文生图任务完成",
                result,
                prompt,
                size,
                auto_save_results,
                enable_auto_save,
            )
    
            return [TextContent(type="text", text=response_text)]
    
        except Exception as exc:
            # 记录异常详情
            logger.error("文生图处理失败", exc_info=True)
    
            # 提供用户友好的故障排除指导
            guidance = "请检查提示词长度、尺寸与模型兼容性,确认 API Key 和网络可用后重试。"
            return [
                TextContent(
                    type="text",
                    text=f"文生图生成失败:{format_error_for_user(exc)}\n{guidance}",
                )
            ]
  • Pydantic input schema for text-to-image tool, defining 'prompt' field with validation and inheriting common generation options from BaseGenerationInput.
    class TextToImageInput(BaseGenerationInput):
        """
        文生图:通过提供清晰准确的文字指令,即可快速获得符合描述的高质量单张图片。
        """
    
        prompt: str = Field(
            ...,
            min_length=1,
            description="用于生成图片的提示词,建议不超过300个汉字或600个英文单词。",
        )
    
        @field_validator("prompt")
        @classmethod
        def validate_prompt_field(cls, value: str) -> str:
            """
            校验并规范化提示词。
    
            Args:
                value: 用户输入的提示词
    
            Returns:
                规范化后的提示词
    
            Raises:
                ValueError: 当提示词格式或长度不符合要求时
            """
            try:
                return validate_prompt(value)
            except SeedreamValidationError as exc:
                raise ValueError(exc.message) from exc

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/tengmmvp/Seedream_MCP'

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