Skip to main content
Glama
jina-ai

Jina AI Remote MCP Server

Official
by jina-ai

deduplicate_images

Remove visually similar images from collections using semantic analysis to identify and return the most diverse subset, reducing redundancy while preserving visual variety.

Instructions

Get top-k semantically unique images (URLs or base64-encoded) using Jina CLIP v2 embeddings and submodular optimization. Use this when you have many visually similar images and want the most diverse subset.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
imagesYesArray of image inputs to deduplicate. Each item can be either an HTTP(S) URL or a raw base64-encoded image string (without data URI prefix).
kNoNumber of unique images to return. If not provided, automatically finds optimal k by looking at diminishing return

Implementation Reference

  • Handler function that embeds input images using Jina CLIP v2 API, selects top-k diverse images via submodular greedy optimization, downloads URLs to base64, and returns processed images.
    async ({ images, k }: { images: string[]; k?: number }) => {
    	try {
    		const props = getProps();
    
    		const tokenError = checkBearerToken(props.bearerToken);
    		if (tokenError) {
    			return tokenError;
    		}
    
    		if (images.length === 0) {
    			throw new Error("No images provided for deduplication");
    		}
    
    		if (k !== undefined && (k <= 0 || k > images.length)) {
    			throw new Error(`Invalid k value: ${k}. Must be between 1 and ${images.length}`);
    		}
    
    		// Prepare input for image embeddings API
    		const embeddingInput = images.map((img) => ({ image: img }));
    
    		// Get image embeddings from Jina API using CLIP v2
    		const response = await fetch('https://api.jina.ai/v1/embeddings', {
    			method: 'POST',
    			headers: {
    				'Accept': 'application/json',
    				'Content-Type': 'application/json',
    				'Authorization': `Bearer ${props.bearerToken}`,
    			},
    			body: JSON.stringify({
    				model: 'jina-clip-v2',
    				input: embeddingInput,
    			}),
    		});
    
    		if (!response.ok) {
    			return handleApiError(response, "Getting image embeddings");
    		}
    
    		const data = await response.json() as any;
    
    		if (!data.data || !Array.isArray(data.data)) {
    			throw new Error("Invalid response format from embeddings API");
    		}
    
    		// Extract embeddings
    		const embeddings = data.data.map((item: any) => item.embedding);
    
    		// Use submodular optimization to select diverse images
    		let selectedIndices: number[];
    		let values: number[];
    
    		if (k !== undefined) {
    			selectedIndices = lazyGreedySelection(embeddings, k);
    			values = [];
    		} else {
    			const result = lazyGreedySelectionWithSaturation(embeddings);
    			selectedIndices = result.selected;
    			values = result.values;
    		}
    
    		// Get the selected images
    		const selectedImages = selectedIndices.map((idx) => ({ index: idx, source: images[idx] }));
    
    
    		// Use our consolidated downloadImages utility for consistency
    		const urlsToDownload = selectedImages
    			.filter(({ source }) => /^https?:\/\//i.test(source))
    			.map(({ source }) => source);
    
    		const base64Images = selectedImages
    			.filter(({ source }) => !/^https?:\/\//i.test(source))
    			.map(({ source }) => source);
    
    		const contentItems: Array<{ type: 'image'; data: string; mimeType: string } | { type: 'text'; text: string }> = [];
    
    		// Download URLs using our utility
    		if (urlsToDownload.length > 0) {
    			const downloadResults = await downloadImages(urlsToDownload, 3, 15000);
    
    			for (let i = 0; i < downloadResults.length; i++) {
    				const result = downloadResults[i];
    				const selectedImage = selectedImages.find(({ source }) => source === urlsToDownload[i]);
    
    				if (result.success && result.data) {
    					contentItems.push({
    						type: 'image' as const,
    						data: result.data,
    						mimeType: result.mimeType,
    					});
    				} else {
    					contentItems.push({
    						type: 'text' as const,
    						text: `Failed to download image at index ${selectedImage?.index || i}: ${result.error || 'Unknown error'}`,
    					});
    				}
    			}
    		}
    
    		// Add base64 images directly
    		for (const base64Image of base64Images) {
    			contentItems.push({
    				type: 'image' as const,
    				data: base64Image,
    				mimeType: 'image/jpeg', // Our utility converts to JPEG
    			});
    		}
    
    		if (contentItems.length === 0) {
    			throw new Error("No images to return after deduplication");
    		}
    
    		return { content: contentItems };
    	} catch (error) {
    		return createErrorResponse(`Error: ${error instanceof Error ? error.message : String(error)}`);
    	}
    },
  • Zod input schema for the deduplicate_images tool defining the images array and optional k parameter.
    {
    	images: z.array(z.string()).describe("Array of image inputs to deduplicate. Each item can be either an HTTP(S) URL or a raw base64-encoded image string (without data URI prefix)."),
    	k: z.number().optional().describe("Number of unique images to return. If not provided, automatically finds optimal k by looking at diminishing return"),
    },
  • Registers the deduplicate_images tool on the MCP server using server.tool if enabled.
    if (isToolEnabled("deduplicate_images")) {
    	server.tool(
    		"deduplicate_images",
    		"Get top-k semantically unique images (URLs or base64-encoded) using Jina CLIP v2 embeddings and submodular optimization. Use this when you have many visually similar images and want the most diverse subset.",
    		{
    			images: z.array(z.string()).describe("Array of image inputs to deduplicate. Each item can be either an HTTP(S) URL or a raw base64-encoded image string (without data URI prefix)."),
    			k: z.number().optional().describe("Number of unique images to return. If not provided, automatically finds optimal k by looking at diminishing return"),
    		},
    		async ({ images, k }: { images: string[]; k?: number }) => {
    			try {
    				const props = getProps();
    
    				const tokenError = checkBearerToken(props.bearerToken);
    				if (tokenError) {
    					return tokenError;
    				}
    
    				if (images.length === 0) {
    					throw new Error("No images provided for deduplication");
    				}
    
    				if (k !== undefined && (k <= 0 || k > images.length)) {
    					throw new Error(`Invalid k value: ${k}. Must be between 1 and ${images.length}`);
    				}
    
    				// Prepare input for image embeddings API
    				const embeddingInput = images.map((img) => ({ image: img }));
    
    				// Get image embeddings from Jina API using CLIP v2
    				const response = await fetch('https://api.jina.ai/v1/embeddings', {
    					method: 'POST',
    					headers: {
    						'Accept': 'application/json',
    						'Content-Type': 'application/json',
    						'Authorization': `Bearer ${props.bearerToken}`,
    					},
    					body: JSON.stringify({
    						model: 'jina-clip-v2',
    						input: embeddingInput,
    					}),
    				});
    
    				if (!response.ok) {
    					return handleApiError(response, "Getting image embeddings");
    				}
    
    				const data = await response.json() as any;
    
    				if (!data.data || !Array.isArray(data.data)) {
    					throw new Error("Invalid response format from embeddings API");
    				}
    
    				// Extract embeddings
    				const embeddings = data.data.map((item: any) => item.embedding);
    
    				// Use submodular optimization to select diverse images
    				let selectedIndices: number[];
    				let values: number[];
    
    				if (k !== undefined) {
    					selectedIndices = lazyGreedySelection(embeddings, k);
    					values = [];
    				} else {
    					const result = lazyGreedySelectionWithSaturation(embeddings);
    					selectedIndices = result.selected;
    					values = result.values;
    				}
    
    				// Get the selected images
    				const selectedImages = selectedIndices.map((idx) => ({ index: idx, source: images[idx] }));
    
    
    				// Use our consolidated downloadImages utility for consistency
    				const urlsToDownload = selectedImages
    					.filter(({ source }) => /^https?:\/\//i.test(source))
    					.map(({ source }) => source);
    
    				const base64Images = selectedImages
    					.filter(({ source }) => !/^https?:\/\//i.test(source))
    					.map(({ source }) => source);
    
    				const contentItems: Array<{ type: 'image'; data: string; mimeType: string } | { type: 'text'; text: string }> = [];
    
    				// Download URLs using our utility
    				if (urlsToDownload.length > 0) {
    					const downloadResults = await downloadImages(urlsToDownload, 3, 15000);
    
    					for (let i = 0; i < downloadResults.length; i++) {
    						const result = downloadResults[i];
    						const selectedImage = selectedImages.find(({ source }) => source === urlsToDownload[i]);
    
    						if (result.success && result.data) {
    							contentItems.push({
    								type: 'image' as const,
    								data: result.data,
    								mimeType: result.mimeType,
    							});
    						} else {
    							contentItems.push({
    								type: 'text' as const,
    								text: `Failed to download image at index ${selectedImage?.index || i}: ${result.error || 'Unknown error'}`,
    							});
    						}
    					}
    				}
    
    				// Add base64 images directly
    				for (const base64Image of base64Images) {
    					contentItems.push({
    						type: 'image' as const,
    						data: base64Image,
    						mimeType: 'image/jpeg', // Our utility converts to JPEG
    					});
    				}
    
    				if (contentItems.length === 0) {
    					throw new Error("No images to return after deduplication");
    				}
    
    				return { content: contentItems };
    			} catch (error) {
    				return createErrorResponse(`Error: ${error instanceof Error ? error.message : String(error)}`);
    			}
    		},
    	);
    }
  • src/index.ts:24-24 (registration)
    Lists 'deduplicate_images' in ALL_TOOLS array used for tool filtering and enabling.
    "sort_by_relevance", "deduplicate_strings", "deduplicate_images", "extract_pdf"
  • src/index.ts:100-100 (registration)
    Calls registerJinaTools which includes the deduplicate_images registration.
    registerJinaTools(server, () => currentProps, enabledTools);
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It effectively describes key traits: it's a read-only operation (implied by 'get'), uses semantic analysis via embeddings, and includes optimization logic. However, it lacks details on performance aspects like rate limits or error handling, which would be helpful for a tool with computational complexity.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is front-loaded with the core purpose in the first sentence and usage guidelines in the second. Both sentences earn their place by adding essential information without redundancy, making it efficient and well-structured for quick comprehension.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (involving embeddings and optimization) and no output schema, the description is mostly complete. It covers purpose, usage, and high-level behavior, but could benefit from mentioning output format (e.g., returned structure) or limitations to fully compensate for the lack of output schema.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents both parameters thoroughly. The description adds minimal value beyond the schema by mentioning 'top-k' and 'semantically unique,' but doesn't provide additional syntax or format details. This meets the baseline for high schema coverage.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose with specific verbs ('get top-k semantically unique images') and resources ('images (URLs or base64-encoded)'), and distinguishes it from sibling tools by specifying its unique function of deduplication using Jina CLIP v2 embeddings and submodular optimization, unlike other tools like search_images or deduplicate_strings.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description explicitly states when to use this tool: 'when you have many visually similar images and want the most diverse subset.' This provides clear context and distinguishes it from alternatives like search_images (for finding images) or deduplicate_strings (for text deduplication), making it easy for an agent to select appropriately.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jina-ai/MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server