-
-
Notifications
You must be signed in to change notification settings - Fork 31
fix: route MCP sessions to their owning Fly machine and 404 lost sessions #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,11 @@ import { | |
| IAPKIT_MCP_SERVER_NAME, | ||
| IAPKIT_MCP_SERVER_VERSION, | ||
| } from "./mcp.js"; | ||
| import { | ||
| buildSessionId, | ||
| currentMachineId, | ||
| routeUnknownSession, | ||
| } from "./session-routing.js"; | ||
|
|
||
| const DEFAULT_MCP_PATH = "/mcp"; | ||
| const DEFAULT_PORT = 3939; | ||
|
|
@@ -48,6 +53,13 @@ export interface RemoteMcpHttpServerOptions { | |
| allowedOrigins?: string[]; | ||
| /** Logger for lifecycle and request failures. Defaults to console. */ | ||
| logger?: Pick<Console, "error" | "info">; | ||
| /** | ||
| * Identity of this process for session affinity. Defaults to | ||
| * FLY_MACHINE_ID; session ids are prefixed with it so a follow-up | ||
| * request landing on a sibling machine can be replayed to the owner | ||
| * (GitHub issue #287). Undefined disables replay routing. | ||
| */ | ||
| machineId?: string; | ||
| } | ||
|
|
||
| /** Runtime handle for an IAPKit remote MCP HTTP server. */ | ||
|
|
@@ -72,6 +84,7 @@ export function createRemoteMcpHttpServer( | |
| const allowedOrigins = | ||
| options.allowedOrigins ?? | ||
| parseAllowedOrigins(process.env.IAPKIT_MCP_ALLOWED_ORIGINS); | ||
| const machineId = options.machineId ?? currentMachineId(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Validate configured machine IDs in both handlers. Both handlers accept an explicit
📍 Affects 2 files
🤖 Prompt for AI Agents |
||
| const transports = new Map<string, StreamableHTTPServerTransport>(); | ||
|
|
||
| const server = createServer(async (req, res) => { | ||
|
|
@@ -134,12 +147,13 @@ export function createRemoteMcpHttpServer( | |
| res, | ||
| transports, | ||
| logger, | ||
| machineId, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| if (req.method === "GET" || req.method === "DELETE") { | ||
| await handleExistingMcpSession(req, res, transports); | ||
| await handleExistingMcpSession(req, res, transports, machineId); | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -223,6 +237,7 @@ async function handleMcpPost( | |
| res: ServerResponse, | ||
| transports: Map<string, StreamableHTTPServerTransport>, | ||
| logger: Pick<Console, "error" | "info">, | ||
| machineId: string | undefined, | ||
| ): Promise<void> { | ||
| const sessionId = headerString(req.headers["mcp-session-id"]); | ||
| const body = await readJsonBody(req); | ||
|
|
@@ -233,7 +248,12 @@ async function handleMcpPost( | |
| return; | ||
| } | ||
|
|
||
| if (sessionId || !isInitializeRequest(body)) { | ||
| if (sessionId) { | ||
| writeUnknownSessionResponse(req, res, sessionId, machineId); | ||
| return; | ||
| } | ||
|
|
||
| if (!isInitializeRequest(body)) { | ||
| writeJsonRpcError( | ||
| res, | ||
| 400, | ||
|
|
@@ -245,7 +265,7 @@ async function handleMcpPost( | |
|
|
||
| let transport!: StreamableHTTPServerTransport; | ||
| transport = new StreamableHTTPServerTransport({ | ||
| sessionIdGenerator: () => randomUUID(), | ||
| sessionIdGenerator: () => buildSessionId(machineId, randomUUID()), | ||
| onsessioninitialized: (initializedSessionId) => { | ||
| transports.set(initializedSessionId, transport); | ||
| logger.info(`IAPKit MCP session initialized: ${initializedSessionId}`); | ||
|
|
@@ -269,18 +289,60 @@ async function handleExistingMcpSession( | |
| req: IncomingMessage, | ||
| res: ServerResponse, | ||
| transports: Map<string, StreamableHTTPServerTransport>, | ||
| machineId: string | undefined, | ||
| ): Promise<void> { | ||
| const sessionId = headerString(req.headers["mcp-session-id"]); | ||
| const transport = sessionId ? transports.get(sessionId) : undefined; | ||
|
|
||
| if (!transport) { | ||
| if (sessionId) { | ||
| writeUnknownSessionResponse(req, res, sessionId, machineId); | ||
| return; | ||
| } | ||
| writeJsonRpcError(res, 400, -32000, "Invalid or missing mcp-session-id"); | ||
| return; | ||
| } | ||
|
|
||
| await transport.handleRequest(req as AuthenticatedRequest, res); | ||
| } | ||
|
|
||
| /** | ||
| * Answers a request whose session id isn't in this process's transport | ||
| * map: replay it to the machine that minted the id when possible, | ||
| * otherwise 404 so a spec-compliant client transparently re-initializes. | ||
| * (The previous 400 "initialize first" reply broke that recovery path — | ||
| * GitHub issue #287.) | ||
| */ | ||
| function writeUnknownSessionResponse( | ||
| req: IncomingMessage, | ||
| res: ServerResponse, | ||
| sessionId: string, | ||
| machineId: string | undefined, | ||
| ): void { | ||
| const routing = routeUnknownSession({ | ||
| sessionId, | ||
| machineId, | ||
| alreadyReplayed: req.headers["fly-replay-src"] !== undefined, | ||
| }); | ||
|
|
||
| if (routing.action === "replay") { | ||
| // Fly's proxy intercepts any response carrying `fly-replay` and | ||
| // re-sends the original request to the named machine; the client | ||
| // never sees this interim response. | ||
| res | ||
| .writeHead(204, { "fly-replay": `instance=${routing.targetMachineId}` }) | ||
| .end(); | ||
| return; | ||
| } | ||
|
|
||
| writeJsonRpcError( | ||
| res, | ||
| 404, | ||
| -32001, | ||
| "Session not found — initialize a new MCP session.", | ||
| ); | ||
| } | ||
|
|
||
| function attachAuthInfo(req: AuthenticatedRequest): void { | ||
| const bearerToken = parseBearerToken(headerString(req.headers.authorization)); | ||
| if (!bearerToken) return; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // MCP session ids are held in per-process memory (the transport object | ||
| // itself is stateful — an SSE stream can't be serialized into a shared | ||
| // store), so a session created on one Fly machine is invisible to its | ||
| // siblings. Fix (GitHub issue #287): embed the creating machine's id in | ||
| // the session id, and when a request lands on the wrong machine, answer | ||
| // with a `fly-replay` header so Fly's proxy re-routes the original | ||
| // request to the owner. Off Fly (no FLY_MACHINE_ID) session ids stay | ||
| // plain UUIDs and routing always resolves to `not-found`. | ||
|
|
||
| /** | ||
| * Fly machine ids are lowercase hex today, but only shape-check them: | ||
| * the prefix is attacker-controlled (it arrives inside the client's | ||
| * `mcp-session-id` header), so the pattern also guards the value we | ||
| * echo back inside the `fly-replay` response header. | ||
| */ | ||
| const MACHINE_ID_PATTERN = /^[A-Za-z0-9]{1,32}$/; | ||
|
|
||
| const SESSION_MACHINE_SEPARATOR = "."; | ||
|
|
||
| /** Reads the Fly machine identity, or undefined when not running on Fly. */ | ||
| export function currentMachineId( | ||
| env: Record<string, string | undefined> = process.env, | ||
| ): string | undefined { | ||
| const raw = env.FLY_MACHINE_ID; | ||
| return raw && MACHINE_ID_PATTERN.test(raw) ? raw : undefined; | ||
| } | ||
|
|
||
| /** Builds a session id that carries the creating machine's identity. */ | ||
| export function buildSessionId( | ||
| machineId: string | undefined, | ||
| uuid: string, | ||
| ): string { | ||
| return machineId ? `${machineId}${SESSION_MACHINE_SEPARATOR}${uuid}` : uuid; | ||
| } | ||
|
|
||
| /** Routing decision for a session id this process doesn't recognize. */ | ||
| export type UnknownSessionRouting = | ||
| | { action: "replay"; targetMachineId: string } | ||
| | { action: "not-found" }; | ||
|
|
||
| /** | ||
| * Decides what to do with a session id that isn't in the local | ||
| * transport map. | ||
| * | ||
| * @param options.sessionId Session id from the `mcp-session-id` header. | ||
| * @param options.machineId This process's machine id (undefined off Fly). | ||
| * @param options.alreadyReplayed True when the request carries | ||
| * `fly-replay-src`, i.e. it was already replayed once — never replay | ||
| * again or two stale machines could bounce a request forever. | ||
| * @returns `replay` toward the owning machine, or `not-found` (the | ||
| * caller answers 404 so the client re-initializes per the MCP spec). | ||
| */ | ||
| export function routeUnknownSession(options: { | ||
| sessionId: string; | ||
| machineId: string | undefined; | ||
| alreadyReplayed: boolean; | ||
| }): UnknownSessionRouting { | ||
| if (!options.machineId || options.alreadyReplayed) { | ||
| return { action: "not-found" }; | ||
| } | ||
|
|
||
| const separatorIndex = options.sessionId.indexOf(SESSION_MACHINE_SEPARATOR); | ||
| if (separatorIndex <= 0) return { action: "not-found" }; | ||
|
|
||
| const prefix = options.sessionId.slice(0, separatorIndex); | ||
| if (!MACHINE_ID_PATTERN.test(prefix) || prefix === options.machineId) { | ||
| // Malformed prefix, or the session was minted by this very machine | ||
| // (map lost to a restart/deploy) — replaying to ourselves would loop. | ||
| return { action: "not-found" }; | ||
| } | ||
|
|
||
| return { action: "replay", targetMachineId: prefix }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Mirror the dependency filters for pull requests.
The
pull_request.pathslist omitsbun.lock,package.json, and.github/workflows/deploy-kit.yml. A pull request that changes only one of these files skips this workflow, including the MCP server suite. Add the same dependency and workflow paths used bypush.🤖 Prompt for AI Agents