Skip to content

Commit e6219bb

Browse files
committed
tool management via server instance
1 parent be8022e commit e6219bb

File tree

2 files changed

+90
-33
lines changed

2 files changed

+90
-33
lines changed

src/server/mcp.test.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,9 @@ describe('tool()', () => {
506506
});
507507

508508
/***
509-
* Test: Tool self removal
509+
* Test: Tool disable, enable, and remove via tool instance
510510
*/
511-
test("should remove tool when using tool.remove()", async () => {
511+
test("should manage tool when using tool instance", async () => {
512512
const mcpServer = new McpServer({
513513
name: "test server",
514514
version: "1.0",
@@ -524,18 +524,28 @@ describe('tool()', () => {
524524
],
525525
}));
526526

527-
expect(mcpServer['_registeredTools']['test']).toBeDefined();
527+
expect(mcpServer['_registeredTools'].test).toBeDefined();
528+
529+
// Now disable the tool
530+
tool.disable();
531+
532+
expect(mcpServer['_registeredTools'].test.enabled).toBe(false);
533+
534+
// Now enable the tool
535+
tool.enable();
536+
537+
expect(mcpServer['_registeredTools'].test.enabled).toBe(true);
528538

529539
// Now delete the tool
530540
tool.remove();
531541

532-
expect(mcpServer['_registeredTools']['test']).toBeUndefined();
542+
expect(mcpServer['_registeredTools'].test).toBeUndefined();
533543
});
534544

535545
/***
536-
* Test: Tool server removal
546+
* Test: Tool disable, enable, and remove via server instance
537547
*/
538-
test("should remove tool when using server.removeTool(...)", async () => {
548+
test("should manage tool when using server instance", async () => {
539549
const mcpServer = new McpServer({
540550
name: "test server",
541551
version: "1.0",
@@ -551,12 +561,22 @@ describe('tool()', () => {
551561
],
552562
}));
553563

554-
expect(mcpServer['_registeredTools']['test']).toBeDefined();
564+
expect(mcpServer['_registeredTools'].test).toBeDefined();
565+
566+
// Now disable the tool
567+
mcpServer.disableTool("test");
568+
569+
expect(mcpServer['_registeredTools'].test.enabled).toBe(false);
570+
571+
// Now enable the tool
572+
mcpServer.enableTool("test");
573+
574+
expect(mcpServer['_registeredTools'].test.enabled).toBe(true);
555575

556576
// Now delete the tool
557577
mcpServer.removeTool("test");
558578

559-
expect(mcpServer['_registeredTools']['test']).toBeUndefined();
579+
expect(mcpServer['_registeredTools'].test).toBeUndefined();
560580
});
561581

562582
/***

src/server/mcp.ts

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -816,15 +816,48 @@ export class McpServer {
816816
);
817817
}
818818

819+
/**
820+
* Enables a tool from the server by name.
821+
* Does nothing if the tool is not registered.
822+
*/
823+
enableTool(name: string) {
824+
const tool = this._registeredTools[name];
825+
if (tool) {
826+
tool.enable();
827+
}
828+
};
829+
830+
/**
831+
* Disables a tool from the server by name.
832+
* Does nothing if the tool is not registered.
833+
*/
834+
disableTool(name: string) {
835+
const tool = this._registeredTools[name];
836+
if (tool) {
837+
tool.disable();
838+
}
839+
};
840+
841+
/**
842+
* Updates a tool from the server by name.
843+
* Does nothing if the tool is not registered.
844+
*/
845+
updateTool<InputArgs extends ZodRawShape, OutputArgs extends ZodRawShape>(name: string, updates: ToolUpdates<InputArgs, OutputArgs>) {
846+
const tool = this._registeredTools[name];
847+
if (tool) {
848+
tool.update(updates);
849+
}
850+
};
851+
819852
/**
820853
* Removes a tool from the server by name.
821854
* Does nothing if the tool is not registered.
822855
*/
823856
removeTool(name: string) {
824-
const tool = this._registeredTools[name];
825-
if (tool) {
857+
const tool = this._registeredTools[name];
858+
if (tool) {
826859
tool.update({ name: null });
827-
}
860+
}
828861
};
829862

830863
/**
@@ -1029,29 +1062,33 @@ export type ToolCallback<Args extends undefined | ZodRawShape = undefined> = Arg
10291062
) => CallToolResult | Promise<CallToolResult>
10301063
: (extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => CallToolResult | Promise<CallToolResult>;
10311064

1065+
export type ToolUpdates<InputArgs extends ZodRawShape, OutputArgs extends ZodRawShape> = {
1066+
name?: string | null,
1067+
title?: string,
1068+
description?: string,
1069+
paramsSchema?: InputArgs,
1070+
outputSchema?: OutputArgs,
1071+
annotations?: ToolAnnotations,
1072+
_meta?: Record<string, unknown>,
1073+
callback?: ToolCallback<InputArgs>,
1074+
enabled?: boolean
1075+
}
1076+
10321077
export type RegisteredTool = {
1033-
title?: string;
1034-
description?: string;
1035-
inputSchema?: AnyZodObject;
1036-
outputSchema?: AnyZodObject;
1037-
annotations?: ToolAnnotations;
1038-
_meta?: Record<string, unknown>;
1039-
callback: ToolCallback<undefined | ZodRawShape>;
1040-
enabled: boolean;
1041-
enable(): void;
1042-
disable(): void;
1043-
update<InputArgs extends ZodRawShape, OutputArgs extends ZodRawShape>(updates: {
1044-
name?: string | null;
1045-
title?: string;
1046-
description?: string;
1047-
paramsSchema?: InputArgs;
1048-
outputSchema?: OutputArgs;
1049-
annotations?: ToolAnnotations;
1050-
_meta?: Record<string, unknown>;
1051-
callback?: ToolCallback<InputArgs>;
1052-
enabled?: boolean;
1053-
}): void;
1054-
remove(): void;
1078+
title?: string;
1079+
description?: string;
1080+
inputSchema?: AnyZodObject;
1081+
outputSchema?: AnyZodObject;
1082+
annotations?: ToolAnnotations;
1083+
_meta?: Record<string, unknown>;
1084+
callback: ToolCallback<undefined | ZodRawShape>;
1085+
enabled: boolean;
1086+
enable(): void;
1087+
disable(): void;
1088+
update<InputArgs extends ZodRawShape, OutputArgs extends ZodRawShape>(
1089+
updates: ToolUpdates<InputArgs, OutputArgs>
1090+
): void
1091+
remove(): void
10551092
};
10561093

10571094
const EMPTY_OBJECT_JSON_SCHEMA = {

0 commit comments

Comments
 (0)