查看图像MCP服务器
基于Stdio的模型上下文协议(MCP)服务器,为AI代理提供图像查看功能。通过文件路径或base64数据URI接受图像,并以标准化的包含元数据的多部分格式返回它们。
特性
- 文件路径输入:直接从文件系统加载图像
- Base64输入:接受图像作为base64编码的数据URI
- 格式转换:自动将JPEG、WebP、GIF转换为PNG
- 元数据抽取:返回图像尺寸和原始格式
- 多部分输出:返回与MCP多部分格式匹配的结构化内容
支持格式
| 格式 | 文件扩展名 | MIME类型 |
|---|---|---|
| PNG | .png |
image/png |
| JPEG | .jpg, .jpeg |
image/jpeg |
| WebP | .webp |
image/webp |
| GIF | .gif |
image/gif |
跑步
从存储库根目录 npm install:
- macOS/Linux:
run.sh - 视窗:
run.cmd
或者使用存储库根目录下的npm脚本:```bash
npm run build:view-image # Build the package
npm run test:view-image # Run tests
## 工具
### `view_image`
从文件路径或base64数据中查看和分析图像。
**论据:**
- `filePath` (可选):磁盘上映像文件的绝对路径
- `base64Data` (可选):Base64编码的图像数据URI(例如。, `data:image/png;base64,...`)
**注:** 正是其中之一 `filePath` 或 `base64Data` 必须提供。
**示例(文件路径):**```json
{ "filePath": "/path/to/image.jpg" }
示例(base64):```json
{ "base64Data": "data:image/png;base64,iVBORw0KGgo..." }
## 响应格式
该工具返回包含元数据和图像数据的多部分内容:```json
{
"content": [
{
"type": "text",
"mimeType": "application/json",
"text": "{\"width\":1920,\"height\":1080,\"originalFormat\":\"jpeg\",\"filePath\":\"/path/to/image.jpg\"}"
},
{
"type": "image",
"mimeType": "image/png",
"data": "iVBORw0KGgo..."
}
]
}
元数据字段
| 字段 | 类型 | 描述 |
|---|---|---|
width |
number | 图像宽度(像素) |
height |
number | 图像高度(像素) |
originalFormat |
string | 转换前的原始格式(png, jpeg, webp, gif) |
filePath |
string(可选) | 原始文件路径(仅在从路径加载时存在) |
错误处理
错误以标准MCP错误格式返回:```json
{
"isError": true,
"content": [
{
"type": "text",
"text": "Error: File not found: /path/to/missing.jpg"
}
]
}
## VS代码(mcp.json)配置
从VS Code运行和测试MCP服务器(使用读取以下内容的MCP扩展 `mcp.json`),在以下位置添加工作区配置文件 `.vscode/mcp.json`.该文件告诉扩展如何启动服务器,并公开 `view_image` 工具模式,以便编辑器工具可以验证请求。
示例 `.vscode/mcp.json` (根据您的环境调整路径/命令):
Windows(使用该项目 `run.cmd`):```json
{
"servers": {
"view-image": {
"type": "stdio",
"command": "C:\\FULL\\PATH\\TO\\REPO\\run.cmd"
}
}
}
macOS/Linux(使用 run.sh):```json
{
"servers": {
"view-image": {
"type": "stdio",
"command": "/full/path/to/repo/run.sh"
}
}
}
## 构建```bash
npm install
npm run build
npm run start
测试```bash
npm run test # All tests
npm run test:unit # Unit tests only
npm run test:integration # Integration tests only
```
