Skip to content

Commit 9ad7813

Browse files
fix(server): a long or malformed sender title no longer breaks peer calls
The forwarded sender title went into an HTTP header unbounded, so a long title pushed the request past the peer's header limit (431), and an unpaired surrogate made encodeURIComponent throw before the call was sent. The title is now cut to 200 UTF-16 units and made well-formed before encoding, which keeps the header under about 1.8 KB. AI-assisted; verified with the tests and tools listed above.
1 parent 05dc0b4 commit 9ad7813

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

‎apps/server/src/peer/PeerMcpClient.test.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NodeHttpServer } from "@effect/platform-node";
22
import { expect, it } from "@effect/vitest";
3-
import { OrchestratorMcpFailure } from "@t3tools/contracts";
3+
import { OrchestratorMcpFailure, ThreadId } from "@t3tools/contracts";
44
import * as Context from "effect/Context";
55
import * as DateTime from "effect/DateTime";
66
import * as Deferred from "effect/Deferred";
@@ -143,10 +143,12 @@ it.effect("renews a missing peer session once and reuses its replacement", () =>
143143
const sessionHeaders: Array<string | undefined> = [];
144144
const statuses = [200, 202, 404, 200, 202, 200, 200, 200];
145145
const outcomes: Array<string | null> = [];
146+
const titleHeaders: Array<string | undefined> = [];
146147
const http = HttpClient.make((request) =>
147148
Effect.sync(() => {
148149
const index = sessionHeaders.length;
149150
sessionHeaders.push(request.headers["mcp-session-id"]);
151+
titleHeaders.push(request.headers["t3-sender-title"]);
150152
const status = statuses[index];
151153
if (status === undefined) throw new Error("Unexpected retry");
152154
return HttpClientResponse.fromWeb(
@@ -203,17 +205,25 @@ it.effect("renews a missing peer session once and reuses its replacement", () =>
203205
),
204206
);
205207
const client = Context.get(context, PeerMcpClient.PeerMcpClient);
206-
for (let call = 0; call < 2; call++) {
208+
// Any title a rename accepts must fit in a header without failing the call.
209+
for (const title of ["界".repeat(2000), "x\ud800"]) {
207210
expect(
208211
yield* client.call({
209212
environmentId: box.environmentId,
210213
tool: "probe",
211214
arguments: {},
212215
limits: { runtimeMode: "approval-required", interactionMode: "plan" },
216+
sender: { environmentId: box.environmentId, threadId: ThreadId.make("thread:a"), title },
213217
success: Result,
214218
}),
215219
).toEqual({ modes: "approval-required/plan" });
216220
}
221+
const longTitle = encodeURIComponent("界".repeat(200));
222+
expect(titleHeaders.filter((title) => title !== undefined)).toEqual([
223+
longTitle,
224+
longTitle,
225+
"x%EF%BF%BD",
226+
]);
217227
// An answer with neither a result nor an error is the link's problem, not the call's.
218228
const empty = yield* client
219229
.call({

‎apps/server/src/peer/PeerMcpClient.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,10 @@ const make = Effect.gen(function* () {
172172
...(input.sender.title === undefined
173173
? {}
174174
: {
175+
// Bounded so a long title stays under the peer's header limit; well-formed
176+
// because encodeURIComponent throws on an unpaired surrogate.
175177
[McpInvocationContext.SENDER_TITLE_HEADER]: encodeURIComponent(
176-
input.sender.title,
178+
input.sender.title.slice(0, 200).toWellFormed(),
177179
),
178180
}),
179181
};

0 commit comments

Comments
 (0)