多媒体处理API服务

一个支持文本、图像、音频、视频、文档和二进制数据的REST API服务,用于处理模型上下文协议(MCP)请求。

  • by samirsatam
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 samirsatam/mcp-schema

使用说明

MCP REST API主机

用于模型上下文协议(MCP)主机的REST API服务,支持多种媒体类型,包括文本、图像、音频、视频、文档和二进制数据。使用FastAPI构建并遵循Python惯例。

特性

  • 多媒体支持:处理文本、图像、音频、视频、文档和二进制内容
  • 灵活的架构:容纳各种内容类型的通用JSON模式
  • Base64和URL支持:接受base64编码的字符串或URL形式的内容
  • 请求验证:内容类型和格式的内置验证
  • RESTful API:使用适当的错误处理清理REST端点
  • OpenAPI文档:上的自动生成API文档 /docs

安装

  1. 创建虚拟环境(推荐):
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. 安装依赖项:
pip install -r requirements.txt

运行服务

启动开发服务器:

python main.py

或者直接使用uvicorn:

uvicorn main:app --reload --host 0.0.0.0 --port 8000

API将于 http://localhost:8000

API 文档

服务器运行后,请访问:

  • Swagger 用户界面: http://localhost:8000/docs
  • ReDoc: http://localhost:8000/redoc

API终点

发布 /mcp/process

处理具有多种内容类型的MCP请求。

请求正文:

{
  "message_id": "msg-123",
  "contents": [
    {
      "type": "text",
      "content": "What is the weather today?",
      "mime_type": "text/plain"
    },
    {
      "type": "image",
      "url": "https://example.com/image.png",
      "mime_type": "image/png"
    }
  ],
  "context": {
    "language": "en",
    "timezone": "UTC"
  },
  "user_id": "user-456",
  "session_id": "session-789",
  "parameters": {}
}

答复:

{
  "message_id": "msg-123",
  "response_id": "resp-456",
  "status": "success",
  "contents": [
    {
      "type": "text",
      "content": "Processed text: What is the weather today?",
      "mime_type": "text/plain"
    }
  ],
  "metadata": {
    "content_items_processed": 2,
    "user_id": "user-456",
    "session_id": "session-789"
  }
}

发布 /mcp/validate

验证MCP请求而不进行处理。

请求正文: 同 /mcp/process

答复:

{
  "valid": true,
  "content_items": [
    {
      "index": 0,
      "type": "text",
      "valid": true,
      "errors": [],
      "content_size": 25
    }
  ],
  "total_items": 1
}

获取 /health

健康检查端点。

答复:

{
  "status": "healthy"
}

支持的媒体类型

API支持以下媒体类型:

  • 文本:纯文本内容
  • 图像:图像(PNG、JPEG、GIF等)
  • 音频:音频文件(WAV、MP3等)
  • 视频:视频文件(MP4、AVI等)
  • 文档:文档文件(PDF、DOCX等)
  • 二进制:通用二进制数据

内容格式

文本内容

{
  "type": "text",
  "content": "Your text here",
  "mime_type": "text/plain"
}

图像内容(URL)

{
  "type": "image",
  "url": "https://example.com/image.png",
  "mime_type": "image/png"
}

图像内容(Base64)

{
  "type": "image",
  "content": "iVBORw0KGgoAAAANSUhEUgAA...",
  "mime_type": "image/png"
}

音频内容(URL)

{
  "type": "audio",
  "url": "https://example.com/audio.wav",
  "mime_type": "audio/wav"
}

音频内容(Base64)

{
  "type": "audio",
  "content": "UklGRiQAAABXQVZFZm10...",
  "mime_type": "audio/wav"
}

示例用法

Python示例

import requests

url = "http://localhost:8000/mcp/process"
payload = {
    "message_id": "msg-001",
    "contents": [
        {
            "type": "text",
            "content": "Analyze this image",
            "mime_type": "text/plain"
        },
        {
            "type": "image",
            "url": "https://example.com/image.jpg",
            "mime_type": "image/jpeg"
        }
    ],
    "user_id": "user-123",
    "session_id": "session-456"
}

response = requests.post(url, json=payload)
print(response.json())

cURL示例

curl -X POST "http://localhost:8000/mcp/process" \
  -H "Content-Type: application/json" \
  -d '{
    "message_id": "msg-001",
    "contents": [
      {
        "type": "text",
        "content": "Hello, MCP!",
        "mime_type": "text/plain"
      }
    ],
    "user_id": "user-123"
  }'

项目结构

mcp-schema/
├── src/
│   ├── __init__.py   # Package initialization
│   ├── main.py       # FastAPI application and endpoints
│   ├── models.py     # Pydantic models for request/response
│   └── utils.py      # Utility functions for media processing
├── main.py           # Entry point for running the server
├── example_client.py # Example client code
├── requirements.txt  # Python dependencies
└── README.md         # This file

错误处理

API返回适当的HTTP状态代码:

  • 200 OK:请求成功
  • 400 Bad Request:请求格式无效或验证错误
  • 500 Internal Server Error:服务器端错误

错误响应格式:

{
  "error": "Error message",
  "error_code": "ERROR_CODE",
  "details": {
    "field": "contents",
    "reason": "At least one content item is required"
  }
}

发展

代码的风格

这个项目遵循Python的惯例:

  • PEP 8风格指南
  • 键入提示以提高代码清晰度
  • 用于数据验证的Pydantic模型
  • 全面的文档字符串

扩展API

要与实际的MCP服务器集成:

  1. 修改 process_mcp_request() 功能在 main.py
  2. 实现MCP协议通信
  3. 根据MCP服务器的能力处理不同的媒体类型

许可证

MIT许可证

贡献

欢迎投稿!请随时提交拉取请求。