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: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:all": "bun run build:api && bun run build:mcp",
"build": "bun run clean && bun run build:all", "build": "bun run clean && bun run build:all",
"start": "./start.sh" "start": "./scripts/start.sh"
}, },
"private": true, "private": true,
"type": "module", "type": "module",

View File

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

View File

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