-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- also fixes #53
- Loading branch information
Showing
32 changed files
with
785 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 155 additions & 32 deletions
187
core/deployment/src/main/java/io/quarkiverse/mcp/server/deployment/McpServerProcessor.java
Large diffs are not rendered by default.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
core/runtime/src/main/java/io/quarkiverse/mcp/server/CompleteArg.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.quarkiverse.mcp.server; | ||
|
||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotates a parameter of a "complete" method, such as {@link CompletePrompt}. | ||
*/ | ||
@Retention(RUNTIME) | ||
@Target(ElementType.PARAMETER) | ||
public @interface CompleteArg { | ||
|
||
/** | ||
* Constant value for {@link #name()} indicating that the annotated element's name should be used as-is. | ||
*/ | ||
String ELEMENT_NAME = "<<element name>>"; | ||
|
||
String name() default ELEMENT_NAME; | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
core/runtime/src/main/java/io/quarkiverse/mcp/server/CompletePrompt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.quarkiverse.mcp.server; | ||
|
||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import java.util.List; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
|
||
/** | ||
* Annotates a business method of a CDI bean used to complete a prompt argument | ||
* <p> | ||
* The result of a "complete" operation is always represented as a {@link CompletionResponse}. However, the annotated method can | ||
* also return other types that are converted according to the following rules. | ||
* <ul> | ||
* <li>If the method returns {@link String} then the reponse contains the single value.</li> | ||
* <li>If the method returns a {@link List} of {@link String}s then the reponse contains the list of values.</li> | ||
* <li>The method may return a {@link Uni} that wraps any of the type mentioned above.</li> | ||
* </ul> | ||
* In other words, the return type must be one of the following list: | ||
* <ul> | ||
* <li>{@code CompletionResponse}</li> | ||
* <li>{@code String}</li> | ||
* <li>{@code List<String>}</li> | ||
* <li>{@code Uni<CompletionResponse>}</li> | ||
* <li>{@code Uni<String>}</li> | ||
* <li>{@code Uni<List<String>>}</li> | ||
* </ul> | ||
* | ||
* @see Prompt#name() | ||
*/ | ||
@Retention(RUNTIME) | ||
@Target(METHOD) | ||
public @interface CompletePrompt { | ||
|
||
/** | ||
* The name reference to a prompt. If not such {@link Prompt} exists then the build fails. | ||
*/ | ||
String value(); | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
core/runtime/src/main/java/io/quarkiverse/mcp/server/CompletionResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.quarkiverse.mcp.server; | ||
|
||
import java.util.List; | ||
|
||
public record CompletionResponse(List<String> values, Integer total, Boolean hasMore) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,8 @@ public JsonObject asJson() { | |
public enum Feature { | ||
PROMPT, | ||
TOOL, | ||
RESOURCE | ||
RESOURCE, | ||
PROMPT_COMPLETE | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
core/runtime/src/main/java/io/quarkiverse/mcp/server/runtime/PromptCompleteManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.quarkiverse.mcp.server.runtime; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import io.quarkiverse.mcp.server.CompletionResponse; | ||
import io.vertx.core.Vertx; | ||
|
||
public class PromptCompleteManager extends FeatureManager<CompletionResponse> { | ||
|
||
// key = prompt name + "_" + argument name | ||
final Map<String, FeatureMetadata<CompletionResponse>> completions; | ||
|
||
protected PromptCompleteManager(McpMetadata metadata, Vertx vertx, ObjectMapper mapper) { | ||
super(vertx, mapper); | ||
this.completions = metadata.promptCompletions().stream() | ||
.collect(Collectors.toMap( | ||
m -> m.info().name() + "_" | ||
+ m.info().arguments().stream().filter(FeatureArgument::isParam).findFirst().orElseThrow() | ||
.name(), | ||
Function.identity())); | ||
} | ||
|
||
@Override | ||
public List<FeatureMetadata<CompletionResponse>> list() { | ||
return completions.values().stream().sorted().toList(); | ||
} | ||
|
||
@Override | ||
protected FeatureMetadata<CompletionResponse> getMetadata(String id) { | ||
return completions.get(id); | ||
} | ||
|
||
@Override | ||
protected McpException notFound(String id) { | ||
return new McpException("Prompt completion does not exist: " + id, JsonRPC.INVALID_PARAMS); | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
...ntime/src/main/java/io/quarkiverse/mcp/server/runtime/PromptCompletionMessageHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package io.quarkiverse.mcp.server.runtime; | ||
|
||
import java.util.Map; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
import io.quarkiverse.mcp.server.CompletionResponse; | ||
import io.quarkiverse.mcp.server.McpConnection; | ||
import io.vertx.core.AsyncResult; | ||
import io.vertx.core.Future; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.json.JsonObject; | ||
|
||
class PromptCompletionMessageHandler { | ||
|
||
private static final Logger LOG = Logger.getLogger(PromptCompletionMessageHandler.class); | ||
|
||
private final PromptCompleteManager manager; | ||
|
||
PromptCompletionMessageHandler(PromptCompleteManager manager) { | ||
this.manager = manager; | ||
} | ||
|
||
void promptComplete(Object id, JsonObject ref, JsonObject argument, Responder responder, McpConnection connection) { | ||
String promptName = ref.getString("name"); | ||
String argumentName = argument.getString("name"); | ||
|
||
LOG.infof("Complete prompt %s for argument %s [id: %s]", promptName, argumentName, id); | ||
|
||
String key = promptName + "_" + argumentName; | ||
|
||
ArgumentProviders argProviders = new ArgumentProviders( | ||
Map.of(argumentName, argument.getString("value")), connection, id); | ||
|
||
try { | ||
Future<CompletionResponse> fu = manager.execute(key, argProviders); | ||
fu.onComplete(new Handler<AsyncResult<CompletionResponse>>() { | ||
@Override | ||
public void handle(AsyncResult<CompletionResponse> ar) { | ||
if (ar.succeeded()) { | ||
CompletionResponse completionResponse = ar.result(); | ||
JsonObject result = new JsonObject(); | ||
JsonObject completion = new JsonObject() | ||
.put("values", completionResponse.values()); | ||
if (completionResponse.total() != null) { | ||
completion.put("total", completionResponse.total()); | ||
} | ||
if (completionResponse.hasMore() != null) { | ||
completion.put("hasMore", completionResponse.hasMore()); | ||
} | ||
result.put("completion", completion); | ||
responder.sendResult(id, result); | ||
} else { | ||
LOG.errorf(ar.cause(), "Unable to complete prompt %s", promptName); | ||
responder.sendInternalError(id); | ||
} | ||
} | ||
}); | ||
} catch (McpException e) { | ||
responder.sendError(id, e.getJsonRpcError(), e.getMessage()); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.