adds http streaming support to server

HTTP streaming is the default. To launch with STDIO the user must pass the `--transport stdio` CLI arguments. This has been documented in the README.md file.
This commit is contained in:
Sampson committed 2025-06-18 12:21:16 -05:00
1 parent 486d4f06f5
commit 81f1c0d6db
7 files changed
+274 -172

No files matched your search

+16 -40
View File
@@ -1,47 +1,17 @@
import { type Tool } from "@modelcontextprotocol/sdk/types.js";
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";
export default {
name: "brave_local_search",
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\n" +
"Use this when the query implies 'near me' or mentions specific locations. " +
"Automatically falls back to web search if no local results are found.",
inputSchema: {
type: "object",
properties: {
query: {
type: "string",
description: "Local search query (e.g. 'pizza near Central Park')"
},
count: {
type: "number",
description: "Number of results (1-20, default 5)",
default: 5
},
},
required: ["query"]
}
} as Tool;
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 function validateArgs(args: unknown): args is { query: string; count?: number } {
return (
typeof args === "object" &&
args !== null &&
"query" in args &&
typeof (args as { query: string }).query === "string"
);
}
export async function performSearch(query: string, count: number = 5) {
export async function performSearch({ query, count }: z.infer<typeof paramsSchema>) {
checkRateLimit();
// Initial search to get location IDs
const webUrl = new URL('https://api.search.brave.com/res/v1/web/search');
@@ -66,7 +36,7 @@ export async function performSearch(query: string, count: number = 5) {
const locationIds = webData.locations?.results?.filter((r): r is { id: string; title?: string } => r.id != null).map(r => r.id) || [];
if (locationIds.length === 0) {
return performWebSearch(query, count); // Fallback to web search
return performWebSearch({ query, count, offset: 0 }); // Fallback to web search with default offset
}
// Get POI details and descriptions in parallel
@@ -75,7 +45,13 @@ export async function performSearch(query: string, count: number = 5) {
getDescriptionsData(locationIds)
]);
return formatLocalResults(poisData, descriptionsData);
return {
content: [{
type: "text" as const,
text: formatLocalResults(poisData, descriptionsData)
}],
isError: false,
};
}
async function getPoisData(ids: string[]): Promise<BravePoiResponse> {
+16 -42
View File
@@ -1,47 +1,17 @@
import { type Tool } 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 { z } from "zod";
export default {
name: "brave_web_search",
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. ",
inputSchema: {
type: "object",
properties: {
query: {
type: "string",
description: "Search query (max 400 chars, 50 words)"
},
count: {
type: "number",
description: "Number of results (1-20, default 10)",
default: 10
},
offset: {
type: "number",
description: "Pagination offset (max 9, default 0)",
default: 0
},
},
required: ["query"],
},
} as Tool;
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 function validateArgs(args: unknown): args is { query: string; count?: number } {
return (
typeof args === "object" &&
args !== null &&
"query" in args &&
typeof (args as { query: string }).query === "string"
);
}
export async function performSearch(query: string, count: number = 10, offset: number = 0) {
export async function performSearch({ query, count, offset }: z.infer<typeof paramsSchema>) {
checkRateLimit();
const url = new URL('https://api.search.brave.com/res/v1/web/search');
url.searchParams.set('q', query);
@@ -69,7 +39,11 @@ export async function performSearch(query: string, count: number = 10, offset: n
url: result.url || ''
}));
return results.map(r =>
`Title: ${r.title}\nDescription: ${r.description}\nURL: ${r.url}`
).join('\n\n');
return {
content: results.map(r => ({
type: "text" as const,
text: `Title: ${r.title}\nDescription: ${r.description}\nURL: ${r.url}`
})),
isError: false,
};
}