Refactor configuration and API; added endpoints for image, video, and news.

This commit is contained in:
Sampson committed 2025-07-02 17:04:22 -05:00
1 parent 7789f05087
commit 8f942b9eee
29 files changed
+2867 -490

No files matched your search

+52
View File
@@ -0,0 +1,52 @@
import { z } from 'zod';
export const params = z.object({
query: z
.string()
.min(1)
.max(400)
.refine((str) => str.split(/\s+/).length <= 50, 'Query cannot exceed 50 words')
.describe(
"The user's search query. Query cannot be empty. Limited to 400 characters and 50 words."
),
country: z
.string()
.default('US')
.describe(
'Search query country, where the results come from. The country string is limited to 2 character country codes of supported countries.'
)
.optional(),
search_lang: z
.string()
.default('en')
.describe(
'Search language preference. The 2 or more character language code for which the search results are provided.'
)
.optional(),
count: z
.number()
.int()
.min(1)
.max(200)
.default(50)
.describe(
'Number of results (1-200, default 50). Combine this parameter with `offset` to paginate search results.'
)
.optional(),
safesearch: z
.union([z.literal('off'), z.literal('strict')])
.default('strict')
.describe(
"Filters search results for adult content. The following values are supported: 'off' - No filtering. 'strict' - Drops all adult content from search results."
)
.optional(),
spellcheck: z
.boolean()
.default(true)
.describe('Whether to spellcheck provided query.')
.optional(),
});
export type QueryParams = z.infer<typeof params>;
export default params;
+67
View File
@@ -0,0 +1,67 @@
import type {
ImageContent,
TextContent,
ToolAnnotations,
} from '@modelcontextprotocol/sdk/types.js';
import params, { type QueryParams } from './QueryParams.js';
import API from '../../BraveAPI/index.js';
import { log, stringify } from '../../utils.js';
export const name = 'brave_image_search';
export const annotations: ToolAnnotations = {
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: ![Image Description](image_url).
`;
export const execute = async (params: QueryParams) => {
const content: (TextContent | ImageContent)[] = [];
const response = await API.issueRequest<'images'>('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: string): Promise<{ mimeType: string; data: string } | null> {
await log('info', `Fetching image data from ${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) {
await log('error', `Error fetching image data from ${url}: ${error}`);
return null;
}
}
export default {
name,
description,
annotations,
inputSchema: params.shape,
execute,
};
+72
View File
@@ -0,0 +1,72 @@
export interface ImageSearchApiResponse {
/** The type of search API result. The value is always images. */
type: 'images';
/** Image search query string. */
query: Query;
/** The list of image results for the given query. */
results: ImageResult[];
/** Additional information about the image search results. */
extra: Extra;
}
interface Query {
/** The original query that was requested. */
original: string;
/** The altered query by the spellchecker. This is the query that is used to search. */
altered?: string;
/** Whether the spellchecker is enabled or disabled. */
spellcheck_off?: boolean;
/** The value is true if the lack of results is due to a strict safesearch setting. Adult content relevant to the query was found, but was blocked by safesearch. */
show_strict_warning?: string;
}
export interface ImageResult {
/** The type of image search API result. The value is always image_result. */
type: 'image_result';
/** The title of the image. */
title?: string;
/** The original page URL where the image was found. */
url?: string;
/** The source domain where the image was found. */
source?: string;
/** The ISO date time when the page was last fetched. The format is YYYY-MM-DDTHH:MM:SSZ. */
page_fetched?: string;
/** The thumbnail for the image. */
thumbnail?: Thumbnail;
/** Metadata for the image. */
properties?: Properties;
/** Aggregated information on the URL associated with the image search result. */
meta_url?: MetaUrl;
/** The confidence level for the image result. */
confidence?: 'low' | 'medium' | 'high';
}
interface Thumbnail {
/** The served URL of the image. */
src?: string;
}
interface Properties {
/** The image URL. */
url?: string;
/** The lower resolution placeholder image URL. */
placeholder?: string;
}
interface MetaUrl {
/** The protocol scheme extracted from the URL. */
scheme?: string;
/** The network location part extracted from the URL. */
netloc?: string;
/** The lowercased domain name extracted from the URL. */
hostname?: string;
/** The favicon used for the URL. */
favicon?: string;
/** The hierarchical path of the URL useful as a display string. */
path?: string;
}
interface Extra {
/** Indicates whether the image search results might contain offensive content. */
might_be_offensive: boolean;
}