@perplexity-ai/mcp-server 0.2.2 → 0.14.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/LICENSE +201 -21
- package/README.md +169 -73
- package/compat.d.mts +58 -0
- package/compat.d.mts.map +1 -0
- package/compat.d.ts +58 -0
- package/compat.d.ts.map +1 -0
- package/compat.js +387 -0
- package/compat.js.map +1 -0
- package/compat.mjs +378 -0
- package/compat.mjs.map +1 -0
- package/dynamic-tools.d.mts +12 -0
- package/dynamic-tools.d.mts.map +1 -0
- package/dynamic-tools.d.ts +12 -0
- package/dynamic-tools.d.ts.map +1 -0
- package/dynamic-tools.js +135 -0
- package/dynamic-tools.js.map +1 -0
- package/dynamic-tools.mjs +132 -0
- package/dynamic-tools.mjs.map +1 -0
- package/filtering.d.mts +2 -0
- package/filtering.d.mts.map +1 -0
- package/filtering.d.ts +2 -0
- package/filtering.d.ts.map +1 -0
- package/filtering.js +20 -0
- package/filtering.js.map +1 -0
- package/filtering.mjs +13 -0
- package/filtering.mjs.map +1 -0
- package/headers.d.mts +4 -0
- package/headers.d.mts.map +1 -0
- package/headers.d.ts +4 -0
- package/headers.d.ts.map +1 -0
- package/headers.js +22 -0
- package/headers.js.map +1 -0
- package/headers.mjs +18 -0
- package/headers.mjs.map +1 -0
- package/http.d.mts +9 -0
- package/http.d.mts.map +1 -0
- package/http.d.ts +9 -0
- package/http.d.ts.map +1 -0
- package/http.js +108 -0
- package/http.js.map +1 -0
- package/http.mjs +100 -0
- package/http.mjs.map +1 -0
- package/index.d.mts +3 -0
- package/index.d.mts.map +1 -0
- package/index.d.ts +3 -0
- package/index.d.ts.map +1 -0
- package/index.js +91 -0
- package/index.js.map +1 -0
- package/index.mjs +89 -0
- package/index.mjs.map +1 -0
- package/options.d.mts +18 -0
- package/options.d.mts.map +1 -0
- package/options.d.ts +18 -0
- package/options.d.ts.map +1 -0
- package/options.js +415 -0
- package/options.js.map +1 -0
- package/options.mjs +408 -0
- package/options.mjs.map +1 -0
- package/package.json +156 -37
- package/server.d.mts +35 -0
- package/server.d.mts.map +1 -0
- package/server.d.ts +35 -0
- package/server.d.ts.map +1 -0
- package/server.js +166 -0
- package/server.js.map +1 -0
- package/server.mjs +152 -0
- package/server.mjs.map +1 -0
- package/src/compat.ts +483 -0
- package/src/dynamic-tools.ts +159 -0
- package/src/filtering.ts +14 -0
- package/src/headers.ts +23 -0
- package/src/http.ts +127 -0
- package/src/index.ts +108 -0
- package/src/options.ts +453 -0
- package/src/server.ts +201 -0
- package/src/stdio.ts +13 -0
- package/src/tools/chat/completions/create-chat-completions.ts +1323 -0
- package/src/tools/index.ts +73 -0
- package/src/tools/search/create-search.ts +89 -0
- package/src/tools/types.ts +103 -0
- package/src/tools.ts +1 -0
- package/src/tsconfig.json +11 -0
- package/stdio.d.mts +3 -0
- package/stdio.d.mts.map +1 -0
- package/stdio.d.ts +3 -0
- package/stdio.d.ts.map +1 -0
- package/stdio.js +14 -0
- package/stdio.js.map +1 -0
- package/stdio.mjs +10 -0
- package/stdio.mjs.map +1 -0
- package/tools/chat/completions/create-chat-completions.d.mts +45 -0
- package/tools/chat/completions/create-chat-completions.d.mts.map +1 -0
- package/tools/chat/completions/create-chat-completions.d.ts +45 -0
- package/tools/chat/completions/create-chat-completions.d.ts.map +1 -0
- package/tools/chat/completions/create-chat-completions.js +1320 -0
- package/tools/chat/completions/create-chat-completions.js.map +1 -0
- package/tools/chat/completions/create-chat-completions.mjs +1316 -0
- package/tools/chat/completions/create-chat-completions.mjs.map +1 -0
- package/tools/index.d.mts +10 -0
- package/tools/index.d.mts.map +1 -0
- package/tools/index.d.ts +10 -0
- package/tools/index.d.ts.map +1 -0
- package/tools/index.js +57 -0
- package/tools/index.js.map +1 -0
- package/tools/index.mjs +50 -0
- package/tools/index.mjs.map +1 -0
- package/tools/search/create-search.d.mts +45 -0
- package/tools/search/create-search.d.mts.map +1 -0
- package/tools/search/create-search.d.ts +45 -0
- package/tools/search/create-search.d.ts.map +1 -0
- package/tools/search/create-search.js +86 -0
- package/tools/search/create-search.js.map +1 -0
- package/tools/search/create-search.mjs +82 -0
- package/tools/search/create-search.mjs.map +1 -0
- package/tools/types.d.mts +51 -0
- package/tools/types.d.mts.map +1 -0
- package/tools/types.d.ts +51 -0
- package/tools/types.d.ts.map +1 -0
- package/tools/types.js +46 -0
- package/tools/types.js.map +1 -0
- package/tools/types.mjs +42 -0
- package/tools/types.mjs.map +1 -0
- package/tools.d.mts +2 -0
- package/tools.d.mts.map +1 -0
- package/tools.d.ts +2 -0
- package/tools.d.ts.map +1 -0
- package/tools.js +18 -0
- package/tools.js.map +1 -0
- package/tools.mjs +2 -0
- package/tools.mjs.map +1 -0
- package/dist/index.js +0 -446
package/src/server.ts
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
4
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
5
|
+
import { Endpoint, endpoints, HandlerFunction, query } from './tools';
|
|
6
|
+
import {
|
|
7
|
+
CallToolRequestSchema,
|
|
8
|
+
ListToolsRequestSchema,
|
|
9
|
+
SetLevelRequestSchema,
|
|
10
|
+
Implementation,
|
|
11
|
+
Tool,
|
|
12
|
+
} from '@modelcontextprotocol/sdk/types.js';
|
|
13
|
+
import { ClientOptions } from '@perplexity-ai/perplexity_ai';
|
|
14
|
+
import Perplexity from '@perplexity-ai/perplexity_ai';
|
|
15
|
+
import {
|
|
16
|
+
applyCompatibilityTransformations,
|
|
17
|
+
ClientCapabilities,
|
|
18
|
+
defaultClientCapabilities,
|
|
19
|
+
knownClients,
|
|
20
|
+
parseEmbeddedJSON,
|
|
21
|
+
} from './compat';
|
|
22
|
+
import { dynamicTools } from './dynamic-tools';
|
|
23
|
+
import { McpOptions } from './options';
|
|
24
|
+
|
|
25
|
+
export { McpOptions } from './options';
|
|
26
|
+
export { ClientType } from './compat';
|
|
27
|
+
export { Filter } from './tools';
|
|
28
|
+
export { ClientOptions } from '@perplexity-ai/perplexity_ai';
|
|
29
|
+
export { endpoints } from './tools';
|
|
30
|
+
|
|
31
|
+
export const newMcpServer = () =>
|
|
32
|
+
new McpServer(
|
|
33
|
+
{
|
|
34
|
+
name: 'perplexity_ai_perplexity_ai_api',
|
|
35
|
+
version: '0.14.0',
|
|
36
|
+
},
|
|
37
|
+
{ capabilities: { tools: {}, logging: {} } },
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
// Create server instance
|
|
41
|
+
export const server = newMcpServer();
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Initializes the provided MCP Server with the given tools and handlers.
|
|
45
|
+
* If not provided, the default client, tools and handlers will be used.
|
|
46
|
+
*/
|
|
47
|
+
export function initMcpServer(params: {
|
|
48
|
+
server: Server | McpServer;
|
|
49
|
+
clientOptions?: ClientOptions;
|
|
50
|
+
mcpOptions?: McpOptions;
|
|
51
|
+
}) {
|
|
52
|
+
const server = params.server instanceof McpServer ? params.server.server : params.server;
|
|
53
|
+
const mcpOptions = params.mcpOptions ?? {};
|
|
54
|
+
|
|
55
|
+
let providedEndpoints: Endpoint[] | null = null;
|
|
56
|
+
let endpointMap: Record<string, Endpoint> | null = null;
|
|
57
|
+
|
|
58
|
+
const initTools = async (implementation?: Implementation) => {
|
|
59
|
+
if (implementation && (!mcpOptions.client || mcpOptions.client === 'infer')) {
|
|
60
|
+
mcpOptions.client =
|
|
61
|
+
implementation.name.toLowerCase().includes('claude') ? 'claude'
|
|
62
|
+
: implementation.name.toLowerCase().includes('cursor') ? 'cursor'
|
|
63
|
+
: undefined;
|
|
64
|
+
mcpOptions.capabilities = {
|
|
65
|
+
...(mcpOptions.client && knownClients[mcpOptions.client]),
|
|
66
|
+
...mcpOptions.capabilities,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
providedEndpoints ??= await selectTools(endpoints, mcpOptions);
|
|
70
|
+
endpointMap ??= Object.fromEntries(providedEndpoints.map((endpoint) => [endpoint.tool.name, endpoint]));
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const logAtLevel =
|
|
74
|
+
(level: 'debug' | 'info' | 'warning' | 'error') =>
|
|
75
|
+
(message: string, ...rest: unknown[]) => {
|
|
76
|
+
void server.sendLoggingMessage({
|
|
77
|
+
level,
|
|
78
|
+
data: { message, rest },
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
const logger = {
|
|
82
|
+
debug: logAtLevel('debug'),
|
|
83
|
+
info: logAtLevel('info'),
|
|
84
|
+
warn: logAtLevel('warning'),
|
|
85
|
+
error: logAtLevel('error'),
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
let client = new Perplexity({
|
|
89
|
+
logger,
|
|
90
|
+
...params.clientOptions,
|
|
91
|
+
defaultHeaders: {
|
|
92
|
+
...params.clientOptions?.defaultHeaders,
|
|
93
|
+
'X-Stainless-MCP': 'true',
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
98
|
+
if (providedEndpoints === null) {
|
|
99
|
+
await initTools(server.getClientVersion());
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
tools: providedEndpoints!.map((endpoint) => endpoint.tool),
|
|
103
|
+
};
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
107
|
+
if (endpointMap === null) {
|
|
108
|
+
await initTools(server.getClientVersion());
|
|
109
|
+
}
|
|
110
|
+
const { name, arguments: args } = request.params;
|
|
111
|
+
const endpoint = endpointMap![name];
|
|
112
|
+
if (!endpoint) {
|
|
113
|
+
throw new Error(`Unknown tool: ${name}`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return executeHandler(endpoint.tool, endpoint.handler, client, args, mcpOptions.capabilities);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
server.setRequestHandler(SetLevelRequestSchema, async (request) => {
|
|
120
|
+
const { level } = request.params;
|
|
121
|
+
switch (level) {
|
|
122
|
+
case 'debug':
|
|
123
|
+
client = client.withOptions({ logLevel: 'debug' });
|
|
124
|
+
break;
|
|
125
|
+
case 'info':
|
|
126
|
+
client = client.withOptions({ logLevel: 'info' });
|
|
127
|
+
break;
|
|
128
|
+
case 'notice':
|
|
129
|
+
case 'warning':
|
|
130
|
+
client = client.withOptions({ logLevel: 'warn' });
|
|
131
|
+
break;
|
|
132
|
+
case 'error':
|
|
133
|
+
client = client.withOptions({ logLevel: 'error' });
|
|
134
|
+
break;
|
|
135
|
+
default:
|
|
136
|
+
client = client.withOptions({ logLevel: 'off' });
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
return {};
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Selects the tools to include in the MCP Server based on the provided options.
|
|
145
|
+
*/
|
|
146
|
+
export async function selectTools(endpoints: Endpoint[], options?: McpOptions): Promise<Endpoint[]> {
|
|
147
|
+
const filteredEndpoints = query(options?.filters ?? [], endpoints);
|
|
148
|
+
|
|
149
|
+
let includedTools = filteredEndpoints.slice();
|
|
150
|
+
|
|
151
|
+
if (includedTools.length > 0) {
|
|
152
|
+
if (options?.includeDynamicTools) {
|
|
153
|
+
includedTools = dynamicTools(includedTools);
|
|
154
|
+
}
|
|
155
|
+
} else {
|
|
156
|
+
if (options?.includeAllTools) {
|
|
157
|
+
includedTools = endpoints.slice();
|
|
158
|
+
} else if (options?.includeDynamicTools) {
|
|
159
|
+
includedTools = dynamicTools(endpoints);
|
|
160
|
+
} else {
|
|
161
|
+
includedTools = endpoints.slice();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const capabilities = { ...defaultClientCapabilities, ...options?.capabilities };
|
|
166
|
+
return applyCompatibilityTransformations(includedTools, capabilities);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Runs the provided handler with the given client and arguments.
|
|
171
|
+
*/
|
|
172
|
+
export async function executeHandler(
|
|
173
|
+
tool: Tool,
|
|
174
|
+
handler: HandlerFunction,
|
|
175
|
+
client: Perplexity,
|
|
176
|
+
args: Record<string, unknown> | undefined,
|
|
177
|
+
compatibilityOptions?: Partial<ClientCapabilities>,
|
|
178
|
+
) {
|
|
179
|
+
const options = { ...defaultClientCapabilities, ...compatibilityOptions };
|
|
180
|
+
if (!options.validJson && args) {
|
|
181
|
+
args = parseEmbeddedJSON(args, tool.inputSchema);
|
|
182
|
+
}
|
|
183
|
+
return await handler(client, args || {});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export const readEnv = (env: string): string | undefined => {
|
|
187
|
+
if (typeof (globalThis as any).process !== 'undefined') {
|
|
188
|
+
return (globalThis as any).process.env?.[env]?.trim();
|
|
189
|
+
} else if (typeof (globalThis as any).Deno !== 'undefined') {
|
|
190
|
+
return (globalThis as any).Deno.env?.get?.(env)?.trim();
|
|
191
|
+
}
|
|
192
|
+
return;
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
export const readEnvOrError = (env: string): string => {
|
|
196
|
+
let envValue = readEnv(env);
|
|
197
|
+
if (envValue === undefined) {
|
|
198
|
+
throw new Error(`Environment variable ${env} is not set`);
|
|
199
|
+
}
|
|
200
|
+
return envValue;
|
|
201
|
+
};
|
package/src/stdio.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
2
|
+
import { initMcpServer, newMcpServer } from './server';
|
|
3
|
+
import { McpOptions } from './options';
|
|
4
|
+
|
|
5
|
+
export const launchStdioServer = async (options: McpOptions) => {
|
|
6
|
+
const server = newMcpServer();
|
|
7
|
+
|
|
8
|
+
initMcpServer({ server, mcpOptions: options });
|
|
9
|
+
|
|
10
|
+
const transport = new StdioServerTransport();
|
|
11
|
+
await server.connect(transport);
|
|
12
|
+
console.error('MCP Server running on stdio');
|
|
13
|
+
};
|