Skip to content

Commit 675efe0

Browse files
committed
fix: resolve TS2589 "Type instantiation is excessively deep" error in registerTools
Simplifies the registerTools() method signature to prevent TypeScript from attempting complex tuple type inference when registering large numbers of tools with complex Zod schemas. The simplified interface maintains full runtime safety through Zod validation while avoiding compile-time type complexity issues.
1 parent c827e15 commit 675efe0

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

src/server/mcp.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ import { UriTemplate, Variables } from "../shared/uriTemplate.js";
4747
import { RequestHandlerExtra } from "../shared/protocol.js";
4848
import { Transport } from "../shared/transport.js";
4949

50+
/**
51+
* Simple interface for tool registration that avoids TypeScript "Type instantiation is excessively deep"
52+
* errors when registering large numbers of tools with complex schemas.
53+
*
54+
* Uses unknown types to prevent TypeScript from attempting deep type inference on complex schemas.
55+
*/
56+
export interface ToolRegistration {
57+
name: string;
58+
config: {
59+
title?: string;
60+
description?: string;
61+
inputSchema?: unknown;
62+
outputSchema?: unknown;
63+
annotations?: ToolAnnotations;
64+
};
65+
callback: (args: unknown, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => CallToolResult | Promise<CallToolResult>;
66+
}
67+
5068
/**
5169
* High-level MCP server that provides a simpler API for working with resources, tools, and prompts.
5270
* For advanced usage (like sending notifications or setting custom request handlers), use the underlying
@@ -671,12 +689,12 @@ export class McpServer {
671689
* This is more efficient than calling registerResource() multiple times,
672690
* especially when registering many resources, as it sends only one list_changed notification.
673691
*/
674-
registerResources<T extends Array<{
692+
registerResources(resources: Array<{
675693
name: string;
676694
uriOrTemplate: string | ResourceTemplate;
677695
config: ResourceMetadata;
678696
callback: ReadResourceCallback | ReadResourceTemplateCallback;
679-
}>>(resources: T): (RegisteredResource | RegisteredResourceTemplate)[] {
697+
}>): (RegisteredResource | RegisteredResourceTemplate)[] {
680698
if (resources.length === 0) {
681699
return [];
682700
}
@@ -731,15 +749,15 @@ export class McpServer {
731749
* This is more efficient than calling registerPrompt() multiple times when registering many prompts,
732750
* especially when registering many prompts, as it sends only one list_changed notification.
733751
*/
734-
registerPrompts<T extends Array<{
752+
registerPrompts(prompts: Array<{
735753
name: string;
736754
config: {
737755
title?: string;
738756
description?: string;
739757
argsSchema?: PromptArgsRawShape;
740758
};
741759
callback: PromptCallback<PromptArgsRawShape | undefined>;
742-
}>>(prompts: T): RegisteredPrompt[] {
760+
}>): RegisteredPrompt[] {
743761
if (prompts.length === 0) {
744762
return [];
745763
}
@@ -1066,18 +1084,10 @@ export class McpServer {
10661084
* Registers multiple tools at once with a single notification.
10671085
* This is more efficient than calling registerTool() multiple times,
10681086
* especially when registering many tools, as it sends only one list_changed notification.
1087+
*
1088+
* Uses simplified types to avoid TypeScript compilation issues with large numbers of complex tools.
10691089
*/
1070-
registerTools<T extends Array<{
1071-
name: string;
1072-
config: {
1073-
title?: string;
1074-
description?: string;
1075-
inputSchema?: ZodRawShape;
1076-
outputSchema?: ZodRawShape;
1077-
annotations?: ToolAnnotations;
1078-
};
1079-
callback: ToolCallback<ZodRawShape | undefined>;
1080-
}>>(tools: T): RegisteredTool[] {
1090+
registerTools(tools: ToolRegistration[]): RegisteredTool[] {
10811091
if (tools.length === 0) {
10821092
return [];
10831093
}
@@ -1099,8 +1109,8 @@ export class McpServer {
10991109
name,
11001110
title,
11011111
description,
1102-
inputSchema,
1103-
outputSchema,
1112+
inputSchema as ZodRawShape | undefined,
1113+
outputSchema as ZodRawShape | undefined,
11041114
annotations,
11051115
callback as ToolCallback<ZodRawShape | undefined>
11061116
);

0 commit comments

Comments
 (0)