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
1 change: 1 addition & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ updates:
- /modules/couchbase
- /modules/databend
- /modules/dind
- /modules/dockermcpgateway
- /modules/dockermodelrunner
- /modules/dolt
- /modules/dynamodb
Expand Down
4 changes: 4 additions & 0 deletions .vscode/.testcontainers-go.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
"name": "module / dind",
"path": "../modules/dind"
},
{
"name": "module / dockermcpgateway",
"path": "../modules/dockermcpgateway"
},
{
"name": "module / dockermodelrunner",
"path": "../modules/dockermodelrunner"
Expand Down
108 changes: 108 additions & 0 deletions docs/modules/dockermcpgateway.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Docker MCP Gateway

Not available until the next release <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a>

## Introduction

The Testcontainers module for the Docker MCP Gateway.

## Adding this module to your project dependencies

Please run the following command to add the Docker MCP Gateway module to your Go dependencies:

```
go get github.com/testcontainers/testcontainers-go/modules/dockermcpgateway
```

## Usage example

<!--codeinclude-->
[Creating a DockerMCPGateway container](../../modules/dockermcpgateway/examples_test.go) inside_block:run_mcp_gateway
<!--/codeinclude-->

## Module Reference

### Run function

- Not available until the next release <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a>

The DockerMCPGateway module exposes one entrypoint function to create the DockerMCPGateway container, and this function receives three parameters:

```golang
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error)
```

- `context.Context`, the Go context.
- `string`, the Docker image to use.
- `testcontainers.ContainerCustomizer`, a variadic argument for passing options.

#### Image

Use the second argument in the `Run` function to set a valid Docker image.
In example: `Run(context.Background(), "docker/mcp-gateway:latest")`.

### Container Options

When starting the DockerMCPGateway container, you can pass options in a variadic way to configure it.

{% include "../features/common_functional_options_list.md" %}

#### WithTools

- Not available until the next release <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a>

Use the `WithTools` option to set the tools from a server to be available in the MCP Gateway container. Adding multiple tools for the same server will append to the existing tools for that server, and no duplicate tools will be added for the same server.

```golang
dockermcpgateway.WithTools("brave", []string{"brave_local_search", "brave_web_search"})
```

#### WithSecrets

- Not available until the next release <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a>

Use the `WithSecrets` option to set the tools from a server to be available in the MCP Gateway container. Empty keys are not allowed, although empty values are allowed for a key.

```golang
dockermcpgateway.WithSecret("github_token", "test_value")
dockermcpgateway.WithSecrets(map[string]{
"github_token": "test_value",
"foo": "bar",
})
```

### Container Methods

The DockerMCPGateway container exposes the following methods:

#### Tools

- Not available until the next release <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a>

Returns a map of tools available in the MCP Gateway container, where the key is the server name and the value is a slice of tool names.

```golang
tools := ctr.Tools()
```

#### GatewayEndpoint

- Not available until the next release <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a>

Returns the endpoint of the MCP Gateway container, which is a string containing the host and mapped port for the default MCP Gateway port (8811/tcp).

```golang
endpoint := ctr.GatewayEndpoint()
```
### Examples

#### Connecting to the MCP Gateway using an MCP client

This example shows the usage of the MCP Gateway module to connect with an [MCP client](https://github.com/modelcontextprotocol/go-sdk).

<!--codeinclude-->
[Run the MCP Gateway](../../modules/dockermcpgateway/examples_test.go) inside_block:run_mcp_gateway
[Get MCP Gateway's endpoint](../../modules/dockermcpgateway/examples_test.go) inside_block:get_gateway
[Connect with an MCP client](../../modules/dockermcpgateway/examples_test.go) inside_block:connect_mcp_client
[List tools](../../modules/dockermcpgateway/examples_test.go) inside_block:list_tools
<!--/codeinclude-->
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ nav:
- modules/couchbase.md
- modules/databend.md
- modules/dind.md
- modules/dockermcpgateway.md
- modules/dockermodelrunner.md
- modules/dolt.md
- modules/dynamodb.md
Expand Down
5 changes: 5 additions & 0 deletions modules/dockermcpgateway/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include ../../commons-test.mk

.PHONY: test
test:
$(MAKE) test-dockermcpgateway
108 changes: 108 additions & 0 deletions modules/dockermcpgateway/dockermcpgateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package dockermcpgateway

import (
"context"
"fmt"
"strings"

"github.com/docker/docker/api/types/container"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/internal/core"
"github.com/testcontainers/testcontainers-go/wait"
)

const (
defaultPort = "8811/tcp"
secretsPath = "/testcontainers/app/secrets"
)

// Container represents the DockerMCPGateway container type used in the module
type Container struct {
testcontainers.Container
tools map[string][]string
}

// Run creates an instance of the DockerMCPGateway container type
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error) {
dockerHostMount := core.MustExtractDockerSocket(ctx)

moduleOpts := []testcontainers.ContainerCustomizer{
testcontainers.WithExposedPorts(defaultPort),
testcontainers.WithHostConfigModifier(func(hc *container.HostConfig) {
hc.Binds = []string{
dockerHostMount + ":/var/run/docker.sock",
}
}),
testcontainers.WithWaitStrategy(wait.ForAll(
wait.ForListeningPort(defaultPort),
wait.ForLog(".*Start sse server on port.*").AsRegexp(),
)),
}

settings := defaultOptions()
for _, opt := range opts {
if apply, ok := opt.(Option); ok {
if err := apply(&settings); err != nil {
return nil, err
}
}
}

cmds := []string{"--transport=sse"}
for server, tools := range settings.tools {
cmds = append(cmds, "--servers="+server)
for _, tool := range tools {
cmds = append(cmds, "--tools="+tool)
}
}
if len(settings.secrets) > 0 {
cmds = append(cmds, "--secrets="+secretsPath)

secretsContent := ""
for key, value := range settings.secrets {
secretsContent += key + "=" + value + "\n"
}

moduleOpts = append(moduleOpts, testcontainers.WithFiles(testcontainers.ContainerFile{
Reader: strings.NewReader(secretsContent),
ContainerFilePath: secretsPath,
FileMode: 0o644,
}))
}

moduleOpts = append(moduleOpts, testcontainers.WithCmd(cmds...))

// append user-defined options
moduleOpts = append(moduleOpts, opts...)

container, err := testcontainers.Run(ctx, img, moduleOpts...)
var c *Container
if container != nil {
c = &Container{Container: container, tools: settings.tools}
}

if err != nil {
return c, fmt.Errorf("generic container: %w", err)
}

return c, nil
}

// GatewayEndpoint returns the endpoint for the DockerMCPGateway container.
// It uses the mapped port for the default port (8811/tcp) and the "http" protocol.
func (c *Container) GatewayEndpoint(ctx context.Context) (string, error) {
endpoint, err := c.PortEndpoint(ctx, defaultPort, "http")
if err != nil {
return "", fmt.Errorf("port endpoint: %w", err)
}

return endpoint, nil
}

// Tools returns the tools configured for the DockerMCPGateway container,
// indexed by server name.
// The keys are the server names and the values are slices of tool names.
func (c *Container) Tools() map[string][]string {
return c.tools
}
89 changes: 89 additions & 0 deletions modules/dockermcpgateway/dockermcpgateway_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package dockermcpgateway_test

import (
"context"
"io"
"testing"

"github.com/stretchr/testify/require"

"github.com/testcontainers/testcontainers-go"
dmcpg "github.com/testcontainers/testcontainers-go/modules/dockermcpgateway"
)

func TestDockerMCPGateway(t *testing.T) {
ctx := context.Background()

ctr, err := dmcpg.Run(ctx, "docker/mcp-gateway:latest")
testcontainers.CleanupContainer(t, ctr)
require.NoError(t, err)

require.Empty(t, ctr.Tools())
}

func TestDockerMCPGateway_withServerAndTools(t *testing.T) {
ctx := context.Background()

ctr, err := dmcpg.Run(
ctx, "docker/mcp-gateway:latest",
dmcpg.WithTools("curl", []string{"curl"}),
dmcpg.WithTools("brave", []string{"brave_local_search", "brave_web_search"}),
dmcpg.WithTools("github-official", []string{"add_issue_comment"}),
)
testcontainers.CleanupContainer(t, ctr)
require.NoError(t, err)

require.Len(t, ctr.Tools(), 3)

for server, tools := range ctr.Tools() {
switch server {
case "curl":
require.Equal(t, []string{"curl"}, tools)
case "brave":
require.ElementsMatch(t, []string{"brave_local_search", "brave_web_search"}, tools)
case "github-official":
require.Equal(t, []string{"add_issue_comment"}, tools)
default:
t.Errorf("unexpected server: %s", server)
}
}
}

func TestDockerMCPGateway_withSecret(t *testing.T) {
ctx := context.Background()

ctr, err := dmcpg.Run(
ctx, "docker/mcp-gateway:latest",
dmcpg.WithSecret("github.personal_access_token", "test_token"),
)
testcontainers.CleanupContainer(t, ctr)
require.NoError(t, err)

r, err := ctr.CopyFileFromContainer(ctx, "/testcontainers/app/secrets")
require.NoError(t, err)

bytes, err := io.ReadAll(r)
require.NoError(t, err)
require.Equal(t, "github.personal_access_token=test_token\n", string(bytes))
}

func TestDockerMCPGateway_withSecrets(t *testing.T) {
ctx := context.Background()

ctr, err := dmcpg.Run(
ctx, "docker/mcp-gateway:latest",
dmcpg.WithSecrets(map[string]string{
"github.personal_access_token": "test_token",
"another.secret": "another_value",
}),
)
testcontainers.CleanupContainer(t, ctr)
require.NoError(t, err)

r, err := ctr.CopyFileFromContainer(ctx, "/testcontainers/app/secrets")
require.NoError(t, err)

bytes, err := io.ReadAll(r)
require.NoError(t, err)
require.Equal(t, "github.personal_access_token=test_token\nanother.secret=another_value\n", string(bytes))
}
Loading
Loading