Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/report-bound-http-port.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@upstash/context7-mcp": patch
---

Report the actual assigned HTTP port when `--port 0` requests an ephemeral port.
7 changes: 6 additions & 1 deletion packages/mcp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,13 @@ async function main() {
});

httpServer.once("listening", () => {
const address = httpServer.address();
if (!address || typeof address === "string") {
console.error("Failed to determine the bound HTTP port");
process.exit(1);
}
console.error(
`Context7 Documentation MCP Server v${SERVER_VERSION} running on HTTP at http://localhost:${port}/mcp`
`Context7 Documentation MCP Server v${SERVER_VERSION} running on HTTP at http://localhost:${address.port}/mcp`
);
});
};
Expand Down
24 changes: 18 additions & 6 deletions packages/mcp/test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,12 @@ function startStubApi(): Promise<string> {
});
}

function startHttpChild(): Promise<{ child: ChildProcess; url: string }> {
function startHttpChild(port = BASE_PORT): Promise<{ child: ChildProcess; url: string }> {
return new Promise((resolve, reject) => {
const child = spawn(
process.execPath,
[DIST, "--transport", "http", "--port", String(BASE_PORT)],
{ env: childEnv, stdio: ["ignore", "ignore", "pipe"] }
);
const child = spawn(process.execPath, [DIST, "--transport", "http", "--port", String(port)], {
env: childEnv,
stdio: ["ignore", "ignore", "pipe"],
});
let stderr = "";
child.stderr!.on("data", (chunk: Buffer) => {
stderr += chunk.toString();
Expand Down Expand Up @@ -105,6 +104,19 @@ afterAll(() => {
stubServer?.close();
});

test("reports the bound port when port zero requests an ephemeral port", async () => {
const { child, url } = await startHttpChild(0);
try {
expect(new URL(url).port).not.toBe("0");

const response = await fetch(new URL("/ping", url));
expect(response.ok).toBe(true);
await expect(response.json()).resolves.toMatchObject({ status: "ok" });
} finally {
child.kill();
}
});

async function connect(transportKind: "http" | "stdio", era: "modern" | "legacy") {
const client = new Client(
{ name: "test-harness", version: "1.0.0" },
Expand Down