|
1 | | -import { ResourceTemplate, type McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; |
2 | | -import { type McpResource } from './server'; |
| 1 | +import { |
| 2 | + ResourceTemplate, |
| 3 | + type McpServer, |
| 4 | + type ResourceMetadata, |
| 5 | + type CompleteResourceTemplateCallback |
| 6 | +} from '@modelcontextprotocol/sdk/server/mcp.js'; |
| 7 | +import { type GlobalOptions } from './options'; |
3 | 8 | import { listAllCombinations, listIncrementalCombinations, splitUri } from './server.helpers'; |
4 | 9 |
|
| 10 | +/** |
| 11 | + * A tool registered with the MCP server. |
| 12 | + * |
| 13 | + * @note Use of `any` here is intentional as part of a pass-through policy around |
| 14 | + * `inputSchema`. Input schemas are actually reconstructed as part of the |
| 15 | + * tools-as-plugins architecture to help guarantee that a minimal tool schema is |
| 16 | + * always available and minimally valid. |
| 17 | + * |
| 18 | + * 0. `name` `{string}`: Name of the tool. |
| 19 | + * 1. `schema` `{Object}`: Descriptions and schemas provided to allow parameter input and outputs in a standardized format. |
| 20 | + * - `schema.description` `{string}`: Concise description of functionality for the tool. |
| 21 | + * - `schema.inputSchema` `{*}`: Internally, a raw Zod schema. Externally, a JSON or raw Zod schema. External tools are |
| 22 | + * converted to Zod for user convenience. |
| 23 | + * 2. `handler` `{Function}`: Tool handler function for returning content. |
| 24 | + */ |
| 25 | +type McpTool = [ |
| 26 | + name: string, |
| 27 | + schema: { |
| 28 | + description: string; |
| 29 | + inputSchema: any; |
| 30 | + }, |
| 31 | + handler: (arg?: unknown) => any | Promise<any> |
| 32 | +]; |
| 33 | + |
| 34 | +/** |
| 35 | + * A function that creates a tool registered with the MCP server. |
| 36 | + */ |
| 37 | +type McpToolCreator = ((options?: GlobalOptions) => McpTool) & { toolName?: string }; |
| 38 | + |
| 39 | +/** |
| 40 | + * Configuration for a generated metadata MCP resource. |
| 41 | + * |
| 42 | + * @interface McpResourceMetadataMetaConfig |
| 43 | + * |
| 44 | + * @property [uri] - Override URI for the meta-resource. (e.g., `test://lorem/meta`, `test://ipsum/meta{?var}`). |
| 45 | + * @property [name] - Registered name for the meta-resource (defaults to `{primaryName}-meta`). |
| 46 | + * @property [title] - Title shown for the meta-resource in listings and generated Markdown. |
| 47 | + * @property [description] - Description for the meta-resource in listings and generated Markdown. |
| 48 | + * @property [searchFields] - Query parameter names included on the meta-URI template for completion. |
| 49 | + * - If an empty array is provided the meta-resource uses a static URI, no template |
| 50 | + * - If omitted the search fields are inferred from the `uri` or the primary resource template. |
| 51 | + * @property [mimeType] - MIME type of the meta-resource body. Acceptable values are: |
| 52 | + * - 'text/markdown' |
| 53 | + * - 'application/json' |
| 54 | + * @property [metaHandler] - A custom handler for the meta-resource. It accepts an optional object as its |
| 55 | + * argument for passing parameters and returns a serialized value to the MCP client. A default fallback |
| 56 | + * async handler is used if none is provided. |
| 57 | + */ |
| 58 | +interface McpResourceMetadataMetaConfig { |
| 59 | + uri?: string; |
| 60 | + name?: string; |
| 61 | + title?: string; |
| 62 | + description?: string; |
| 63 | + searchFields?: string[] | undefined; |
| 64 | + mimeType?: 'text/markdown' | 'application/json'; |
| 65 | + metaHandler?: (params: Record<string, string> | undefined) => Promise<unknown> | unknown; |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * A resource metadata configuration for the MCP server. |
| 70 | + * |
| 71 | + * @property registerAllSearchCombinations - Whether to register all search combinations for the resource. |
| 72 | + * @property metaConfig - Optional configuration for generating a metadata resource. Being defined |
| 73 | + * (e.g. `{ metadata: { metaConfig: {} }}`) means a meta-resource will be generated for the related MCP resource. |
| 74 | + * @property complete - Callback functions for resource completion. |
| 75 | + */ |
| 76 | +interface McpResourceMetadata { |
| 77 | + registerAllSearchCombinations?: boolean | undefined; |
| 78 | + metaConfig?: McpResourceMetadataMetaConfig; |
| 79 | + complete?: { |
| 80 | + [key: string]: CompleteResourceTemplateCallback; |
| 81 | + } | undefined; |
| 82 | + [key: string]: unknown; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * A resource registered with the MCP server. |
| 87 | + * |
| 88 | + * 0. `name`: Registered name of the resource. |
| 89 | + * 1. `uriOrTemplate`: URI string or template. {@link ResourceTemplate} |
| 90 | + * 2. `config`: Resource configuration metadata. {@link ResourceMetadata} |
| 91 | + * 3. `handler`: Resource handler function. |
| 92 | + * 4. `metadata`: Optional **internal metadata** object, not used by the standard MCP SDK |
| 93 | + * resource registry. {@link McpResourceMetadata} |
| 94 | + */ |
| 95 | +type McpResource = [ |
| 96 | + name: string, |
| 97 | + uriOrTemplate: string | ResourceTemplate, |
| 98 | + config: ResourceMetadata, |
| 99 | + handler: (...args: any[]) => any | Promise<any>, |
| 100 | + metadata?: McpResourceMetadata | undefined |
| 101 | +]; |
| 102 | + |
| 103 | +/** |
| 104 | + * A function that creates a resource registered with the MCP server. |
| 105 | + */ |
| 106 | +type McpResourceCreator = ((options?: GlobalOptions) => McpResource) & { resourceName?: string }; |
| 107 | + |
5 | 108 | /** |
6 | 109 | * Register an MCP resource. |
7 | 110 | * |
@@ -98,4 +201,12 @@ const registerResource = ( |
98 | 201 | server.registerResource(name, uriOrTemplate as any, config, callback); |
99 | 202 | }; |
100 | 203 |
|
101 | | -export { registerResource }; |
| 204 | +export { |
| 205 | + registerResource, |
| 206 | + type McpTool, |
| 207 | + type McpToolCreator, |
| 208 | + type McpResourceMetadataMetaConfig, |
| 209 | + type McpResourceMetadata, |
| 210 | + type McpResource, |
| 211 | + type McpResourceCreator |
| 212 | +}; |
0 commit comments