Skip to content

Commit 3f3cbcd

Browse files
committed
Fix: Handle network errors in instruction fetch gracefully
Wrap the fetch() call in fetchLatestInstructions with a try-catch block to handle transport-level errors (DNS failure, connection refused, network timeout) that cause fetch to throw rather than returning a Response object. Previously, these errors would propagate unhandled through getInstructions and newMcpServer, causing the entire MCP server to fail to handle requests in HTTP mode, or preventing server startup in stdio mode. Now, network errors are caught and logged, and the server falls back to the default instructions, matching the existing behavior for HTTP-level errors (!response.ok).
1 parent 83d87fb commit 3f3cbcd

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

packages/mcp-server/src/instructions.ts

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,43 @@ export async function getInstructions(stainlessApiKey: string | undefined): Prom
3737
return fetchedInstructions;
3838
}
3939

40-
async function fetchLatestInstructions(stainlessApiKey: string | undefined): Promise<string> {
41-
// Setting the stainless API key is optional, but may be required
42-
// to authenticate requests to the Stainless API.
43-
const response = await fetch(
44-
readEnv('CODE_MODE_INSTRUCTIONS_URL') ?? 'https://api.stainless.com/api/ai/instructions/dedalus-sdk',
45-
{
46-
method: 'GET',
47-
headers: { ...(stainlessApiKey && { Authorization: stainlessApiKey }) },
48-
},
49-
);
40+
const DEFAULT_INSTRUCTIONS = `
41+
This is the dedalus-sdk MCP server. You will use Code Mode to help the user perform
42+
actions. You can use search_docs tool to learn about how to take action with this server. Then,
43+
you will write TypeScript code using the execute tool take action. It is CRITICAL that you be
44+
thoughtful and deliberate when executing code. Always try to entirely solve the problem in code
45+
block: it can be as long as you need to get the job done!
46+
`;
5047

48+
async function fetchLatestInstructions(stainlessApiKey: string | undefined): Promise<string> {
5149
let instructions: string | undefined;
52-
if (!response.ok) {
53-
console.warn(
54-
'Warning: failed to retrieve MCP server instructions. Proceeding with default instructions...',
50+
51+
try {
52+
// Setting the stainless API key is optional, but may be required
53+
// to authenticate requests to the Stainless API.
54+
const response = await fetch(
55+
readEnv('CODE_MODE_INSTRUCTIONS_URL') ?? 'https://api.stainless.com/api/ai/instructions/dedalus-sdk',
56+
{
57+
method: 'GET',
58+
headers: { ...(stainlessApiKey && { Authorization: stainlessApiKey }) },
59+
},
5560
);
5661

57-
instructions = `
58-
This is the dedalus-sdk MCP server. You will use Code Mode to help the user perform
59-
actions. You can use search_docs tool to learn about how to take action with this server. Then,
60-
you will write TypeScript code using the execute tool take action. It is CRITICAL that you be
61-
thoughtful and deliberate when executing code. Always try to entirely solve the problem in code
62-
block: it can be as long as you need to get the job done!
63-
`;
62+
if (!response.ok) {
63+
console.warn(
64+
'Warning: failed to retrieve MCP server instructions. Proceeding with default instructions...',
65+
);
66+
instructions = DEFAULT_INSTRUCTIONS;
67+
} else {
68+
instructions = ((await response.json()) as { instructions: string }).instructions;
69+
}
70+
} catch (error) {
71+
console.warn(
72+
'Warning: network error while fetching MCP server instructions. Proceeding with default instructions...',
73+
error,
74+
);
75+
instructions = DEFAULT_INSTRUCTIONS;
6476
}
65-
66-
instructions ??= ((await response.json()) as { instructions: string }).instructions;
6777
instructions = `
6878
If needed, you can get the current time by executing Date.now().
6979

0 commit comments

Comments
 (0)