Skip to content

Commit 6fe96e9

Browse files
authored
Add network transport section (#63)
1 parent fd04abf commit 6fe96e9

File tree

1 file changed

+71
-25
lines changed

1 file changed

+71
-25
lines changed

docs/toolhive/guides-cli/run-mcp-servers.mdx

Lines changed: 71 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ To run an MCP server from the [ToolHive registry](./registry.md), use the
1717
want to run. The server name is the same as its name in the registry.
1818

1919
```bash
20-
thv run <server-name>
20+
thv run <SERVER_NAME>
2121
```
2222

23-
For example:
23+
For example, to run the `fetch` server, which is a simple MCP server that
24+
fetches website contents:
2425

2526
```bash
2627
thv run fetch
@@ -32,18 +33,18 @@ When you run an MCP server from the registry, ToolHive:
3233

3334
1. Pulls the image and launches a container using the configuration from the
3435
registry.
35-
2. Launches an HTTP proxy on a random port to forward client requests to the
36-
container.
36+
2. Starts an HTTP proxy process on a random port to forward client requests to
37+
the container.
3738
3. Labels the container so it can be tracked by ToolHive:
3839
```yaml
3940
toolhive: true
40-
toolhive-name: <server-name>
41+
toolhive-name: <SERVER_NAME>
4142
```
4243
4344
:::
4445
45-
See [Running custom MCP servers](#run-a-custom-mcp-server) to run a server that
46-
is not in the registry.
46+
See [Run a custom MCP server](#run-a-custom-mcp-server) to run a server that is
47+
not in the registry.
4748
4849
## Customize server settings
4950
@@ -62,7 +63,7 @@ is automatically generated from the image name when you run a custom server. To
6263
give your server instance a custom name, use the `--name` option:
6364

6465
```bash
65-
thv run --name my-custom-name <server-name>
66+
thv run --name <FRIENDLY_NAME> <SERVER>
6667
```
6768

6869
For example:
@@ -80,7 +81,7 @@ starting the server.
8081
To pass a secret to an MCP server, use the `--secret` option:
8182

8283
```bash
83-
thv run --secret <secret-name>,target=<env-var-name> <server-name>
84+
thv run --secret <SECRET_NAME>,target=<ENV_VAR_NAME> <SERVER>
8485
```
8586

8687
The `target` parameter specifies the name of the environment variable in the MCP
@@ -121,20 +122,20 @@ The filesystem server can now access the contents of `~/code/toolhive` as
121122
`/projects/toolhive` in the container (the filesystem MCP server expects
122123
directories to be mounted at `/projects` by default).
123124

124-
### Pass additional arguments
125+
### Add command-line arguments
125126

126127
Some MCP servers require additional arguments to run correctly. You can pass
127128
these arguments after the server name in the
128129
[`thv run`](../reference/cli/thv_run.md) command:
129130

130131
```bash
131-
thv run <server-name> -- <additional-args>
132+
thv run <SERVER> -- <ARGS>
132133
```
133134

134-
For example, to run the `fetch` server with custom arguments:
135+
For example:
135136

136137
```bash
137-
thv run fetch -- --arg1 value1 --arg2 value2
138+
thv run my-mcp-server:latest -- --arg1 value1 --arg2 value2
138139
```
139140

140141
Check the MCP server's documentation for the required arguments.
@@ -146,7 +147,7 @@ container. This is the port that client applications connect to. To set a
146147
specific proxy port instead, use the `--proxy-port` flag:
147148

148149
```bash
149-
thv run --proxy-port <port-number> <server-name>
150+
thv run --proxy-port <PORT_NUMBER> <SERVER>
150151
```
151152

152153
## Run a custom MCP server
@@ -195,7 +196,7 @@ name for the server instance, the transport method, and any additional arguments
195196
required by the MCP server.
196197

197198
```bash
198-
thv run [--name <friendly-name>] [--transport <stdio/sse/streamable-http>] <image-name:tag> -- <server-args>
199+
thv run [--name <FRIENDLY_NAME>] [--transport <stdio/sse/streamable-http>] <IMAGE_REFERENCE> -- <ARGS>
199200
```
200201

201202
For example, to run an MCP server from a Docker image named
@@ -215,7 +216,7 @@ When you run an MCP server from a Docker image, ToolHive:
215216
1. Pulls the image (`my-mcp-server-image:latest`) and launches a container with
216217
the options and arguments you specified.
217218
2. Launches an HTTP proxy on a random port (optionally, add
218-
`--proxy-port <port-number>` to specify the port).
219+
`--proxy-port <PORT_NUMBER>` to specify the port).
219220
3. Labels the container so it can be tracked by ToolHive:
220221
```yaml
221222
toolhive: true
@@ -241,7 +242,7 @@ Currently, three protocol schemes are supported:
241242
- `go://`: For Go-based MCP servers
242243

243244
```bash
244-
thv run <protocol>://<package-name>@<version>
245+
thv run <uvx|npx|go>://<PACKAGE_NAME>@<VERSION|latest>
245246
```
246247

247248
You'll likely need to specify additional arguments like the transport method,
@@ -313,6 +314,55 @@ thv run go:///path/to/my-go-project
313314
</TabItem>
314315
</Tabs>
315316

317+
### Configure network transport
318+
319+
When you run custom MCP servers using the SSE (`--transport sse`) or Streamable
320+
HTTP (`--transport streamable-http`) transport method, ToolHive automatically
321+
selects a random port to expose from the container to the host and sets the
322+
`MCP_PORT` and `FASTMCP_PORT` environment variables in the container.
323+
324+
```mermaid
325+
graph LR
326+
A[MCP client] -->|HTTP request to<br/>proxy port: 5432| B[ToolHive proxy<br/>process<br/>Port: 5432]
327+
B -->|Forwards to<br/>host port: 9876| C[<strong>MCP container</strong><br/>Host: 9876 → Container: 7654<br/>ENV: MCP_PORT=7654]
328+
C -->|Response| B
329+
B -->|Response| A
330+
```
331+
332+
This is equivalent to running a Docker container with
333+
`docker run -p <random_host_port>:<random_container_port> ...`
334+
335+
For MCP servers that use a specific port or don't recognize those environment
336+
variables, specify the container port for ToolHive to expose using the
337+
`--target-port` flag:
338+
339+
```bash
340+
thv run --transport streamable-http --target-port <PORT_NUMBER> <SERVER>
341+
```
342+
343+
ToolHive still maps the container port to a random port on the host to avoid
344+
conflicts with commonly used ports. This is equivalent to running a Docker
345+
container with `docker run -p <random_port>:<PORT_NUMBER> ...`
346+
347+
```mermaid
348+
graph LR
349+
A[MCP client] -->|HTTP request to<br/>proxy port: 5432| B[ToolHive proxy<br/>process<br/>Port: 5432]
350+
B -->|Forwards to<br/>host port: 9876| C["<strong>MCP container</strong><br/>(--target-port 3000)<br/>Host: 9876 → Container: 3000<br/>ENV: MCP_PORT=3000"]
351+
C -->|Response| B
352+
B -->|Response| A
353+
```
354+
355+
Some MCP servers use command-line arguments to specify their transport and port.
356+
For example, if your server expects the transport type as a positional argument
357+
and requires the `--port` flag, you can pass it like this:
358+
359+
```bash
360+
thv run --transport streamable-http --target-port <PORT_NUMBER> <SERVER> -- http --port <PORT_NUMBER>
361+
```
362+
363+
Check your MCP server's documentation for the required transport and port
364+
configuration.
365+
316366
### Add a custom CA certificate
317367

318368
In corporate environments with TLS inspection or custom certificate authorities,
@@ -409,7 +459,7 @@ If a server starts but isn't accessible:
409459
1. Check the server logs:
410460

411461
```bash
412-
thv logs <server-name>
462+
thv logs <SERVER_NAME>
413463
```
414464

415465
2. Verify the port isn't blocked by a firewall
@@ -433,15 +483,11 @@ If a server crashes or exits unexpectedly:
433483
2. Check the logs for error messages:
434484

435485
```bash
436-
thv logs <server-name>
486+
thv logs <SERVER_NAME>
437487
```
438488

439-
3. Try running with a different permission profile:
440-
441-
```bash
442-
thv run --permission-profile network <server-name>
443-
```
489+
3. Check if the server requires any secrets or environment variables
444490

445-
4. Check if the server requires any secrets or environment variables
491+
4. Verify the server's configuration and arguments
446492

447493
</details>

0 commit comments

Comments
 (0)