@brave/brave-search-mcp-server
Version:
MCP server for Brave Search. Uses the Brave Search API to return results from the web, including ranked links, images, and videos, as well as AI summaries of pages, rich results, and more.
49 lines (48 loc) • 1.89 kB
JavaScript
import params from './params.js';
import API from '../../BraveAPI/index.js';
import { stringify } from '../../utils.js';
export const name = 'brave_image_search';
export const annotations = {
title: 'Brave Image Search',
openWorldHint: true,
};
export const description = `
Performs an image search using the Brave Search API. Helpful for when you need pictures of people, places, or things, ideas for graphic design, inspiration for art, or anything else where images are useful. When relaying the results in a markdown-supporting environment, it is helpful to include some/all of the images in the results. Example: .
`;
export const execute = async (params) => {
const content = [];
const response = await API.issueRequest('images', params);
for (const { url: page_url, title, thumbnail, properties } of response.results) {
// Skip results without an image
if (!thumbnail?.src)
continue;
// Prefer property URL as it is the shortest-possible URL
const image_url = properties?.url ?? thumbnail.src;
const fetched_image = await fetchImage(image_url);
if (fetched_image) {
const { mimeType, data } = fetched_image;
content.push({ type: 'text', text: stringify({ title, page_url, image_url }) }, { type: 'image', mimeType, data });
}
}
return { content, isError: false };
};
async function fetchImage(url) {
try {
const response = await fetch(url);
const buffer = await response.arrayBuffer();
return {
data: Buffer.from(buffer).toString('base64'),
mimeType: response.headers.get('content-type') ?? 'image/jpeg',
};
}
catch (error) {
return null;
}
}
export default {
name,
description,
annotations,
inputSchema: params.shape,
execute,
};