-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathresource.patternFlyComponentsIndex.ts
More file actions
237 lines (209 loc) · 7.4 KB
/
Copy pathresource.patternFlyComponentsIndex.ts
File metadata and controls
237 lines (209 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import {
ResourceTemplate,
type CompleteResourceTemplateCallback
} from '@modelcontextprotocol/sdk/server/mcp.js';
import { type McpResource } from './mcpSdk';
import { memo } from './server.caching';
import { buildSearchString, stringJoin } from './server.helpers';
import { assertInput, assertInputStringLength } from './server.assertions';
import { getOptions, runWithOptions } from './options.context';
import { normalizeEnumeratedPatternFlyVersion } from './patternFly.helpers';
import { getPatternFlyMcpResources } from './patternFly.getResources';
import { filterPatternFly } from './patternFly.search';
import {
type PatternFlyListResourceResult,
type ExtendedCompleteResourceTemplateCallback
} from './resource.patternFlyDocsIndex';
import { paramCompletion } from './resource.helpers';
/**
* Name of the resource.
*/
const NAME = 'patternfly-components-index';
/**
* URI template for the resource.
*/
const URI_TEMPLATE = 'patternfly://components/index{?version,category}';
/**
* URI description for the resource.
*/
const URI_DESCRIPTION = `Filter by PatternFly version and category. ${URI_TEMPLATE}`;
/**
* Resource configuration.
*/
const CONFIG = {
title: 'PatternFly Components Index',
description: `A list of all PatternFly component names available for documentation retrieval. ${URI_DESCRIPTION}`,
mimeType: 'text/markdown',
annotations: {
priority: 0.9,
audience: ['assistant' as const]
}
};
/**
* List resources callback for the URI template.
*
* @note We use "byVersionComponentNames" instead of "byVersion" because it's specific to components.
* Docs resources don't necessarily contain all components.
*
* @returns {Promise<PatternFlyListResourceResult>} The list of available resources.
*/
const listResources = async () => {
const { availableVersions, byVersionComponentNames } = await getPatternFlyMcpResources.memo();
const resources: PatternFlyListResourceResult[] = [];
Array.from(byVersionComponentNames)
.filter(([version]) => availableVersions.includes(version))
.sort(([a], [b]) => b.localeCompare(a))
.forEach(([version]) => {
resources.push({
uri: `patternfly://components/index?version=${encodeURIComponent(version)}`,
mimeType: 'text/markdown',
name: `Component Index (${version})`,
description: `Component documentation entry point for PatternFly version ${version}. ${URI_DESCRIPTION}`
});
});
return {
resources: [
{
uri: 'patternfly://components/index',
mimeType: 'text/markdown',
name: 'Components Index (Latest)',
description: `Component documentation entry point for the latest PatternFly version. This is the recommended starting point. ${URI_DESCRIPTION}`
},
...resources.sort((a, b) => a.name.localeCompare(b.name))
]
};
};
/**
* Memoized version of listResources.
*/
listResources.memo = memo(listResources);
/**
* Category completion callback for the URI template.
*
* @param category - The value to filter-by/complete.
* @param context - The completion context containing arguments for the URI template.
* @returns The list of available categories, or an empty list.
*/
const uriCategoryComplete: ExtendedCompleteResourceTemplateCallback = async (category: string, context) => {
const { version, name } = context?.arguments || {};
const section = 'components';
const { categories } = await paramCompletion({ category, name, section, version });
return categories;
};
/**
* Memoized version of uriCategoryComplete.
*/
uriCategoryComplete.memo = memo(uriCategoryComplete);
/**
* Version completion callback for the URI template.
*
* @param version - The value to complete.
* @param context - The completion context containing arguments for the URI template.
* @returns The list of available versions, or an empty list.
*/
const uriVersionComplete: ExtendedCompleteResourceTemplateCallback = async (version: string, context) => {
const { category, name } = context?.arguments || {};
const section = 'components';
const { versions } = await paramCompletion({ category, name, section, version });
return versions;
};
/**
* Memoized version of uriVersionComplete.
*/
uriVersionComplete.memo = memo(uriVersionComplete);
/**
* Resource callback for the documentation index.
*
* @param passedUri - URI of the resource.
* @param variables - Variables for the resource.
* @param options - Options for the resource.
* @returns The resource contents.
*/
const resourceCallback = async (passedUri: URL, variables: Record<string, string | string[]>, options = getOptions()) => {
const { version, category } = variables || {};
const section = 'components';
if (version) {
assertInputStringLength(version, {
...options.minMax.inputStrings,
inputDisplayName: 'version'
});
}
if (category) {
assertInputStringLength(category, {
...options.minMax.inputStrings,
inputDisplayName: 'category'
});
}
const { availableVersions, latestVersion } = await getPatternFlyMcpResources.memo();
const normalizedVersion = await normalizeEnumeratedPatternFlyVersion.memo(version);
assertInput(
!version || Boolean(normalizedVersion),
`Invalid PatternFly version "${version?.trim()}". Available versions are: ${availableVersions.join(', ')}`
);
const updatedVersion = normalizedVersion || latestVersion;
const { byResource } = await filterPatternFly.memo({ version: updatedVersion, section, category });
const docsIndex = Array.from(byResource.values())
.sort((a, b) => a.name.localeCompare(b.name))
.map((resource, index) => {
const searchString = buildSearchString({ category }, { prefix: true, base: resource.uri });
return `${index + 1}. [${resource.name} (${updatedVersion})](${resource.uri}${searchString || ''})`;
});
return {
contents: [{
uri: passedUri?.toString(),
mimeType: 'text/markdown',
text: stringJoin.newline(
`# PatternFly Components Index for "${updatedVersion}"`,
'',
'',
...docsIndex || []
)
}]
};
};
/**
* Resource creator for the components index and metadata resources.
*
* @note The `metaConfig` determines if a metadata resource is generated. Remove
* the config to disable it.
*
* @param options - Global options
* @returns {McpResource} The resource definition tuple
*/
const patternFlyComponentsIndexResource = (options = getOptions()): McpResource => {
const list = async () => runWithOptions(options, async () => listResources.memo());
const complete: { [callback: string]: CompleteResourceTemplateCallback } = {
category: async (...args) => runWithOptions(options, async () => uriCategoryComplete.memo(...args)),
version: async (...args) => runWithOptions(options, async () => uriVersionComplete.memo(...args))
};
const callback: McpResource[3] = async (uri, variables) =>
runWithOptions(options, async () => resourceCallback(uri, variables));
return [
NAME,
new ResourceTemplate(URI_TEMPLATE, {
list,
complete
}),
CONFIG,
callback,
{
complete,
metaConfig: {
uri: 'patternfly://components/meta{?version}',
title: `${CONFIG.title} Metadata`,
description: 'Use these parameters to filter the list of PatternFly components.'
}
}
];
};
export {
patternFlyComponentsIndexResource,
listResources,
resourceCallback,
uriCategoryComplete,
uriVersionComplete,
NAME,
URI_TEMPLATE,
URI_DESCRIPTION,
CONFIG
};