tmcp 1.18.1 → 1.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +264 -42
- package/package.json +18 -2
- package/src/index.js +251 -12
- package/src/internal/internal.d.ts +55 -0
- package/src/prompt.js +23 -0
- package/src/resource.js +20 -0
- package/src/template.js +27 -0
- package/src/tool.js +23 -0
- package/src/types/index.d.ts +1060 -79
- package/src/types/index.d.ts.map +33 -8
- package/src/validation/version.js +2 -2
package/src/index.js
CHANGED
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
* @import { JSONRPCRequest, JSONRPCParams } from "json-rpc-2.0";
|
|
6
6
|
* @import { ExtractURITemplateVariables } from "./internal/uri-template.js";
|
|
7
7
|
* @import { CallToolResult as CallToolResultType, ReadResourceResult as ReadResourceResultType, GetPromptResult as GetPromptResultType, ServerInfo as ServerInfoType, ClientCapabilities as ClientCapabilitiesType, JSONRPCRequest as JSONRPCRequestType, JSONRPCResponse, CreateMessageRequestParams as CreateMessageRequestParamsType, CreateMessageResult as CreateMessageResultType, Resource as ResourceType, LoggingLevel as LoggingLevelType, ToolAnnotations, ClientInfo as ClientInfoType, ElicitResult as ElicitResultType, Icons as IconsType, JSONRPCMessage, InitializeResult as InitializeResultType, ListToolsResult as ListToolsResultType, ListPromptsResult as ListPromptsResultType, ListResourceTemplatesResult as ListResourceTemplatesResultType, ListResourcesResult as ListResourcesResultType, CompleteResult as CompleteResultType } from "./validation/index.js";
|
|
8
|
-
* @import { Tool, Completion, Prompt, StoredResource, ServerOptions, SubscriptionsKeys, ChangedArgs, McpEvents } from "./internal/internal.js";
|
|
8
|
+
* @import { Tool, Completion, Prompt, StoredResource, ServerOptions, SubscriptionsKeys, ChangedArgs, McpEvents, AllSame, TemplateOptions } from "./internal/internal.js";
|
|
9
|
+
* @import { CreatedTool, ToolOptions, CreatedPrompt, PromptOptions, CreatedResource, CreatedTemplate, ResourceOptions } from "./internal/internal.js";
|
|
9
10
|
*/
|
|
10
11
|
import { JSONRPCClient, JSONRPCServer } from 'json-rpc-2.0';
|
|
11
12
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
@@ -667,7 +668,7 @@ export class McpServer {
|
|
|
667
668
|
// Add static resources
|
|
668
669
|
for (const [
|
|
669
670
|
uri,
|
|
670
|
-
{ description, name, title, icons, ...resource },
|
|
671
|
+
{ description, name, title, icons, mimeType, ...resource },
|
|
671
672
|
] of this.#resources) {
|
|
672
673
|
if (!resource.template) {
|
|
673
674
|
if (
|
|
@@ -680,6 +681,7 @@ export class McpServer {
|
|
|
680
681
|
title: title || description,
|
|
681
682
|
description,
|
|
682
683
|
uri,
|
|
684
|
+
mimeType,
|
|
683
685
|
icons,
|
|
684
686
|
});
|
|
685
687
|
} else if (resource.list_resources) {
|
|
@@ -726,6 +728,7 @@ export class McpServer {
|
|
|
726
728
|
name,
|
|
727
729
|
title,
|
|
728
730
|
template,
|
|
731
|
+
mimeType,
|
|
729
732
|
icons,
|
|
730
733
|
enabled,
|
|
731
734
|
},
|
|
@@ -741,6 +744,7 @@ export class McpServer {
|
|
|
741
744
|
icons,
|
|
742
745
|
title: title || description,
|
|
743
746
|
description,
|
|
747
|
+
mimeType,
|
|
744
748
|
uriTemplate: uri,
|
|
745
749
|
};
|
|
746
750
|
},
|
|
@@ -860,6 +864,119 @@ export class McpServer {
|
|
|
860
864
|
}
|
|
861
865
|
}
|
|
862
866
|
|
|
867
|
+
/**
|
|
868
|
+
* Use the `defineTool` utility to create a reusable tool and pass it to this method to add it to the server.
|
|
869
|
+
* @template {Array<CreatedTool<any, any>>} T
|
|
870
|
+
* @template {T extends Array<CreatedTool<infer TSchema, infer TOutputSchema>> ? AllSame<TSchema, StandardSchema | undefined> extends true ? AllSame<TOutputSchema, StandardSchema | undefined> extends true ? T : never : never : never} U
|
|
871
|
+
* @param {T & NoInfer<U>} tools
|
|
872
|
+
*/
|
|
873
|
+
tools(tools) {
|
|
874
|
+
for (const tool of tools) {
|
|
875
|
+
this.tool(
|
|
876
|
+
{
|
|
877
|
+
name: tool.name,
|
|
878
|
+
description: tool.description,
|
|
879
|
+
title: tool.title,
|
|
880
|
+
schema: tool.schema,
|
|
881
|
+
outputSchema: tool.outputSchema,
|
|
882
|
+
annotations: tool.annotations,
|
|
883
|
+
enabled: tool.enabled,
|
|
884
|
+
icons: tool.icons,
|
|
885
|
+
_meta: tool._meta,
|
|
886
|
+
},
|
|
887
|
+
// @ts-expect-error typescript doesn't know about execute because of an egregious hack to prevent it
|
|
888
|
+
// from showing in intellisense when declaring a tool inline
|
|
889
|
+
tool.execute,
|
|
890
|
+
);
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
/**
|
|
895
|
+
* Use the `definePrompt` utility to create a reusable tool and pass it to this method to add it to the server.
|
|
896
|
+
* @template {Array<CreatedPrompt<any>>} T
|
|
897
|
+
* @template {T extends Array<CreatedPrompt<infer TSchema>> ? AllSame<TSchema, StandardSchema | undefined> extends true ? T : never : never} U
|
|
898
|
+
* @param {T & NoInfer<U>} prompts
|
|
899
|
+
*/
|
|
900
|
+
prompts(prompts) {
|
|
901
|
+
for (const prompt of prompts) {
|
|
902
|
+
this.prompt(
|
|
903
|
+
{
|
|
904
|
+
name: prompt.name,
|
|
905
|
+
description: prompt.description,
|
|
906
|
+
title: prompt.title,
|
|
907
|
+
schema: prompt.schema,
|
|
908
|
+
complete: prompt.complete,
|
|
909
|
+
enabled: prompt.enabled,
|
|
910
|
+
icons: prompt.icons,
|
|
911
|
+
},
|
|
912
|
+
// @ts-expect-error typescript doesn't know about execute because of an egregious hack to prevent it
|
|
913
|
+
// from showing in intellisense when declaring a tool inline
|
|
914
|
+
prompt.execute,
|
|
915
|
+
);
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
/**
|
|
920
|
+
* Use the `defineResource` utility to create a reusable resource and pass it to this method to add it to the server.
|
|
921
|
+
*
|
|
922
|
+
* @param {CreatedResource[]} resources
|
|
923
|
+
*/
|
|
924
|
+
resources(resources) {
|
|
925
|
+
for (const resource of resources) {
|
|
926
|
+
this.resource(
|
|
927
|
+
{
|
|
928
|
+
name: resource.name,
|
|
929
|
+
description: resource.description,
|
|
930
|
+
title: resource.title,
|
|
931
|
+
uri: resource.uri,
|
|
932
|
+
mimeType: resource.mimeType,
|
|
933
|
+
enabled: resource.enabled,
|
|
934
|
+
icons: resource.icons,
|
|
935
|
+
},
|
|
936
|
+
// @ts-expect-error typescript doesn't know about execute because of an egregious hack to prevent it
|
|
937
|
+
// from showing in intellisense when declaring a tool inline
|
|
938
|
+
resource.execute,
|
|
939
|
+
);
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
/**
|
|
944
|
+
* Use the `defineTemplate` utility to create a reusable template and pass it to this method to add it to the server.
|
|
945
|
+
*
|
|
946
|
+
* @param {CreatedTemplate<any>[]} templates
|
|
947
|
+
*/
|
|
948
|
+
templates(templates) {
|
|
949
|
+
for (const template of templates) {
|
|
950
|
+
this.template(
|
|
951
|
+
{
|
|
952
|
+
name: template.name,
|
|
953
|
+
description: template.description,
|
|
954
|
+
title: template.title,
|
|
955
|
+
uri: template.uri,
|
|
956
|
+
mimeType: template.mimeType,
|
|
957
|
+
complete: template.complete,
|
|
958
|
+
list: template.list,
|
|
959
|
+
enabled: template.enabled,
|
|
960
|
+
icons: template.icons,
|
|
961
|
+
},
|
|
962
|
+
// @ts-expect-error typescript doesn't know about execute because of an egregious hack to prevent it
|
|
963
|
+
// from showing in intellisense when declaring a tool inline
|
|
964
|
+
template.execute,
|
|
965
|
+
);
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
/**
|
|
970
|
+
* Add a tool to the server. If you want to receive any input you need to provide a schema. The schema needs to be a valid Standard Schema V1 schema and needs to be an Object with the properties you need,
|
|
971
|
+
* Use the description and title to help the LLM to understand what the tool does and when to use it. If you provide an outputSchema, you need to return a structuredContent that matches the schema.
|
|
972
|
+
*
|
|
973
|
+
* Tools will be invoked by the LLM when it thinks it needs to use them, you can use the annotations to provide additional information about the tool, like what it does, how to use it, etc.
|
|
974
|
+
* @template {StandardSchema | undefined} [TSchema=undefined]
|
|
975
|
+
* @template {StandardSchema | undefined} [TOutputSchema=undefined]
|
|
976
|
+
* @overload
|
|
977
|
+
* @param {CreatedTool<TSchema, TOutputSchema>} tool_or_options
|
|
978
|
+
* @returns {void}
|
|
979
|
+
*/
|
|
863
980
|
/**
|
|
864
981
|
* Add a tool to the server. If you want to receive any input you need to provide a schema. The schema needs to be a valid Standard Schema V1 schema and needs to be an Object with the properties you need,
|
|
865
982
|
* Use the description and title to help the LLM to understand what the tool does and when to use it. If you provide an outputSchema, you need to return a structuredContent that matches the schema.
|
|
@@ -867,8 +984,20 @@ export class McpServer {
|
|
|
867
984
|
* Tools will be invoked by the LLM when it thinks it needs to use them, you can use the annotations to provide additional information about the tool, like what it does, how to use it, etc.
|
|
868
985
|
* @template {StandardSchema | undefined} [TSchema=undefined]
|
|
869
986
|
* @template {StandardSchema | undefined} [TOutputSchema=undefined]
|
|
870
|
-
* @
|
|
987
|
+
* @overload
|
|
988
|
+
* @param {ToolOptions<TSchema, TOutputSchema>} tool_or_options
|
|
871
989
|
* @param {TSchema extends undefined ? (()=>Promise<CallToolResult<TOutputSchema extends undefined ? undefined : StandardSchemaV1.InferInput<TOutputSchema extends undefined ? never : TOutputSchema>>> | CallToolResult<TOutputSchema extends undefined ? undefined : StandardSchemaV1.InferInput<TOutputSchema extends undefined ? never : TOutputSchema>>) : ((input: StandardSchemaV1.InferInput<TSchema extends undefined ? never : TSchema>) => Promise<CallToolResult<TOutputSchema extends undefined ? undefined : StandardSchemaV1.InferInput<TOutputSchema extends undefined ? never : TOutputSchema>>> | CallToolResult<TOutputSchema extends undefined ? undefined : StandardSchemaV1.InferInput<TOutputSchema extends undefined ? never : TOutputSchema>>)} execute
|
|
990
|
+
* @returns {void}
|
|
991
|
+
* */
|
|
992
|
+
/**
|
|
993
|
+
* Add a tool to the server. If you want to receive any input you need to provide a schema. The schema needs to be a valid Standard Schema V1 schema and needs to be an Object with the properties you need,
|
|
994
|
+
* Use the description and title to help the LLM to understand what the tool does and when to use it. If you provide an outputSchema, you need to return a structuredContent that matches the schema.
|
|
995
|
+
*
|
|
996
|
+
* Tools will be invoked by the LLM when it thinks it needs to use them, you can use the annotations to provide additional information about the tool, like what it does, how to use it, etc.
|
|
997
|
+
* @template {StandardSchema | undefined} [TSchema=undefined]
|
|
998
|
+
* @template {StandardSchema | undefined} [TOutputSchema=undefined]
|
|
999
|
+
* @param {CreatedTool<TSchema, TOutputSchema> | ToolOptions<TSchema, TOutputSchema>} tool_or_options
|
|
1000
|
+
* @param {undefined | TSchema extends undefined ? (()=>Promise<CallToolResult<TOutputSchema extends undefined ? undefined : StandardSchemaV1.InferInput<TOutputSchema extends undefined ? never : TOutputSchema>>> | CallToolResult<TOutputSchema extends undefined ? undefined : StandardSchemaV1.InferInput<TOutputSchema extends undefined ? never : TOutputSchema>>) : ((input: StandardSchemaV1.InferInput<TSchema extends undefined ? never : TSchema>) => Promise<CallToolResult<TOutputSchema extends undefined ? undefined : StandardSchemaV1.InferInput<TOutputSchema extends undefined ? never : TOutputSchema>>> | CallToolResult<TOutputSchema extends undefined ? undefined : StandardSchemaV1.InferInput<TOutputSchema extends undefined ? never : TOutputSchema>>)} [execute]
|
|
872
1001
|
*/
|
|
873
1002
|
tool(
|
|
874
1003
|
{
|
|
@@ -881,9 +1010,15 @@ export class McpServer {
|
|
|
881
1010
|
enabled,
|
|
882
1011
|
icons,
|
|
883
1012
|
_meta,
|
|
1013
|
+
...tool_or_options
|
|
884
1014
|
},
|
|
885
1015
|
execute,
|
|
886
1016
|
) {
|
|
1017
|
+
if ('execute' in tool_or_options) {
|
|
1018
|
+
// @ts-expect-error typescript doesn't know about execute because of an egregious hack to prevent it
|
|
1019
|
+
// from showing in intellisense when declaring a tool inline
|
|
1020
|
+
execute = tool_or_options.execute;
|
|
1021
|
+
}
|
|
887
1022
|
this.#notify_tools_list_changed();
|
|
888
1023
|
this.#tools.set(name, {
|
|
889
1024
|
description,
|
|
@@ -891,12 +1026,24 @@ export class McpServer {
|
|
|
891
1026
|
enabled,
|
|
892
1027
|
schema,
|
|
893
1028
|
outputSchema,
|
|
894
|
-
execute,
|
|
1029
|
+
execute: /** @type {NonNullable<typeof execute>} */ (execute),
|
|
895
1030
|
annotations,
|
|
896
1031
|
icons,
|
|
897
1032
|
_meta,
|
|
898
1033
|
});
|
|
899
1034
|
}
|
|
1035
|
+
|
|
1036
|
+
/**
|
|
1037
|
+
* Add a prompt to the server. Prompts are used to provide the user with pre-defined messages that adds context to the LLM.
|
|
1038
|
+
* Use the description and title to help the user to understand what the prompt does and when to use it.
|
|
1039
|
+
*
|
|
1040
|
+
* A prompt can also have a schema that defines the input it expects, the user will be prompted to enter the inputs you request. It can also have a complete function
|
|
1041
|
+
* for each input that will be used to provide completions for the user.
|
|
1042
|
+
* @template {StandardSchema | undefined} [TSchema=undefined]
|
|
1043
|
+
* @overload
|
|
1044
|
+
* @param {CreatedPrompt<TSchema>} prompt_or_options
|
|
1045
|
+
* @returns {void}
|
|
1046
|
+
*/
|
|
900
1047
|
/**
|
|
901
1048
|
* Add a prompt to the server. Prompts are used to provide the user with pre-defined messages that adds context to the LLM.
|
|
902
1049
|
* Use the description and title to help the user to understand what the prompt does and when to use it.
|
|
@@ -904,13 +1051,39 @@ export class McpServer {
|
|
|
904
1051
|
* A prompt can also have a schema that defines the input it expects, the user will be prompted to enter the inputs you request. It can also have a complete function
|
|
905
1052
|
* for each input that will be used to provide completions for the user.
|
|
906
1053
|
* @template {StandardSchema | undefined} [TSchema=undefined]
|
|
907
|
-
* @
|
|
1054
|
+
* @overload
|
|
1055
|
+
* @param {PromptOptions<TSchema>} prompt_or_options
|
|
908
1056
|
* @param {TSchema extends undefined ? (()=>Promise<GetPromptResult> | GetPromptResult) : (input: StandardSchemaV1.InferInput<TSchema extends undefined ? never : TSchema>) => Promise<GetPromptResult> | GetPromptResult} execute
|
|
1057
|
+
* @returns {void}
|
|
1058
|
+
* */
|
|
1059
|
+
/**
|
|
1060
|
+
* Add a prompt to the server. Prompts are used to provide the user with pre-defined messages that adds context to the LLM.
|
|
1061
|
+
* Use the description and title to help the user to understand what the prompt does and when to use it.
|
|
1062
|
+
*
|
|
1063
|
+
* A prompt can also have a schema that defines the input it expects, the user will be prompted to enter the inputs you request. It can also have a complete function
|
|
1064
|
+
* for each input that will be used to provide completions for the user.
|
|
1065
|
+
* @template {StandardSchema | undefined} [TSchema=undefined]
|
|
1066
|
+
* @param {CreatedPrompt<TSchema> | PromptOptions<TSchema>} options
|
|
1067
|
+
* @param {TSchema extends undefined ? (()=>Promise<GetPromptResult> | GetPromptResult) : (input: StandardSchemaV1.InferInput<TSchema extends undefined ? never : TSchema>) => Promise<GetPromptResult> | GetPromptResult} [execute]
|
|
909
1068
|
*/
|
|
910
1069
|
prompt(
|
|
911
|
-
{
|
|
1070
|
+
{
|
|
1071
|
+
name,
|
|
1072
|
+
description,
|
|
1073
|
+
title,
|
|
1074
|
+
schema,
|
|
1075
|
+
complete,
|
|
1076
|
+
enabled,
|
|
1077
|
+
icons,
|
|
1078
|
+
...prompt_or_options
|
|
1079
|
+
},
|
|
912
1080
|
execute,
|
|
913
1081
|
) {
|
|
1082
|
+
if ('execute' in prompt_or_options) {
|
|
1083
|
+
// @ts-expect-error typescript doesn't know about execute because of an egregious hack to prevent it
|
|
1084
|
+
// from showing in intellisense when declaring a tool inline
|
|
1085
|
+
execute = prompt_or_options.execute;
|
|
1086
|
+
}
|
|
914
1087
|
if (complete) {
|
|
915
1088
|
this.#completions['ref/prompt'].set(name, complete);
|
|
916
1089
|
}
|
|
@@ -919,7 +1092,7 @@ export class McpServer {
|
|
|
919
1092
|
description,
|
|
920
1093
|
title,
|
|
921
1094
|
schema,
|
|
922
|
-
execute,
|
|
1095
|
+
execute: /** @type {NonNullable<typeof execute>} */ (execute),
|
|
923
1096
|
enabled,
|
|
924
1097
|
icons,
|
|
925
1098
|
});
|
|
@@ -941,16 +1114,49 @@ export class McpServer {
|
|
|
941
1114
|
/**
|
|
942
1115
|
* Add a resource to the server. Resources are added manually to the context by the user to provide the LLM with additional context.
|
|
943
1116
|
* Use the description and title to help the user to understand what the resource is.
|
|
944
|
-
* @
|
|
1117
|
+
* @overload
|
|
1118
|
+
* @param {CreatedResource} resource_or_options
|
|
1119
|
+
* @returns {void}
|
|
1120
|
+
*/
|
|
1121
|
+
/**
|
|
1122
|
+
* Add a resource to the server. Resources are added manually to the context by the user to provide the LLM with additional context.
|
|
1123
|
+
* Use the description and title to help the user to understand what the resource is.
|
|
1124
|
+
* @overload
|
|
1125
|
+
* @param {ResourceOptions} resource_or_options
|
|
945
1126
|
* @param {(uri: string) => Promise<ReadResourceResult> | ReadResourceResult} execute
|
|
1127
|
+
* @returns {void}
|
|
946
1128
|
*/
|
|
947
|
-
|
|
1129
|
+
/**
|
|
1130
|
+
* Add a resource to the server. Resources are added manually to the context by the user to provide the LLM with additional context.
|
|
1131
|
+
* Use the description and title to help the user to understand what the resource is.
|
|
1132
|
+
* @param {CreatedResource | ResourceOptions} resource_or_options
|
|
1133
|
+
* @param {(uri: string) => Promise<ReadResourceResult> | ReadResourceResult} [execute]
|
|
1134
|
+
*/
|
|
1135
|
+
resource(
|
|
1136
|
+
{
|
|
1137
|
+
name,
|
|
1138
|
+
description,
|
|
1139
|
+
title,
|
|
1140
|
+
uri,
|
|
1141
|
+
mimeType,
|
|
1142
|
+
enabled,
|
|
1143
|
+
icons,
|
|
1144
|
+
...resource_or_options
|
|
1145
|
+
},
|
|
1146
|
+
execute,
|
|
1147
|
+
) {
|
|
1148
|
+
if ('execute' in resource_or_options) {
|
|
1149
|
+
// @ts-expect-error typescript doesn't know about execute because of an egregious hack to prevent it
|
|
1150
|
+
// from showing in intellisense when declaring a tool inline
|
|
1151
|
+
execute = resource_or_options.execute;
|
|
1152
|
+
}
|
|
948
1153
|
this.#resource({
|
|
949
1154
|
name,
|
|
950
1155
|
description,
|
|
951
1156
|
title,
|
|
952
1157
|
uri,
|
|
953
|
-
|
|
1158
|
+
mimeType,
|
|
1159
|
+
execute: /** @type {NonNullable<typeof execute>} */ (execute),
|
|
954
1160
|
enabled,
|
|
955
1161
|
icons,
|
|
956
1162
|
template: false,
|
|
@@ -964,8 +1170,33 @@ export class McpServer {
|
|
|
964
1170
|
* Use the description and title to help the user to understand what the resource is.
|
|
965
1171
|
* @template {string} TUri
|
|
966
1172
|
* @template {ExtractURITemplateVariables<TUri>} TVariables
|
|
967
|
-
* @
|
|
1173
|
+
* @overload
|
|
1174
|
+
* @param {CreatedTemplate<TUri>} template_or_options
|
|
1175
|
+
* @returns {void}
|
|
1176
|
+
*/
|
|
1177
|
+
/**
|
|
1178
|
+
* Add a resource template to the server. Resources are added manually to the context by the user to provide the LLM with additional context.
|
|
1179
|
+
* Resource templates are used to create resources dynamically based on a URI template. The URI template should be a valid URI template as defined in RFC 6570.
|
|
1180
|
+
* Resource templates can have a list method that returns a list of resources that match the template and a complete method that returns a list of resources given one of the template variables, this method will
|
|
1181
|
+
* be invoked to provide completions for the template variables to the user.
|
|
1182
|
+
* Use the description and title to help the user to understand what the resource is.
|
|
1183
|
+
* @template {string} TUri
|
|
1184
|
+
* @template {ExtractURITemplateVariables<TUri>} TVariables
|
|
1185
|
+
* @overload
|
|
1186
|
+
* @param {TemplateOptions<TUri>} template_or_options
|
|
968
1187
|
* @param {(uri: string, params: Record<TVariables, string | string[]>) => Promise<ReadResourceResult> | ReadResourceResult} execute
|
|
1188
|
+
* @returns {void}
|
|
1189
|
+
*/
|
|
1190
|
+
/**
|
|
1191
|
+
* Add a resource template to the server. Resources are added manually to the context by the user to provide the LLM with additional context.
|
|
1192
|
+
* Resource templates are used to create resources dynamically based on a URI template. The URI template should be a valid URI template as defined in RFC 6570.
|
|
1193
|
+
* Resource templates can have a list method that returns a list of resources that match the template and a complete method that returns a list of resources given one of the template variables, this method will
|
|
1194
|
+
* be invoked to provide completions for the template variables to the user.
|
|
1195
|
+
* Use the description and title to help the user to understand what the resource is.
|
|
1196
|
+
* @template {string} TUri
|
|
1197
|
+
* @template {ExtractURITemplateVariables<TUri>} TVariables
|
|
1198
|
+
* @param {CreatedTemplate<TUri> | TemplateOptions<TUri>} template_or_options
|
|
1199
|
+
* @param {(uri: string, params: Record<TVariables, string | string[]>) => Promise<ReadResourceResult> | ReadResourceResult} [execute]
|
|
969
1200
|
*/
|
|
970
1201
|
template(
|
|
971
1202
|
{
|
|
@@ -973,20 +1204,28 @@ export class McpServer {
|
|
|
973
1204
|
description,
|
|
974
1205
|
title,
|
|
975
1206
|
uri,
|
|
1207
|
+
mimeType,
|
|
976
1208
|
complete,
|
|
977
1209
|
list: list_resources,
|
|
978
1210
|
enabled,
|
|
979
1211
|
icons,
|
|
1212
|
+
...template_or_options
|
|
980
1213
|
},
|
|
981
1214
|
execute,
|
|
982
1215
|
) {
|
|
1216
|
+
if ('execute' in template_or_options) {
|
|
1217
|
+
// @ts-expect-error typescript doesn't know about execute because of an egregious hack to prevent it
|
|
1218
|
+
// from showing in intellisense when declaring a tool inline
|
|
1219
|
+
execute = template_or_options.execute;
|
|
1220
|
+
}
|
|
983
1221
|
this.#resource({
|
|
984
1222
|
name,
|
|
985
1223
|
enabled,
|
|
986
1224
|
description,
|
|
987
1225
|
title,
|
|
988
1226
|
uri,
|
|
989
|
-
|
|
1227
|
+
mimeType,
|
|
1228
|
+
execute: /** @type {NonNullable<typeof execute>} */ (execute),
|
|
990
1229
|
complete,
|
|
991
1230
|
icons,
|
|
992
1231
|
list_resources,
|
|
@@ -13,6 +13,59 @@ import {
|
|
|
13
13
|
ToolAnnotations,
|
|
14
14
|
Icons
|
|
15
15
|
} from '../validation/index.js';
|
|
16
|
+
import { ExtractURITemplateVariables } from './uri-template.js';
|
|
17
|
+
|
|
18
|
+
declare const created_tool: unique symbol;
|
|
19
|
+
declare const created_prompt: unique symbol;
|
|
20
|
+
declare const created_resource: unique symbol;
|
|
21
|
+
declare const created_template: unique symbol;
|
|
22
|
+
|
|
23
|
+
export type AllSame<T, U> = [T] extends [U] ? true : false;
|
|
24
|
+
|
|
25
|
+
export type PromptOptions<TSchema extends StandardSchemaV1 | undefined = undefined> = {
|
|
26
|
+
name: string;
|
|
27
|
+
description: string;
|
|
28
|
+
title?: string;
|
|
29
|
+
enabled?: ()=>boolean | Promise<boolean>;
|
|
30
|
+
schema?: StandardSchemaV1.InferInput<TSchema extends undefined ? never : TSchema> extends Record<string, unknown> ? TSchema : never;
|
|
31
|
+
complete?: NoInfer<TSchema extends undefined ? never : Partial<Record<keyof (StandardSchemaV1.InferInput<TSchema extends undefined ? never : TSchema>), Completion>>>
|
|
32
|
+
} & Icons
|
|
33
|
+
|
|
34
|
+
export type ToolOptions<TSchema extends StandardSchemaV1 | undefined = undefined, TOutputSchema extends StandardSchemaV1 | undefined = undefined> = {
|
|
35
|
+
name: string;
|
|
36
|
+
_meta?: Record<string, any>;
|
|
37
|
+
description: string;
|
|
38
|
+
title?: string;
|
|
39
|
+
enabled?: () => boolean | Promise<boolean>;
|
|
40
|
+
schema?: StandardSchemaV1.InferInput<TSchema extends undefined ? never : TSchema> extends Record<string, unknown> ? TSchema : never;
|
|
41
|
+
outputSchema?: StandardSchemaV1.InferOutput<TOutputSchema extends undefined ? never : TOutputSchema> extends Record<string, unknown> ? TOutputSchema : never;
|
|
42
|
+
annotations?: ToolAnnotations;
|
|
43
|
+
} & Icons;
|
|
44
|
+
|
|
45
|
+
export type ResourceOptions = {
|
|
46
|
+
name: string;
|
|
47
|
+
description: string;
|
|
48
|
+
title?: string;
|
|
49
|
+
uri: string;
|
|
50
|
+
mimeType?: string;
|
|
51
|
+
enabled?: ()=>boolean | Promise<boolean>;
|
|
52
|
+
} & Icons
|
|
53
|
+
|
|
54
|
+
export type TemplateOptions<TUri extends string = string, TVariables extends ExtractURITemplateVariables<TUri> = ExtractURITemplateVariables<TUri>> = {
|
|
55
|
+
name: string;
|
|
56
|
+
description: string;
|
|
57
|
+
title?: string;
|
|
58
|
+
mimeType?: string;
|
|
59
|
+
enabled?: ()=>boolean | Promise<boolean>;
|
|
60
|
+
uri: TUri;
|
|
61
|
+
complete?: NoInfer<TVariables extends never ? never : Partial<Record<TVariables, Completion>>>;
|
|
62
|
+
list?: () => Promise<Array<Resource>> | Array<Resource>
|
|
63
|
+
} & Icons
|
|
64
|
+
|
|
65
|
+
export type CreatedTool<TSchema extends StandardSchemaV1 | undefined = undefined, TOutputSchema extends StandardSchemaV1 | undefined = undefined> = ToolOptions<TSchema, TOutputSchema> & { [created_tool]: created_tool };
|
|
66
|
+
export type CreatedPrompt<TSchema extends StandardSchemaV1 | undefined = undefined> = PromptOptions<TSchema> & { [created_prompt]: created_prompt };
|
|
67
|
+
export type CreatedResource = ResourceOptions & { [created_resource]: created_resource };
|
|
68
|
+
export type CreatedTemplate<TUri extends string = string> = TemplateOptions<TUri> & { [created_template]: created_template };
|
|
16
69
|
|
|
17
70
|
export type Tool<TSchema extends StandardSchemaV1 = StandardSchemaV1<any>, TOutputSchema extends StandardSchemaV1 = StandardSchemaV1<any>> = {
|
|
18
71
|
description: string;
|
|
@@ -48,6 +101,7 @@ export type StoredResource =
|
|
|
48
101
|
name: string;
|
|
49
102
|
template: true;
|
|
50
103
|
title?: string;
|
|
104
|
+
mimeType?: string;
|
|
51
105
|
enabled?: () => boolean | Promise<boolean>;
|
|
52
106
|
list_resources?: () =>
|
|
53
107
|
| Promise<Array<Resource>>
|
|
@@ -62,6 +116,7 @@ export type StoredResource =
|
|
|
62
116
|
name: string;
|
|
63
117
|
template: false;
|
|
64
118
|
title?: string;
|
|
119
|
+
mimeType?: string;
|
|
65
120
|
enabled?: () => boolean | Promise<boolean>;
|
|
66
121
|
execute: (
|
|
67
122
|
uri: string,
|
package/src/prompt.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
|
+
* @import { PromptOptions, CreatedPrompt } from "./internal/internal.js";
|
|
4
|
+
* @import { GetPromptResult } from "./validation/index.js"
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Add a tool to the server. If you want to receive any input you need to provide a schema. The schema needs to be a valid Standard Schema V1 schema and needs to be an Object with the properties you need,
|
|
9
|
+
* Use the description and title to help the LLM to understand what the tool does and when to use it. If you provide an outputSchema, you need to return a structuredContent that matches the schema.
|
|
10
|
+
*
|
|
11
|
+
* @template {StandardSchemaV1 | undefined} [TSchema=undefined]
|
|
12
|
+
* @param {PromptOptions<TSchema>} options
|
|
13
|
+
* @param {TSchema extends undefined ? (()=>Promise<GetPromptResult> | GetPromptResult) : (input: StandardSchemaV1.InferInput<TSchema extends undefined ? never : TSchema>) => Promise<GetPromptResult> | GetPromptResult} execute
|
|
14
|
+
*/
|
|
15
|
+
export function definePrompt(options, execute) {
|
|
16
|
+
// eslint-disable-next-line jsdoc/no-undefined-types
|
|
17
|
+
return /** @type {CreatedPrompt<TSchema>} */ (
|
|
18
|
+
/** @type {unknown} */ ({
|
|
19
|
+
...options,
|
|
20
|
+
execute,
|
|
21
|
+
})
|
|
22
|
+
);
|
|
23
|
+
}
|
package/src/resource.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { ResourceOptions, CreatedResource } from "./internal/internal.js";
|
|
3
|
+
* @import { ReadResourceResult } from "./validation/index.js";
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Add a resource to the server. Resources are added manually to the context by the user to provide the LLM with additional context.
|
|
8
|
+
* Use the description and title to help the user to understand what the resource is. The mimeType can be used to indicate the type of content.
|
|
9
|
+
*
|
|
10
|
+
* @param {ResourceOptions} options
|
|
11
|
+
* @param {(uri: string) => Promise<ReadResourceResult> | ReadResourceResult} execute
|
|
12
|
+
*/
|
|
13
|
+
export function defineResource(options, execute) {
|
|
14
|
+
return /** @type {CreatedResource} */ (
|
|
15
|
+
/** @type {unknown} */ ({
|
|
16
|
+
...options,
|
|
17
|
+
execute,
|
|
18
|
+
})
|
|
19
|
+
);
|
|
20
|
+
}
|
package/src/template.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/* eslint-disable jsdoc/no-undefined-types */
|
|
2
|
+
/**
|
|
3
|
+
* @import { ExtractURITemplateVariables } from "./internal/uri-template.js";
|
|
4
|
+
* @import { TemplateOptions, CreatedTemplate } from "./internal/internal.js";
|
|
5
|
+
* @import { ReadResourceResult } from "./validation/index.js";
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Add a resource template to the server. Resources are added manually to the context by the user to provide the LLM with additional context.
|
|
10
|
+
* Resource templates are used to create resources dynamically based on a URI template. The URI template should be a valid URI template as defined in RFC 6570.
|
|
11
|
+
* Resource templates can have a list method that returns a list of resources that match the template and a complete method that returns a list of resources given one of the template variables, this method will
|
|
12
|
+
* be invoked to provide completions for the template variables to the user.
|
|
13
|
+
* Use the description and title to help the user to understand what the resource is. The mimeType can be used to indicate the type of content.
|
|
14
|
+
*
|
|
15
|
+
* @template {string} TUri
|
|
16
|
+
* @template {ExtractURITemplateVariables<TUri>} TVariables
|
|
17
|
+
* @param {TemplateOptions<TUri>} options
|
|
18
|
+
* @param {(uri: string, params: Record<TVariables, string | string[]>) => Promise<ReadResourceResult> | ReadResourceResult} execute
|
|
19
|
+
*/
|
|
20
|
+
export function defineTemplate(options, execute) {
|
|
21
|
+
return /** @type {CreatedTemplate<TUri>} */ (
|
|
22
|
+
/** @type {unknown} */ ({
|
|
23
|
+
...options,
|
|
24
|
+
execute,
|
|
25
|
+
})
|
|
26
|
+
);
|
|
27
|
+
}
|
package/src/tool.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
|
+
* @import { ToolOptions, CreatedTool } from "./internal/internal.js";
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Add a tool to the server. If you want to receive any input you need to provide a schema. The schema needs to be a valid Standard Schema V1 schema and needs to be an Object with the properties you need,
|
|
8
|
+
* Use the description and title to help the LLM to understand what the tool does and when to use it. If you provide an outputSchema, you need to return a structuredContent that matches the schema.
|
|
9
|
+
*
|
|
10
|
+
* @template {StandardSchemaV1 | undefined} [TSchema=undefined]
|
|
11
|
+
* @template {StandardSchemaV1 | undefined} [TOutputSchema=undefined]
|
|
12
|
+
* @param {ToolOptions<TSchema, TOutputSchema>} options
|
|
13
|
+
* @param {TSchema extends undefined ? (()=>Promise<import("./index.js").CallToolResult<TOutputSchema extends undefined ? undefined : StandardSchemaV1.InferInput<TOutputSchema extends undefined ? never : TOutputSchema>>> | import("./index.js").CallToolResult<TOutputSchema extends undefined ? undefined : StandardSchemaV1.InferInput<TOutputSchema extends undefined ? never : TOutputSchema>>) : ((input: StandardSchemaV1.InferInput<TSchema extends undefined ? never : TSchema>) => Promise<import("./index.js").CallToolResult<TOutputSchema extends undefined ? undefined : StandardSchemaV1.InferInput<TOutputSchema extends undefined ? never : TOutputSchema>>> | import("./index.js").CallToolResult<TOutputSchema extends undefined ? undefined : StandardSchemaV1.InferInput<TOutputSchema extends undefined ? never : TOutputSchema>>)} execute
|
|
14
|
+
*/
|
|
15
|
+
export function defineTool(options, execute) {
|
|
16
|
+
// eslint-disable-next-line jsdoc/no-undefined-types
|
|
17
|
+
return /** @type {CreatedTool<TSchema, TOutputSchema>} */ (
|
|
18
|
+
/** @type {unknown} */ ({
|
|
19
|
+
...options,
|
|
20
|
+
execute,
|
|
21
|
+
})
|
|
22
|
+
);
|
|
23
|
+
}
|