Skip to content

feat: 多模态工具支持 (图像/音频/视频) #9

@xiaotonng

Description

@xiaotonng

功能描述

增强 lc2mcp 对多模态输入输出的支持,使工具能够处理图像、音频、视频等非文本数据。

使用场景

from langchain.tools import tool
from lc2mcp.multimodal import Image, Audio, Video

@tool
def analyze_image(image: Image, question: str) -> str:
    """分析图像内容并回答问题。
    
    Args:
        image: 待分析的图像
        question: 关于图像的问题
    """
    # 使用视觉模型分析
    return vision_model.analyze(image.data, question)

@tool
def generate_image(prompt: str, style: str = "realistic") -> Image:
    """根据描述生成图像。"""
    image_data = image_generator.generate(prompt, style)
    return Image(data=image_data, format="png")

@tool
def transcribe_audio(audio: Audio) -> str:
    """将音频转换为文字。"""
    return whisper.transcribe(audio.data)

多模态类型

Image

class Image:
    data: bytes  # 图像二进制数据
    format: str  # png, jpg, webp, gif
    width: int
    height: int
    url: str  # 可选,远程 URL
    base64: str  # 可选,base64 编码

Audio

class Audio:
    data: bytes
    format: str  # mp3, wav, ogg, flac
    duration: float  # 秒
    sample_rate: int

Video

class Video:
    data: bytes
    format: str  # mp4, webm, mov
    duration: float
    width: int
    height: int
    fps: float

MCP 协议扩展

{
  "name": "analyze_image",
  "parameters": {
    "type": "object",
    "properties": {
      "image": {
        "type": "string",
        "format": "binary",
        "contentMediaType": "image/*",
        "x-lc2mcp-multimodal": {
          "type": "image",
          "maxSize": "10MB",
          "formats": ["png", "jpg", "webp"]
        }
      },
      "question": {
        "type": "string"
      }
    }
  }
}

数据传输

1. Base64 内联

适用于小文件 (<1MB)

{
  "image": "data:image/png;base64,iVBORw0KGgo..."
}

2. URL 引用

适用于大文件或已有资源

{
  "image": "https://example.com/images/photo.jpg"
}

3. 分块上传

适用于超大文件

{
  "image": {
    "upload_id": "upload_abc123",
    "chunk_index": 0,
    "total_chunks": 5
  }
}

平台兼容性

平台 图像输入 图像输出 音频 视频
ChatGPT
Claude
Cursor

任务清单

  • 设计多模态类型系统
  • 实现 Image/Audio/Video 类
  • 扩展 MCP schema 生成
  • 实现 Base64 编解码
  • 实现 URL 资源获取
  • 添加文件大小限制
  • 实现分块上传
  • 测试各平台兼容性
  • 编写文档和示例

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions