Skip to content

Commit c4ec6b8

Browse files
committed
refactor ai slop
1 parent 7d73fa2 commit c4ec6b8

File tree

6 files changed

+38
-81
lines changed

6 files changed

+38
-81
lines changed

src/mcp/server.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
ListToolsRequestSchema,
1313
McpError,
1414
} from '@modelcontextprotocol/sdk/types.js';
15-
import type { ActorCallOptions } from 'apify-client';
15+
import { type ActorCallOptions, ApifyApiError } from 'apify-client';
1616

1717
import log from '@apify/log';
1818

@@ -453,6 +453,14 @@ export class ActorsMcpServer {
453453
return { content };
454454
}
455455
} catch (error) {
456+
if (error instanceof ApifyApiError) {
457+
log.error(`Apify API error calling tool ${name}: ${error.message}`);
458+
return {
459+
content: [
460+
{ type: 'text', text: `Apify API erro calling tool ${name}: ${error.message}` },
461+
],
462+
};
463+
}
456464
log.error(`Error calling tool ${name}: ${error}`);
457465
throw new McpError(
458466
ErrorCode.InternalError,

src/tools/actor.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -250,18 +250,11 @@ export const getActor: ToolEntry = {
250250
return { content: [{ type: 'text', text: 'Actor ID is required.' }] };
251251
}
252252
const client = new ApifyClient({ token: apifyToken });
253-
try {
254-
const actor = await client.actor(actorId).get();
255-
if (!actor) {
256-
return { content: [{ type: 'text', text: `Actor '${actorId}' not found.` }] };
257-
}
258-
return { content: [{ type: 'text', text: JSON.stringify(actor) }] };
259-
} catch (error) {
260-
if (error instanceof ApifyApiError) {
261-
return { content: [{ type: 'text', text: `Failed to get actor details: ${error.message}` }] };
262-
}
263-
throw error;
253+
const actor = await client.actor(actorId).get();
254+
if (!actor) {
255+
return { content: [{ type: 'text', text: `Actor '${actorId}' not found.` }] };
264256
}
257+
return { content: [{ type: 'text', text: JSON.stringify(actor) }] };
265258
},
266259
} as InternalTool,
267260
};

src/tools/dataset.ts

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,11 @@ export const getDataset: ToolEntry = {
5656
return { content: [{ type: 'text', text: 'Dataset ID is required.' }] };
5757
}
5858
const client = new ApifyClient({ token: apifyToken });
59-
try {
60-
const v = await client.dataset(parsed.datasetId).get();
61-
if (!v) {
62-
return { content: [{ type: 'text', text: `Dataset '${parsed.datasetId}' not found.` }] };
63-
}
64-
return { content: [{ type: 'text', text: JSON.stringify(v) }] };
65-
} catch {
66-
return { content: [{ type: 'text', text: `Invalid dataset ID or dataset not found.` }] };
59+
const v = await client.dataset(parsed.datasetId).get();
60+
if (!v) {
61+
return { content: [{ type: 'text', text: `Dataset '${parsed.datasetId}' not found.` }] };
6762
}
63+
return { content: [{ type: 'text', text: JSON.stringify(v) }] };
6864
},
6965
} as InternalTool,
7066
};
@@ -96,28 +92,24 @@ export const getDatasetItems: ToolEntry = {
9692
return { content: [{ type: 'text', text: 'Dataset ID is required.' }] };
9793
}
9894
const client = new ApifyClient({ token: apifyToken });
99-
try {
100-
// Convert comma-separated strings to arrays
101-
const fields = parsed.fields?.split(',').map((f) => f.trim());
102-
const omit = parsed.omit?.split(',').map((f) => f.trim());
103-
const flatten = parsed.flatten?.split(',').map((f) => f.trim());
95+
// Convert comma-separated strings to arrays
96+
const fields = parsed.fields?.split(',').map((f) => f.trim());
97+
const omit = parsed.omit?.split(',').map((f) => f.trim());
98+
const flatten = parsed.flatten?.split(',').map((f) => f.trim());
10499

105-
const v = await client.dataset(parsed.datasetId).listItems({
106-
clean: parsed.clean,
107-
offset: parsed.offset,
108-
limit: parsed.limit,
109-
fields,
110-
omit,
111-
desc: parsed.desc,
112-
flatten,
113-
});
114-
if (!v) {
115-
return { content: [{ type: 'text', text: `Dataset '${parsed.datasetId}' not found.` }] };
116-
}
117-
return { content: [{ type: 'text', text: JSON.stringify(v) }] };
118-
} catch {
119-
return { content: [{ type: 'text', text: `Invalid input or dataset not found.` }] };
100+
const v = await client.dataset(parsed.datasetId).listItems({
101+
clean: parsed.clean,
102+
offset: parsed.offset,
103+
limit: parsed.limit,
104+
fields,
105+
omit,
106+
desc: parsed.desc,
107+
flatten,
108+
});
109+
if (!v) {
110+
return { content: [{ type: 'text', text: `Dataset '${parsed.datasetId}' not found.` }] };
120111
}
112+
return { content: [{ type: 'text', text: JSON.stringify(v) }] };
121113
},
122114
} as InternalTool,
123115
};

src/tools/helpers.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { Ajv } from 'ajv';
2-
import { ApifyApiError } from 'apify-client';
32
import { z } from 'zod';
43
import zodToJsonSchema from 'zod-to-json-schema';
54

6-
import log from '@apify/log';
7-
85
import { HelperTools } from '../const.js';
96
import type { InternalTool, ToolEntry } from '../types';
107
import { getActorsAsTools } from './actor.js';
@@ -93,21 +90,7 @@ export const addTool: ToolEntry = {
9390
}],
9491
};
9592
}
96-
let tools;
97-
try {
98-
tools = await getActorsAsTools([parsed.actorName], apifyToken);
99-
} catch (error) {
100-
if (error instanceof ApifyApiError) {
101-
log.error(`[addTool] Failed to add Actor ${parsed.actorName}: ${error.message}`);
102-
return {
103-
content: [{
104-
type: 'text',
105-
text: `Failed to add Actor ${parsed.actorName}. Error: ${error.message}`,
106-
}],
107-
};
108-
}
109-
throw error;
110-
}
93+
const tools = await getActorsAsTools([parsed.actorName], apifyToken);
11194
const toolsAdded = apifyMcpServer.upsertTools(tools, true);
11295
await mcpServer.notification({ method: 'notifications/tools/list_changed' });
11396

src/tools/run.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { Ajv } from 'ajv';
2-
import { ApifyApiError } from 'apify-client';
32
import { z } from 'zod';
43
import zodToJsonSchema from 'zod-to-json-schema';
54

6-
import log from '@apify/log';
7-
85
import { ApifyClient } from '../apify-client.js';
96
import { HelperTools } from '../const.js';
107
import type { InternalTool, ToolEntry } from '../types.js';
@@ -105,16 +102,8 @@ export const abortActorRun: ToolEntry = {
105102
return { content: [{ type: 'text', text: 'Run ID is required.' }] };
106103
}
107104
const client = new ApifyClient({ token: apifyToken });
108-
try {
109-
const v = await client.run(parsed.runId).abort({ gracefully: parsed.gracefully });
110-
return { content: [{ type: 'text', text: JSON.stringify(v) }] };
111-
} catch (error) {
112-
if (error instanceof ApifyApiError) {
113-
log.error(`[abortActorRun] Failed to abort run ${parsed.runId}: ${error.message}`);
114-
return { content: [{ type: 'text', text: `Failed to abort run: ${error.message}` }] };
115-
}
116-
throw error;
117-
}
105+
const v = await client.run(parsed.runId).abort({ gracefully: parsed.gracefully });
106+
return { content: [{ type: 'text', text: JSON.stringify(v) }] };
118107
},
119108
} as InternalTool,
120109
};

src/tools/run_collection.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Ajv } from 'ajv';
2-
import { ApifyApiError } from 'apify-client';
32
import { z } from 'zod';
43
import zodToJsonSchema from 'zod-to-json-schema';
54

@@ -42,15 +41,8 @@ export const getUserRunsList: ToolEntry = {
4241
const { args, apifyToken } = toolArgs;
4342
const parsed = getUserRunsListArgs.parse(args);
4443
const client = new ApifyClient({ token: apifyToken });
45-
try {
46-
const runs = await client.runs().list({ limit: parsed.limit, offset: parsed.offset, desc: parsed.desc, status: parsed.status });
47-
return { content: [{ type: 'text', text: JSON.stringify(runs) }] };
48-
} catch (error) {
49-
if (error instanceof ApifyApiError) {
50-
return { content: [{ type: 'text', text: `Failed to get runs list: ${error.message}` }] };
51-
}
52-
throw error;
53-
}
44+
const runs = await client.runs().list({ limit: parsed.limit, offset: parsed.offset, desc: parsed.desc, status: parsed.status });
45+
return { content: [{ type: 'text', text: JSON.stringify(runs) }] };
5446
},
5547
} as InternalTool,
5648
};

0 commit comments

Comments
 (0)