The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 25, 03:45 UTC
fix(server): use bound host for MCP endpoint (#3114)
Ishan committed
Jun 19, 2026 at 09:11 UTC
0651af92b88728785a39bd16e8d411ad741b681f
2 files changed
+40
-7
apps/server/src/mcp/McpSessionRegistry.test.ts
+28
-6
@@ -8,16 +8,18 @@ import { ServerEnvironment } from "../environment/Services/ServerEnvironment.ts"
8
import * as McpSessionRegistry from "./McpSessionRegistry.ts";
9
10
const environmentId = EnvironmentId.make("environment-1");
11
-const fakeHttpServer = HttpServer.HttpServer.of({
12
- address: { _tag: "TcpAddress", hostname: "127.0.0.1", port: 43123 },
13
- serve: (() => Effect.void) as HttpServer.HttpServer["Service"]["serve"],
14
-});
11
+const makeFakeHttpServer = (hostname: string, port = 43123) =>
12
+ HttpServer.HttpServer.of({
13
+ address: { _tag: "TcpAddress", hostname, port },
14
+ serve: (() => Effect.void) as HttpServer.HttpServer["Service"]["serve"],
15
+ });
16
+const fakeHttpServer = makeFakeHttpServer("127.0.0.1");
17
const fakeEnvironment = ServerEnvironment.of({
18
getEnvironmentId: Effect.succeed(environmentId),
19
getDescriptor: Effect.die("unused"),
20
});
21
20
-const makeRegistry = (now: () => number) =>
22
+const makeRegistry = (now: () => number, httpServer = fakeHttpServer) =>
23
McpSessionRegistry.__testing
24
.make({
25
now,
@@ -25,7 +27,7 @@ const makeRegistry = (now: () => number) =>
27
maximumLifetimeMs: 1_000,
28
})
29
.pipe(
28
- Effect.provideService(HttpServer.HttpServer, fakeHttpServer),
30
+ Effect.provideService(HttpServer.HttpServer, httpServer),
31
Effect.provideService(ServerEnvironment, fakeEnvironment),
32
Effect.provide(NodeServices.layer),
33
);
@@ -53,6 +55,26 @@ it.effect("stores only a token hash, resolves the bearer token, and revokes by t
55
}),
56
);
57
58
+it.effect("builds MCP endpoints from the bound server host", () =>
59
+ Effect.gen(function* () {
60
+ const cases = [
61
+ ["100.64.0.40", "http://100.64.0.40:43123/mcp"],
62
+ ["0.0.0.0", "http://127.0.0.1:43123/mcp"],
63
+ ["localhost", "http://localhost:43123/mcp"],
64
+ ["127.0.0.1", "http://127.0.0.1:43123/mcp"],
65
+ ] as const;
66
+
67
+ for (const [hostname, expectedEndpoint] of cases) {
68
+ const registry = yield* makeRegistry(() => 1_000, makeFakeHttpServer(hostname));
69
+ const issued = yield* registry.issue({
70
+ threadId: ThreadId.make(`thread-${hostname}`),
71
+ providerInstanceId: ProviderInstanceId.make("codex"),
72
+ });
73
+ expect(issued.config.endpoint).toBe(expectedEndpoint);
74
+ }
75
+ }),
76
+);
77
+
78
it.effect("expires credentials after inactivity", () =>
79
Effect.gen(function* () {
80
let timestamp = 1_000;
apps/server/src/mcp/McpSessionRegistry.ts
+12
-1
@@ -60,6 +60,17 @@ const bytesToHex = (bytes: Uint8Array): string =>
60
61
const tokenFromBytes = (bytes: Uint8Array): string => Buffer.from(bytes).toString("base64url");
62
63
+const getHttpMcpEndpointHost = (hostname: string): string => {
64
+ const normalized = hostname.toLowerCase();
65
+ const endpointHostname =
66
+ normalized === "0.0.0.0" || normalized === "::" || normalized === "[::]"
67
+ ? "127.0.0.1"
68
+ : hostname;
69
+ return endpointHostname.includes(":") && !endpointHostname.startsWith("[")
70
+ ? `[${endpointHostname}]`
71
+ : endpointHostname;
72
+};
73
+
74
const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
75
options: McpSessionRegistryOptions = {},
76
) {
@@ -73,7 +84,7 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
84
const maximumLifetimeMs = options.maximumLifetimeMs ?? DEFAULT_MAXIMUM_LIFETIME_MS;
85
const endpoint =
86
httpServer.address._tag === "TcpAddress"
76
- ? `http://127.0.0.1:${httpServer.address.port}/mcp`
87
+ ? `http://${getHttpMcpEndpointHost(httpServer.address.hostname)}:${httpServer.address.port}/mcp`
88
: "http://127.0.0.1/mcp";
89
90
const hashToken = (token: string) =>