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:
1 parent
a48cf65c9d
commit
42e1861f64
3 files changed
+118
-69
No files matched your search
@@ -1,5 +1,4 @@
|
|||||||
import type { TextContent, ToolAnnotations } from '@modelcontextprotocol/sdk/types.js';
|
import type { TextContent, ToolAnnotations } from '@modelcontextprotocol/sdk/types.js';
|
||||||
import { z } from 'zod';
|
|
||||||
import API from '../../BraveAPI/index.js';
|
import API from '../../BraveAPI/index.js';
|
||||||
import { type McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
import { type McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
||||||
import {
|
import {
|
||||||
@@ -18,17 +17,20 @@ export const annotations: ToolAnnotations = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const description = `
|
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:
|
When to use:
|
||||||
- Finding places near a set of coordinates or a named location (e.g. "coffee shops near me", "bookstores in Paris")
|
- 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 points of interest in an area when no query is supplied
|
- Browsing general POIs (omit 'query'; supply 'latitude'+'longitude' or 'location').
|
||||||
- Building a place-finding experience that needs structured business data (hours, ratings, etc.)
|
- Disambiguating a bare city name (e.g. "springfield") -> 'cities'.
|
||||||
- Augmenting an answer with the location's address, phone number, or rating
|
- 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).
|
Inputs:
|
||||||
|
- Anchor the search via 'latitude'+'longitude' or 'location' (or both). With neither, 'query' is required.
|
||||||
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').
|
- '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();
|
`.trim();
|
||||||
|
|
||||||
export const execute = async (params: QueryParams) => {
|
export const execute = async (params: QueryParams) => {
|
||||||
@@ -40,13 +42,11 @@ export const execute = async (params: QueryParams) => {
|
|||||||
parsedParams,
|
parsedParams,
|
||||||
parsedHeaders
|
parsedHeaders
|
||||||
);
|
);
|
||||||
const parsed = PlaceSearchApiResponseSchema.safeParse(response);
|
|
||||||
const payload = parsed.success ? parsed.data : response;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
content: [{ type: 'text', text: JSON.stringify(payload) } as TextContent],
|
content: [{ type: 'text', text: JSON.stringify(response) } as TextContent],
|
||||||
isError: false,
|
isError: false,
|
||||||
structuredContent: payload,
|
structuredContent: response,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -151,14 +151,14 @@ export const RequestParamsSchema = z.object({
|
|||||||
)
|
)
|
||||||
.transform((str) => (str.length === 0 ? undefined : str))
|
.transform((str) => (str.length === 0 ? undefined : str))
|
||||||
.describe(
|
.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(),
|
.optional(),
|
||||||
radius: z
|
radius: z
|
||||||
.number()
|
.number()
|
||||||
.min(0)
|
.min(0)
|
||||||
.describe(
|
.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(),
|
.optional(),
|
||||||
count: z
|
count: z
|
||||||
|
|||||||
@@ -5,13 +5,24 @@ import { z } from 'zod';
|
|||||||
* https://api-dashboard.search.brave.com/api-reference/web/place_search#responses
|
* 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({
|
const ThumbnailSchema = z.looseObject({
|
||||||
src: z.string().describe('The served URL of the picture thumbnail.').optional(),
|
src: z.string().describe('The served URL of the picture thumbnail.'),
|
||||||
original: z.string().describe('The original URL of the image.').optional(),
|
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({
|
const PostalAddressSchema = z.looseObject({
|
||||||
type: z.literal('PostalAddress').optional(),
|
type: z.literal('PostalAddress'),
|
||||||
country: z.string().describe('The country associated with the location.').optional(),
|
country: z.string().describe('The country associated with the location.').optional(),
|
||||||
postalCode: z.string().describe('The postal code 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(),
|
streetAddress: z.string().describe('The street address associated with the location.').optional(),
|
||||||
@@ -23,7 +34,7 @@ const PostalAddressSchema = z.looseObject({
|
|||||||
.string()
|
.string()
|
||||||
.describe('The address locality or subregion associated with the location.')
|
.describe('The address locality or subregion associated with the location.')
|
||||||
.optional(),
|
.optional(),
|
||||||
displayAddress: z.string().describe('The displayed address string.').optional(),
|
displayAddress: z.string().describe('The displayed address string.'),
|
||||||
});
|
});
|
||||||
|
|
||||||
const DayOpeningHoursSchema = z.looseObject({
|
const DayOpeningHoursSchema = z.looseObject({
|
||||||
@@ -121,11 +132,18 @@ const MetaUrlSchema = z.looseObject({
|
|||||||
.optional(),
|
.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.'),
|
title: z.string().describe('The display title of the location.'),
|
||||||
url: z.string().describe('Primary URL associated with the location.'),
|
url: z
|
||||||
is_source_local: z.boolean().optional(),
|
.union([z.url(), z.literal('')])
|
||||||
is_source_both: z.boolean().optional(),
|
.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
|
description: z
|
||||||
.string()
|
.string()
|
||||||
.describe('Short description of the location (e.g. "Plaza", "Theater").')
|
.describe('Short description of the location (e.g. "Plaza", "Theater").')
|
||||||
@@ -139,60 +157,48 @@ const ResultsSchema = z.looseObject({
|
|||||||
.number()
|
.number()
|
||||||
.describe('The timestamp of the content when it was fetched.')
|
.describe('The timestamp of the content when it was fetched.')
|
||||||
.optional(),
|
.optional(),
|
||||||
profile: ProfileSchema.optional(),
|
profile: ProfileSchema.describe('The profile associated with the result.').optional(),
|
||||||
language: z.string().describe('The language of the page.').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(),
|
meta_url: MetaUrlSchema.optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const ResultSchema = z.looseObject({
|
/**
|
||||||
title: z.string().describe('The display title of the location.'),
|
* A location/POI result. Used both at the top level (`results[]`) and inside
|
||||||
url: z.string().describe('Primary URL associated with the location.'),
|
* addresses (`pois[]` / `pois_nearby[]`).
|
||||||
is_source_local: z.boolean().optional(),
|
*/
|
||||||
is_source_both: z.boolean().optional(),
|
const LocationResultSchema = WebResultBaseSchema.extend({
|
||||||
description: z
|
type: z.literal('location_result').describe('Result type identifier.'),
|
||||||
.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(),
|
|
||||||
provider_url: z
|
provider_url: z
|
||||||
.string()
|
.union([z.url(), z.literal('')])
|
||||||
.describe('URL of the upstream provider for this result. May be an empty string.')
|
.describe('URL of the upstream provider for this result. May be an empty string.'),
|
||||||
.optional(),
|
coordinates: CoordinatesSchema.optional(),
|
||||||
coordinates: z
|
zoom_level: z.number().describe('Suggested zoom level when displaying on a map.'),
|
||||||
.array(z.number())
|
thumbnail: ThumbnailSchema.describe('The thumbnail associated with the location.').optional(),
|
||||||
.describe('Latitude/longitude pair for the location, when available.')
|
postal_address: PostalAddressSchema.describe('The postal address of the location.').optional(),
|
||||||
.optional(),
|
opening_hours: OpeningHoursSchema.describe('The opening hours of the location.').optional(),
|
||||||
zoom_level: z.number().describe('Suggested zoom level when displaying on a map.').optional(),
|
contact: ContactSchema.describe('The contact of the location.').optional(),
|
||||||
thumbnail: ThumbnailSchema.optional(),
|
|
||||||
postal_address: PostalAddressSchema.optional(),
|
|
||||||
opening_hours: OpeningHoursSchema.optional(),
|
|
||||||
contact: ContactSchema.optional(),
|
|
||||||
price_range: z
|
price_range: z
|
||||||
.string()
|
.string()
|
||||||
.describe('Price classification string for the business (e.g. "$", "$$").')
|
.describe('Price classification string for the business (e.g. "$", "$$").')
|
||||||
.optional(),
|
.optional(),
|
||||||
rating: RatingSchema.optional(),
|
rating: RatingSchema.describe('The rating of the result.').optional(),
|
||||||
distance: DistanceSchema.optional(),
|
distance: DistanceSchema.describe(
|
||||||
|
"Distance from the user's geolocation, if available."
|
||||||
|
).optional(),
|
||||||
profiles: z
|
profiles: z
|
||||||
.array(ProfileSchema)
|
.array(ProfileSchema)
|
||||||
.describe('External profiles (e.g. data providers) associated with the result.')
|
.describe('External profiles (e.g. data providers) associated with the result.')
|
||||||
.optional(),
|
.optional(),
|
||||||
reviews: ReviewsSchema.optional(),
|
reviews: ReviewsSchema.describe('Reviews associated with the result.').optional(),
|
||||||
pictures: PicturesSchema.optional(),
|
pictures: PicturesSchema.describe('Pictures associated with the result.').optional(),
|
||||||
action: ActionSchema.optional(),
|
action: ActionSchema.describe('The action associated with the result.').optional(),
|
||||||
serves_cuisine: z.array(z.string()).describe('List of cuisine categories served.').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(),
|
categories: z.array(z.string()).describe('List of category labels.').optional(),
|
||||||
icon_category: z.string().describe('Suggested icon category (e.g. "cafe").').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.'
|
'Temporary identifier for the location, valid for ~8 hours. Can be used with brave_local_search-style endpoints to fetch additional information.'
|
||||||
)
|
)
|
||||||
.optional(),
|
.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({
|
const QuerySchema = z.looseObject({
|
||||||
@@ -222,8 +228,33 @@ const QuerySchema = z.looseObject({
|
|||||||
show_strict_warning: z.boolean().optional(),
|
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({
|
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(),
|
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(),
|
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.'),
|
.describe('Top-level response discriminator. Always "locations" for Place Search.'),
|
||||||
query: QuerySchema.optional(),
|
query: QuerySchema.optional(),
|
||||||
results: z
|
results: z
|
||||||
.array(ResultSchema)
|
.array(LocationResultSchema)
|
||||||
.describe('Array of points-of-interest matching the search.')
|
.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(),
|
.optional(),
|
||||||
location: LocationSchema.describe(
|
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(),
|
).optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user