Skip to content

Commit 50ebb04

Browse files
authored
refactor(mcpSdk,server): pf-3836 sdk related typings (#198)
* mcpSdk, centralize sdk related typings * server, move sdk related typings to mcpSdk
1 parent 7318252 commit 50ebb04

18 files changed

Lines changed: 137 additions & 128 deletions

src/__tests__/options.context.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { z } from 'zod';
22
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
33
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
4-
import { runServer, type McpTool } from '../server';
4+
import { type McpTool } from '../mcpSdk';
5+
import { runServer } from '../server';
56
import { getOptions, setOptions } from '../options.context';
67
import { DEFAULT_OPTIONS } from '../options.defaults';
78

src/mcpSdk.ts

Lines changed: 114 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,110 @@
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';
38
import { listAllCombinations, listIncrementalCombinations, splitUri } from './server.helpers';
49

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+
5108
/**
6109
* Register an MCP resource.
7110
*
@@ -98,4 +201,12 @@ const registerResource = (
98201
server.registerResource(name, uriOrTemplate as any, config, callback);
99202
};
100203

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+
};

src/options.registry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type McpToolCreator, type McpResourceCreator } from './server';
1+
import { type McpToolCreator, type McpResourceCreator } from './mcpSdk';
22
import { usePatternFlyDocsTool } from './tool.patternFlyDocs';
33
import { searchPatternFlyDocsTool } from './tool.searchPatternFlyDocs';
44
import { patternFlyComponentsIndexResource } from './resource.patternFlyComponentsIndex';

src/resource.patternFlyComponentsIndex.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
ResourceTemplate,
33
type CompleteResourceTemplateCallback
44
} from '@modelcontextprotocol/sdk/server/mcp.js';
5-
import { type McpResource } from './server';
5+
import { type McpResource } from './mcpSdk';
66
import { memo } from './server.caching';
77
import { buildSearchString, stringJoin } from './server.helpers';
88
import { assertInput, assertInputStringLength } from './server.assertions';

src/resource.patternFlyContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type McpResource } from './server';
1+
import { type McpResource } from './mcpSdk';
22
import { stringJoin } from './server.helpers';
33
import { getOptions, runWithOptions } from './options.context';
44

src/resource.patternFlyDocsIndex.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
ResourceTemplate,
33
type CompleteResourceTemplateCallback
44
} from '@modelcontextprotocol/sdk/server/mcp.js';
5-
import { type McpResource } from './server';
5+
import { type McpResource } from './mcpSdk';
66
import { memo } from './server.caching';
77
import { buildSearchString, stringJoin } from './server.helpers';
88
import { assertInput, assertInputStringLength } from './server.assertions';

src/resource.patternFlyDocsTemplate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
type CompleteResourceTemplateCallback
44
} from '@modelcontextprotocol/sdk/server/mcp.js';
55
import { ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js';
6-
import { type McpResource } from './server';
6+
import { type McpResource } from './mcpSdk';
77
import { processDocsFunction } from './server.getResources';
88
import { stringJoin } from './server.helpers';
99
import { assertInput, assertInputStringLength } from './server.assertions';

src/resource.patternFlySchemasIndex.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
ResourceTemplate,
33
type CompleteResourceTemplateCallback
44
} from '@modelcontextprotocol/sdk/server/mcp.js';
5-
import { type McpResource } from './server';
5+
import { type McpResource } from './mcpSdk';
66
import { memo } from './server.caching';
77
import { stringJoin } from './server.helpers';
88
import { assertInput, assertInputStringLength } from './server.assertions';

src/resource.patternFlySchemasTemplate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
ResourceTemplate,
33
type CompleteResourceTemplateCallback
44
} from '@modelcontextprotocol/sdk/server/mcp.js';
5-
import { type McpResource } from './server';
5+
import { type McpResource } from './mcpSdk';
66
import { memo } from './server.caching';
77
import { assertInput, assertInputStringLength } from './server.assertions';
88
import { getOptions, runWithOptions } from './options.context';

src/server.resourceMeta.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
type McpResourceCreator,
88
type McpResourceMetadata,
99
type McpResourceMetadataMetaConfig
10-
} from './server';
10+
} from './mcpSdk';
1111
import {
1212
buildSearchString,
1313
isPlainObject,

0 commit comments

Comments
 (0)