Skip to content

Commit 0238855

Browse files
committed
Added update methods as well
1 parent df9b812 commit 0238855

File tree

1 file changed

+165
-8
lines changed

1 file changed

+165
-8
lines changed

src/server/mcp.ts

+165-8
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,57 @@ export class McpServer {
489489
name: string,
490490
uriOrTemplate: string | ResourceTemplate,
491491
...rest: unknown[]
492+
): void {
493+
this._setResource(name, uriOrTemplate, rest, false);
494+
}
495+
496+
/**
497+
* Updates a resource `name` at a fixed URI, which will use the given callback to respond to read requests.
498+
*/
499+
updateResource(name: string, uri: string, readCallback: ReadResourceCallback): void;
500+
501+
/**
502+
* Updates a resource `name` at a fixed URI with metadata, which will use the given callback to respond to read requests.
503+
*/
504+
updateResource(
505+
name: string,
506+
uri: string,
507+
metadata: ResourceMetadata,
508+
readCallback: ReadResourceCallback,
509+
): void;
510+
511+
/**
512+
* Updates a resource `name` with a template pattern, which will use the given callback to respond to read requests.
513+
*/
514+
updateResource(
515+
name: string,
516+
template: ResourceTemplate,
517+
readCallback: ReadResourceTemplateCallback,
518+
): void;
519+
520+
/**
521+
* Updates a resource `name` with a template pattern and metadata, which will use the given callback to respond to read requests.
522+
*/
523+
updateResource(
524+
name: string,
525+
template: ResourceTemplate,
526+
metadata: ResourceMetadata,
527+
readCallback: ReadResourceTemplateCallback,
528+
): void;
529+
530+
updateResource(
531+
name: string,
532+
uriOrTemplate: string | ResourceTemplate,
533+
...rest: unknown[]
534+
): void {
535+
this._setResource(name, uriOrTemplate, rest, true);
536+
}
537+
538+
private _setResource(
539+
name: string,
540+
uriOrTemplate: string | ResourceTemplate,
541+
rest: unknown[],
542+
update: boolean
492543
): void {
493544
let metadata: ResourceMetadata | undefined;
494545
if (typeof rest[0] === "object") {
@@ -500,8 +551,14 @@ export class McpServer {
500551
| ReadResourceTemplateCallback;
501552

502553
if (typeof uriOrTemplate === "string") {
503-
if (this._registeredResources[uriOrTemplate]) {
504-
throw new Error(`Resource ${uriOrTemplate} is already registered`);
554+
if (update) {
555+
if (!this._registeredResources[uriOrTemplate]) {
556+
throw new Error(`Resource ${uriOrTemplate} is not registered`);
557+
}
558+
} else {
559+
if (this._registeredResources[uriOrTemplate]) {
560+
throw new Error(`Resource ${uriOrTemplate} is already registered`);
561+
}
505562
}
506563

507564
this._registeredResources[uriOrTemplate] = {
@@ -510,8 +567,14 @@ export class McpServer {
510567
readCallback: readCallback as ReadResourceCallback,
511568
};
512569
} else {
513-
if (this._registeredResourceTemplates[name]) {
514-
throw new Error(`Resource template ${name} is already registered`);
570+
if (update) {
571+
if (!this._registeredResourceTemplates[name]) {
572+
throw new Error(`Resource template ${name} is not registered`);
573+
}
574+
} else {
575+
if (this._registeredResourceTemplates[name]) {
576+
throw new Error(`Resource template ${name} is already registered`);
577+
}
515578
}
516579

517580
this._registeredResourceTemplates[name] = {
@@ -585,8 +648,55 @@ export class McpServer {
585648
): void;
586649

587650
tool(name: string, ...rest: unknown[]): void {
588-
if (this._registeredTools[name]) {
589-
throw new Error(`Tool ${name} is already registered`);
651+
this._setTool(name, rest, false);
652+
}
653+
654+
/**
655+
* Updates a zero-argument tool `name`, which will run the given function when the client calls it.
656+
*/
657+
updateTool(name: string, cb: ToolCallback): void;
658+
659+
/**
660+
* Updates a zero-argument tool `name` (with a description) which will run the given function when the client calls it.
661+
*/
662+
updateTool(name: string, description: string, cb: ToolCallback): void;
663+
664+
/**
665+
* Updates a tool `name` accepting the given arguments, which must be an object containing named properties associated with Zod schemas. When the client calls it, the function will be run with the parsed and validated arguments.
666+
*/
667+
updateTool<Args extends ZodRawShape>(
668+
name: string,
669+
paramsSchema: Args,
670+
cb: ToolCallback<Args>,
671+
): void;
672+
673+
/**
674+
* Updates a tool `name` (with a description) accepting the given arguments, which must be an object containing named properties associated with Zod schemas. When the client calls it, the function will be run with the parsed and validated arguments.
675+
*/
676+
updateTool<Args extends ZodRawShape>(
677+
name: string,
678+
description: string,
679+
paramsSchema: Args,
680+
cb: ToolCallback<Args>,
681+
): void;
682+
683+
updateTool(name: string, ...rest: unknown[]): void {
684+
this._setTool(name, rest, true);
685+
}
686+
687+
private _setTool(
688+
name: string,
689+
rest: unknown[],
690+
update: boolean
691+
): void {
692+
if (update) {
693+
if (!this._registeredTools[name]) {
694+
throw new Error(`Tool ${name} is not registered`);
695+
}
696+
} else {
697+
if (this._registeredTools[name]) {
698+
throw new Error(`Tool ${name} is already registered`);
699+
}
590700
}
591701

592702
let description: string | undefined;
@@ -659,8 +769,55 @@ export class McpServer {
659769
): void;
660770

661771
prompt(name: string, ...rest: unknown[]): void {
662-
if (this._registeredPrompts[name]) {
663-
throw new Error(`Prompt ${name} is already registered`);
772+
this._setPrompt(name, rest, false);
773+
}
774+
775+
/**
776+
* Updates a zero-argument prompt `name`, which will run the given function when the client calls it.
777+
*/
778+
updatePrompt(name: string, cb: PromptCallback): void;
779+
780+
/**
781+
* Updates a zero-argument prompt `name` (with a description) which will run the given function when the client calls it.
782+
*/
783+
updatePrompt(name: string, description: string, cb: PromptCallback): void;
784+
785+
/**
786+
* Updates a prompt `name` accepting the given arguments, which must be an object containing named properties associated with Zod schemas. When the client calls it, the function will be run with the parsed and validated arguments.
787+
*/
788+
updatePrompt<Args extends PromptArgsRawShape>(
789+
name: string,
790+
argsSchema: Args,
791+
cb: PromptCallback<Args>,
792+
): void;
793+
794+
/**
795+
* Updates a prompt `name` (with a description) accepting the given arguments, which must be an object containing named properties associated with Zod schemas. When the client calls it, the function will be run with the parsed and validated arguments.
796+
*/
797+
updatePrompt<Args extends PromptArgsRawShape>(
798+
name: string,
799+
description: string,
800+
argsSchema: Args,
801+
cb: PromptCallback<Args>,
802+
): void;
803+
804+
updatePrompt(name: string, ...rest: unknown[]): void {
805+
this._setPrompt(name, rest, true);
806+
}
807+
808+
private _setPrompt(
809+
name: string,
810+
rest: unknown[],
811+
update: boolean
812+
): void {
813+
if (update) {
814+
if (!this._registeredPrompts[name]) {
815+
throw new Error(`Prompt ${name} is not registered`);
816+
}
817+
} else {
818+
if (this._registeredPrompts[name]) {
819+
throw new Error(`Prompt ${name} is already registered`);
820+
}
664821
}
665822

666823
let description: string | undefined;

0 commit comments

Comments
 (0)