Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to select which transport is used when multiple exists #55

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkiverse.mcp.server.deployment;

import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;

@ConfigMapping(prefix = "quarkus.mcp.server")
@ConfigRoot(phase = ConfigPhase.BUILD_TIME)
public interface McpBuildTimeConfig {

/**
* If multiple transports extensions are on the classpath, select which one is used
*/
Optional<String> transport();
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,33 @@

class McpServerProcessor {

@BuildStep
SelectedTransportBuildItem selectTransport(McpBuildTimeConfig buildTimeConfig,
List<TransportCandidateBuildItem> transportCandidates) {
if (transportCandidates.isEmpty()) {
throw new IllegalStateException(
"At least one MCP transport must be used. Consider adding the one of the 'quarkus-mcp-server-stdio', 'quarkus-mcp-server-sse' extensions");
}
if (transportCandidates.size() == 1) {
return new SelectedTransportBuildItem(transportCandidates.get(0).getName());
}
if (buildTimeConfig.transport().isPresent()) {
String selectedTransportName = buildTimeConfig.transport().get();
for (TransportCandidateBuildItem transportCandidate : transportCandidates) {
if (transportCandidate.getName().equals(selectedTransportName)) {
return new SelectedTransportBuildItem(transportCandidate.getName());
}
}
throw new IllegalStateException(
"Selected transport '" + selectedTransportName + "' does not correspond to any of the added extensions");
} else {
throw new IllegalStateException(
"Multiple transport candidates were found, please select one by setting 'quarkus.mcp.server.transport' to one of the following values: "
+ transportCandidates.stream().map(TransportCandidateBuildItem::getName)
.collect(Collectors.joining(", ")));
}
}

@BuildStep
void addBeans(BuildProducer<AdditionalBeanBuildItem> additionalBeans) {
additionalBeans.produce(AdditionalBeanBuildItem.unremovableOf("io.quarkiverse.mcp.server.runtime.ConnectionManager"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.quarkiverse.mcp.server.deployment;

import io.quarkus.builder.item.SimpleBuildItem;

/**
* The selected MCP transport
*/
public final class SelectedTransportBuildItem extends SimpleBuildItem {

private final String name;

public SelectedTransportBuildItem(String name) {
this.name = name;
}

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.quarkiverse.mcp.server.deployment;

import io.quarkus.builder.item.MultiBuildItem;

/**
* Generated by MCP transport implementations to advertise their existence
*/
public final class TransportCandidateBuildItem extends MultiBuildItem {

private final String name;

public TransportCandidateBuildItem(String name) {
this.name = name;
}

public String getName() {
return name;
}
}
17 changes: 17 additions & 0 deletions docs/modules/ROOT/pages/includes/quarkus-mcp-server-core.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ h|[.header-title]##Configuration property##
h|Type
h|Default

a|icon:lock[title=Fixed at build time] [[quarkus-mcp-server-core_quarkus-mcp-server-transport]] [.property-path]##link:#quarkus-mcp-server-core_quarkus-mcp-server-transport[`quarkus.mcp.server.transport`]##

[.description]
--
If multiple transports extensions are on the classpath, select which one is used


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_MCP_SERVER_TRANSPORT+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_MCP_SERVER_TRANSPORT+++`
endif::add-copy-button-to-env-var[]
--
|string
|

a| [[quarkus-mcp-server-core_quarkus-mcp-server-server-info-name]] [.property-path]##link:#quarkus-mcp-server-core_quarkus-mcp-server-server-info-name[`quarkus.mcp.server.server-info.name`]##

[.description]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ h|[.header-title]##Configuration property##
h|Type
h|Default

a|icon:lock[title=Fixed at build time] [[quarkus-mcp-server-core_quarkus-mcp-server-transport]] [.property-path]##link:#quarkus-mcp-server-core_quarkus-mcp-server-transport[`quarkus.mcp.server.transport`]##

[.description]
--
If multiple transports extensions are on the classpath, select which one is used


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_MCP_SERVER_TRANSPORT+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_MCP_SERVER_TRANSPORT+++`
endif::add-copy-button-to-env-var[]
--
|string
|

a| [[quarkus-mcp-server-core_quarkus-mcp-server-server-info-name]] [.property-path]##link:#quarkus-mcp-server-core_quarkus-mcp-server-server-info-name[`quarkus.mcp.server.server-info.name`]##

[.description]
Expand Down
4 changes: 4 additions & 0 deletions docs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<artifactId>quarkus-mcp-server-docs</artifactId>
<name>Quarkus MCP Server - Documentation</name>

<properties>
<quarkus.mcp.server.transport>stdio</quarkus.mcp.server.transport> <!-- added just to have the build pass -->
</properties>

<dependencies>
<!-- Make sure the doc is built after the other artifacts -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static io.quarkus.deployment.annotations.ExecutionTime.RUNTIME_INIT;

import io.quarkiverse.mcp.server.deployment.SelectedTransportBuildItem;
import io.quarkiverse.mcp.server.deployment.TransportCandidateBuildItem;
import io.quarkiverse.mcp.server.sse.runtime.SseMcpServerRecorder;
import io.quarkiverse.mcp.server.sse.runtime.config.McpSseBuildTimeConfig;
import io.quarkus.arc.deployment.SyntheticBeansRuntimeInitBuildItem;
Expand All @@ -21,12 +23,21 @@ FeatureBuildItem feature() {
return new FeatureBuildItem("mcp-server-sse");
}

@BuildStep
TransportCandidateBuildItem transportCandidate() {
return new TransportCandidateBuildItem("sse");
}

@Record(RUNTIME_INIT)
@Consume(SyntheticBeansRuntimeInitBuildItem.class)
@BuildStep
void registerEndpoints(McpSseBuildTimeConfig config, HttpRootPathBuildItem rootPath, SseMcpServerRecorder recorder,
void registerEndpoints(SelectedTransportBuildItem selectedTransportCandidateBuildItem,
McpSseBuildTimeConfig config, HttpRootPathBuildItem rootPath, SseMcpServerRecorder recorder,
BodyHandlerBuildItem bodyHandler,
BuildProducer<RouteBuildItem> routes) {
if (!selectedTransportCandidateBuildItem.getName().equals("sse")) {
return;
}
String mcpPath = rootPath.relativePath(config.rootPath());

routes.produce(RouteBuildItem.newFrameworkRoute(mcpPath + "/" + "sse")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static io.quarkus.deployment.annotations.ExecutionTime.RUNTIME_INIT;

import io.quarkiverse.mcp.server.deployment.SelectedTransportBuildItem;
import io.quarkiverse.mcp.server.deployment.TransportCandidateBuildItem;
import io.quarkiverse.mcp.server.stdio.runtime.StdioMcpServerRecorder;
import io.quarkus.arc.deployment.SyntheticBeansRuntimeInitBuildItem;
import io.quarkus.deployment.annotations.BuildStep;
Expand All @@ -16,10 +18,18 @@ FeatureBuildItem feature() {
return new FeatureBuildItem("mcp-server-stdio");
}

@BuildStep
TransportCandidateBuildItem transportCandidate() {
return new TransportCandidateBuildItem("stdio");
}

@Record(RUNTIME_INIT)
@Consume(SyntheticBeansRuntimeInitBuildItem.class)
@BuildStep
void initialize(StdioMcpServerRecorder recorder) {
void initialize(SelectedTransportBuildItem selectedTransportCandidateBuildItem, StdioMcpServerRecorder recorder) {
if (!selectedTransportCandidateBuildItem.getName().equals("stdio")) {
return;
}
recorder.initialize();
}

Expand Down
Loading