@perplexity-ai/mcp-server 1.1.0 → 1.2.0
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 +20 -0
- package/dist/server.d.ts +7 -6
- package/dist/server.js +32 -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.
|
|
9
|
+
"version": "1.2.0"
|
|
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.
|
|
16
|
+
"version": "1.2.0",
|
|
17
17
|
"author": {
|
|
18
18
|
"name": "Perplexity AI",
|
|
19
19
|
"email": "api@perplexity.ai"
|
package/README.md
CHANGED
|
@@ -147,6 +147,26 @@ npm install && npm run build && npm run start:http
|
|
|
147
147
|
|
|
148
148
|
The server will be accessible at `http://localhost:8080/mcp`
|
|
149
149
|
|
|
150
|
+
## Use as a Library
|
|
151
|
+
|
|
152
|
+
The package also exports the server factory for embedding in your own Node process:
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
import { createPerplexityServer } from "@perplexity-ai/mcp-server";
|
|
156
|
+
|
|
157
|
+
// Single-tenant: reads PERPLEXITY_API_KEY from the environment.
|
|
158
|
+
const server = createPerplexityServer("my-service");
|
|
159
|
+
|
|
160
|
+
// Multi-tenant hosts resolve the key per call instead. When a provider is
|
|
161
|
+
// set, the environment variable is never consulted, and a provider that
|
|
162
|
+
// returns no key fails the call rather than falling back.
|
|
163
|
+
const tenantServer = createPerplexityServer("my-service", {
|
|
164
|
+
apiKey: () => currentRequestApiKey,
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Mount the returned server on any MCP transport (stdio, streamable HTTP, in-memory).
|
|
169
|
+
|
|
150
170
|
## Troubleshooting
|
|
151
171
|
|
|
152
172
|
- **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.
|
|
7
|
+
const VERSION = "1.2.0";
|
|
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,7 +79,7 @@ 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",
|
|
73
85
|
};
|
|
@@ -106,9 +118,9 @@ async function makeApiRequest(endpoint, body, serviceOrigin, signal) {
|
|
|
106
118
|
return response;
|
|
107
119
|
}
|
|
108
120
|
/** Best-effort cancellation of an agent run so an abandoned request stops billing. */
|
|
109
|
-
export async function cancelAgentResponse(responseId, serviceOrigin) {
|
|
121
|
+
export async function cancelAgentResponse(responseId, serviceOrigin, apiKey) {
|
|
110
122
|
try {
|
|
111
|
-
await makeApiRequest(`v1/agent/${encodeURIComponent(responseId)}/cancel`, {}, serviceOrigin);
|
|
123
|
+
await makeApiRequest(`v1/agent/${encodeURIComponent(responseId)}/cancel`, {}, serviceOrigin, undefined, apiKey);
|
|
112
124
|
}
|
|
113
125
|
catch {
|
|
114
126
|
// The run may already be terminal; nothing actionable either way.
|
|
@@ -118,7 +130,7 @@ export async function cancelAgentResponse(responseId, serviceOrigin) {
|
|
|
118
130
|
* Consume an Agent API SSE stream and return the final response object.
|
|
119
131
|
* Emits progress via hooks.onProgress as reasoning events arrive.
|
|
120
132
|
*/
|
|
121
|
-
export async function consumeAgentStream(response, hooks, serviceOrigin, deadlineSignal) {
|
|
133
|
+
export async function consumeAgentStream(response, hooks, serviceOrigin, deadlineSignal, apiKey) {
|
|
122
134
|
const body = response.body;
|
|
123
135
|
if (!body) {
|
|
124
136
|
throw new Error("Response body is null");
|
|
@@ -228,7 +240,7 @@ export async function consumeAgentStream(response, hooks, serviceOrigin, deadlin
|
|
|
228
240
|
if (hooks?.signal?.aborted || deadlineSignal?.aborted) {
|
|
229
241
|
if (responseId) {
|
|
230
242
|
// Stop the server-side run so an abandoned request stops billing.
|
|
231
|
-
void cancelAgentResponse(responseId, serviceOrigin);
|
|
243
|
+
void cancelAgentResponse(responseId, serviceOrigin, apiKey);
|
|
232
244
|
}
|
|
233
245
|
if (hooks?.signal?.aborted) {
|
|
234
246
|
throw new Error("Request cancelled");
|
|
@@ -239,7 +251,7 @@ export async function consumeAgentStream(response, hooks, serviceOrigin, deadlin
|
|
|
239
251
|
}
|
|
240
252
|
if (hooks?.signal?.aborted) {
|
|
241
253
|
if (responseId) {
|
|
242
|
-
void cancelAgentResponse(responseId, serviceOrigin);
|
|
254
|
+
void cancelAgentResponse(responseId, serviceOrigin, apiKey);
|
|
243
255
|
}
|
|
244
256
|
throw new Error("Request cancelled");
|
|
245
257
|
}
|
|
@@ -338,7 +350,7 @@ function buildWebSearchTool(options) {
|
|
|
338
350
|
// No override means the preset's own web_search config stays in effect.
|
|
339
351
|
return Object.keys(tool).length > 1 ? tool : undefined;
|
|
340
352
|
}
|
|
341
|
-
export async function performAgentResponse(messages, preset, serviceOrigin, options, hooks) {
|
|
353
|
+
export async function performAgentResponse(messages, preset, serviceOrigin, options, hooks, apiKey) {
|
|
342
354
|
const webSearchTool = buildWebSearchTool(options);
|
|
343
355
|
const body = {
|
|
344
356
|
preset,
|
|
@@ -368,8 +380,8 @@ export async function performAgentResponse(messages, preset, serviceOrigin, opti
|
|
|
368
380
|
}
|
|
369
381
|
}
|
|
370
382
|
try {
|
|
371
|
-
const response = await makeApiRequest("v1/agent", body, serviceOrigin, deadline.signal);
|
|
372
|
-
const agentResponse = await consumeAgentStream(response, hooks, serviceOrigin, deadline.signal);
|
|
383
|
+
const response = await makeApiRequest("v1/agent", body, serviceOrigin, deadline.signal, apiKey);
|
|
384
|
+
const agentResponse = await consumeAgentStream(response, hooks, serviceOrigin, deadline.signal, apiKey);
|
|
373
385
|
return formatAgentResponseText(agentResponse);
|
|
374
386
|
}
|
|
375
387
|
catch (error) {
|
|
@@ -404,7 +416,7 @@ export function formatSearchResults(data) {
|
|
|
404
416
|
});
|
|
405
417
|
return formattedResults;
|
|
406
418
|
}
|
|
407
|
-
export async function performSearch(query, maxResults = 10, maxTokensPerPage = 1024, country, filters, serviceOrigin) {
|
|
419
|
+
export async function performSearch(query, maxResults = 10, maxTokensPerPage = 1024, country, filters, serviceOrigin, apiKey) {
|
|
408
420
|
const body = {
|
|
409
421
|
query: query,
|
|
410
422
|
max_results: maxResults,
|
|
@@ -413,7 +425,7 @@ export async function performSearch(query, maxResults = 10, maxTokensPerPage = 1
|
|
|
413
425
|
...(filters?.search_recency_filter && { search_recency_filter: filters.search_recency_filter }),
|
|
414
426
|
...(filters?.search_domain_filter && { search_domain_filter: filters.search_domain_filter }),
|
|
415
427
|
};
|
|
416
|
-
const response = await makeApiRequest("search", body, serviceOrigin);
|
|
428
|
+
const response = await makeApiRequest("search", body, serviceOrigin, undefined, apiKey);
|
|
417
429
|
let data;
|
|
418
430
|
try {
|
|
419
431
|
const json = await response.json();
|
|
@@ -444,7 +456,7 @@ function buildHooks(extra) {
|
|
|
444
456
|
: undefined,
|
|
445
457
|
};
|
|
446
458
|
}
|
|
447
|
-
export function createPerplexityServer(serviceOrigin) {
|
|
459
|
+
export function createPerplexityServer(serviceOrigin, serverOptions) {
|
|
448
460
|
const server = new McpServer({
|
|
449
461
|
name: "ai.perplexity/mcp-server",
|
|
450
462
|
version: VERSION,
|
|
@@ -504,7 +516,7 @@ export function createPerplexityServer(serviceOrigin) {
|
|
|
504
516
|
...(search_domain_filter && { search_domain_filter }),
|
|
505
517
|
...(search_context_size && { search_context_size }),
|
|
506
518
|
};
|
|
507
|
-
const result = await performAgentResponse(messages, ASK_PRESET, serviceOrigin, Object.keys(options).length > 0 ? options : undefined, buildHooks(extra));
|
|
519
|
+
const result = await performAgentResponse(messages, ASK_PRESET, serviceOrigin, Object.keys(options).length > 0 ? options : undefined, buildHooks(extra), serverOptions?.apiKey);
|
|
508
520
|
return {
|
|
509
521
|
content: [{ type: "text", text: result }],
|
|
510
522
|
structuredContent: { response: result },
|
|
@@ -529,7 +541,7 @@ export function createPerplexityServer(serviceOrigin) {
|
|
|
529
541
|
}, async (args, extra) => {
|
|
530
542
|
const { messages } = args;
|
|
531
543
|
validateMessages(messages, "perplexity_research");
|
|
532
|
-
const result = await performAgentResponse(messages, RESEARCH_PRESET, serviceOrigin, undefined, buildHooks(extra));
|
|
544
|
+
const result = await performAgentResponse(messages, RESEARCH_PRESET, serviceOrigin, undefined, buildHooks(extra), serverOptions?.apiKey);
|
|
533
545
|
return {
|
|
534
546
|
content: [{ type: "text", text: result }],
|
|
535
547
|
structuredContent: { response: result },
|
|
@@ -559,7 +571,7 @@ export function createPerplexityServer(serviceOrigin) {
|
|
|
559
571
|
...(search_domain_filter && { search_domain_filter }),
|
|
560
572
|
...(search_context_size && { search_context_size }),
|
|
561
573
|
};
|
|
562
|
-
const result = await performAgentResponse(messages, REASON_PRESET, serviceOrigin, Object.keys(options).length > 0 ? options : undefined, buildHooks(extra));
|
|
574
|
+
const result = await performAgentResponse(messages, REASON_PRESET, serviceOrigin, Object.keys(options).length > 0 ? options : undefined, buildHooks(extra), serverOptions?.apiKey);
|
|
563
575
|
return {
|
|
564
576
|
content: [{ type: "text", text: result }],
|
|
565
577
|
structuredContent: { response: result },
|
|
@@ -603,7 +615,7 @@ export function createPerplexityServer(serviceOrigin) {
|
|
|
603
615
|
...(search_recency_filter && { search_recency_filter }),
|
|
604
616
|
...(search_domain_filter && { search_domain_filter }),
|
|
605
617
|
};
|
|
606
|
-
const result = await performSearch(query, maxResults, maxTokensPerPage, countryCode, filters, serviceOrigin);
|
|
618
|
+
const result = await performSearch(query, maxResults, maxTokensPerPage, countryCode, filters, serviceOrigin, serverOptions?.apiKey);
|
|
607
619
|
return {
|
|
608
620
|
content: [{ type: "text", text: result }],
|
|
609
621
|
structuredContent: { results: result },
|
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;
|