Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down
4 changes: 3 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading