Skip to content

Commit d491bbe

Browse files
authored
Merge pull request #54 from perplexityai/kesku/deep-research-timeout
Fix timeout issues for sonar-deep-research
2 parents 124f38f + c159706 commit d491bbe

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Advanced reasoning and problem-solving using the `sonar-reasoning-pro` model. Pe
2929
### Get Your API Key
3030
1. Get your Perplexity API Key from the [API Portal](https://www.perplexity.ai/account/api/group)
3131
2. Set it as an environment variable: `PERPLEXITY_API_KEY=your_key_here`
32+
3. (Optional) Set a timeout for requests: `PERPLEXITY_TIMEOUT_MS=600000`. The default is 5 minutes.
3233

3334
### Claude Code
3435

@@ -51,6 +52,7 @@ Or add to your `claude.json`:
5152
],
5253
"env": {
5354
"PERPLEXITY_API_KEY": "your_key_here",
55+
"PERPLEXITY_TIMEOUT_MS": "600000"
5456
}
5557
}
5658
}
@@ -68,6 +70,7 @@ Add to your `mcp.json`:
6870
"args": ["-y", "@perplexity-ai/mcp-server"],
6971
"env": {
7072
"PERPLEXITY_API_KEY": "your_key_here",
73+
"PERPLEXITY_TIMEOUT_MS": "600000"
7174
}
7275
}
7376
}
@@ -86,6 +89,7 @@ Add to your `claude_desktop_config.json`:
8689
"args": ["-y", "@perplexity-ai/mcp-server"],
8790
"env": {
8891
"PERPLEXITY_API_KEY": "your_key_here",
92+
"PERPLEXITY_TIMEOUT_MS": "600000"
8993
}
9094
}
9195
}
@@ -105,6 +109,7 @@ npx @perplexity-ai/mcp-server
105109
- **API Key Issues**: Ensure `PERPLEXITY_API_KEY` is set correctly
106110
- **Connection Errors**: Check your internet connection and API key validity
107111
- **Tool Not Found**: Make sure the package is installed and the command path is correct
112+
- **Timeout Errors**: For very long research queries, set `PERPLEXITY_TIMEOUT_MS` to a higher value
108113

109114
For support, visit [community.perplexity.ai](https://community.perplexity.ai) or [file an issue](https://github.com/perplexityai/modelcontextprotocol/issues).
110115

index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ if (!PERPLEXITY_API_KEY) {
161161
process.exit(1);
162162
}
163163

164+
// Configure timeout for API requests (default: 5 minutes)
165+
// Can be overridden via PERPLEXITY_TIMEOUT_MS environment variable
166+
const TIMEOUT_MS = parseInt(process.env.PERPLEXITY_TIMEOUT_MS || "300000", 10);
167+
164168
/**
165169
* Performs a chat completion by sending a request to the Perplexity API.
166170
* Appends citations to the returned message content if they exist.
@@ -184,6 +188,9 @@ async function performChatCompletion(
184188
// https://docs.perplexity.ai/api-reference/chat-completions
185189
};
186190

191+
const controller = new AbortController();
192+
const timeoutId = setTimeout(() => controller.abort(), TIMEOUT_MS);
193+
187194
let response;
188195
try {
189196
response = await fetch(url.toString(), {
@@ -193,8 +200,14 @@ async function performChatCompletion(
193200
"Authorization": `Bearer ${PERPLEXITY_API_KEY}`,
194201
},
195202
body: JSON.stringify(body),
203+
signal: controller.signal,
196204
});
205+
clearTimeout(timeoutId);
197206
} catch (error) {
207+
clearTimeout(timeoutId);
208+
if (error instanceof Error && error.name === "AbortError") {
209+
throw new Error(`Request timeout: Perplexity API did not respond within ${TIMEOUT_MS}ms. Consider increasing PERPLEXITY_TIMEOUT_MS.`);
210+
}
198211
throw new Error(`Network error while calling Perplexity API: ${error}`);
199212
}
200213

@@ -288,6 +301,9 @@ async function performSearch(
288301
body.country = country;
289302
}
290303

304+
const controller = new AbortController();
305+
const timeoutId = setTimeout(() => controller.abort(), TIMEOUT_MS);
306+
291307
let response;
292308
try {
293309
response = await fetch(url.toString(), {
@@ -297,8 +313,14 @@ async function performSearch(
297313
"Authorization": `Bearer ${PERPLEXITY_API_KEY}`,
298314
},
299315
body: JSON.stringify(body),
316+
signal: controller.signal,
300317
});
318+
clearTimeout(timeoutId);
301319
} catch (error) {
320+
clearTimeout(timeoutId);
321+
if (error instanceof Error && error.name === "AbortError") {
322+
throw new Error(`Request timeout: Perplexity Search API did not respond within ${TIMEOUT_MS}ms. Consider increasing PERPLEXITY_TIMEOUT_MS.`);
323+
}
302324
throw new Error(`Network error while calling Perplexity Search API: ${error}`);
303325
}
304326

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@perplexity-ai/mcp-server",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "Official MCP server for Perplexity API Platform",
55
"keywords": [
66
"ai",

0 commit comments

Comments
 (0)