tos_image_process
Process images stored in TOS buckets by applying operations like resize, format conversion, crop, rotate, and quality adjustment with specified parameters.
Instructions
图片处理(组合操作,支持多种处理参数)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| bucket_name | Yes | 存储桶名称 | |
| object_key | Yes | 图片对象键名 | |
| process | Yes | 图片处理参数。参数格式通常为 'image/操作,参数',如: 'image/resize,h_100' 或 'image/format,jpg'。常用操作包括:resize(缩放),format(格式转换),quality(质量),crop(裁剪),rotate(旋转)等。 | |
| save_bucket | Yes | 保存的存储桶名称 | |
| save_key | Yes | 保存的对象键名 |
Implementation Reference
- src/tos_mcp_server/handlers.py:218-260 (handler)Core handler function that executes the image processing using TOS SDK's get_object method with process, save_bucket, and save_object parameters for persistent processing.async def image_process(args: Dict[str, Any]) -> List[TextContent]: """图片处理(支持持久化)""" bucket_name = args["bucket_name"] object_key = args["object_key"] process = args["process"] save_bucket = args["save_bucket"] save_key = args["save_key"] try: # 使用官方SDK写法,通过save_bucket和save_object参数执行图片处理和持久化 resp = tos_client.get_object( bucket=bucket_name, key=object_key, process=process, save_bucket=base64.b64encode(save_bucket.encode("utf-8")).decode("utf-8"), save_object=base64.b64encode(save_key.encode("utf-8")).decode("utf-8") ) # 读取处理结果以确保处理完成 processed_data = resp.read() # 等待一下确保回写完成 import time time.sleep(1.0) # 生成处理后对象的预签名 URL download_url = tos_client.pre_signed_url(tos.HttpMethodType.Http_Method_Get, save_bucket, save_key, 3600) result = { "presigned_url": download_url.signed_url, "source_bucket": bucket_name, "source_key": object_key, "save_bucket": save_bucket, "save_key": save_key, "process": process, "processed_size": len(processed_data), "expires_in": 3600, "status": "processed" } return [TextContent(type="text", text=json.dumps(result, indent=2, ensure_ascii=False))] except Exception as e: return [TextContent(type="text", text=f"图片处理失败: {str(e)}")]
- src/tos_mcp_server/server.py:244-273 (registration)Tool registration in list_tools() including name, description, and input schema definition.Tool( name="tos_image_process", description="图片处理(组合操作,支持多种处理参数)", inputSchema={ "type": "object", "properties": { "bucket_name": { "type": "string", "description": "存储桶名称" }, "object_key": { "type": "string", "description": "图片对象键名" }, "process": { "type": "string", "description": "图片处理参数。参数格式通常为 'image/操作,参数',如: 'image/resize,h_100' 或 'image/format,jpg'。常用操作包括:resize(缩放),format(格式转换),quality(质量),crop(裁剪),rotate(旋转)等。" }, "save_bucket": { "type": "string", "description": "保存的存储桶名称" }, "save_key": { "type": "string", "description": "保存的对象键名" } }, "required": ["bucket_name", "object_key", "process", "save_bucket", "save_key"] } ),
- src/tos_mcp_server/server.py:355-356 (registration)Dispatch logic in call_tool() that routes tos_image_process calls to the image_process handler.elif name == "tos_image_process": return await image_process(arguments)
- src/tos_mcp_server/server.py:16-20 (registration)Import of the image_process handler function from handlers module.create_bucket, list_buckets, get_bucket_meta, delete_bucket, put_object, get_object, list_objects, delete_object, presigned_url, image_process, image_info, video_snapshot, video_info )