Skip to content

feat: add perplexity_context tool for the Context API (v1.5.0) - #157

Merged
rbuchmayer-pplx merged 2 commits into
mainfrom
context-tool
Nov 25, 2026
Merged

rbuchmayer-pplx merged 2 commits into
mainfrom
context-tool

Conversation

@andrewmadson-pplx

@andrewmadson-pplx andrewmadson-pplx commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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_search finds URLs, and no tool reads a page when the caller has the URL.

What

  • New tool perplexity_context. Its input is the request body of POST /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.
  • The server sends the arguments as given in the /v1/context body. It adds no defaults, so a parameter that is not set gets the API default. Rules between parameters (for example snapshot_as_of with max_age_seconds) stay in the API, and the API's 400 message comes back as the tool error.
  • Output has the shape of the other four tools: one text block, and the same string in structuredContent.results. The text has every response field: for each URL the status, every metadata key, criteria answers or criteria_error, and the requested formats, then usage and request_id.
  • One sentence in the server instructions, and a README entry. The last instructions sentence said that all tools access live web data. Context can return a stored copy, so the sentence changed.
  • Version 1.5.0 (package.json, lockfile, server.json, plugin.json, marketplace.json, advertised VERSION).

Three differences from the other four tools, each on purpose:

  1. The input schema is a strict Zod object at every level. The API rejects a parameter that it does not have. With the plain field map that the other tools register, the SDK removes unknown keys and the call succeeds without them. With the strict schema, tools/list shows additionalProperties: false, and an unknown key returns an input validation error with no API call.
  2. The handler passes the request's abort signal to makeApiRequest. One call can run for the full timeout_ms.
  3. A criteria ID of __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 915cc31 on 2026-11-24 against the prod API, on Linux, unless a line says otherwise. The API calls used an API key.

Check Result
npm ci, npm run build, tsc --noEmit, vitest run in clean clones on Node 18.20.8, 22.23.5, and 24.22.3 Pass. 125/125 tests (112 on main).
Parity matrix over stdio: 69 cases, 61 live API calls 0 problems
HTTP transport and failure modes: 26 checks 26 pass
One live call to each of the five tools: branch build, npm pack tarball install, and Node 18, 22, 24 Pass
tools/list entries of the four existing tools Byte-identical to main
Offline fuzz against ContextRequest in the API reference: 8,558 requests, 2,212 responses One difference found, in the SDK. See "Known and not changed here".
Models that call the tool: 228 live runs with six models See "Models"

Stdio transcript

Stdio client against the built package, 19:14 UTC. The API calls went through PERPLEXITY_PROXY.

server: {"name":"ai.perplexity/mcp-server","version":"1.5.0"}
tools/list: perplexity_ask, perplexity_research, perplexity_reason, perplexity_search, perplexity_context
1 default (urls only)                          isError=false  602ms  status ok | billed 1 | origin cached
2 formats markdown, html, links                isError=false  472ms  status ok | billed 1 | origin cached
3 formats [] (metadata only)                   isError=false  442ms  status ok | billed 1 | origin cached
4 highlights with max_tokens_per_url           isError=false  583ms  status ok | billed 1 | origin cached
5 max_age_seconds 0 (live fetch)               isError=false 1816ms  status ok | billed 1 | origin live
6 if_modified_since                            isError=false 1118ms  status not_modified | billed 1 | origin live
7 cache_only                                   isError=false  381ms  status ok, no_content | billed 1 | origin cached
8 snapshot_as_of                               isError=false  541ms  status ok | billed 1 | origin cached
9 snapshot_as_of, URL with no stored copy      isError=false  927ms  status no_content | billed 0
10 timeout_ms 1000 with a live fetch           isError=false  807ms  status crawl_timeout | billed 0
11 criteria                                    isError=false  687ms  status ok | billed 1 | noul 0.9961809879303074 | origin cached
12 URLs that fail                              isError=false  624ms  status invalid_url, crawl_failed | billed 0
13 wrong combination (API 400)                 isError=true   477ms  highlights is required when formats has highlights
14 reserved criteria ID (API 400)              isError=true   408ms  criterion ID query is reserved
15 unknown parameter (local)                   isError=true     1ms  Unrecognized key(s) in object: 'not_a_field'
16 unknown nested parameter (local)            isError=true     1ms  Unrecognized key(s) in object: 'max_tokens'
17 empty urls (local)                          isError=true     1ms  Array must contain at least 1 element(s)
18 51 urls (local)                             isError=true     1ms  Array must contain at most 50 element(s)
19 null value (local)                          isError=true     1ms  Expected number, received null
20 timeout_ms out of range (local)             isError=true     1ms  Number must be less than or equal to 110000
21 criteria ID __proto__ (local)               isError=true     0ms  '__proto__' cannot be used as a question ID in this tool

In calls 1 to 12, structuredContent.results equals the text block. Call 10 depends on how fast the site answers: in other runs of the same script with timeout_ms: 1000, two other URLs returned ok and crawl_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:

  • The request body with the tool arguments. They were equal in all 61 calls.
  • The request headers: User-Agent: perplexity-mcp/1.5.0, X-Source, X-Pplx-Integration, and X-Service: local-mcp.
  • Each value in the API response with the tool output. Every value was in the output, and the responses had no key that the formatter does not know.
  • For an API 400, the API error body with the tool error. The body came back unchanged.
  • For a local rejection (8 cases), the relay log. No API call was made.

Statuses in the live responses: ok, no_content, invalid_url, crawl_timeout, crawl_failed, not_modified. Results with ok or not_modified had metadata; the others did not. The other status values are covered by unit test fixtures only.

HTTP transport and failure modes

  • Streamable HTTP (dist/http.js): /health, initialize reports 1.5.0, tools/list has five tools, five live perplexity_context calls with the request body equal to the arguments, a local rejection, a wrong Host (421), and an Origin that is not allowed (403).
  • A wrong API key returns the API's 401 message as the tool error.
  • A client cancel after 1.5 s stops the API request, and the next call works.
  • PERPLEXITY_TIMEOUT_MS=1500 returns the timeout error that names the variable.
  • With a mock upstream: 429, an HTML 500, invalid JSON, a wrong response shape, an empty body, a connection reset, and a body cut in half. Each returns isError: true with a message.

Offline fuzz

Run on ee7b822. Commit 915cc31 changes four description strings and no other part of tools/list (compared key by key).

  • 8,558 generated requests, each checked against ContextRequest with 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.
  • 2,212 generated valid responses. No exception, and structuredContent.results equals the text in each.

Models

Six models from five providers got the tools/list schema 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.

  • 17 prompts, one run each, on 915cc31 (102 runs): the first call succeeded in 97. All 5 errors were corrected on the second call.
  • The 7 prompts with the most errors, three runs each (126 runs): on ee7b822, the first call succeeded in 107, and 7 of the 19 errors were not corrected on the second call. On 915cc31, the first call succeeded in 122, and 1 of the 4 errors was not corrected.

Counts change between runs. The errors on ee7b822 were "metadata" in formats, highlights without "highlights" in formats, and criteria values that were strings or had no type. That is why 915cc31 changes the descriptions. The errors that remain on 915cc31 are the same kinds at a lower rate, plus one unknown key and one highlights value 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/list input schema for perplexity_context against ContextRequest:

  • Same: parameter names, nesting, types, required fields (urls, highlights.query, and type and instructions in a question), enum values, urls count (1 to 50), every integer range, and additionalProperties: false on the four object levels.
  • No default keywords. The descriptions state the defaults for formats, highlights.max_tokens_per_url, max_age_seconds, and cache_only. The API reference gives 110000 for timeout_ms. I did not confirm that value with a live call, so the description gives the range and no default.
  • No minLength or maxLength on highlights.query (1 to 1024), instructions (1 to 1000), and criteria.true and criteria.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.
  • No minProperties or maxProperties on criteria (1 to 5 questions). The limit is in the description and the API enforces it.

Not tested

  • A call with an OAuth token.
  • MCP client applications. The model runs used a function-calling API, not a desktop client.
  • What MCP clients do with a large result. The tool adds no size limit, as the API has none.
  • The limit on urls for lower usage tiers.
  • Windows and macOS.
  • The package from the npm registry. The tests used the npm pack tarball.

Known and not changed here

  • The MCP SDK removes a top-level argument named __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 named constructor returns JSON-RPC error -32603 with no API call. Both happen in the SDK, and perplexity_search does the same.
  • Zod also drops a metadata key named __proto__ in a response, so a page meta tag with that name does not reach the output.
  • The output has the response fields that the formatter knows. A field that the API adds later does not show until the tool is updated.
  • criteria is 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.
  • In makeApiRequest, the PERPLEXITY_TIMEOUT_MS timer is cleared when the response headers arrive, so it does not cover the body read. perplexity_search has the same behavior.

Merging publishes 1.5.0: publish.yml and publish-mcp.yml start on a package.json change to main and wait for approval in the production environment. npm latest is 1.3.0, so this release also publishes the commits on main since 1.3.0 for the first time. The hosted server at api.perplexity.ai/mcp does not change with this PR.

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.
@andrewmadson-pplx
andrewmadson-pplx marked this pull request as ready for review November 25, 2026 17:13

@qirh qirh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 rbuchmayer-pplx left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rbuchmayer-pplx
rbuchmayer-pplx merged commit 51c650a into main Nov 25, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants