feat: add 60-second timeouts to MCP request handlers for reliability

This commit is contained in:
2026-01-23 13:10:45 -05:00
parent 72525609ed
commit eb6705df0f
3 changed files with 51 additions and 27 deletions

View File

@@ -8,7 +8,7 @@
"build:mcp": "bun build ./packages/mcp-server/src/index.ts --target=bun --outdir=./dist/mcp --minify",
"build:all": "bun run build:api && bun run build:mcp",
"build": "bun run clean && bun run build:all",
"start": "./start.sh"
"start": "./scripts/start.sh"
},
"private": true,
"type": "module",

View File

@@ -115,13 +115,21 @@ export async function handleMcpRequest(req: Request): Promise<Response> {
priceMin: args.priceMin,
priceMax: args.priceMax,
};
const items = await fetchKijijiItems(
const items = await Promise.race([
fetchKijijiItems(
query,
1,
"https://www.kijiji.ca",
searchOptions,
{},
);
),
new Promise((_, reject) =>
setTimeout(
() => reject(new Error("Request timed out after 60 seconds")),
60000,
),
),
]);
result = items || [];
} else if (name === "search_facebook") {
const query = args.query;
@@ -132,14 +140,22 @@ export async function handleMcpRequest(req: Request): Promise<Response> {
error: { code: -32602, message: "query parameter is required" },
});
}
const items = await fetchFacebookItems(
const items = await Promise.race([
fetchFacebookItems(
query,
1,
args.location || "toronto",
args.maxItems || 25,
args.cookiesSource,
undefined,
);
),
new Promise((_, reject) =>
setTimeout(
() => reject(new Error("Request timed out after 60 seconds")),
60000,
),
),
]);
result = items || [];
} else if (name === "search_ebay") {
const query = args.query;
@@ -150,7 +166,8 @@ export async function handleMcpRequest(req: Request): Promise<Response> {
error: { code: -32602, message: "query parameter is required" },
});
}
const items = await fetchEbayItems(query, 1, {
const items = await Promise.race([
fetchEbayItems(query, 1, {
minPrice: args.minPrice,
maxPrice: args.maxPrice,
strictMode: args.strictMode || false,
@@ -158,7 +175,14 @@ export async function handleMcpRequest(req: Request): Promise<Response> {
keywords: args.keywords || [query],
buyItNowOnly: args.buyItNowOnly !== false,
canadaOnly: args.canadaOnly !== false,
});
}),
new Promise((_, reject) =>
setTimeout(
() => reject(new Error("Request timed out after 60 seconds")),
60000,
),
),
]);
const results = args.maxItems ? items.slice(0, args.maxItems) : items;
result = results || [];

View File

@@ -6,7 +6,7 @@ trap 'echo "Received shutdown signal, stopping services..."; kill -TERM $API_PID
# Start API Server in background
echo "Starting API Server on port ${API_PORT:-4005}..."
bun ../dist/api/index.js &
bun dist/api/index.js &
API_PID=$!
# Give API server a moment to initialize
@@ -14,7 +14,7 @@ sleep 1
# Start MCP Server in background
echo "Starting MCP Server on port ${API_PORT:-4006}..."
bun ../dist/mcp/index.js &
bun dist/mcp/index.js &
MCP_PID=$!
echo "Both services started successfully"