@perplexity-ai/mcp-server 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +2 -2
- package/README.md +57 -17
- package/dist/server.d.ts +7 -6
- package/dist/server.js +59 -20
- package/dist/types.d.ts +15 -0
- package/package.json +1 -1
|
@@ -6,14 +6,14 @@
|
|
|
6
6
|
},
|
|
7
7
|
"metadata": {
|
|
8
8
|
"description": "Official Perplexity AI plugin providing real-time web search, reasoning, and research capabilities",
|
|
9
|
-
"version": "1.1
|
|
9
|
+
"version": "1.2.1"
|
|
10
10
|
},
|
|
11
11
|
"plugins": [
|
|
12
12
|
{
|
|
13
13
|
"name": "perplexity",
|
|
14
14
|
"source": "./",
|
|
15
15
|
"description": "Real-time web search, reasoning, and research through Perplexity's API",
|
|
16
|
-
"version": "1.1
|
|
16
|
+
"version": "1.2.1",
|
|
17
17
|
"author": {
|
|
18
18
|
"name": "Perplexity AI",
|
|
19
19
|
"email": "api@perplexity.ai"
|
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# Perplexity API Platform MCP Server
|
|
2
2
|
|
|
3
|
-
[](https://cursor.com/
|
|
3
|
+
[](https://cursor.com/install-mcp?name=perplexity&config=eyJ1cmwiOiJodHRwczovL2FwaS5wZXJwbGV4aXR5LmFpL21jcCIsImhlYWRlcnMiOnsiQXV0aG9yaXphdGlvbiI6IkJlYXJlciBZT1VSX0FQSV9LRVkifX0%3D)
|
|
4
4
|
|
|
5
|
-
[](https://vscode.dev/redirect/mcp/install?name=perplexity&config=%7B%22type%22%3A%
|
|
5
|
+
[](https://vscode.dev/redirect/mcp/install?name=perplexity&config=%7B%22type%22%3A%22http%22%2C%22url%22%3A%22https%3A%2F%2Fapi.perplexity.ai%2Fmcp%22%2C%22headers%22%3A%7B%22Authorization%22%3A%22Bearer%20%24%7Binput%3Aperplexity-api-key%7D%22%7D%7D&inputs=%5B%7B%22type%22%3A%22promptString%22%2C%22id%22%3A%22perplexity-api-key%22%2C%22description%22%3A%22Perplexity%20API%20Key%22%2C%22password%22%3Atrue%7D%5D)
|
|
6
6
|
|
|
7
7
|
[](https://kiro.dev/launch/mcp/add?name=perplexity&config=%7B%22command%22%3A%22npx%22%2C%22args%22%3A%5B%22-y%22%2C%22%40perplexity-ai%2Fmcp-server%22%5D%2C%22env%22%3A%7B%22PERPLEXITY_API_KEY%22%3A%22your_key_here%22%7D%7D)
|
|
8
8
|
|
|
@@ -10,28 +10,27 @@
|
|
|
10
10
|
|
|
11
11
|
The official MCP server implementation for the Perplexity API Platform, providing AI assistants with real-time web search, reasoning, and research capabilities through the [Agent API](https://docs.perplexity.ai/docs/agent-api/quickstart) and the Search API.
|
|
12
12
|
|
|
13
|
-
##
|
|
13
|
+
## Remote MCP Server
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
Direct web search using the Perplexity Search API. Returns ranked search results with metadata, perfect for finding current information. Supports recency filters (`search_recency_filter`) and domain restrictions (`search_domain_filter`).
|
|
15
|
+
The remote MCP server is hosted by Perplexity and is the easiest way to get started: same tools, nothing to install or update. The Cursor and VS Code buttons at the top of this page connect to it with one click. If your MCP client does not support remote servers yet, skip to the [local server](#local-mcp-server) setup below. Connect over Streamable HTTP with your [Perplexity API key](https://console.perplexity.ai):
|
|
17
16
|
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
```
|
|
18
|
+
https://api.perplexity.ai/mcp
|
|
19
|
+
```
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
Deep, comprehensive research backed by the Agent API `high` preset. Ideal for thorough analysis and detailed reports. Runs can take minutes; the server streams the run and reports progress to clients that request it.
|
|
21
|
+
For Claude Code:
|
|
23
22
|
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
```bash
|
|
24
|
+
claude mcp add --transport http perplexity https://api.perplexity.ai/mcp --header "Authorization: Bearer YOUR_API_KEY"
|
|
25
|
+
```
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
> Presets are managed configurations (model, search setup, step budget) that Perplexity keeps tuned over time; see the [presets guide](https://docs.perplexity.ai/docs/agent-api/presets). Earlier versions of this server called the legacy `sonar-pro`, `sonar-reasoning-pro`, and `sonar-deep-research` models and accepted `strip_thinking` / `reasoning_effort` parameters. Those parameters are no longer part of the tool schemas and are ignored if sent; the Agent API produces no `<think>` tags.
|
|
27
|
+
See the [MCP integration docs](https://docs.perplexity.ai/docs/getting-started/integrations/mcp-server) for manual Cursor/VS Code configuration, usage from the Anthropic API, and setup for other clients.
|
|
29
28
|
|
|
30
|
-
##
|
|
29
|
+
## Local MCP Server
|
|
31
30
|
|
|
32
31
|
### Get Your API Key
|
|
33
32
|
|
|
34
|
-
1. Get your Perplexity API Key from the [API Portal](https://
|
|
33
|
+
1. Get your Perplexity API Key from the [API Portal](https://console.perplexity.ai)
|
|
35
34
|
2. Replace `your_key_here` in the configurations below with your API key
|
|
36
35
|
3. (Optional) Set timeout: `PERPLEXITY_TIMEOUT_MS=600000` (default: 5 minutes)
|
|
37
36
|
4. (Optional) Set custom base URL: `PERPLEXITY_BASE_URL=https://your-custom-url.com` (default: https://api.perplexity.ai)
|
|
@@ -57,7 +56,11 @@ claude
|
|
|
57
56
|
codex mcp add perplexity --env PERPLEXITY_API_KEY="your_key_here" -- npx -y @perplexity-ai/mcp-server
|
|
58
57
|
```
|
|
59
58
|
|
|
60
|
-
###
|
|
59
|
+
### Agent Plugins
|
|
60
|
+
|
|
61
|
+
This repository is packaged as an [Agent Plugin](https://agent-plugins.org), so clients that support the standard can install it directly from this repository. The Agent Plugins format does not carry secrets, so set the `PERPLEXITY_API_KEY` environment variable through your client's plugin or MCP settings.
|
|
62
|
+
|
|
63
|
+
### Other MCP Clients
|
|
61
64
|
|
|
62
65
|
Most clients can be configured manually using the same `mcpServers` wrapper in their client config (as shown for Cursor). If a client has a different schema, check its docs for the exact wrapper format.
|
|
63
66
|
|
|
@@ -116,7 +119,7 @@ If you'd rather use the standard variables, we support `HTTPS_PROXY` and `HTTP_P
|
|
|
116
119
|
> The server checks proxy settings in this order: `PERPLEXITY_PROXY` → `HTTPS_PROXY` → `HTTP_PROXY`. If none are set, it connects directly to the internet.
|
|
117
120
|
> URLs must include `https://`. Typical ports are `8080`, `3128`, and `80`.
|
|
118
121
|
|
|
119
|
-
### HTTP
|
|
122
|
+
### Self-Hosted HTTP Mode
|
|
120
123
|
|
|
121
124
|
For cloud or shared deployments, run the server in HTTP mode.
|
|
122
125
|
|
|
@@ -147,6 +150,43 @@ npm install && npm run build && npm run start:http
|
|
|
147
150
|
|
|
148
151
|
The server will be accessible at `http://localhost:8080/mcp`
|
|
149
152
|
|
|
153
|
+
## Available Tools
|
|
154
|
+
|
|
155
|
+
### **perplexity_search**
|
|
156
|
+
Direct web search using the Perplexity Search API. Returns ranked search results with metadata, perfect for finding current information. Supports recency filters (`search_recency_filter`) and domain restrictions (`search_domain_filter`).
|
|
157
|
+
|
|
158
|
+
### **perplexity_ask**
|
|
159
|
+
General-purpose conversational AI with real-time web search, backed by the Agent API `fast` preset. Great for quick questions and everyday searches.
|
|
160
|
+
|
|
161
|
+
### **perplexity_research**
|
|
162
|
+
Deep, comprehensive research backed by the Agent API `high` preset. Ideal for thorough analysis and detailed reports. Runs can take minutes; the server streams the run and reports progress to clients that request it.
|
|
163
|
+
|
|
164
|
+
### **perplexity_reason**
|
|
165
|
+
Advanced reasoning and problem-solving backed by the Agent API `medium` preset. Perfect for complex analytical tasks.
|
|
166
|
+
|
|
167
|
+
> [!NOTE]
|
|
168
|
+
> Presets are managed configurations (model, search setup, step budget) that Perplexity keeps tuned over time; see the [presets guide](https://docs.perplexity.ai/docs/agent-api/presets). Earlier versions of this server called the legacy `sonar-pro`, `sonar-reasoning-pro`, and `sonar-deep-research` models and accepted `strip_thinking` / `reasoning_effort` parameters. Those parameters are no longer part of the tool schemas and are ignored if sent; the Agent API produces no `<think>` tags.
|
|
169
|
+
|
|
170
|
+
## Use as a Library
|
|
171
|
+
|
|
172
|
+
The package also exports the server factory for embedding in your own Node process:
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
import { createPerplexityServer } from "@perplexity-ai/mcp-server";
|
|
176
|
+
|
|
177
|
+
// Single-tenant: reads PERPLEXITY_API_KEY from the environment.
|
|
178
|
+
const server = createPerplexityServer("my-service");
|
|
179
|
+
|
|
180
|
+
// Multi-tenant hosts resolve the key per call instead. When a provider is
|
|
181
|
+
// set, the environment variable is never consulted, and a provider that
|
|
182
|
+
// returns no key fails the call rather than falling back.
|
|
183
|
+
const tenantServer = createPerplexityServer("my-service", {
|
|
184
|
+
apiKey: () => currentRequestApiKey,
|
|
185
|
+
});
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
Mount the returned server on any MCP transport (stdio, streamable HTTP, in-memory).
|
|
189
|
+
|
|
150
190
|
## Troubleshooting
|
|
151
191
|
|
|
152
192
|
- **API Key Issues**: Ensure `PERPLEXITY_API_KEY` is set correctly
|
package/dist/server.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { Message, AgentResponse, AgentToolOptions, AgentCallHooks, SearchResponse } from "./types.js";
|
|
1
|
+
import type { Message, AgentResponse, AgentToolOptions, AgentCallHooks, ApiKeyProvider, PerplexityServerOptions, SearchResponse } from "./types.js";
|
|
2
|
+
export type { ApiKeyProvider, PerplexityServerOptions } from "./types.js";
|
|
2
3
|
export declare const ASK_PRESET = "fast";
|
|
3
4
|
export declare const REASON_PRESET = "medium";
|
|
4
5
|
export declare const RESEARCH_PRESET = "high";
|
|
@@ -6,12 +7,12 @@ export declare function getProxyUrl(): string | undefined;
|
|
|
6
7
|
export declare function proxyAwareFetch(url: string, options?: RequestInit): Promise<Response>;
|
|
7
8
|
export declare function validateMessages(messages: unknown, toolName: string): asserts messages is Message[];
|
|
8
9
|
/** Best-effort cancellation of an agent run so an abandoned request stops billing. */
|
|
9
|
-
export declare function cancelAgentResponse(responseId: string, serviceOrigin?: string): Promise<void>;
|
|
10
|
+
export declare function cancelAgentResponse(responseId: string, serviceOrigin?: string, apiKey?: ApiKeyProvider): Promise<void>;
|
|
10
11
|
/**
|
|
11
12
|
* Consume an Agent API SSE stream and return the final response object.
|
|
12
13
|
* Emits progress via hooks.onProgress as reasoning events arrive.
|
|
13
14
|
*/
|
|
14
|
-
export declare function consumeAgentStream(response: Response, hooks?: AgentCallHooks, serviceOrigin?: string, deadlineSignal?: AbortSignal): Promise<AgentResponse>;
|
|
15
|
+
export declare function consumeAgentStream(response: Response, hooks?: AgentCallHooks, serviceOrigin?: string, deadlineSignal?: AbortSignal, apiKey?: ApiKeyProvider): Promise<AgentResponse>;
|
|
15
16
|
export declare function extractAgentText(response: AgentResponse): string;
|
|
16
17
|
/**
|
|
17
18
|
* Append the "Citations:" block ("[n] url" lines) to the answer text, keyed
|
|
@@ -22,10 +23,10 @@ export declare function extractAgentText(response: AgentResponse): string;
|
|
|
22
23
|
* numbering instead.
|
|
23
24
|
*/
|
|
24
25
|
export declare function formatAgentResponseText(response: AgentResponse): string;
|
|
25
|
-
export declare function performAgentResponse(messages: Message[], preset: string, serviceOrigin?: string, options?: AgentToolOptions, hooks?: AgentCallHooks): Promise<string>;
|
|
26
|
+
export declare function performAgentResponse(messages: Message[], preset: string, serviceOrigin?: string, options?: AgentToolOptions, hooks?: AgentCallHooks, apiKey?: ApiKeyProvider): Promise<string>;
|
|
26
27
|
export declare function formatSearchResults(data: SearchResponse): string;
|
|
27
|
-
export declare function performSearch(query: string, maxResults?: number, maxTokensPerPage?: number, country?: string, filters?: Pick<AgentToolOptions, "search_recency_filter" | "search_domain_filter">, serviceOrigin?: string): Promise<string>;
|
|
28
|
-
export declare function createPerplexityServer(serviceOrigin?: string): import("@modelcontextprotocol/sdk/server/index.js").Server<{
|
|
28
|
+
export declare function performSearch(query: string, maxResults?: number, maxTokensPerPage?: number, country?: string, filters?: Pick<AgentToolOptions, "search_recency_filter" | "search_domain_filter">, serviceOrigin?: string, apiKey?: ApiKeyProvider): Promise<string>;
|
|
29
|
+
export declare function createPerplexityServer(serviceOrigin?: string, serverOptions?: PerplexityServerOptions): import("@modelcontextprotocol/sdk/server/index.js").Server<{
|
|
29
30
|
method: string;
|
|
30
31
|
params?: {
|
|
31
32
|
[x: string]: unknown;
|
package/dist/server.js
CHANGED
|
@@ -4,7 +4,7 @@ import { fetch as undiciFetch, ProxyAgent } from "undici";
|
|
|
4
4
|
import { AgentResponseSchema, SearchResponseSchema } from "./validation.js";
|
|
5
5
|
const PERPLEXITY_API_KEY = process.env.PERPLEXITY_API_KEY;
|
|
6
6
|
const PERPLEXITY_BASE_URL = process.env.PERPLEXITY_BASE_URL || "https://api.perplexity.ai";
|
|
7
|
-
const VERSION = "1.1
|
|
7
|
+
const VERSION = "1.2.1";
|
|
8
8
|
// Agent API presets backing each tool: https://docs.perplexity.ai/docs/agent-api/presets
|
|
9
9
|
export const ASK_PRESET = "fast";
|
|
10
10
|
export const REASON_PRESET = "medium";
|
|
@@ -45,9 +45,21 @@ export function validateMessages(messages, toolName) {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
|
-
async function makeApiRequest(endpoint, body, serviceOrigin, signal) {
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
async function makeApiRequest(endpoint, body, serviceOrigin, signal, apiKey) {
|
|
49
|
+
// A configured provider fully replaces the env var: falling back would let
|
|
50
|
+
// a multi-tenant misconfiguration silently bill the process-wide key.
|
|
51
|
+
let resolvedApiKey;
|
|
52
|
+
if (apiKey) {
|
|
53
|
+
resolvedApiKey = apiKey();
|
|
54
|
+
if (!resolvedApiKey) {
|
|
55
|
+
throw new Error("API key provider returned no key");
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
resolvedApiKey = PERPLEXITY_API_KEY;
|
|
60
|
+
if (!resolvedApiKey) {
|
|
61
|
+
throw new Error("PERPLEXITY_API_KEY environment variable is required");
|
|
62
|
+
}
|
|
51
63
|
}
|
|
52
64
|
// Read timeout fresh each time to respect env var changes
|
|
53
65
|
const TIMEOUT_MS = parseInt(process.env.PERPLEXITY_TIMEOUT_MS || "300000", 10);
|
|
@@ -67,9 +79,10 @@ async function makeApiRequest(endpoint, body, serviceOrigin, signal) {
|
|
|
67
79
|
try {
|
|
68
80
|
const headers = {
|
|
69
81
|
"Content-Type": "application/json",
|
|
70
|
-
"Authorization": `Bearer ${
|
|
82
|
+
"Authorization": `Bearer ${resolvedApiKey}`,
|
|
71
83
|
"User-Agent": `perplexity-mcp/${VERSION}`,
|
|
72
84
|
"X-Source": "pplx-mcp-server",
|
|
85
|
+
"X-Pplx-Integration": `perplexity-mcp/${VERSION}`,
|
|
73
86
|
};
|
|
74
87
|
if (serviceOrigin) {
|
|
75
88
|
headers["X-Service"] = serviceOrigin;
|
|
@@ -106,9 +119,9 @@ async function makeApiRequest(endpoint, body, serviceOrigin, signal) {
|
|
|
106
119
|
return response;
|
|
107
120
|
}
|
|
108
121
|
/** Best-effort cancellation of an agent run so an abandoned request stops billing. */
|
|
109
|
-
export async function cancelAgentResponse(responseId, serviceOrigin) {
|
|
122
|
+
export async function cancelAgentResponse(responseId, serviceOrigin, apiKey) {
|
|
110
123
|
try {
|
|
111
|
-
await makeApiRequest(`v1/agent/${encodeURIComponent(responseId)}/cancel`, {}, serviceOrigin);
|
|
124
|
+
await makeApiRequest(`v1/agent/${encodeURIComponent(responseId)}/cancel`, {}, serviceOrigin, undefined, apiKey);
|
|
112
125
|
}
|
|
113
126
|
catch {
|
|
114
127
|
// The run may already be terminal; nothing actionable either way.
|
|
@@ -118,7 +131,7 @@ export async function cancelAgentResponse(responseId, serviceOrigin) {
|
|
|
118
131
|
* Consume an Agent API SSE stream and return the final response object.
|
|
119
132
|
* Emits progress via hooks.onProgress as reasoning events arrive.
|
|
120
133
|
*/
|
|
121
|
-
export async function consumeAgentStream(response, hooks, serviceOrigin, deadlineSignal) {
|
|
134
|
+
export async function consumeAgentStream(response, hooks, serviceOrigin, deadlineSignal, apiKey) {
|
|
122
135
|
const body = response.body;
|
|
123
136
|
if (!body) {
|
|
124
137
|
throw new Error("Response body is null");
|
|
@@ -228,7 +241,7 @@ export async function consumeAgentStream(response, hooks, serviceOrigin, deadlin
|
|
|
228
241
|
if (hooks?.signal?.aborted || deadlineSignal?.aborted) {
|
|
229
242
|
if (responseId) {
|
|
230
243
|
// Stop the server-side run so an abandoned request stops billing.
|
|
231
|
-
void cancelAgentResponse(responseId, serviceOrigin);
|
|
244
|
+
void cancelAgentResponse(responseId, serviceOrigin, apiKey);
|
|
232
245
|
}
|
|
233
246
|
if (hooks?.signal?.aborted) {
|
|
234
247
|
throw new Error("Request cancelled");
|
|
@@ -239,7 +252,7 @@ export async function consumeAgentStream(response, hooks, serviceOrigin, deadlin
|
|
|
239
252
|
}
|
|
240
253
|
if (hooks?.signal?.aborted) {
|
|
241
254
|
if (responseId) {
|
|
242
|
-
void cancelAgentResponse(responseId, serviceOrigin);
|
|
255
|
+
void cancelAgentResponse(responseId, serviceOrigin, apiKey);
|
|
243
256
|
}
|
|
244
257
|
throw new Error("Request cancelled");
|
|
245
258
|
}
|
|
@@ -338,7 +351,7 @@ function buildWebSearchTool(options) {
|
|
|
338
351
|
// No override means the preset's own web_search config stays in effect.
|
|
339
352
|
return Object.keys(tool).length > 1 ? tool : undefined;
|
|
340
353
|
}
|
|
341
|
-
export async function performAgentResponse(messages, preset, serviceOrigin, options, hooks) {
|
|
354
|
+
export async function performAgentResponse(messages, preset, serviceOrigin, options, hooks, apiKey) {
|
|
342
355
|
const webSearchTool = buildWebSearchTool(options);
|
|
343
356
|
const body = {
|
|
344
357
|
preset,
|
|
@@ -368,8 +381,8 @@ export async function performAgentResponse(messages, preset, serviceOrigin, opti
|
|
|
368
381
|
}
|
|
369
382
|
}
|
|
370
383
|
try {
|
|
371
|
-
const response = await makeApiRequest("v1/agent", body, serviceOrigin, deadline.signal);
|
|
372
|
-
const agentResponse = await consumeAgentStream(response, hooks, serviceOrigin, deadline.signal);
|
|
384
|
+
const response = await makeApiRequest("v1/agent", body, serviceOrigin, deadline.signal, apiKey);
|
|
385
|
+
const agentResponse = await consumeAgentStream(response, hooks, serviceOrigin, deadline.signal, apiKey);
|
|
373
386
|
return formatAgentResponseText(agentResponse);
|
|
374
387
|
}
|
|
375
388
|
catch (error) {
|
|
@@ -404,7 +417,7 @@ export function formatSearchResults(data) {
|
|
|
404
417
|
});
|
|
405
418
|
return formattedResults;
|
|
406
419
|
}
|
|
407
|
-
export async function performSearch(query, maxResults = 10, maxTokensPerPage = 1024, country, filters, serviceOrigin) {
|
|
420
|
+
export async function performSearch(query, maxResults = 10, maxTokensPerPage = 1024, country, filters, serviceOrigin, apiKey) {
|
|
408
421
|
const body = {
|
|
409
422
|
query: query,
|
|
410
423
|
max_results: maxResults,
|
|
@@ -413,7 +426,7 @@ export async function performSearch(query, maxResults = 10, maxTokensPerPage = 1
|
|
|
413
426
|
...(filters?.search_recency_filter && { search_recency_filter: filters.search_recency_filter }),
|
|
414
427
|
...(filters?.search_domain_filter && { search_domain_filter: filters.search_domain_filter }),
|
|
415
428
|
};
|
|
416
|
-
const response = await makeApiRequest("search", body, serviceOrigin);
|
|
429
|
+
const response = await makeApiRequest("search", body, serviceOrigin, undefined, apiKey);
|
|
417
430
|
let data;
|
|
418
431
|
try {
|
|
419
432
|
const json = await response.json();
|
|
@@ -444,7 +457,7 @@ function buildHooks(extra) {
|
|
|
444
457
|
: undefined,
|
|
445
458
|
};
|
|
446
459
|
}
|
|
447
|
-
export function createPerplexityServer(serviceOrigin) {
|
|
460
|
+
export function createPerplexityServer(serviceOrigin, serverOptions) {
|
|
448
461
|
const server = new McpServer({
|
|
449
462
|
name: "ai.perplexity/mcp-server",
|
|
450
463
|
version: VERSION,
|
|
@@ -504,7 +517,7 @@ export function createPerplexityServer(serviceOrigin) {
|
|
|
504
517
|
...(search_domain_filter && { search_domain_filter }),
|
|
505
518
|
...(search_context_size && { search_context_size }),
|
|
506
519
|
};
|
|
507
|
-
const result = await performAgentResponse(messages, ASK_PRESET, serviceOrigin, Object.keys(options).length > 0 ? options : undefined, buildHooks(extra));
|
|
520
|
+
const result = await performAgentResponse(messages, ASK_PRESET, serviceOrigin, Object.keys(options).length > 0 ? options : undefined, buildHooks(extra), serverOptions?.apiKey);
|
|
508
521
|
return {
|
|
509
522
|
content: [{ type: "text", text: result }],
|
|
510
523
|
structuredContent: { response: result },
|
|
@@ -529,7 +542,7 @@ export function createPerplexityServer(serviceOrigin) {
|
|
|
529
542
|
}, async (args, extra) => {
|
|
530
543
|
const { messages } = args;
|
|
531
544
|
validateMessages(messages, "perplexity_research");
|
|
532
|
-
const result = await performAgentResponse(messages, RESEARCH_PRESET, serviceOrigin, undefined, buildHooks(extra));
|
|
545
|
+
const result = await performAgentResponse(messages, RESEARCH_PRESET, serviceOrigin, undefined, buildHooks(extra), serverOptions?.apiKey);
|
|
533
546
|
return {
|
|
534
547
|
content: [{ type: "text", text: result }],
|
|
535
548
|
structuredContent: { response: result },
|
|
@@ -559,7 +572,7 @@ export function createPerplexityServer(serviceOrigin) {
|
|
|
559
572
|
...(search_domain_filter && { search_domain_filter }),
|
|
560
573
|
...(search_context_size && { search_context_size }),
|
|
561
574
|
};
|
|
562
|
-
const result = await performAgentResponse(messages, REASON_PRESET, serviceOrigin, Object.keys(options).length > 0 ? options : undefined, buildHooks(extra));
|
|
575
|
+
const result = await performAgentResponse(messages, REASON_PRESET, serviceOrigin, Object.keys(options).length > 0 ? options : undefined, buildHooks(extra), serverOptions?.apiKey);
|
|
563
576
|
return {
|
|
564
577
|
content: [{ type: "text", text: result }],
|
|
565
578
|
structuredContent: { response: result },
|
|
@@ -603,11 +616,37 @@ export function createPerplexityServer(serviceOrigin) {
|
|
|
603
616
|
...(search_recency_filter && { search_recency_filter }),
|
|
604
617
|
...(search_domain_filter && { search_domain_filter }),
|
|
605
618
|
};
|
|
606
|
-
const result = await performSearch(query, maxResults, maxTokensPerPage, countryCode, filters, serviceOrigin);
|
|
619
|
+
const result = await performSearch(query, maxResults, maxTokensPerPage, countryCode, filters, serviceOrigin, serverOptions?.apiKey);
|
|
607
620
|
return {
|
|
608
621
|
content: [{ type: "text", text: result }],
|
|
609
622
|
structuredContent: { results: result },
|
|
610
623
|
};
|
|
611
624
|
});
|
|
625
|
+
advertiseJsonSchema202012(server.server);
|
|
612
626
|
return server.server;
|
|
613
627
|
}
|
|
628
|
+
// The MCP TypeScript SDK stamps draft-07 on tools/list schemas
|
|
629
|
+
// (modelcontextprotocol/typescript-sdk#2084); 2020-12-only clients reject
|
|
630
|
+
// them. The schemas are valid under both dialects, so only the declared
|
|
631
|
+
// dialect needs rewriting. Remove once typescript-sdk#2085 ships.
|
|
632
|
+
const JSON_SCHEMA_2020_12 = "https://json-schema.org/draft/2020-12/schema";
|
|
633
|
+
function advertiseJsonSchema202012(server) {
|
|
634
|
+
// _requestHandlers is private SDK state; goes away with this shim.
|
|
635
|
+
const handlers = server._requestHandlers;
|
|
636
|
+
const listTools = handlers.get("tools/list");
|
|
637
|
+
if (!listTools) {
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
handlers.set("tools/list", async (request, extra) => {
|
|
641
|
+
const result = await listTools(request, extra);
|
|
642
|
+
for (const tool of result.tools ?? []) {
|
|
643
|
+
if (tool.inputSchema?.$schema) {
|
|
644
|
+
tool.inputSchema.$schema = JSON_SCHEMA_2020_12;
|
|
645
|
+
}
|
|
646
|
+
if (tool.outputSchema?.$schema) {
|
|
647
|
+
tool.outputSchema.$schema = JSON_SCHEMA_2020_12;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
return result;
|
|
651
|
+
});
|
|
652
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -81,6 +81,21 @@ export interface AgentToolOptions {
|
|
|
81
81
|
export interface AgentProgressUpdate {
|
|
82
82
|
message: string;
|
|
83
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* Resolves the API key for an upstream Perplexity API call. Invoked once per
|
|
86
|
+
* request, so a closure over per-request state (e.g. an inbound Authorization
|
|
87
|
+
* header) gives each embedded server instance its own key.
|
|
88
|
+
*/
|
|
89
|
+
export type ApiKeyProvider = () => string | undefined;
|
|
90
|
+
export interface PerplexityServerOptions {
|
|
91
|
+
/**
|
|
92
|
+
* Per-call API key resolution for embedders hosting the server for more
|
|
93
|
+
* than one key (multi-tenant). When set, the PERPLEXITY_API_KEY environment
|
|
94
|
+
* variable is never consulted: a provider that returns no key fails the
|
|
95
|
+
* call rather than silently falling back to the process-wide key.
|
|
96
|
+
*/
|
|
97
|
+
apiKey?: ApiKeyProvider;
|
|
98
|
+
}
|
|
84
99
|
export interface AgentCallHooks {
|
|
85
100
|
/** Abort signal from the MCP request; triggers server-side cancellation. */
|
|
86
101
|
signal?: AbortSignal;
|