Repository navigation
feat: add perplexity_context tool for the Context API (v1.5.0) - #157
Conversation
Models that read the tool schema made three repeated mistakes in live runs: 'metadata' in formats, highlights without 'highlights' in formats, and criteria values that were not objects. The descriptions now state these rules and give one criteria example. No behavior change.
qirh
left a comment
There was a problem hiding this comment.
Non-blocking: the snapshot_as_of description could also say it cannot be combined with the html and links formats, which the API reference lists.
rbuchmayer-pplx
left a comment
There was a problem hiding this comment.
Adds the perplexity_context tool as a strict pass-through of the POST /v1/context body, plus the 1.5.0 version bump. I read the full diff, rebuilt it, and ran tsc and vitest on 915cc31 (125/125 pass). An in-memory client showed tools/list advertising additionalProperties: false and rejecting an unknown key locally with no API call. The other four tools and makeApiRequest are unchanged apart from passing the abort signal. The endpoint is live and broadly used with API keys, so I approved. Two separate gates: the npm release (it also ships the unpublished 1.4.0 commits) waits for production-environment approval, and the hosted server pins 1.3.0, so exposing the tool there needs its own bump with OAuth coverage.
Why
The Context API (
POST /v1/context) fetches pages by URL and returns Markdown, HTML, links, highlights, metadata, and criteria scores. The server had no tool for it.perplexity_searchfinds URLs, and no tool reads a page when the caller has the URL.What
perplexity_context. Its input is the request body ofPOST /v1/context, with the same nine parameter names and the same nesting:urls,formats,highlights,if_modified_since,max_age_seconds,cache_only,snapshot_as_of,timeout_ms,criteria./v1/contextbody. It adds no defaults, so a parameter that is not set gets the API default. Rules between parameters (for examplesnapshot_as_ofwithmax_age_seconds) stay in the API, and the API's 400 message comes back as the tool error.structuredContent.results. The text has every response field: for each URL the status, everymetadatakey,criteriaanswers orcriteria_error, and the requested formats, thenusageandrequest_id.instructions, and a README entry. The lastinstructionssentence said that all tools access live web data. Context can return a stored copy, so the sentence changed.package.json, lockfile,server.json,plugin.json,marketplace.json, advertisedVERSION).Three differences from the other four tools, each on purpose:
tools/listshowsadditionalProperties: false, and an unknown key returns an input validation error with no API call.makeApiRequest. One call can run for the fulltimeout_ms.__proto__is rejected locally. The API's ID pattern allows it, but Zod drops a record key with that name, so the request would go out without that question.Verification
All results are for head
915cc31on 2026-11-24 against the prod API, on Linux, unless a line says otherwise. The API calls used an API key.npm ci,npm run build,tsc --noEmit,vitest runin clean clones on Node 18.20.8, 22.23.5, and 24.22.3npm packtarball install, and Node 18, 22, 24tools/listentries of the four existing toolsContextRequestin the API reference: 8,558 requests, 2,212 responsesStdio transcript
Stdio client against the built package, 19:14 UTC. The API calls went through
PERPLEXITY_PROXY.In calls 1 to 12,
structuredContent.resultsequals the text block. Call 10 depends on how fast the site answers: in other runs of the same script withtimeout_ms: 1000, two other URLs returnedokandcrawl_failed.Parity matrix
A recording relay (set with
PERPLEXITY_BASE_URL) sat between the server and the API. The cases cover each parameter, its limits, values outside the limits, and the rules between parameters. For each case the script compared:User-Agent: perplexity-mcp/1.5.0,X-Source,X-Pplx-Integration, andX-Service: local-mcp.Statuses in the live responses:
ok,no_content,invalid_url,crawl_timeout,crawl_failed,not_modified. Results withokornot_modifiedhadmetadata; the others did not. The other status values are covered by unit test fixtures only.HTTP transport and failure modes
dist/http.js):/health,initializereports 1.5.0,tools/listhas five tools, five liveperplexity_contextcalls with the request body equal to the arguments, a local rejection, a wrongHost(421), and anOriginthat is not allowed (403).PERPLEXITY_TIMEOUT_MS=1500returns the timeout error that names the variable.isError: truewith a message.Offline fuzz
Run on
ee7b822. Commit915cc31changes four description strings and no other part oftools/list(compared key by key).ContextRequestwith a JSON Schema validator and sent to the tool with a mock upstream. 2,384 valid requests went out with the body equal to the arguments. 5,088 invalid requests were rejected locally. 1,085 invalid requests went out: 1,084 break a length or property-count limit that this tool leaves to the API (listed below), and 1 has the top-level__proto__key described in "Known and not changed here". 1 valid request was rejected: the__proto__criteria ID.structuredContent.resultsequals the text in each.Models
Six models from five providers got the
tools/listschema as function tools (strict mode off) through a function-calling API. Each tool call ran live through this server. On an error, the model got the error text and one more try.915cc31(102 runs): the first call succeeded in 97. All 5 errors were corrected on the second call.ee7b822, the first call succeeded in 107, and 7 of the 19 errors were not corrected on the second call. On915cc31, the first call succeeded in 122, and 1 of the 4 errors was not corrected.Counts change between runs. The errors on
ee7b822were"metadata"informats,highlightswithout"highlights"informats, andcriteriavalues that were strings or had notype. That is why915cc31changes the descriptions. The errors that remain on915cc31are the same kinds at a lower rate, plus one unknown key and onehighlightsvalue sent as a string. In each case the model got the validation error or the API's 400 message.Input schema against the API reference
tools/listinput schema forperplexity_contextagainstContextRequest:urls,highlights.query, andtypeandinstructionsin a question), enum values,urlscount (1 to 50), every integer range, andadditionalProperties: falseon the four object levels.defaultkeywords. The descriptions state the defaults forformats,highlights.max_tokens_per_url,max_age_seconds, andcache_only. The API reference gives 110000 fortimeout_ms. I did not confirm that value with a live call, so the description gives the range and no default.minLengthormaxLengthonhighlights.query(1 to 1024),instructions(1 to 1000), andcriteria.trueandcriteria.false(1 to 500). The limits are in the descriptions and the API enforces them. Zod counts UTF-16 units, so a local maximum could reject a string that the API accepts.minPropertiesormaxPropertiesoncriteria(1 to 5 questions). The limit is in the description and the API enforces it.Not tested
urlsfor lower usage tiers.npm packtarball.Known and not changed here
__proto__before the tool schema runs, so that call goes out without the key and is not rejected as an unknown parameter. A top-level argument namedconstructorreturns JSON-RPC error -32603 with no API call. Both happen in the SDK, andperplexity_searchdoes the same.metadatakey named__proto__in a response, so a page meta tag with that name does not reach the output.criteriais an object with caller-chosen keys. The one function-calling API that I tested with strict schemas rejected the tool definition for that reason, and accepted it with strict mode off. I did not check which MCP clients use strict schemas.makeApiRequest, thePERPLEXITY_TIMEOUT_MStimer is cleared when the response headers arrive, so it does not cover the body read.perplexity_searchhas the same behavior.Merging publishes 1.5.0:
publish.ymlandpublish-mcp.ymlstart on apackage.jsonchange to main and wait for approval in theproductionenvironment. npmlatestis 1.3.0, so this release also publishes the commits on main since 1.3.0 for the first time. The hosted server atapi.perplexity.ai/mcpdoes not change with this PR.