@@ -169,7 +169,7 @@ export class McpServer {
169
169
}
170
170
171
171
const args = parseResult . data ;
172
- const cb = tool . callback as ToolCallback < ZodRawShape > ;
172
+ const cb = tool . callback as ToolCallback < ZodRawShape , ZodRawShape > ;
173
173
try {
174
174
result = await Promise . resolve ( cb ( args , extra ) ) ;
175
175
} catch ( error ) {
@@ -184,7 +184,7 @@ export class McpServer {
184
184
} ;
185
185
}
186
186
} else {
187
- const cb = tool . callback as ToolCallback < undefined > ;
187
+ const cb = tool . callback as ToolCallback < undefined , ZodRawShape > ;
188
188
try {
189
189
result = await Promise . resolve ( cb ( extra ) ) ;
190
190
} catch ( error ) {
@@ -760,7 +760,7 @@ export class McpServer {
760
760
inputSchema : ZodRawShape | undefined ,
761
761
outputSchema : ZodRawShape | undefined ,
762
762
annotations : ToolAnnotations | undefined ,
763
- callback : ToolCallback < ZodRawShape | undefined >
763
+ callback : ToolCallback < ZodRawShape | undefined , ZodRawShape | undefined >
764
764
) : RegisteredTool {
765
765
const registeredTool : RegisteredTool = {
766
766
title,
@@ -917,7 +917,7 @@ export class McpServer {
917
917
outputSchema ?: OutputArgs ;
918
918
annotations ?: ToolAnnotations ;
919
919
} ,
920
- cb : ToolCallback < InputArgs >
920
+ cb : ToolCallback < InputArgs , OutputArgs >
921
921
) : RegisteredTool {
922
922
if ( this . _registeredTools [ name ] ) {
923
923
throw new Error ( `Tool ${ name } is already registered` ) ;
@@ -932,7 +932,7 @@ export class McpServer {
932
932
inputSchema ,
933
933
outputSchema ,
934
934
annotations ,
935
- cb as ToolCallback < ZodRawShape | undefined >
935
+ cb as ToolCallback < ZodRawShape | undefined , ZodRawShape | undefined >
936
936
) ;
937
937
}
938
938
@@ -1126,6 +1126,16 @@ export class ResourceTemplate {
1126
1126
}
1127
1127
}
1128
1128
1129
+ /**
1130
+ * Type helper to create a strongly-typed CallToolResult with structuredContent
1131
+ */
1132
+ type TypedCallToolResult < OutputArgs extends undefined | ZodRawShape > =
1133
+ OutputArgs extends ZodRawShape
1134
+ ? CallToolResult & {
1135
+ structuredContent ?: z . objectOutputType < OutputArgs , ZodTypeAny > ;
1136
+ }
1137
+ : CallToolResult ;
1138
+
1129
1139
/**
1130
1140
* Callback for a tool handler registered with Server.tool().
1131
1141
*
@@ -1136,36 +1146,46 @@ export class ResourceTemplate {
1136
1146
* - `content` if the tool does not have an outputSchema
1137
1147
* - Both fields are optional but typically one should be provided
1138
1148
*/
1139
- export type ToolCallback < Args extends undefined | ZodRawShape = undefined > =
1140
- Args extends ZodRawShape
1149
+ export type ToolCallback <
1150
+ InputArgs extends undefined | ZodRawShape = undefined ,
1151
+ OutputArgs extends undefined | ZodRawShape = undefined
1152
+ > = InputArgs extends ZodRawShape
1141
1153
? (
1142
- args : z . objectOutputType < Args , ZodTypeAny > ,
1143
- extra : RequestHandlerExtra < ServerRequest , ServerNotification > ,
1144
- ) => CallToolResult | Promise < CallToolResult >
1145
- : ( extra : RequestHandlerExtra < ServerRequest , ServerNotification > ) => CallToolResult | Promise < CallToolResult > ;
1154
+ args : z . objectOutputType < InputArgs , ZodTypeAny > ,
1155
+ extra : RequestHandlerExtra < ServerRequest , ServerNotification >
1156
+ ) =>
1157
+ | TypedCallToolResult < OutputArgs >
1158
+ | Promise < TypedCallToolResult < OutputArgs > >
1159
+ : (
1160
+ extra : RequestHandlerExtra < ServerRequest , ServerNotification >
1161
+ ) =>
1162
+ | TypedCallToolResult < OutputArgs >
1163
+ | Promise < TypedCallToolResult < OutputArgs > > ;
1146
1164
1147
1165
export type RegisteredTool = {
1148
1166
title ?: string ;
1149
1167
description ?: string ;
1150
1168
inputSchema ?: AnyZodObject ;
1151
1169
outputSchema ?: AnyZodObject ;
1152
1170
annotations ?: ToolAnnotations ;
1153
- callback : ToolCallback < undefined | ZodRawShape > ;
1171
+ callback : ToolCallback < ZodRawShape | undefined , ZodRawShape | undefined > ;
1154
1172
enabled : boolean ;
1155
1173
enable ( ) : void ;
1156
1174
disable ( ) : void ;
1157
- update < InputArgs extends ZodRawShape , OutputArgs extends ZodRawShape > (
1158
- updates : {
1159
- name ?: string | null ,
1160
- title ?: string ,
1161
- description ?: string ,
1162
- paramsSchema ?: InputArgs ,
1163
- outputSchema ?: OutputArgs ,
1164
- annotations ?: ToolAnnotations ,
1165
- callback ?: ToolCallback < InputArgs > ,
1166
- enabled ?: boolean
1167
- } ) : void
1168
- remove ( ) : void
1175
+ update <
1176
+ InputArgs extends ZodRawShape ,
1177
+ OutputArgs extends ZodRawShape
1178
+ > ( updates : {
1179
+ name ?: string | null ;
1180
+ title ?: string ;
1181
+ description ?: string ;
1182
+ paramsSchema ?: InputArgs ;
1183
+ outputSchema ?: OutputArgs ;
1184
+ annotations ?: ToolAnnotations ;
1185
+ callback ?: ToolCallback < InputArgs , OutputArgs >
1186
+ enabled ?: boolean
1187
+ } ) : void ;
1188
+ remove ( ) : void ;
1169
1189
} ;
1170
1190
1171
1191
const EMPTY_OBJECT_JSON_SCHEMA = {
0 commit comments