VSCode-Perplexity-MCP icon indicating copy to clipboard operation
VSCode-Perplexity-MCP copied to clipboard

Fix available for stdio MCP startup and search-mode docs

Open juangrukat opened this issue 7 months ago • 1 comments

Hi! I ran into a Codex/MCP startup issue while using this repo and prepared a small fix branch here:

https://github.com/juangrukat/VSCode-Perplexity-MCP/tree/codex/mcp-stdio-docs

Compare link: https://github.com/Automations-Project/VSCode-Perplexity-MCP/compare/main...juangrukat:VSCode-Perplexity-MCP:codex/mcp-stdio-docs

Summary of the changes:

  • Fixes a stdio lifecycle issue where the MCP server can start, print its profile line, and then exit before the client finishes the MCP handshake.
  • Keeps the stdio server alive until stdin closes, then disposes the reinit watcher and shuts down the Perplexity client.
  • Adds a regression test for this startup behavior.
  • Updates README docs with web, scholar, and social source modes; shorthand examples for search/ask/reason/research/ASI; defaults; request-flow notes; and optimization opportunities.

Validation run locally:

  • npx vitest run packages/mcp-server/test/index-startup.test.ts packages/mcp-server/test/daemon/attach.test.js packages/mcp-server/test/daemon/server.integration.test.js packages/mcp-server/test/cli.test.js
  • npm run typecheck -w perplexity-user-mcp
  • npm run build -w perplexity-user-mcp
  • Direct stdio smoke test listed all 14 MCP tools
  • Codex MCP discovery loaded Perplexity tools after restart
  • Live perplexity_search call succeeded with citations/thread URL

I tried to open a PR from the fork using gh pr create, but GitHub returned CreatePullRequest permission denied for my token. If maintainers prefer, I can open it manually from the compare link above.

juangrukat avatar May 02 '26 16:05 juangrukat

Thanks @juangrukat — really appreciate the quality of this contribution. The diagnosis is correct and the fix matches the pattern we already use in packages/mcp-server/src/daemon/attach.ts (sourceIn.on("end" / "close", ...)), so bringing the in-process stdio path into parity is exactly right.

Some context on what I observed when reviewing:

  • The lifecycle gap is real and slightly broader than "exit before handshake": watchReinit uses { persistent: false } by design, and the SDK's StdioServerTransport.start() only registers a 'data' listener. Once the client closes its end of stdin, neither pins the loop and the process exits without disposing the watcher or shutting down the browser context — leaking patchright/keytar resources on every disconnect.
  • I cross-checked all the README claims (sources: ["web","scholar","social"], ["web"] default, PERPLEXITY_*_MODEL env vars, model fallbacks, the /rest/sse/perplexity_ask endpoint) against tools.ts / client.ts — every claim is accurate.

We're a pre-public repo committing directly to main, so I cherry-picked your commit (authorship preserved) rather than asking you to wrestle with cross-fork PR permissions. I also pushed a small follow-up commit hardening the shutdown path with a 5s timeout — Promise.race(client.shutdown(), timer) applied to all three exit paths (stdin-close, SIGINT, SIGTERM) plus the fatal-error catch, so a wedged browser context can't pin the process forever. The timer is unref()'d so it doesn't itself extend lifetime.

Both commits on main:

  • f32090e — your fix (Author: juangrukat)
  • 57080fa — shutdown timeout follow-up

npm run typecheck -w perplexity-user-mcp and the full mcp-server test suite (567/567) pass.

Closing this as resolved. Thanks again — the diff was a pleasure to review.

ARHAEEM avatar May 02 '26 17:05 ARHAEEM