Skip to content
Merged
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,105 @@
package org.testcontainers.containers;

import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.images.builder.Transferable;
import org.testcontainers.utility.DockerImageName;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Testcontainers implementation of the Docker MCP Gateway container.
* <p>
* Supported images: {@code docker/agents_gateway}
* <p>
* Exposed ports: 8811
*/
public class DockerMcpGatewayContainer extends GenericContainer<DockerMcpGatewayContainer> {

private static final String DOCKER_AGENT_GATEWAY_IMAGE = "docker/agents_gateway";

private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse(DOCKER_AGENT_GATEWAY_IMAGE);

private static final int DEFAULT_PORT = 8811;

private static final String SECRETS_PATH = "/testcontainers/app/secrets";

private final List<String> servers = new ArrayList<>();

private final List<String> tools = new ArrayList<>();

private final Map<String, String> secrets = new HashMap<>();

public DockerMcpGatewayContainer(String dockerImageName) {
this(DockerImageName.parse(dockerImageName));
}

public DockerMcpGatewayContainer(DockerImageName dockerImageName) {
super(dockerImageName);
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);
withExposedPorts(DEFAULT_PORT);
withFileSystemBind(DockerClientFactory.instance().getRemoteDockerUnixSocketPath(), "/var/run/docker.sock");
waitingFor(Wait.forLogMessage(".*Start sse server on port.*", 1));
}

@Override
protected void configure() {
List<String> command = new ArrayList<>();
command.add("--transport=sse");
for (String server : this.servers) {
if (!server.isEmpty()) {
command.add("--servers=" + server);
}
}
for (String tool : this.tools) {
if (!tool.isEmpty()) {
command.add("--tools=" + tool);
}
}
if (this.secrets != null && !this.secrets.isEmpty()) {
command.add("--secrets=" + SECRETS_PATH);
}
withCommand(String.join(" ", command));
}

@Override
protected void containerIsCreated(String containerId) {
if (this.secrets != null && !this.secrets.isEmpty()) {
StringBuilder secretsFile = new StringBuilder();
for (Map.Entry<String, String> entry : this.secrets.entrySet()) {
secretsFile.append(entry.getKey()).append("=").append(entry.getValue()).append("\n");
}
copyFileToContainer(Transferable.of(secretsFile.toString()), SECRETS_PATH);
}
}

public DockerMcpGatewayContainer withServer(String server, List<String> tools) {
this.servers.add(server);
this.tools.addAll(tools);
return this;
}

public DockerMcpGatewayContainer withServer(String server, String... tools) {
this.servers.add(server);
this.tools.addAll(Arrays.asList(tools));
return this;
}

public DockerMcpGatewayContainer withSecrets(Map<String, String> secrets) {
this.secrets.putAll(secrets);
return this;
}

public DockerMcpGatewayContainer withSecret(String secretKey, String secretValue) {
this.secrets.put(secretKey, secretValue);
return this;
}

public String getEndpoint() {
return "http://" + getHost() + ":" + getMappedPort(DEFAULT_PORT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.testcontainers.containers;

import org.junit.Test;

import java.util.Collections;

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

public class DockerMcpGatewayContainerTest {

@Test
public void serviceSuccessfullyStarts() {
try (DockerMcpGatewayContainer gateway = new DockerMcpGatewayContainer("docker/agents_gateway:v2")) {
gateway.start();

assertThat(gateway.isRunning()).isTrue();
}
}

@Test
public void gatewayStartsWithServers() {
try (
// container {
DockerMcpGatewayContainer gateway = new DockerMcpGatewayContainer("docker/agents_gateway:v2")
.withServer("curl", "curl")
.withServer("brave", "brave_local_search", "brave_web_search")
.withServer("github-official", Collections.singletonList("add_issue_comment"))
.withSecret("brave.api_key", "test_key")
.withSecrets(Collections.singletonMap("github.personal_access_token", "test_token"))
// }
) {
gateway.start();

assertThat(gateway.getLogs()).contains("4 tools listed");
}
}
}
32 changes: 32 additions & 0 deletions docs/modules/docker_mcp_gateway.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Docker MCP Gateway

Testcontainers module for [Docker MCP Gateway](https://hub.docker.com/r/docker/agents_gateway).

## DockerMcpGatewayContainer's usage examples

You can start a Docker MCP Gateway container instance from any Java application by using:

<!--codeinclude-->
[Create a DockerMcpGatewayContainer](../../core/src/test/java/org/testcontainers/containers/DockerMcpGatewayContainerTest.java) inside_block:container
<!--/codeinclude-->

## Adding this module to your project dependencies

*Docker MCP Gateway support is part of the core Testcontainers library.*

Add the following dependency to your `pom.xml`/`build.gradle` file:

=== "Gradle"
```groovy
testImplementation "org.testcontainers:testcontainers:{{latest_version}}"
```
=== "Maven"
```xml
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>{{latest_version}}</version>
<scope>test</scope>
</dependency>
```

1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ nav:
- modules/chromadb.md
- modules/consul.md
- modules/docker_compose.md
- modules/docker_mcp_gateway.md
- modules/docker_model_runner.md
- modules/elasticsearch.md
- modules/gcloud.md
Expand Down
Loading