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

+82
View File
@@ -0,0 +1,82 @@
import { z } from 'zod';
export const params = z.object({
query: z
.string()
.max(400)
.refine((str) => str.split(/\s+/).length <= 50, 'Query cannot exceed 50 words')
.describe('Search query (max 400 chars, 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(),
ui_lang: z
.string()
.default('en-US')
.describe(
'User interface language preferred in response. Usually of the format <language_code>-<country_code>. For more, see RFC 9110.'
)
.optional(),
count: z
.number()
.int()
.min(1)
.max(50)
.default(20)
.describe('Number of results (1-50, default 20)')
.optional(),
offset: z
.number()
.int()
.min(0)
.max(9)
.default(0)
.describe('Pagination offset (max 9, default 0)')
.optional(),
spellcheck: z
.boolean()
.default(true)
.describe('Whether to spellcheck provided query.')
.optional(),
safesearch: z
.union([z.literal('off'), z.literal('moderate'), z.literal('strict')])
.default('moderate')
.describe(
"Filters search results for adult content. The following values are supported: 'off' - No filtering. 'moderate' - Filter out explicit content. 'strict' - Filter out explicit and suggestive content. The default value is 'moderate'."
)
.optional(),
freshness: z
.union([z.literal('pd'), z.literal('pw'), z.literal('pm'), z.literal('py'), z.string()])
.default('pd')
.describe(
"Filters search results by when they were discovered. The following values are supported: 'pd' - Discovered within the last 24 hours. 'pw' - Discovered within the last 7 Days. 'pm' - Discovered within the last 31 Days. 'py' - Discovered within the last 365 Days. 'YYYY-MM-DDtoYYYY-MM-DD' - Timeframe is also supported by specifying the date range e.g. 2022-04-01to2022-07-30."
)
.optional(),
extra_snippets: z
.boolean()
.default(false)
.describe(
'A snippet is an excerpt from a page you get as a result of the query, and extra_snippets allow you to get up to 5 additional, alternative excerpts. Only available under Free AI, Base AI, Pro AI, Base Data, Pro Data and Custom plans.'
)
.optional(),
goggles: z
.array(z.string())
.describe(
"Goggles act as a custom re-ranking on top of Brave's search index. The parameter supports both a url where the Goggle is hosted or the definition of the Goggle. For more details, refer to the Goggles repository (i.e., https://github.com/brave/goggles-quickstart)."
)
.optional(),
});
export type QueryParams = z.infer<typeof params>;
export default params;
+60
View File
@@ -0,0 +1,60 @@
import type { ToolAnnotations } from '@modelcontextprotocol/sdk/types.js';
import params, { type QueryParams } from './QueryParams.js';
import API from '../../BraveAPI/index.js';
import { stringify } from '../../utils.js';
export const name = 'brave_news_search';
export const annotations: ToolAnnotations = {
title: 'Brave News Search',
openWorldHint: true,
};
export const description = `
This tool searches for news articles using Brave's News Search API based on the user's query. Use it when you need current news information, breaking news updates, or articles about specific topics, events, or entities.
When to use:
- Finding recent news articles on specific topics
- Getting breaking news updates
- Researching current events or trending stories
- Gathering news sources and headlines for analysis
Returns a JSON list of news-related results with title, url, and description. Some results may contain snippets of text from the article.
When relaying results in markdown-supporting environments, always cite sources with hyperlinks.
Examples:
- "According to [Reuters](https://www.reuters.com/technology/china-bans/), China bans uncertified and recalled power banks on planes".
- "The [New York Times](https://www.nytimes.com/2025/06/27/us/technology/ev-sales.html) reports that Tesla's EV sales have increased by 20%".
- "According to [BBC News](https://www.bbc.com/news/world-europe-65910000), the UK government has announced a new policy to support renewable energy".
`;
export const execute = async (params: QueryParams) => {
const response = await API.issueRequest<'news'>('news', params);
return {
content: response.results.map((newsResult) => {
return {
type: 'text' as const,
text: stringify({
url: newsResult.url,
title: newsResult.title,
age: newsResult.age,
page_age: newsResult.page_age,
breaking: newsResult.breaking ?? false,
description: newsResult.description,
extra_snippets: newsResult.extra_snippets,
thumbnail: newsResult.thumbnail?.src,
}),
};
}),
};
};
export default {
name,
description,
annotations,
inputSchema: params.shape,
execute,
};
+66
View File
@@ -0,0 +1,66 @@
export interface NewsSearchApiResponse {
/** The type of search API result. The value is always news. */
type: 'news';
/** News search query string. */
query: Query;
/** The list of news results for the given query. */
results: NewsResult[];
}
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 if any. */
altered?: string;
/** The cleaned normalized query by the spellchecker. This is the query that is used to search if any. */
cleaned?: 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?: boolean;
}
export interface NewsResult {
/** The type of news search API result. The value is always news_result. */
type: 'news_result';
/** The source URL of the news article. */
url: string;
/** The title of the news article. */
title: string;
/** The description for the news article. */
description?: string;
/** A human readable representation of the page age. */
age?: string;
/** The page age found from the source web page. */
page_age?: string;
/** The ISO date time when the page was last fetched. The format is YYYY-MM-DDTHH:MM:SSZ. */
page_fetched?: string;
/** Whether the result includes breaking news. */
breaking?: boolean;
/** The thumbnail for the news article. */
thumbnail?: Thumbnail;
/** Aggregated information on the URL associated with the news search result. */
meta_url?: MetaUrl;
/** A list of extra alternate snippets for the news search result. */
extra_snippets?: string[];
}
interface Thumbnail {
/** The served URL of the thumbnail associated with the news article. */
src: string;
/** The original URL of the thumbnail associated with the news article. */
original?: 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;
}