fix(server): use bound host for MCP endpoint (#3114)
This commit is contained in:
1 parent
52a24c8902
commit
0651af92b8
2 files changed
+40
-7
No files matched your search
@@ -8,16 +8,18 @@ import { ServerEnvironment } from "../environment/Services/ServerEnvironment.ts"
|
||||
import * as McpSessionRegistry from "./McpSessionRegistry.ts";
|
||||
|
||||
const environmentId = EnvironmentId.make("environment-1");
|
||||
const fakeHttpServer = HttpServer.HttpServer.of({
|
||||
address: { _tag: "TcpAddress", hostname: "127.0.0.1", port: 43123 },
|
||||
serve: (() => Effect.void) as HttpServer.HttpServer["Service"]["serve"],
|
||||
});
|
||||
const makeFakeHttpServer = (hostname: string, port = 43123) =>
|
||||
HttpServer.HttpServer.of({
|
||||
address: { _tag: "TcpAddress", hostname, port },
|
||||
serve: (() => Effect.void) as HttpServer.HttpServer["Service"]["serve"],
|
||||
});
|
||||
const fakeHttpServer = makeFakeHttpServer("127.0.0.1");
|
||||
const fakeEnvironment = ServerEnvironment.of({
|
||||
getEnvironmentId: Effect.succeed(environmentId),
|
||||
getDescriptor: Effect.die("unused"),
|
||||
});
|
||||
|
||||
const makeRegistry = (now: () => number) =>
|
||||
const makeRegistry = (now: () => number, httpServer = fakeHttpServer) =>
|
||||
McpSessionRegistry.__testing
|
||||
.make({
|
||||
now,
|
||||
@@ -25,7 +27,7 @@ const makeRegistry = (now: () => number) =>
|
||||
maximumLifetimeMs: 1_000,
|
||||
})
|
||||
.pipe(
|
||||
Effect.provideService(HttpServer.HttpServer, fakeHttpServer),
|
||||
Effect.provideService(HttpServer.HttpServer, httpServer),
|
||||
Effect.provideService(ServerEnvironment, fakeEnvironment),
|
||||
Effect.provide(NodeServices.layer),
|
||||
);
|
||||
@@ -53,6 +55,26 @@ it.effect("stores only a token hash, resolves the bearer token, and revokes by t
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("builds MCP endpoints from the bound server host", () =>
|
||||
Effect.gen(function* () {
|
||||
const cases = [
|
||||
["100.64.0.40", "http://100.64.0.40:43123/mcp"],
|
||||
["0.0.0.0", "http://127.0.0.1:43123/mcp"],
|
||||
["localhost", "http://localhost:43123/mcp"],
|
||||
["127.0.0.1", "http://127.0.0.1:43123/mcp"],
|
||||
] as const;
|
||||
|
||||
for (const [hostname, expectedEndpoint] of cases) {
|
||||
const registry = yield* makeRegistry(() => 1_000, makeFakeHttpServer(hostname));
|
||||
const issued = yield* registry.issue({
|
||||
threadId: ThreadId.make(`thread-${hostname}`),
|
||||
providerInstanceId: ProviderInstanceId.make("codex"),
|
||||
});
|
||||
expect(issued.config.endpoint).toBe(expectedEndpoint);
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("expires credentials after inactivity", () =>
|
||||
Effect.gen(function* () {
|
||||
let timestamp = 1_000;
|
||||
|
||||
@@ -60,6 +60,17 @@ const bytesToHex = (bytes: Uint8Array): string =>
|
||||
|
||||
const tokenFromBytes = (bytes: Uint8Array): string => Buffer.from(bytes).toString("base64url");
|
||||
|
||||
const getHttpMcpEndpointHost = (hostname: string): string => {
|
||||
const normalized = hostname.toLowerCase();
|
||||
const endpointHostname =
|
||||
normalized === "0.0.0.0" || normalized === "::" || normalized === "[::]"
|
||||
? "127.0.0.1"
|
||||
: hostname;
|
||||
return endpointHostname.includes(":") && !endpointHostname.startsWith("[")
|
||||
? `[${endpointHostname}]`
|
||||
: endpointHostname;
|
||||
};
|
||||
|
||||
const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
|
||||
options: McpSessionRegistryOptions = {},
|
||||
) {
|
||||
@@ -73,7 +84,7 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
|
||||
const maximumLifetimeMs = options.maximumLifetimeMs ?? DEFAULT_MAXIMUM_LIFETIME_MS;
|
||||
const endpoint =
|
||||
httpServer.address._tag === "TcpAddress"
|
||||
? `http://127.0.0.1:${httpServer.address.port}/mcp`
|
||||
? `http://${getHttpMcpEndpointHost(httpServer.address.hostname)}:${httpServer.address.port}/mcp`
|
||||
: "http://127.0.0.1/mcp";
|
||||
|
||||
const hashToken = (token: string) =>
|
||||
|
||||
Reference in new issue
Block a user