improvements to place_search support (#291)

Improves place_search schemas for better documentation. Avoids double-validation by deferring to MCP SDK for validation of response against outputSchema.
This commit is contained in:
Sampson authored and GitHub committed 2026-05-03 17:16:16 -05:00
1 parent a48cf65c9d
commit 42e1861f64
3 files changed
+118 -69

No files matched your search

+13 -13
View File
@@ -1,5 +1,4 @@
import type { TextContent, ToolAnnotations } from '@modelcontextprotocol/sdk/types.js';
import { z } from 'zod';
import API from '../../BraveAPI/index.js';
import { type McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import {
@@ -18,17 +17,20 @@ export const annotations: ToolAnnotations = {
};
export const description = `
Searches for points of interest (POIs) in a specified geographic area using Brave's Place Search API. Each result includes rich, structured data such as the place's name, URL, postal address, opening hours, contact info, ratings, photos, categories, and timezone.
Searches Brave's Place Search API. A single call may populate any combination of 'results' (POIs), 'cities', 'addresses', 'streets', and 'location' (the resolved search area), depending on the query's shape.
When to use:
- Finding places near a set of coordinates or a named location (e.g. "coffee shops near me", "bookstores in Paris")
- Browsing general points of interest in an area when no query is supplied
- Building a place-finding experience that needs structured business data (hours, ratings, etc.)
- Augmenting an answer with the location's address, phone number, or rating
- POIs near coordinates or a named area (e.g. "coffee shops in Paris") -> 'results', each with structured business data (postal address, hours, contact, ratings, photos, categories, timezone).
- Browsing general POIs (omit 'query'; supply 'latitude'+'longitude' or 'location').
- Disambiguating a bare city name (e.g. "springfield") -> 'cities'.
- Resolving a specific address (e.g. "350 5th avenue" with NYC coords) -> 'addresses' (often plus 'streets').
- Looking up a street by name (e.g. "michigan avenue" with Chicago coords) -> 'streets'.
Provide a search area via 'latitude' + 'longitude' or a 'location' string (or both). When neither is provided, a 'query' is expected. Use 'count' to keep responses small (max 50, default 20).
For US locations the recommended 'location' format is '<city> <state> <country name>' (e.g. 'san francisco ca united states'); for non-US locations use '<city> <country name>' (e.g. 'tokyo japan').
Inputs:
- Anchor the search via 'latitude'+'longitude' or 'location' (or both). With neither, 'query' is required.
- 'addresses' / 'streets' only surface when the query is address-/street-shaped AND geographically anchored.
- 'location' format: US -- '<city> <state> <country>' (e.g. 'san francisco ca united states'); non-US -- '<city> <country>' (e.g. 'tokyo japan'). Capitalization and commas don't matter.
- 'count' caps results (max 50, default 20). 'radius' (meters) biases toward closer results; it does NOT hard-limit the search area.
`.trim();
export const execute = async (params: QueryParams) => {
@@ -40,13 +42,11 @@ export const execute = async (params: QueryParams) => {
parsedParams,
parsedHeaders
);
const parsed = PlaceSearchApiResponseSchema.safeParse(response);
const payload = parsed.success ? parsed.data : response;
return {
content: [{ type: 'text', text: JSON.stringify(payload) } as TextContent],
content: [{ type: 'text', text: JSON.stringify(response) } as TextContent],
isError: false,
structuredContent: payload,
structuredContent: response,
};
};
+2 -2
View File
@@ -151,14 +151,14 @@ export const RequestParamsSchema = z.object({
)
.transform((str) => (str.length === 0 ? undefined : str))
.describe(
'Query string to search for points of interest in an area. If no query is provided, the endpoint will return general points of interest in the given area.'
'Query string. Shape influences the response: POI-like queries -> `results`; bare/ambiguous city names -> `cities`; address- or street-shaped queries with a geographic anchor -> `addresses` and/or `streets`. If omitted, returns general POIs in the supplied area.'
)
.optional(),
radius: z
.number()
.min(0)
.describe(
'Search radius around the supplied coordinates, in meters. If omitted, the search is performed globally.'
'Bias toward results closer to the supplied coordinates, in meters. NOT a hard cutoff -- the API may still return more distant results. If omitted, the search is performed globally.'
)
.optional(),
count: z
+103 -54
View File
@@ -5,13 +5,24 @@ import { z } from 'zod';
* https://api-dashboard.search.brave.com/api-reference/web/place_search#responses
*/
const CoordinatesSchema = z
.tuple([z.number(), z.number()])
.describe('Latitude/longitude pair for the location, when available.');
const ThumbnailSchema = z.looseObject({
src: z.string().describe('The served URL of the picture thumbnail.').optional(),
original: z.string().describe('The original URL of the image.').optional(),
src: z.string().describe('The served URL of the picture thumbnail.'),
alt: z.string().describe('The alt text for the thumbnail.').optional(),
height: z.number().describe('The height of the thumbnail.').optional(),
width: z.number().describe('The width of the thumbnail.').optional(),
bg_color: z.string().describe('The background color of the thumbnail.').optional(),
original: z.url().describe('The original URL of the image.').optional(),
logo: z.boolean().describe('Whether the thumbnail is a logo.').optional(),
duplicated: z.boolean().describe('Whether the thumbnail is duplicated.').optional(),
theme: z.string().describe('The theme of the thumbnail (e.g. "light", "dark").').optional(),
});
const PostalAddressSchema = z.looseObject({
type: z.literal('PostalAddress').optional(),
type: z.literal('PostalAddress'),
country: z.string().describe('The country associated with the location.').optional(),
postalCode: z.string().describe('The postal code associated with the location.').optional(),
streetAddress: z.string().describe('The street address associated with the location.').optional(),
@@ -23,7 +34,7 @@ const PostalAddressSchema = z.looseObject({
.string()
.describe('The address locality or subregion associated with the location.')
.optional(),
displayAddress: z.string().describe('The displayed address string.').optional(),
displayAddress: z.string().describe('The displayed address string.'),
});
const DayOpeningHoursSchema = z.looseObject({
@@ -121,11 +132,18 @@ const MetaUrlSchema = z.looseObject({
.optional(),
});
const ResultsSchema = z.looseObject({
/**
* Fields shared by every "web-style" result entry: both inline web results
* (which decorate location results via `results`) and the location/POI
* results themselves. Defined once and composed via `.extend()` below.
*/
const WebResultBaseSchema = z.looseObject({
title: z.string().describe('The display title of the location.'),
url: z.string().describe('Primary URL associated with the location.'),
is_source_local: z.boolean().optional(),
is_source_both: z.boolean().optional(),
url: z
.union([z.url(), z.literal('')])
.describe('Primary URL associated with the location. May be an empty string.'),
is_source_local: z.boolean().describe('Whether the result is from local sources.'),
is_source_both: z.boolean().describe('Whether the result is from both local and global sources.'),
description: z
.string()
.describe('Short description of the location (e.g. "Plaza", "Theater").')
@@ -139,60 +157,48 @@ const ResultsSchema = z.looseObject({
.number()
.describe('The timestamp of the content when it was fetched.')
.optional(),
profile: ProfileSchema.optional(),
profile: ProfileSchema.describe('The profile associated with the result.').optional(),
language: z.string().describe('The language of the page.').optional(),
family_friendly: z.boolean().optional(),
family_friendly: z.boolean().describe('Whether the result is family friendly.').optional(),
});
/**
* An inline web result attached to a location (under `LocationResultSchema.results`).
*/
const WebResultSchema = WebResultBaseSchema.extend({
meta_url: MetaUrlSchema.optional(),
});
const ResultSchema = z.looseObject({
title: z.string().describe('The display title of the location.'),
url: z.string().describe('Primary URL associated with the location.'),
is_source_local: z.boolean().optional(),
is_source_both: z.boolean().optional(),
description: z
.string()
.describe('Short description of the location (e.g. "Plaza", "Theater").')
.optional(),
page_age: z.string().describe('The age of the page as a date string.').optional(),
page_fetched: z
.string()
.describe('The date the page was last fetched as a date string.')
.optional(),
fetched_content_timestamp: z
.number()
.describe('The timestamp of the content when it was fetched.')
.optional(),
profile: ProfileSchema.optional(),
language: z.string().describe('The language of the page.').optional(),
family_friendly: z.boolean().optional(),
type: z.literal('location_result').describe('Result type identifier.').optional(),
/**
* A location/POI result. Used both at the top level (`results[]`) and inside
* addresses (`pois[]` / `pois_nearby[]`).
*/
const LocationResultSchema = WebResultBaseSchema.extend({
type: z.literal('location_result').describe('Result type identifier.'),
provider_url: z
.string()
.describe('URL of the upstream provider for this result. May be an empty string.')
.optional(),
coordinates: z
.array(z.number())
.describe('Latitude/longitude pair for the location, when available.')
.optional(),
zoom_level: z.number().describe('Suggested zoom level when displaying on a map.').optional(),
thumbnail: ThumbnailSchema.optional(),
postal_address: PostalAddressSchema.optional(),
opening_hours: OpeningHoursSchema.optional(),
contact: ContactSchema.optional(),
.union([z.url(), z.literal('')])
.describe('URL of the upstream provider for this result. May be an empty string.'),
coordinates: CoordinatesSchema.optional(),
zoom_level: z.number().describe('Suggested zoom level when displaying on a map.'),
thumbnail: ThumbnailSchema.describe('The thumbnail associated with the location.').optional(),
postal_address: PostalAddressSchema.describe('The postal address of the location.').optional(),
opening_hours: OpeningHoursSchema.describe('The opening hours of the location.').optional(),
contact: ContactSchema.describe('The contact of the location.').optional(),
price_range: z
.string()
.describe('Price classification string for the business (e.g. "$", "$$").')
.optional(),
rating: RatingSchema.optional(),
distance: DistanceSchema.optional(),
rating: RatingSchema.describe('The rating of the result.').optional(),
distance: DistanceSchema.describe(
"Distance from the user's geolocation, if available."
).optional(),
profiles: z
.array(ProfileSchema)
.describe('External profiles (e.g. data providers) associated with the result.')
.optional(),
reviews: ReviewsSchema.optional(),
pictures: PicturesSchema.optional(),
action: ActionSchema.optional(),
reviews: ReviewsSchema.describe('Reviews associated with the result.').optional(),
pictures: PicturesSchema.describe('Pictures associated with the result.').optional(),
action: ActionSchema.describe('The action associated with the result.').optional(),
serves_cuisine: z.array(z.string()).describe('List of cuisine categories served.').optional(),
categories: z.array(z.string()).describe('List of category labels.').optional(),
icon_category: z.string().describe('Suggested icon category (e.g. "cafe").').optional(),
@@ -207,7 +213,7 @@ const ResultSchema = z.looseObject({
'Temporary identifier for the location, valid for ~8 hours. Can be used with brave_local_search-style endpoints to fetch additional information.'
)
.optional(),
results: z.array(ResultsSchema).describe('Web results related to this location.').optional(),
results: z.array(WebResultSchema).describe('Web results related to this location.').optional(),
});
const QuerySchema = z.looseObject({
@@ -222,8 +228,33 @@ const QuerySchema = z.looseObject({
show_strict_warning: z.boolean().optional(),
});
const AddressSchema = z.looseObject({
type: z
.enum(['address', 'street'])
.describe(
'Result is "address" when the result refers to an explicit street + number, but "street" when it refers only to the street itself.'
),
name: z.string().describe('The name of the address.'),
coordinates: CoordinatesSchema.describe('Latitude/longitude of the address'),
pois: z.array(LocationResultSchema).describe('List of POIs located at this address.'),
pois_nearby: z.array(LocationResultSchema).describe('List of POIs nearby this address.'),
zoom_level: z.number().describe('Suggested zoom level when displaying on a map.'),
distance: DistanceSchema.describe(
"Distance from the user's geolocation, if available."
).optional(),
postal_address: PostalAddressSchema.describe('The postal address of the address.').optional(),
});
const CitySchema = z.looseObject({
type: z.literal('city'),
name: z.string().describe('Name of the city'),
country: z.string().describe('ISO country code for the city'),
coordinates: CoordinatesSchema.describe('Latitude/longitude of the city'),
thumbnail: ThumbnailSchema.describe('Primary image for the city'),
});
const LocationSchema = z.looseObject({
coordinates: z.array(z.number()).describe('Latitude/longitude of the resolved search area.'),
coordinates: CoordinatesSchema.describe('Latitude/longitude of the resolved search area.'),
name: z.string().describe('Human-readable name of the resolved search area.').optional(),
country: z.string().describe('ISO country code for the resolved search area.').optional(),
});
@@ -234,11 +265,29 @@ export const PlaceSearchApiResponseSchema = z.looseObject({
.describe('Top-level response discriminator. Always "locations" for Place Search.'),
query: QuerySchema.optional(),
results: z
.array(ResultSchema)
.describe('Array of points-of-interest matching the search.')
.array(LocationResultSchema)
.describe('Points of interest matching the search. Populated for POI-shaped queries.')
.optional(),
cities: z
.array(CitySchema)
.describe(
'City matches for the query. Typically populated when the query is a bare or ambiguous city name (e.g. "springfield", "san francisco").'
)
.optional(),
addresses: z
.array(AddressSchema)
.describe(
'Address matches. Typically populated when the query is a street + number AND is geographically anchored via `latitude`+`longitude` or a specific `location`.'
)
.optional(),
streets: z
.array(AddressSchema)
.describe(
'Street matches. Typically populated when the query is a street name AND is geographically anchored via `latitude`+`longitude` or a specific `location`.'
)
.optional(),
location: LocationSchema.describe(
'The resolved search-area metadata, when available.'
'The search area as resolved by the API (e.g. coordinates + city name). Useful for confirming the API interpreted the input as expected and for grounding follow-up queries.'
).optional(),
});