Skip to content

Commit 57530ce

Browse files
committed
Add a new completion content factory method.
1 parent 2042a61 commit 57530ce

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

codegen/src/main/java/io/helidon/extensions/mcp/codegen/McpCodegen.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,8 @@ private void addCompletionMethod(Method.Builder builder, ClassModel.Builder clas
337337
.addContent(element.elementName())
338338
.addContent("(")
339339
.addContent(params)
340-
.addContentLine(").toArray(new String[0]));")
340+
.addContentLine("));")
341+
.decreaseContentPadding()
341342
.addContentLine("};");
342343
return;
343344
}

examples/calendar-application/calendar-declarative/src/main/java/io/helidon/extensions/mcp/examples/calendar/declarative/McpCalendarServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ McpCompletionContent eventResourceTemplateCompletion(String nameValue) {
147147
.stream()
148148
.filter(name -> name.contains(nameValue))
149149
.toList();
150-
return McpCompletionContents.completion(values.toArray(new String[0]));
150+
return McpCompletionContents.completion(values);
151151
}
152152

153153
// -- Prompts -------------------------------------------------------------

examples/calendar-application/calendar/src/main/java/io/helidon/extensions/mcp/examples/calendar/CalendarEventResourceCompletion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ private McpCompletionContent complete(McpRequest request) {
5959
.stream()
6060
.filter(name -> name.contains(nameValue))
6161
.toList();
62-
return McpCompletionContents.completion(values.toArray(new String[0]));
62+
return McpCompletionContents.completion(values);
6363
}
6464
}

server/src/main/java/io/helidon/extensions/mcp/server/McpCompletionContents.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package io.helidon.extensions.mcp.server;
1817

1918
import java.util.List;
@@ -27,7 +26,8 @@ private McpCompletionContents() {
2726
}
2827

2928
/**
30-
* Create a completion content from the provided list of string.
29+
* Create a completion content from the provided list of string. The maximum
30+
* number of suggestion cannot exceed 100.
3131
*
3232
* @param values completion values
3333
* @return completion content
@@ -36,6 +36,17 @@ public static McpCompletionContent completion(String... values) {
3636
return new McpCompletionContentImpl(List.of(values));
3737
}
3838

39+
/**
40+
* Create a completion content from provided list of string. The maximum
41+
* number of suggestion cannot exceed 100.
42+
*
43+
* @param values completion values
44+
* @return completion content
45+
*/
46+
public static McpCompletionContent completion(List<String> values) {
47+
return new McpCompletionContentImpl(values);
48+
}
49+
3950
/**
4051
* Completion content default implementation.
4152
*/
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.helidon.extensions.mcp.server;
17+
18+
import java.util.List;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
import static org.hamcrest.MatcherAssert.assertThat;
23+
import static org.hamcrest.Matchers.is;
24+
25+
class McpCompletionContentTest {
26+
27+
@Test
28+
void testDefaultMcpCompletion() {
29+
var content = McpCompletionContents.completion(List.of());
30+
assertThat(content.total(), is(0));
31+
assertThat(content.hasMore(), is(false));
32+
assertThat(content.values(), is(List.of()));
33+
}
34+
35+
@Test
36+
void testDefaultArrayMcpCompletion() {
37+
var content = McpCompletionContents.completion();
38+
assertThat(content.total(), is(0));
39+
assertThat(content.hasMore(), is(false));
40+
assertThat(content.values(), is(List.of()));
41+
}
42+
43+
@Test
44+
void testMcpCompletion() {
45+
var content = McpCompletionContents.completion(List.of("foo"));
46+
assertThat(content.total(), is(1));
47+
assertThat(content.hasMore(), is(false));
48+
assertThat(content.values(), is(List.of("foo")));
49+
}
50+
51+
@Test
52+
void testArrayMcpCompletion() {
53+
var content = McpCompletionContents.completion("foo");
54+
assertThat(content.total(), is(1));
55+
assertThat(content.hasMore(), is(false));
56+
assertThat(content.values(), is(List.of("foo")));
57+
}
58+
}

0 commit comments

Comments
 (0)