From 6b4bbc36741919bdd90cd68a49641309ca7d6bec Mon Sep 17 00:00:00 2001 From: Saleh Alghusson <1331721+qirh@users.noreply.github.com> Date: Mon, 19 Oct 2026 20:49:06 +0000 Subject: [PATCH] fix: preserve PERPLEXITY_BASE_URL query parameters when joining paths --- src/index.test.ts | 21 +++++++++++++++++++++ src/server.ts | 4 +++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/index.test.ts b/src/index.test.ts index e08c59c..782b7f1 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -579,6 +579,27 @@ describe("Perplexity MCP Server", () => { } }); + it("should keep PERPLEXITY_BASE_URL path and query on the agent path", async () => { + vi.resetModules(); + process.env.PERPLEXITY_BASE_URL = "https://proxy.example.com/api/?key=x"; + try { + const serverModule = await import("./server.js"); + global.fetch = vi + .fn() + .mockResolvedValue(sseResponse([completedEvent("ok")])); + + await serverModule.performAgentResponse(TEST_MESSAGES, "fast"); + + expect(global.fetch).toHaveBeenCalledWith( + "https://proxy.example.com/api/v1/agent?key=x", + expect.anything() + ); + } finally { + delete process.env.PERPLEXITY_BASE_URL; + vi.resetModules(); + } + }); + it("should skip malformed stream chunks and still complete", async () => { const payload = "data: this-is-not-json\n\n" + diff --git a/src/server.ts b/src/server.ts index 0c1941a..b6ec968 100644 --- a/src/server.ts +++ b/src/server.ts @@ -94,7 +94,9 @@ async function makeApiRequest( // Read timeout fresh each time to respect env var changes const TIMEOUT_MS = parseInt(process.env.PERPLEXITY_TIMEOUT_MS || "300000", 10); - const url = new URL(`${PERPLEXITY_BASE_URL}/${endpoint}`); + // Append to the base path so a query string in the base URL is preserved. + const url = new URL(PERPLEXITY_BASE_URL); + url.pathname = `${url.pathname.replace(/\/$/, "")}/${endpoint}`; const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), TIMEOUT_MS); // An abort from the caller's signal also tears down an in-flight body stream.