9
9
ReadResourceRequestSchema ,
10
10
SubscribeRequestSchema ,
11
11
UnsubscribeRequestSchema ,
12
+ CompleteRequestSchema ,
13
+ ListResourceTemplatesRequestSchema ,
12
14
} from "@modelcontextprotocol/sdk/types.js" ;
13
15
import { ToolProtocol } from "../tools/BaseTool.js" ;
14
16
import { PromptProtocol } from "../prompts/BasePrompt.js" ;
@@ -59,7 +61,7 @@ export class MCPServer {
59
61
this . serverVersion = config . version ?? this . getDefaultVersion ( ) ;
60
62
61
63
logger . info (
62
- `Initializing MCP Server: ${ this . serverName } @${ this . serverVersion } `
64
+ `Initializing MCP Server: ${ this . serverName } @${ this . serverVersion } ` ,
63
65
) ;
64
66
65
67
this . toolLoader = new ToolLoader ( this . basePath ) ;
@@ -77,7 +79,7 @@ export class MCPServer {
77
79
prompts : { enabled : false } ,
78
80
resources : { enabled : false } ,
79
81
} ,
80
- }
82
+ } ,
81
83
) ;
82
84
83
85
this . setupHandlers ( ) ;
@@ -128,7 +130,7 @@ export class MCPServer {
128
130
this . server . setRequestHandler ( ListToolsRequestSchema , async ( ) => {
129
131
return {
130
132
tools : Array . from ( this . toolsMap . values ( ) ) . map (
131
- ( tool ) => tool . toolDefinition
133
+ ( tool ) => tool . toolDefinition ,
132
134
) ,
133
135
} ;
134
136
} ) ;
@@ -138,8 +140,8 @@ export class MCPServer {
138
140
if ( ! tool ) {
139
141
throw new Error (
140
142
`Unknown tool: ${ request . params . name } . Available tools: ${ Array . from (
141
- this . toolsMap . keys ( )
142
- ) . join ( ", " ) } `
143
+ this . toolsMap . keys ( ) ,
144
+ ) . join ( ", " ) } `,
143
145
) ;
144
146
}
145
147
@@ -154,7 +156,7 @@ export class MCPServer {
154
156
this . server . setRequestHandler ( ListPromptsRequestSchema , async ( ) => {
155
157
return {
156
158
prompts : Array . from ( this . promptsMap . values ( ) ) . map (
157
- ( prompt ) => prompt . promptDefinition
159
+ ( prompt ) => prompt . promptDefinition ,
158
160
) ,
159
161
} ;
160
162
} ) ;
@@ -166,8 +168,8 @@ export class MCPServer {
166
168
`Unknown prompt: ${
167
169
request . params . name
168
170
} . Available prompts: ${ Array . from ( this . promptsMap . keys ( ) ) . join (
169
- ", "
170
- ) } `
171
+ ", " ,
172
+ ) } `,
171
173
) ;
172
174
}
173
175
@@ -179,11 +181,24 @@ export class MCPServer {
179
181
this . server . setRequestHandler ( ListResourcesRequestSchema , async ( ) => {
180
182
return {
181
183
resources : Array . from ( this . resourcesMap . values ( ) ) . map (
182
- ( resource ) => resource . resourceDefinition
184
+ ( resource ) => resource . resourceDefinition ,
183
185
) ,
184
186
} ;
185
187
} ) ;
186
188
189
+ this . server . setRequestHandler (
190
+ ListResourceTemplatesRequestSchema ,
191
+ async ( ) => {
192
+ const templates = Array . from ( this . resourcesMap . values ( ) )
193
+ . map ( ( resource ) => resource . templateDefinition )
194
+ . filter ( ( template ) : template is NonNullable < typeof template > =>
195
+ Boolean ( template ) ,
196
+ ) ;
197
+
198
+ return { resourceTemplates : templates } ;
199
+ } ,
200
+ ) ;
201
+
187
202
this . server . setRequestHandler (
188
203
ReadResourceRequestSchema ,
189
204
async ( request ) => {
@@ -193,15 +208,15 @@ export class MCPServer {
193
208
`Unknown resource: ${
194
209
request . params . uri
195
210
} . Available resources: ${ Array . from ( this . resourcesMap . keys ( ) ) . join (
196
- ", "
197
- ) } `
211
+ ", " ,
212
+ ) } `,
198
213
) ;
199
214
}
200
215
201
216
return {
202
217
contents : await resource . read ( ) ,
203
218
} ;
204
- }
219
+ } ,
205
220
) ;
206
221
207
222
this . server . setRequestHandler ( SubscribeRequestSchema , async ( request ) => {
@@ -212,7 +227,7 @@ export class MCPServer {
212
227
213
228
if ( ! resource . subscribe ) {
214
229
throw new Error (
215
- `Resource ${ request . params . uri } does not support subscriptions`
230
+ `Resource ${ request . params . uri } does not support subscriptions` ,
216
231
) ;
217
232
}
218
233
@@ -228,13 +243,39 @@ export class MCPServer {
228
243
229
244
if ( ! resource . unsubscribe ) {
230
245
throw new Error (
231
- `Resource ${ request . params . uri } does not support subscriptions`
246
+ `Resource ${ request . params . uri } does not support subscriptions` ,
232
247
) ;
233
248
}
234
249
235
250
await resource . unsubscribe ( ) ;
236
251
return { } ;
237
252
} ) ;
253
+
254
+ this . server . setRequestHandler ( CompleteRequestSchema , async ( request ) => {
255
+ const { ref, argument } = request . params ;
256
+
257
+ if ( ref . type === "ref/prompt" ) {
258
+ const prompt = this . promptsMap . get ( ref . name ) ;
259
+ if ( ! prompt ?. complete ) {
260
+ return { completion : { values : [ ] } } ;
261
+ }
262
+ return {
263
+ completion : await prompt . complete ( argument . name , argument . value ) ,
264
+ } ;
265
+ }
266
+
267
+ if ( ref . type === "ref/resource" ) {
268
+ const resource = this . resourcesMap . get ( ref . uri ) ;
269
+ if ( ! resource ?. complete ) {
270
+ return { completion : { values : [ ] } } ;
271
+ }
272
+ return {
273
+ completion : await resource . complete ( argument . name , argument . value ) ,
274
+ } ;
275
+ }
276
+
277
+ throw new Error ( `Unknown reference type: ${ ref } ` ) ;
278
+ } ) ;
238
279
}
239
280
240
281
private async detectCapabilities ( ) : Promise < ServerCapabilities > {
@@ -262,17 +303,17 @@ export class MCPServer {
262
303
try {
263
304
const tools = await this . toolLoader . loadTools ( ) ;
264
305
this . toolsMap = new Map (
265
- tools . map ( ( tool : ToolProtocol ) => [ tool . name , tool ] )
306
+ tools . map ( ( tool : ToolProtocol ) => [ tool . name , tool ] ) ,
266
307
) ;
267
308
268
309
const prompts = await this . promptLoader . loadPrompts ( ) ;
269
310
this . promptsMap = new Map (
270
- prompts . map ( ( prompt : PromptProtocol ) => [ prompt . name , prompt ] )
311
+ prompts . map ( ( prompt : PromptProtocol ) => [ prompt . name , prompt ] ) ,
271
312
) ;
272
313
273
314
const resources = await this . resourceLoader . loadResources ( ) ;
274
315
this . resourcesMap = new Map (
275
- resources . map ( ( resource : ResourceProtocol ) => [ resource . uri , resource ] )
316
+ resources . map ( ( resource : ResourceProtocol ) => [ resource . uri , resource ] ) ,
276
317
) ;
277
318
278
319
await this . detectCapabilities ( ) ;
@@ -285,22 +326,22 @@ export class MCPServer {
285
326
if ( tools . length > 0 ) {
286
327
logger . info (
287
328
`Tools (${ tools . length } ): ${ Array . from ( this . toolsMap . keys ( ) ) . join (
288
- ", "
289
- ) } `
329
+ ", " ,
330
+ ) } `,
290
331
) ;
291
332
}
292
333
if ( prompts . length > 0 ) {
293
334
logger . info (
294
335
`Prompts (${ prompts . length } ): ${ Array . from (
295
- this . promptsMap . keys ( )
296
- ) . join ( ", " ) } `
336
+ this . promptsMap . keys ( ) ,
337
+ ) . join ( ", " ) } `,
297
338
) ;
298
339
}
299
340
if ( resources . length > 0 ) {
300
341
logger . info (
301
342
`Resources (${ resources . length } ): ${ Array . from (
302
- this . resourcesMap . keys ( )
303
- ) . join ( ", " ) } `
343
+ this . resourcesMap . keys ( ) ,
344
+ ) . join ( ", " ) } `,
304
345
) ;
305
346
}
306
347
} catch ( error ) {
0 commit comments