Skip to content

Add docker/mcp-gateway docker compose service connection #3536

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

Open
wants to merge 3 commits 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,28 @@
/*
* Copyright 2025-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.ai.mcp.client.autoconfigure;

import org.springframework.ai.mcp.client.autoconfigure.properties.McpSseClientProperties;
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;

import java.util.Map;

public interface McpSseClientConnectionDetails extends ConnectionDetails {

Map<String, McpSseClientProperties.SseParameters> getConnections();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2025-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.ai.mcp.client.autoconfigure;

import org.springframework.ai.mcp.client.autoconfigure.properties.McpSseClientProperties;

import java.util.Map;

class PropertiesMcpSseClientConnectionDetails implements McpSseClientConnectionDetails {

private final McpSseClientProperties properties;

PropertiesMcpSseClientConnectionDetails(McpSseClientProperties properties) {
this.properties = properties;
}

@Override
public Map<String, McpSseClientProperties.SseParameters> getConnections() {
return this.properties.getConnections();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
matchIfMissing = true)
public class SseHttpClientTransportAutoConfiguration {

@Bean
PropertiesMcpSseClientConnectionDetails mcpSseClientConnectionDetails(McpSseClientProperties sseProperties) {
return new PropertiesMcpSseClientConnectionDetails(sseProperties);
}

/**
* Creates a list of HTTP client-based SSE transports for MCP communication.
*
Expand All @@ -79,20 +84,21 @@ public class SseHttpClientTransportAutoConfiguration {
* <li>Server URL from properties
* <li>ObjectMapper for JSON processing
* </ul>
* @param sseProperties the SSE client properties containing server configurations
* @param connectionDetails the SSE client connection details containing server
* configurations
* @param objectMapperProvider the provider for ObjectMapper or a new instance if not
* available
* @return list of named MCP transports
*/
@Bean
public List<NamedClientMcpTransport> mcpHttpClientTransports(McpSseClientProperties sseProperties,
public List<NamedClientMcpTransport> mcpHttpClientTransports(McpSseClientConnectionDetails connectionDetails,
ObjectProvider<ObjectMapper> objectMapperProvider) {

ObjectMapper objectMapper = objectMapperProvider.getIfAvailable(ObjectMapper::new);

List<NamedClientMcpTransport> sseTransports = new ArrayList<>();

for (Map.Entry<String, SseParameters> serverParameters : sseProperties.getConnections().entrySet()) {
for (Map.Entry<String, SseParameters> serverParameters : connectionDetails.getConnections().entrySet()) {

String baseUrl = serverParameters.getValue().url();
String sseEndpoint = serverParameters.getValue().sseEndpoint() != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
matchIfMissing = true)
public class SseWebFluxTransportAutoConfiguration {

@Bean
PropertiesMcpSseClientConnectionDetails mcpSseClientConnectionDetails(McpSseClientProperties sseProperties) {
return new PropertiesMcpSseClientConnectionDetails(sseProperties);
}

/**
* Creates a list of WebFlux-based SSE transports for MCP communication.
*
Expand All @@ -79,7 +84,7 @@ public class SseWebFluxTransportAutoConfiguration {
* @return list of named MCP transports
*/
@Bean
public List<NamedClientMcpTransport> webFluxClientTransports(McpSseClientProperties sseProperties,
public List<NamedClientMcpTransport> webFluxClientTransports(McpSseClientConnectionDetails connectionDetails,
ObjectProvider<WebClient.Builder> webClientBuilderProvider,
ObjectProvider<ObjectMapper> objectMapperProvider) {

Expand All @@ -88,7 +93,7 @@ public List<NamedClientMcpTransport> webFluxClientTransports(McpSseClientPropert
var webClientBuilderTemplate = webClientBuilderProvider.getIfAvailable(WebClient::builder);
var objectMapper = objectMapperProvider.getIfAvailable(ObjectMapper::new);

for (Map.Entry<String, SseParameters> serverParameters : sseProperties.getConnections().entrySet()) {
for (Map.Entry<String, SseParameters> serverParameters : connectionDetails.getConnections().entrySet()) {
var webClientBuilder = webClientBuilderTemplate.clone().baseUrl(serverParameters.getValue().url());
String sseEndpoint = serverParameters.getValue().sseEndpoint() != null
? serverParameters.getValue().sseEndpoint() : "/sse";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ The following service connection factories are provided in the `spring-ai-spring

| `WeaviateConnectionDetails`
| Containers named `semitechnologies/weaviate`, `cr.weaviate.io/semitechnologies/weaviate`

| `McpSseClientConnectionDetails`
| Containers named `docker/mcp-gateway`
|====
7 changes: 7 additions & 0 deletions spring-ai-spring-boot-docker-compose/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-autoconfigure-mcp-client</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>

<!-- production dependencies -->

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.ai.docker.compose.service.connection.docker;

import org.springframework.ai.mcp.client.autoconfigure.McpSseClientConnectionDetails;
import org.springframework.ai.mcp.client.autoconfigure.properties.McpSseClientProperties;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource;

import java.util.Map;

/**
* A {@link DockerComposeConnectionDetailsFactory} implementation that creates
* {@link McpSseClientConnectionDetails} for a Docker MCP Gateway instance running in a
* Docker container.
*
* @author Eddú Meléndez
*/
class DockerMcpGatewayDockerComposeConnectionDetailsFactory
extends DockerComposeConnectionDetailsFactory<McpSseClientConnectionDetails> {

private static final int GATEWAY_PORT = 8811;

protected DockerMcpGatewayDockerComposeConnectionDetailsFactory() {
super("docker/mcp-gateway");
}

@Override
protected McpSseClientConnectionDetails getDockerComposeConnectionDetails(DockerComposeConnectionSource source) {
return new DockerAgentsGatewayContainerConnectionDetails(source.getRunningService());
}

/**
* {@link McpSseClientConnectionDetails} backed by a {@code Docker MCP Gateway}
* {@link RunningService}.
*/
static class DockerAgentsGatewayContainerConnectionDetails extends DockerComposeConnectionDetails
implements McpSseClientConnectionDetails {

private final String url;

DockerAgentsGatewayContainerConnectionDetails(RunningService service) {
super(service);
this.url = String.format("http://%s:%d", service.host(), service.ports().get(GATEWAY_PORT));
}

@Override
public Map<String, McpSseClientProperties.SseParameters> getConnections() {
return Map.of("gateway", new McpSseClientProperties.SseParameters(url, "/sse"));
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactory=\
org.springframework.ai.docker.compose.service.connection.chroma.ChromaDockerComposeConnectionDetailsFactory,\
org.springframework.ai.docker.compose.service.connection.docker.DockerMcpGatewayDockerComposeConnectionDetailsFactory,\
org.springframework.ai.docker.compose.service.connection.mongo.MongoDbAtlasLocalDockerComposeConnectionDetailsFactory,\
org.springframework.ai.docker.compose.service.connection.ollama.OllamaDockerComposeConnectionDetailsFactory,\
org.springframework.ai.docker.compose.service.connection.opensearch.AwsOpenSearchDockerComposeConnectionDetailsFactory,\
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.ai.docker.compose.service.connection.docker;

import org.junit.jupiter.api.Test;
import org.springframework.ai.mcp.client.autoconfigure.McpSseClientConnectionDetails;
import org.springframework.boot.docker.compose.service.connection.test.AbstractDockerComposeIT;
import org.testcontainers.utility.DockerImageName;

import static org.assertj.core.api.Assertions.assertThat;

class DockerMcpGatewayDockerComposeConnectionDetailsFactoryIT extends AbstractDockerComposeIT {

protected DockerMcpGatewayDockerComposeConnectionDetailsFactoryIT() {
super("docker-agents-gateway-compose.yaml", DockerImageName.parse("docker/mcp-gateway"));
}

@Test
void runCreatesConnectionDetails() {
McpSseClientConnectionDetails connectionDetails = run(McpSseClientConnectionDetails.class);
assertThat(connectionDetails.getConnections()).hasSize(1);
assertThat(connectionDetails.getConnections().get("gateway").url()).startsWith("http://");
assertThat(connectionDetails.getConnections().get("gateway").sseEndpoint()).contains("/sse");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
mcp-gateway:
image: '{imageName}'
ports:
- 8811
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
command:
- --transport=sse