@@ -20,7 +20,7 @@ import type {PaginationOptions} from '../utils/types.js';
2020
2121import type { ToolCategory } from './categories.js' ;
2222
23- export interface ToolDefinition <
23+ export interface BaseToolDefinition <
2424 Schema extends zod . ZodRawShape = zod . ZodRawShape ,
2525> {
2626 name : string ;
@@ -33,14 +33,14 @@ export interface ToolDefinition<
3333 */
3434 readOnlyHint : boolean ;
3535 conditions ?: string [ ] ;
36- /**
37- * If true, the tool operates on a specific page.
38- * The `pageId` schema field is auto-injected and the resolved
39- * page is provided via `request.page`.
40- */
41- pageScoped ?: boolean ;
4236 } ;
4337 schema : Schema ;
38+ }
39+
40+ export interface ToolDefinition <
41+ Schema extends zod . ZodRawShape = zod . ZodRawShape ,
42+ > extends BaseToolDefinition < Schema > {
43+ schema : Schema ;
4444 handler : (
4545 request : Request < Schema > ,
4646 response : Response ,
@@ -50,8 +50,6 @@ export interface ToolDefinition<
5050
5151export interface Request < Schema extends zod . ZodRawShape > {
5252 params : zod . objectOutputType < Schema , zod . ZodTypeAny > ;
53- /** Populated centrally for tools with `pageScoped: true`. */
54- page ?: Page ;
5553}
5654
5755export interface ImageContentData {
@@ -215,28 +213,66 @@ export function defineTool<
215213 if ( typeof definition === 'function' ) {
216214 const factory = definition ;
217215 return ( args : Args ) => {
218- const tool = factory ( args ) ;
219- wrapPageScopedHandler ( tool ) ;
220- return tool ;
216+ return factory ( args ) ;
221217 } ;
222218 }
223- wrapPageScopedHandler ( definition ) ;
224219 return definition ;
225220}
226221
227- function wrapPageScopedHandler < Schema extends zod . ZodRawShape > (
228- definition : ToolDefinition < Schema > ,
229- ) {
230- if ( definition . annotations . pageScoped ) {
231- const originalHandler = definition . handler ;
232- definition . handler = async ( request , response , context ) => {
233- // In production, main.ts resolves request.page centrally before calling
234- // the handler. This fallback exists for tests that invoke handlers
235- // directly without going through main.ts.
236- request . page ??= context . getSelectedPage ( ) ;
237- return originalHandler ( request , response , context ) ;
222+ interface PageToolDefinition <
223+ Schema extends zod . ZodRawShape = zod . ZodRawShape ,
224+ > extends BaseToolDefinition < Schema > {
225+ handler : (
226+ request : Request < Schema > & { page : Page } ,
227+ response : Response ,
228+ context : Context ,
229+ ) => Promise < void > ;
230+ }
231+
232+ export type DefinedPageTool <
233+ Schema extends zod . ZodRawShape = zod . ZodRawShape ,
234+ > = PageToolDefinition < Schema > & {
235+ pageScoped : true ;
236+ handler : (
237+ request : Request < Schema > & { page : Page } ,
238+ response : Response ,
239+ context : Context ,
240+ ) => Promise < void > ;
241+ } ;
242+
243+ export function definePageTool < Schema extends zod . ZodRawShape > (
244+ definition : PageToolDefinition < Schema > ,
245+ ) : DefinedPageTool < Schema > ;
246+
247+ export function definePageTool <
248+ Schema extends zod . ZodRawShape ,
249+ Args extends ParsedArguments = ParsedArguments ,
250+ > (
251+ definition : ( args ?: Args ) => PageToolDefinition < Schema > ,
252+ ) : ( args ?: Args ) => DefinedPageTool < Schema > ;
253+
254+ export function definePageTool <
255+ Schema extends zod . ZodRawShape ,
256+ Args extends ParsedArguments = ParsedArguments ,
257+ > (
258+ definition :
259+ | PageToolDefinition < Schema >
260+ | ( ( args ?: Args ) => PageToolDefinition < Schema > ) ,
261+ ) : DefinedPageTool < Schema > | ( ( args ?: Args ) => DefinedPageTool < Schema > ) {
262+ if ( typeof definition === 'function' ) {
263+ return ( args ?: Args ) : DefinedPageTool < Schema > => {
264+ const tool = definition ( args ) ;
265+ return {
266+ ...tool ,
267+ pageScoped : true ,
268+ } ;
238269 } ;
239270 }
271+
272+ return {
273+ ...definition ,
274+ pageScoped : true ,
275+ } as DefinedPageTool < Schema > ;
240276}
241277
242278export const CLOSE_PAGE_ERROR =
0 commit comments