Skip to main content

claude-vision-mcp

基于 Claude API 的视觉识别 MCP server:为不支持视觉识别的主模型多一双眼睛。

PyPI

这是什么

一个 stdio MCP server,暴露 describe_image 工具。主代理(如 Claude Code)调用它传入本地图片路径,工具内部用 http 直调视觉网关的 /v1/messages,把图片放在 user message 顶层 image content block(绕开网关只解析顶层 image、不解析 tool_result 内嵌 image 的兼容缺陷),视觉模型返回的文字作为 tool_result 返回给主代理。

为什么需要它:Claude Code 的 Read 工具读图时,会把图片塞进 tool_result.content 内嵌的 image block。但部分网关(如本环境的 xopkimik26)只解析 user message 顶层 image、不解析 tool_result 内嵌 image——结果主模型「看不到图」。本工具把识图挪到一个独立的 MCP server 里,对主代理消息流可见的永远是纯文字 tool_result,图片在工具内部的 http 调用中消化,从源头绕开缺陷。

详细背景见 cnpc/claude #48

安装

方式一:uvx(推荐,Claude Code 集成)

无需 clone,直接在 Claude Code 配置里挂载:

// ~/.claude.json 或项目 .claude.json
{
  "mcpServers": {
    "claude-vision-mcp": {
      "command": "uvx",
      "args": ["claude-vision-mcp"],
      "env": {
        "ANTHROPIC_BASE_URL": "${ANTHROPIC_BASE_URL}",
        "ANTHROPIC_AUTH_TOKEN": "${ANTHROPIC_AUTH_TOKEN}",
        "VISION_MODEL": "${VISION_MODEL}"
      },
      "timeout": 120000
    }
  }
}

uvx 会自动从 PyPI 拉起最新版本,隔离虚拟环境,不污染系统 Python。

方式二:本地开发

git clone https://cnb.cool/cnpc/mcp/claude-vision-mcp.git
cd claude-vision-mcp
uv sync --extra dev           # 装依赖 + dev 依赖
uv run pytest                 # 跑测试
uv run python -m claude_vision_mcp.server   # 直接启动 stdio server

环境变量

网关配置(必填)

变量 必填 说明
ANTHROPIC_BASE_URL 视觉网关基地址(如 https://api.cnb.cool/...),不带尾部斜杠
ANTHROPIC_AUTH_TOKEN 网关认证令牌,同时用作 x-api-keyAuthorization: Bearer
VISION_MODEL 支持视觉的多模态模型 ID(如 xopkimik26),由调用方注入不写死

超时/重试配置(选填,未设时用默认值)

网关调用对瞬态错误(超时、连接失败、429 限流、5xx)做有限重试,指数退避。以下变量可覆盖默认值,在 .claude.jsonenv 字段透传:

变量 默认值 说明
VISION_CONNECT_TIMEOUT 10 TCP 连接建立超时(秒)
VISION_READ_TIMEOUT 60 等待响应超时(秒,视觉模型较慢需留足)
VISION_WRITE_TIMEOUT 10 发送请求体超时(秒)
VISION_POOL_TIMEOUT 10 从连接池获取连接超时(秒)
VISION_MAX_RETRIES 2 最大重试次数(不含首次请求,0 = 不重试)
VISION_RETRY_BASE_DELAY 1 退避基础延迟(秒,第 n 次重试前等待 base * 2^n)

所有变量均从环境读取,非法值(非数字、负数)回退默认值不抛错。

工具

describe_image

识别本地图片并返回文字描述。

参数:

参数 类型 必填 说明
image_path string 本地图片文件路径(绝对或相对)
instruction string 视觉理解指令,覆盖默认通用描述提示词

返回: 纯文字描述(tool_result 只含 text block,绝不返回 image block)。

失败处理: 图片读取失败、网关配置缺失、网关调用失败均以文字诊断返回(不抛错),主代理据此向用户反馈或重试。

架构

src/claude_vision_mcp/
├── pure.py     # 纯函数:请求体构造/响应提取/错误诊断/脱敏/media_type 推断(单测覆盖)
├── gateway.py  # http 调用:httpx 直调 /v1/messages,图片放顶层 image block(不纳入单测)
├── server.py   # FastMCP stdio 入口:注册 describe_image 工具
└── __init__.py

纪律:

  • 纯函数优先:网关请求体构造、响应文字提取为纯函数,单测覆盖;http 调用本身不纳入单测。
  • 禁止硬编码端点/模型/密钥:全走环境变量。
  • 日志脱敏:server 内部 stderr 日志经 redact_sensitive 脱敏,无密钥/图片 base64 明文泄露。
  • MCP stdio 协议:日志走 stderr,stdout 仅供 JSON-RPC。

后续扩展

当前仅 describe_image。后续可在 server.py 增量注册 describe_video / describe_audio 等工具,共用 gateway.py 的网关调用层。包名 claude-vision-mcp 已为多模态预留命名空间。

开发

uv sync --extra dev
uv run pytest            # 测试(纯函数 + gateway 重试路径,共 68 用例)
uv run ruff check .      # lint
uv run ruff format .     # 格式化
uv run mypy src tests    # 类型检查

发布

v* tag 触发 CI 自动发布到公网 PyPI。打 tag 前需更新版本号:

  1. pyproject.tomlversion = "0.x.y"(唯一需改的地方,__init__.py/server.py 运行时从包元数据自动读取)
  2. 提交后打 tag:git tag v0.x.y && git push origin v0.x.y

未更新版本号会导致 uv publish 报 409(版本已存在)。

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

claude_vision_mcp-0.1.2.tar.gz (21.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

claude_vision_mcp-0.1.2-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file claude_vision_mcp-0.1.2.tar.gz.

File metadata

  • Download URL: claude_vision_mcp-0.1.2.tar.gz
  • Upload date:
  • Size: 21.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for claude_vision_mcp-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2d4936bd9ad6b894244c04f7fc05818f41c10ba10abe7135d445b7e49529372c
MD5 bfb788cb51b2cb005f21d8febb9f0361
BLAKE2b-256 29c300e16ee20c164cf2e3497e20210fd81c2e088dc2956eab360181d31bfa11

See more details on using hashes here.

File details

Details for the file claude_vision_mcp-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: claude_vision_mcp-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for claude_vision_mcp-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 043406d0dfcb302931ef1d46e16a64ac19a92657f89d55190a196c39bae3dc53
MD5 f7ce5f4498e663924941ecee34d493a5
BLAKE2b-256 3415a8b8d0c0d5cbf7c07f30b5215dcda82ded64bb58cd16244797e6f66cc058

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.3

2 files

This release

0.1.2 This release

2 files

0.1.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page