99 ReadResourceRequestSchema ,
1010 SubscribeRequestSchema ,
1111 UnsubscribeRequestSchema ,
12+ CompleteRequestSchema ,
13+ ListResourceTemplatesRequestSchema ,
1214} from "@modelcontextprotocol/sdk/types.js" ;
1315import { ToolProtocol } from "../tools/BaseTool.js" ;
1416import { PromptProtocol } from "../prompts/BasePrompt.js" ;
@@ -59,7 +61,7 @@ export class MCPServer {
5961 this . serverVersion = config . version ?? this . getDefaultVersion ( ) ;
6062
6163 logger . info (
62- `Initializing MCP Server: ${ this . serverName } @${ this . serverVersion } `
64+ `Initializing MCP Server: ${ this . serverName } @${ this . serverVersion } ` ,
6365 ) ;
6466
6567 this . toolLoader = new ToolLoader ( this . basePath ) ;
@@ -77,7 +79,7 @@ export class MCPServer {
7779 prompts : { enabled : false } ,
7880 resources : { enabled : false } ,
7981 } ,
80- }
82+ } ,
8183 ) ;
8284
8385 this . setupHandlers ( ) ;
@@ -128,7 +130,7 @@ export class MCPServer {
128130 this . server . setRequestHandler ( ListToolsRequestSchema , async ( ) => {
129131 return {
130132 tools : Array . from ( this . toolsMap . values ( ) ) . map (
131- ( tool ) => tool . toolDefinition
133+ ( tool ) => tool . toolDefinition ,
132134 ) ,
133135 } ;
134136 } ) ;
@@ -138,8 +140,8 @@ export class MCPServer {
138140 if ( ! tool ) {
139141 throw new Error (
140142 `Unknown tool: ${ request . params . name } . Available tools: ${ Array . from (
141- this . toolsMap . keys ( )
142- ) . join ( ", " ) } `
143+ this . toolsMap . keys ( ) ,
144+ ) . join ( ", " ) } `,
143145 ) ;
144146 }
145147
@@ -154,7 +156,7 @@ export class MCPServer {
154156 this . server . setRequestHandler ( ListPromptsRequestSchema , async ( ) => {
155157 return {
156158 prompts : Array . from ( this . promptsMap . values ( ) ) . map (
157- ( prompt ) => prompt . promptDefinition
159+ ( prompt ) => prompt . promptDefinition ,
158160 ) ,
159161 } ;
160162 } ) ;
@@ -166,8 +168,8 @@ export class MCPServer {
166168 `Unknown prompt: ${
167169 request . params . name
168170 } . Available prompts: ${ Array . from ( this . promptsMap . keys ( ) ) . join (
169- ", "
170- ) } `
171+ ", " ,
172+ ) } `,
171173 ) ;
172174 }
173175
@@ -179,11 +181,24 @@ export class MCPServer {
179181 this . server . setRequestHandler ( ListResourcesRequestSchema , async ( ) => {
180182 return {
181183 resources : Array . from ( this . resourcesMap . values ( ) ) . map (
182- ( resource ) => resource . resourceDefinition
184+ ( resource ) => resource . resourceDefinition ,
183185 ) ,
184186 } ;
185187 } ) ;
186188
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+
187202 this . server . setRequestHandler (
188203 ReadResourceRequestSchema ,
189204 async ( request ) => {
@@ -193,15 +208,15 @@ export class MCPServer {
193208 `Unknown resource: ${
194209 request . params . uri
195210 } . Available resources: ${ Array . from ( this . resourcesMap . keys ( ) ) . join (
196- ", "
197- ) } `
211+ ", " ,
212+ ) } `,
198213 ) ;
199214 }
200215
201216 return {
202217 contents : await resource . read ( ) ,
203218 } ;
204- }
219+ } ,
205220 ) ;
206221
207222 this . server . setRequestHandler ( SubscribeRequestSchema , async ( request ) => {
@@ -212,7 +227,7 @@ export class MCPServer {
212227
213228 if ( ! resource . subscribe ) {
214229 throw new Error (
215- `Resource ${ request . params . uri } does not support subscriptions`
230+ `Resource ${ request . params . uri } does not support subscriptions` ,
216231 ) ;
217232 }
218233
@@ -228,13 +243,39 @@ export class MCPServer {
228243
229244 if ( ! resource . unsubscribe ) {
230245 throw new Error (
231- `Resource ${ request . params . uri } does not support subscriptions`
246+ `Resource ${ request . params . uri } does not support subscriptions` ,
232247 ) ;
233248 }
234249
235250 await resource . unsubscribe ( ) ;
236251 return { } ;
237252 } ) ;
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+ } ) ;
238279 }
239280
240281 private async detectCapabilities ( ) : Promise < ServerCapabilities > {
@@ -262,17 +303,17 @@ export class MCPServer {
262303 try {
263304 const tools = await this . toolLoader . loadTools ( ) ;
264305 this . toolsMap = new Map (
265- tools . map ( ( tool : ToolProtocol ) => [ tool . name , tool ] )
306+ tools . map ( ( tool : ToolProtocol ) => [ tool . name , tool ] ) ,
266307 ) ;
267308
268309 const prompts = await this . promptLoader . loadPrompts ( ) ;
269310 this . promptsMap = new Map (
270- prompts . map ( ( prompt : PromptProtocol ) => [ prompt . name , prompt ] )
311+ prompts . map ( ( prompt : PromptProtocol ) => [ prompt . name , prompt ] ) ,
271312 ) ;
272313
273314 const resources = await this . resourceLoader . loadResources ( ) ;
274315 this . resourcesMap = new Map (
275- resources . map ( ( resource : ResourceProtocol ) => [ resource . uri , resource ] )
316+ resources . map ( ( resource : ResourceProtocol ) => [ resource . uri , resource ] ) ,
276317 ) ;
277318
278319 await this . detectCapabilities ( ) ;
@@ -285,22 +326,22 @@ export class MCPServer {
285326 if ( tools . length > 0 ) {
286327 logger . info (
287328 `Tools (${ tools . length } ): ${ Array . from ( this . toolsMap . keys ( ) ) . join (
288- ", "
289- ) } `
329+ ", " ,
330+ ) } `,
290331 ) ;
291332 }
292333 if ( prompts . length > 0 ) {
293334 logger . info (
294335 `Prompts (${ prompts . length } ): ${ Array . from (
295- this . promptsMap . keys ( )
296- ) . join ( ", " ) } `
336+ this . promptsMap . keys ( ) ,
337+ ) . join ( ", " ) } `,
297338 ) ;
298339 }
299340 if ( resources . length > 0 ) {
300341 logger . info (
301342 `Resources (${ resources . length } ): ${ Array . from (
302- this . resourcesMap . keys ( )
303- ) . join ( ", " ) } `
343+ this . resourcesMap . keys ( ) ,
344+ ) . join ( ", " ) } `,
304345 ) ;
305346 }
306347 } catch ( error ) {
0 commit comments