improved structure and implementation
This commit is contained in:
12 files changed
+300
-154
No files matched your search
@@ -0,0 +1,4 @@
|
||||
import WebSearchTool from "./web/index.js";
|
||||
import LocalSearchTool from "./local/index.js";
|
||||
|
||||
export default { WebSearchTool, LocalSearchTool };
|
||||
@@ -0,0 +1,16 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const params = z.object({
|
||||
query: z.string()
|
||||
.describe("Local search query (e.g. 'pizza near Central Park')"),
|
||||
count: z.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.max(20)
|
||||
.default(5)
|
||||
.describe("Number of results (1-20, default 5)")
|
||||
});
|
||||
|
||||
export type QueryParams = z.infer<typeof params>;
|
||||
|
||||
export default params;
|
||||
@@ -1,17 +1,23 @@
|
||||
import { z } from "zod";
|
||||
import { type WebResponse as BraveWeb, type BravePoiResponse, type BraveDescription } from "../types/index.js";
|
||||
import { performSearch as performWebSearch } from "./web.js";
|
||||
import { BRAVE_API_KEY } from "../constants.js";
|
||||
import { checkRateLimit } from "../utils.js";
|
||||
import { type WebResponse as BraveWeb, type BravePoiResponse, type BraveDescription } from "../../types/index.js";
|
||||
import { execute as performWebSearch } from "../web/index.js";
|
||||
import { BRAVE_API_KEY } from "../../constants.js";
|
||||
import { checkRateLimit } from "../../utils.js";
|
||||
import type { ToolAnnotations } from "@modelcontextprotocol/sdk/types.js";
|
||||
import params, { type QueryParams } from "./QueryParams.js";
|
||||
|
||||
export const name = "brave_local_search";
|
||||
export const description = "Searches for local businesses and places using Brave's Local Search API. Best for queries related to physical locations, businesses, restaurants, services, etc. Returns detailed information including:\n- Business names and addresses\n- Ratings and review counts\n- Phone numbers and opening hours\nUse this when the query implies 'near me' or mentions specific locations. Automatically falls back to web search if no local results are found.";
|
||||
export const paramsSchema = z.object({
|
||||
query: z.string().describe("Local search query (e.g. 'pizza near Central Park')"),
|
||||
count: z.number().min(1).max(20).default(5).describe("Number of results (1-20, default 5)"),
|
||||
});
|
||||
|
||||
export async function performSearch({ query, count }: z.infer<typeof paramsSchema>) {
|
||||
export const annotations: ToolAnnotations = {
|
||||
title: "Brave Local Search",
|
||||
openWorldHint: true,
|
||||
};
|
||||
|
||||
export const description = "Searches for local businesses and places using Brave's Local Search API. Best for queries related to physical locations, businesses, restaurants, services, etc. Returns detailed information including:\n- Business names and addresses\n- Ratings and review counts\n- Phone numbers and opening hours\nUse this when the query implies 'near me' or mentions specific locations. Automatically falls back to web search if no local results are found.";
|
||||
|
||||
// TODO (Sampson): Add output schema
|
||||
// export const outputSchema = z.object({});
|
||||
|
||||
export async function execute({ query, count }: QueryParams) {
|
||||
checkRateLimit();
|
||||
// Initial search to get location IDs
|
||||
const webUrl = new URL('https://api.search.brave.com/res/v1/web/search');
|
||||
@@ -113,3 +119,11 @@ function formatLocalResults(poisData: BravePoiResponse, descData: BraveDescripti
|
||||
`;
|
||||
}).join('\n---\n') || 'No local results found';
|
||||
}
|
||||
|
||||
export default {
|
||||
name,
|
||||
description,
|
||||
annotations,
|
||||
inputSchema: params.shape,
|
||||
execute
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
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)"),
|
||||
count: z.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.max(20)
|
||||
.default(10)
|
||||
.describe("Number of results (1-20, default 10)"),
|
||||
offset: z.number()
|
||||
.int()
|
||||
.min(0)
|
||||
.max(9)
|
||||
.default(0)
|
||||
.describe("Pagination offset (max 9, default 0)")
|
||||
});
|
||||
|
||||
export type QueryParams = z.infer<typeof params>;
|
||||
|
||||
export default params;
|
||||
@@ -1,17 +1,22 @@
|
||||
import { type WebResponse } from "../types/index.js";
|
||||
import { BRAVE_API_KEY } from "../constants.js";
|
||||
import { checkRateLimit } from "../utils.js";
|
||||
import { z } from "zod";
|
||||
import type { ToolAnnotations } from "@modelcontextprotocol/sdk/types.js";
|
||||
import { type WebResponse } from "../../types/index.js";
|
||||
import { BRAVE_API_KEY } from "../../constants.js";
|
||||
import { checkRateLimit } from "../../utils.js";
|
||||
import params, { type QueryParams } from "./QueryParams.js";
|
||||
|
||||
export const name = "brave_web_search";
|
||||
export const description = "Performs a web search using the Brave Search API, ideal for general queries, news, articles, and online content. Use this for broad information gathering, recent events, or when you need diverse web sources. Supports pagination, content filtering, and freshness controls. Maximum 20 results per request, with offset for pagination.";
|
||||
export const paramsSchema = z.object({
|
||||
query: z.string().describe("Search query (max 400 chars, 50 words)"),
|
||||
count: z.number().min(1).max(20).default(10).describe("Number of results (1-20, default 10)"),
|
||||
offset: z.number().min(0).max(9).default(0).describe("Pagination offset (max 9, default 0)")
|
||||
});
|
||||
|
||||
export async function performSearch({ query, count, offset }: z.infer<typeof paramsSchema>) {
|
||||
export const annotations: ToolAnnotations = {
|
||||
title: "Brave Web Search",
|
||||
openWorldHint: true,
|
||||
};
|
||||
|
||||
export const description = "Performs a web search using the Brave Search API, ideal for general queries, news, articles, and online content. Use this for broad information gathering, recent events, or when you need diverse web sources. Supports pagination, content filtering, and freshness controls. Maximum 20 results per request, with offset for pagination.";
|
||||
|
||||
// TODO (Sampson): Add output schema
|
||||
// export const outputSchema = z.object({});
|
||||
|
||||
export async function execute({ query, count, offset }: QueryParams) {
|
||||
checkRateLimit();
|
||||
const url = new URL('https://api.search.brave.com/res/v1/web/search');
|
||||
url.searchParams.set('q', query);
|
||||
@@ -47,3 +52,11 @@ export async function performSearch({ query, count, offset }: z.infer<typeof par
|
||||
isError: false,
|
||||
};
|
||||
}
|
||||
|
||||
export default {
|
||||
name,
|
||||
description,
|
||||
annotations,
|
||||
inputSchema: params.shape,
|
||||
execute
|
||||
};
|
||||
Reference in new issue
Block a user