@perplexity-ai/mcp-server 0.13.0 → 1.0.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.
Files changed (137) hide show
  1. package/.claude-plugin/marketplace.json +49 -0
  2. package/LICENSE +21 -201
  3. package/README.md +105 -157
  4. package/dist/http.js +190 -0
  5. package/dist/index.js +23 -0
  6. package/dist/logger.js +82 -0
  7. package/dist/server.js +604 -0
  8. package/dist/types.js +1 -0
  9. package/dist/validation.js +62 -0
  10. package/package.json +56 -155
  11. package/compat.d.mts +0 -58
  12. package/compat.d.mts.map +0 -1
  13. package/compat.d.ts +0 -58
  14. package/compat.d.ts.map +0 -1
  15. package/compat.js +0 -387
  16. package/compat.js.map +0 -1
  17. package/compat.mjs +0 -378
  18. package/compat.mjs.map +0 -1
  19. package/dynamic-tools.d.mts +0 -12
  20. package/dynamic-tools.d.mts.map +0 -1
  21. package/dynamic-tools.d.ts +0 -12
  22. package/dynamic-tools.d.ts.map +0 -1
  23. package/dynamic-tools.js +0 -135
  24. package/dynamic-tools.js.map +0 -1
  25. package/dynamic-tools.mjs +0 -132
  26. package/dynamic-tools.mjs.map +0 -1
  27. package/filtering.d.mts +0 -2
  28. package/filtering.d.mts.map +0 -1
  29. package/filtering.d.ts +0 -2
  30. package/filtering.d.ts.map +0 -1
  31. package/filtering.js +0 -20
  32. package/filtering.js.map +0 -1
  33. package/filtering.mjs +0 -13
  34. package/filtering.mjs.map +0 -1
  35. package/headers.d.mts +0 -4
  36. package/headers.d.mts.map +0 -1
  37. package/headers.d.ts +0 -4
  38. package/headers.d.ts.map +0 -1
  39. package/headers.js +0 -22
  40. package/headers.js.map +0 -1
  41. package/headers.mjs +0 -18
  42. package/headers.mjs.map +0 -1
  43. package/http.d.mts +0 -9
  44. package/http.d.mts.map +0 -1
  45. package/http.d.ts +0 -9
  46. package/http.d.ts.map +0 -1
  47. package/http.js +0 -108
  48. package/http.js.map +0 -1
  49. package/http.mjs +0 -100
  50. package/http.mjs.map +0 -1
  51. package/index.d.mts +0 -3
  52. package/index.d.mts.map +0 -1
  53. package/index.d.ts +0 -3
  54. package/index.d.ts.map +0 -1
  55. package/index.js +0 -91
  56. package/index.js.map +0 -1
  57. package/index.mjs +0 -89
  58. package/index.mjs.map +0 -1
  59. package/options.d.mts +0 -18
  60. package/options.d.mts.map +0 -1
  61. package/options.d.ts +0 -18
  62. package/options.d.ts.map +0 -1
  63. package/options.js +0 -415
  64. package/options.js.map +0 -1
  65. package/options.mjs +0 -408
  66. package/options.mjs.map +0 -1
  67. package/server.d.mts +0 -35
  68. package/server.d.mts.map +0 -1
  69. package/server.d.ts +0 -35
  70. package/server.d.ts.map +0 -1
  71. package/server.js +0 -166
  72. package/server.js.map +0 -1
  73. package/server.mjs +0 -152
  74. package/server.mjs.map +0 -1
  75. package/src/compat.ts +0 -483
  76. package/src/dynamic-tools.ts +0 -159
  77. package/src/filtering.ts +0 -14
  78. package/src/headers.ts +0 -23
  79. package/src/http.ts +0 -127
  80. package/src/index.ts +0 -108
  81. package/src/options.ts +0 -453
  82. package/src/server.ts +0 -201
  83. package/src/stdio.ts +0 -13
  84. package/src/tools/chat/completions/create-chat-completions.ts +0 -1360
  85. package/src/tools/index.ts +0 -73
  86. package/src/tools/search/create-search.ts +0 -89
  87. package/src/tools/types.ts +0 -103
  88. package/src/tools.ts +0 -1
  89. package/src/tsconfig.json +0 -11
  90. package/stdio.d.mts +0 -3
  91. package/stdio.d.mts.map +0 -1
  92. package/stdio.d.ts +0 -3
  93. package/stdio.d.ts.map +0 -1
  94. package/stdio.js +0 -14
  95. package/stdio.js.map +0 -1
  96. package/stdio.mjs +0 -10
  97. package/stdio.mjs.map +0 -1
  98. package/tools/chat/completions/create-chat-completions.d.mts +0 -45
  99. package/tools/chat/completions/create-chat-completions.d.mts.map +0 -1
  100. package/tools/chat/completions/create-chat-completions.d.ts +0 -45
  101. package/tools/chat/completions/create-chat-completions.d.ts.map +0 -1
  102. package/tools/chat/completions/create-chat-completions.js +0 -1357
  103. package/tools/chat/completions/create-chat-completions.js.map +0 -1
  104. package/tools/chat/completions/create-chat-completions.mjs +0 -1353
  105. package/tools/chat/completions/create-chat-completions.mjs.map +0 -1
  106. package/tools/index.d.mts +0 -10
  107. package/tools/index.d.mts.map +0 -1
  108. package/tools/index.d.ts +0 -10
  109. package/tools/index.d.ts.map +0 -1
  110. package/tools/index.js +0 -57
  111. package/tools/index.js.map +0 -1
  112. package/tools/index.mjs +0 -50
  113. package/tools/index.mjs.map +0 -1
  114. package/tools/search/create-search.d.mts +0 -45
  115. package/tools/search/create-search.d.mts.map +0 -1
  116. package/tools/search/create-search.d.ts +0 -45
  117. package/tools/search/create-search.d.ts.map +0 -1
  118. package/tools/search/create-search.js +0 -86
  119. package/tools/search/create-search.js.map +0 -1
  120. package/tools/search/create-search.mjs +0 -82
  121. package/tools/search/create-search.mjs.map +0 -1
  122. package/tools/types.d.mts +0 -51
  123. package/tools/types.d.mts.map +0 -1
  124. package/tools/types.d.ts +0 -51
  125. package/tools/types.d.ts.map +0 -1
  126. package/tools/types.js +0 -46
  127. package/tools/types.js.map +0 -1
  128. package/tools/types.mjs +0 -42
  129. package/tools/types.mjs.map +0 -1
  130. package/tools.d.mts +0 -2
  131. package/tools.d.mts.map +0 -1
  132. package/tools.d.ts +0 -2
  133. package/tools.d.ts.map +0 -1
  134. package/tools.js +0 -18
  135. package/tools.js.map +0 -1
  136. package/tools.mjs +0 -2
  137. package/tools.mjs.map +0 -1
package/src/server.ts DELETED
@@ -1,201 +0,0 @@
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.13.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 DELETED
@@ -1,13 +0,0 @@
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
- };