improve response structure of image tool (#52)

Tool now more closely resembles the documented response shape of the image API endpoint. This tool also no longer preemptively loads base64-encoded image data. The images tool now uses a structured content response. Following the suggestion in https://modelcontextprotocol.io/specification/2025-06-18/server/tools#output-schema, we also provide an Output Schema, enabling clients to validate the response. As a result of this work, the Images tool requires a slightly different registration approach. For this reason, registration can now be handled via a local "register" method in each tool module. When tools were registered externally in a for-loop, the type system was unable to infer the shape of each tool due to the overloaded union (resulting from only the Image tool having an OutputSchema export).
This commit is contained in:
Sampson authored and GitHub committed 2025-09-06 10:35:08 -05:00
1 parent 398e88ffb8
commit 4fb81fb605
12 files changed
+234 -42

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
import { z } from 'zod';
/**
* https://api-dashboard.search.brave.com/app/documentation/image-search/responses
*/
const QuerySchema = z.object({
original: z.string().describe('The original query string.'),
altered: z.string().optional().describe('The altered query string.'),
spellcheck_off: z.boolean().optional().describe('Whether spellcheck was disabled.'),
show_strict_warning: z
.boolean()
.optional()
.describe('When true, some results were blocked by safesearch.'),
});
const ThumbnailSchema = z.object({
src: z.string().url().optional().describe('The URL of the thumbnail.'),
width: z.number().int().positive().optional().describe('The width of the thumbnail.'),
height: z.number().int().positive().optional().describe('The height of the thumbnail.'),
});
const PropertiesSchema = z.object({
url: z.string().url().optional().describe('The URL of the image.'),
placeholder: z.string().url().optional().describe('The lower resolution placeholder image.'),
width: z.number().int().positive().optional().describe('The width of the image.'),
height: z.number().int().positive().optional().describe('The height of the image.'),
});
const MetaUrlSchema = z.object({
scheme: z.enum(['https', 'http']).optional().describe('The scheme of the URL.'),
netloc: z.string().optional().describe('The network location of the URL.'),
hostname: z.string().optional().describe('The lowercased hostname of the URL.'),
favicon: z.string().url().optional().describe('The URL of the favicon of the URL.'),
path: z.string().optional().describe('The path of the URL (useful as a display string).'),
});
export const ConfidenceSchema = z
.enum(['low', 'medium', 'high'])
.describe('The confidence level of the result.');
const ImageResultSchema = z.object({
type: z.literal('image_result').describe('The type of result.'),
title: z.string().optional().describe('The title of the image.'),
url: z.string().url().optional().describe('The URL of the image.'),
source: z.string().url().optional().describe('The source URL of the image.'),
page_fetched: z
.string()
.datetime()
.optional()
.describe('The date and time the page was fetched.'),
thumbnail: ThumbnailSchema.optional().describe('The thumbnail of the image.'),
properties: PropertiesSchema.optional().describe('The metadata for the image.'),
meta_url: MetaUrlSchema.optional().describe(
'Information about the URL associated with the image.'
),
confidence: ConfidenceSchema.optional(),
});
export const ExtraSchema = z.object({
might_be_offensive: z.boolean().describe('Whether the image might be offensive.'),
});
export const ImageSearchApiResponseSchema = z.object({
type: z.literal('images').describe('The type of API response.'),
query: QuerySchema.describe('The query used to generate the results.'),
results: z.array(ImageResultSchema).describe('The results of the image search.'),
extra: ExtraSchema.describe('Extra information about the search.'),
});