mcp.ts•3.9 kB
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import * as imageViewer from "./imageViewer.js";
const server = new McpServer({
name: "Image Viewer",
version: "1.0.0",
});
server.tool(
"display-image",
"Display an image from the filesystem. Returns the image as base64 data that Claude can render.",
{
imagePath: z.string().describe("Path to the image file (supports ~ for home directory)"),
},
async ({ imagePath }) => {
try {
const imageInfo = await imageViewer.loadImage(imagePath);
return {
content: [
{
type: "image",
data: imageInfo.base64Data,
mimeType: imageInfo.mimeType,
},
{
type: "text",
text: `Image: ${imageInfo.name}\nPath: ${imageInfo.path}\nSize: ${Math.round(imageInfo.size / 1024)} KB\nType: ${imageInfo.mimeType}`,
},
],
};
} catch (error) {
return {
content: [
{
type: "text",
text: `Error loading image: ${error instanceof Error ? error.message : 'Unknown error'}`,
},
],
};
}
}
);
server.tool(
"list-images",
"Find and list image files in a directory",
{
searchPath: z.string().describe("Directory path to search for images (supports ~ for home directory)"),
recursive: z.boolean().optional().default(false).describe("Whether to search subdirectories recursively"),
},
async ({ searchPath, recursive }) => {
try {
const imagePaths = await imageViewer.findImages(searchPath, recursive);
return {
content: [
{
type: "text",
text: JSON.stringify({
searchPath,
recursive,
count: imagePaths.length,
images: imagePaths
}, null, 2),
},
],
};
} catch (error) {
return {
content: [
{
type: "text",
text: `Error listing images: ${error instanceof Error ? error.message : 'Unknown error'}`,
},
],
};
}
}
);
server.tool(
"image-info",
"Get detailed information about an image file without loading the full image data",
{
imagePath: z.string().describe("Path to the image file (supports ~ for home directory)"),
},
async ({ imagePath }) => {
try {
const imageInfo = await imageViewer.loadImage(imagePath);
return {
content: [
{
type: "text",
text: JSON.stringify({
name: imageInfo.name,
path: imageInfo.path,
size: imageInfo.size,
sizeKB: Math.round(imageInfo.size / 1024),
mimeType: imageInfo.mimeType
}, null, 2),
},
],
};
} catch (error) {
return {
content: [
{
type: "text",
text: `Error getting image info: ${error instanceof Error ? error.message : 'Unknown error'}`,
},
],
};
}
}
);
export { server };