Skip to content

Commit 4512904

Browse files
authored
fix: simplify error handling and return the error message for all errors (#306)
* fix: simplify error handling and return the error message for all errors * lint
1 parent 6f2e5c3 commit 4512904

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

src/mcp/server.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
SetLevelRequestSchema,
2222
} from '@modelcontextprotocol/sdk/types.js';
2323
import type { ValidateFunction } from 'ajv';
24-
import { type ActorCallOptions, ApifyApiError } from 'apify-client';
24+
import { type ActorCallOptions } from 'apify-client';
2525

2626
import log from '@apify/log';
2727

@@ -39,6 +39,7 @@ import { callActorGetDataset, defaultTools, getActorsAsTools, toolCategories } f
3939
import { decodeDotPropertyNames } from '../tools/utils.js';
4040
import type { ActorMcpTool, ActorTool, HelperTool, ToolEntry } from '../types.js';
4141
import { buildActorResponseContent } from '../utils/actor-response.js';
42+
import { buildMCPResponse } from '../utils/mcp.js';
4243
import { createProgressTracker } from '../utils/progress.js';
4344
import { getToolPublicFieldOnly } from '../utils/tools.js';
4445
import { connectMCPClient } from './client.js';
@@ -654,19 +655,11 @@ export class ActorsMcpServer {
654655
}
655656
}
656657
} catch (error) {
657-
if (error instanceof ApifyApiError) {
658-
log.error('Apify API error calling tool', { toolName: name, error });
659-
return {
660-
content: [
661-
{ type: 'text', text: `Apify API error calling tool ${name}: ${error.message}` },
662-
],
663-
};
664-
}
665-
log.error('Error calling tool', { toolName: name, error });
666-
throw new McpError(
667-
ErrorCode.InternalError,
668-
`An error occurred while calling the tool.`,
669-
);
658+
log.error('Error occurred while calling tool', { toolName: name, error });
659+
const errorMessage = (error instanceof Error) ? error.message : 'Unknown error';
660+
return buildMCPResponse([
661+
`Error calling tool ${name}: ${errorMessage}`,
662+
]);
670663
}
671664

672665
const msg = `Unknown tool: ${name}`;

0 commit comments

Comments
 (0)