Skip to content

Commit fb00bab

Browse files
authored
feat(cli): add --host option and bind host for SSE/streamable (#13)
- Add --host/-H CLI flag and plumb through index -> server -> services - Support explicit host binding via Express (app.listen(port, host)) - Improve startup logs to show actual host
1 parent a965237 commit fb00bab

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

src/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ const { values } = parseArgs({
1919
short: "p",
2020
default: "3033",
2121
},
22+
host: {
23+
type: "string",
24+
short: "H",
25+
default: "",
26+
},
2227
endpoint: {
2328
type: "string",
2429
short: "e",
@@ -39,6 +44,7 @@ MCP Mermaid CLI
3944
Options:
4045
--transport, -t Specify the transport protocol: "stdio", "sse", or "streamable" (default: "stdio")
4146
--port, -p Specify the port for SSE or streamable transport (default: 3033)
47+
--host, -H Specify the host to bind (e.g., 0.0.0.0)
4248
--endpoint, -e Specify the endpoint for the transport:
4349
- For SSE: default is "/sse"
4450
- For streamable: default is "/mcp"
@@ -54,12 +60,14 @@ if (transport === "sse") {
5460
const port = Number.parseInt(values.port as string, 10);
5561
// Use provided endpoint or default to "/sse" for SSE
5662
const endpoint = values.endpoint || "/sse";
57-
runSSEServer(endpoint, port).catch(console.error);
63+
const host = (values.host as string) || undefined;
64+
runSSEServer(endpoint, port, host).catch(console.error);
5865
} else if (transport === "streamable") {
5966
const port = Number.parseInt(values.port as string, 10);
6067
// Use provided endpoint or default to "/mcp" for streamable
6168
const endpoint = values.endpoint || "/mcp";
62-
runHTTPStreamableServer(endpoint, port).catch(console.error);
69+
const host = (values.host as string) || undefined;
70+
runHTTPStreamableServer(endpoint, port, host).catch(console.error);
6371
} else {
6472
runStdioServer().catch(console.error);
6573
}

src/server.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,10 @@ export async function runStdioServer(): Promise<void> {
125125
export async function runSSEServer(
126126
endpoint = "/sse",
127127
port = 3033,
128+
host?: string,
128129
): Promise<void> {
129130
const server = createServer();
130-
await startSSEMcpServer(server, endpoint, port);
131+
await startSSEMcpServer(server, endpoint, port, host);
131132
}
132133

133134
/**
@@ -136,6 +137,7 @@ export async function runSSEServer(
136137
export async function runHTTPStreamableServer(
137138
endpoint = "/mcp",
138139
port = 3033,
140+
host?: string,
139141
): Promise<void> {
140-
await startHTTPStreamableServer(createServer, endpoint, port);
142+
await startHTTPStreamableServer(createServer, endpoint, port, host);
141143
}

src/services/sse.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const startSSEMcpServer = async (
77
server: Server,
88
endpoint = "/sse",
99
port = 3033,
10+
host?: string,
1011
): Promise<void> => {
1112
const app = express();
1213
app.use(express.json());
@@ -39,7 +40,12 @@ export const startSSEMcpServer = async (
3940
}
4041
});
4142

42-
app.listen(port, () => {
43-
console.log(`SSE Server listening on http://localhost:${port}${endpoint}`);
44-
});
43+
const cb = () => {
44+
const shownHost = host || "localhost";
45+
console.log(
46+
`SSE Server listening on http://${shownHost}:${port}${endpoint}`,
47+
);
48+
};
49+
if (host) app.listen(port, host, cb);
50+
else app.listen(port, cb);
4551
};

src/services/streamable.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const startHTTPStreamableServer = async (
88
createServer: () => Server,
99
endpoint = "/mcp",
1010
port = 1122,
11+
host?: string,
1112
): Promise<void> => {
1213
const app = express();
1314
app.use(express.json());
@@ -52,9 +53,12 @@ export const startHTTPStreamableServer = async (
5253
});
5354
});
5455

55-
app.listen(port, () => {
56+
const cb = () => {
57+
const shownHost = host || "localhost";
5658
console.log(
57-
`Streamable HTTP Server listening on http://localhost:${port}${endpoint}`,
59+
`Streamable HTTP Server listening on http://${shownHost}:${port}${endpoint}`,
5860
);
59-
});
61+
};
62+
if (host) app.listen(port, host, cb);
63+
else app.listen(port, cb);
6064
};

0 commit comments

Comments
 (0)