@@ -152,6 +152,37 @@ public String toSourceCode(boolean kt) throws IOException {
152152 completionGroups .computeIfAbsent (ref , k -> new ArrayList <>()).add (route );
153153 }
154154
155+ // Gather all valid references so we can register dummy completions for targets that lack them
156+ var allCompletionRefs = new java .util .LinkedHashSet <String >();
157+ for (var route : prompts ) {
158+ var annotation =
159+ AnnotationSupport .findAnnotationByName (
160+ route .getMethod (), "io.jooby.annotation.mcp.McpPrompt" );
161+ var name =
162+ annotation != null
163+ ? AnnotationSupport .findAnnotationValue (annotation , "name" ::equals ).stream ()
164+ .findFirst ()
165+ .orElse ("" )
166+ : "" ;
167+ if (name .isEmpty ()) name = route .getMethodName ();
168+ allCompletionRefs .add (name );
169+ }
170+ for (var route : resources ) {
171+ if (route .isMcpResourceTemplate ()) {
172+ var annotation =
173+ AnnotationSupport .findAnnotationByName (
174+ route .getMethod (), "io.jooby.annotation.mcp.McpResource" );
175+ var uri =
176+ annotation != null
177+ ? AnnotationSupport .findAnnotationValue (annotation , "uri" ::equals ).stream ()
178+ .findFirst ()
179+ .orElse ("" )
180+ : "" ;
181+ allCompletionRefs .add (uri );
182+ }
183+ }
184+ allCompletionRefs .addAll (completionGroups .keySet ());
185+
155186 if (kt ) {
156187 buffer .append (
157188 statement (
@@ -233,18 +264,29 @@ public String toSourceCode(boolean kt) throws IOException {
233264 semicolon (kt )));
234265 }
235266
236- for (var ref : completionGroups .keySet ()) {
267+ // Loop over ALL possible refs, not just the ones with explicit handlers
268+ for (var ref : allCompletionRefs ) {
237269 var isResource = ref .contains ("://" );
238- var handlerName = findTargetMethodName (ref ) + "CompletionHandler" ;
239270 var refObj =
240271 isResource
241272 ? "io.modelcontextprotocol.spec.McpSchema.ResourceReference"
242273 : "io.modelcontextprotocol.spec.McpSchema.PromptReference" ;
243274
244- String lambda =
245- kt
246- ? "{ exchange, req -> this." + handlerName + "(exchange, null, req) }"
247- : "(exchange, req) -> this." + handlerName + "(exchange, null, req)" ;
275+ String lambda ;
276+ if (completionGroups .containsKey (ref )) {
277+ var handlerName = findTargetMethodName (ref ) + "CompletionHandler" ;
278+ lambda =
279+ kt
280+ ? "{ exchange, req -> this." + handlerName + "(exchange, null, req) }"
281+ : "(exchange, req) -> this." + handlerName + "(exchange, null, req)" ;
282+ } else {
283+ // Fallback: Return an empty completion result safely
284+ lambda =
285+ kt
286+ ? "{ _, _ -> io.jooby.mcp.McpResult(this.json).toCompleteResult(emptyList<Any>()) }"
287+ : "(exchange, req) -> new"
288+ + " io.jooby.mcp.McpResult(this.json).toCompleteResult(java.util.List.of())" ;
289+ }
248290
249291 if (kt ) {
250292 buffer .append (
@@ -305,18 +347,29 @@ public String toSourceCode(boolean kt) throws IOException {
305347 semicolon (kt )));
306348 }
307349
308- for (var ref : completionGroups .keySet ()) {
350+ // Loop over ALL possible refs
351+ for (var ref : allCompletionRefs ) {
309352 var isResource = ref .contains ("://" );
310- var handlerName = findTargetMethodName (ref ) + "CompletionHandler" ;
311353 var refObj =
312354 isResource
313355 ? "io.modelcontextprotocol.spec.McpSchema.ResourceReference"
314356 : "io.modelcontextprotocol.spec.McpSchema.PromptReference" ;
315357
316- String lambda =
317- kt
318- ? "{ ctx, req -> this." + handlerName + "(null, ctx, req) }"
319- : "(ctx, req) -> this." + handlerName + "(null, ctx, req)" ;
358+ String lambda ;
359+ if (completionGroups .containsKey (ref )) {
360+ var handlerName = findTargetMethodName (ref ) + "CompletionHandler" ;
361+ lambda =
362+ kt
363+ ? "{ ctx, req -> this." + handlerName + "(null, ctx, req) }"
364+ : "(ctx, req) -> this." + handlerName + "(null, ctx, req)" ;
365+ } else {
366+ // Fallback: Return an empty completion result safely
367+ lambda =
368+ kt
369+ ? "{ _, _ -> io.jooby.mcp.McpResult(this.json).toCompleteResult(emptyList<Any>()) }"
370+ : "(ctx, req) -> new"
371+ + " io.jooby.mcp.McpResult(this.json).toCompleteResult(java.util.List.of())" ;
372+ }
320373
321374 if (kt ) {
322375 buffer .append (
0 commit comments