11import type { Client } from '@modelcontextprotocol/sdk/client/index.js' ;
22import { Ajv } from 'ajv' ;
3- import type { ActorCallOptions , ActorRun , Dataset , PaginatedList } from 'apify-client' ;
3+ import type { ActorCallOptions , ActorRun , PaginatedList } from 'apify-client' ;
44import { z } from 'zod' ;
55import zodToJsonSchema from 'zod-to-json-schema' ;
66
@@ -10,14 +10,15 @@ import { ApifyClient } from '../apify-client.js';
1010import {
1111 ACTOR_ADDITIONAL_INSTRUCTIONS ,
1212 ACTOR_MAX_MEMORY_MBYTES ,
13- ACTOR_RUN_DATASET_OUTPUT_MAX_ITEMS ,
1413 HelperTools ,
1514} from '../const.js' ;
1615import { getActorMCPServerPath , getActorMCPServerURL } from '../mcp/actors.js' ;
1716import { connectMCPClient } from '../mcp/client.js' ;
1817import { getMCPServerTools } from '../mcp/proxy.js' ;
1918import { actorDefinitionPrunedCache } from '../state.js' ;
20- import type { ActorInfo , InternalTool , ToolEntry } from '../types.js' ;
19+ import type { ActorDefinitionStorage , ActorInfo , InternalTool , ToolEntry } from '../types.js' ;
20+ import { getActorDefinitionStorageFieldNames } from '../utils/actor.js' ;
21+ import { getValuesByDotKeys } from '../utils/generic.js' ;
2122import { getActorDefinition } from './build.js' ;
2223import {
2324 actorNameToToolName ,
@@ -34,8 +35,6 @@ const ajv = new Ajv({ coerceTypes: 'array', strict: false });
3435
3536// Define a named return type for callActorGetDataset
3637export type CallActorGetDatasetResult = {
37- actorRun : ActorRun ;
38- datasetInfo : Dataset | undefined ;
3938 items : PaginatedList < Record < string , unknown > > ;
4039} ;
4140
@@ -50,7 +49,6 @@ export type CallActorGetDatasetResult = {
5049 * @param {ActorCallOptions } callOptions - The options to pass to the actor.
5150 * @param {unknown } input - The input to pass to the actor.
5251 * @param {string } apifyToken - The Apify token to use for authentication.
53- * @param {number } limit - The maximum number of items to retrieve from the dataset.
5452 * @returns {Promise<{ actorRun: any, items: object[] }> } - A promise that resolves to an object containing the actor run and dataset items.
5553 * @throws {Error } - Throws an error if the `APIFY_TOKEN` is not set
5654 */
@@ -59,7 +57,6 @@ export async function callActorGetDataset(
5957 input : unknown ,
6058 apifyToken : string ,
6159 callOptions : ActorCallOptions | undefined = undefined ,
62- limit = ACTOR_RUN_DATASET_OUTPUT_MAX_ITEMS ,
6360) : Promise < CallActorGetDatasetResult > {
6461 try {
6562 log . info ( `Calling Actor ${ actorName } with input: ${ JSON . stringify ( input ) } ` ) ;
@@ -69,13 +66,24 @@ export async function callActorGetDataset(
6966
7067 const actorRun : ActorRun = await actorClient . call ( input , callOptions ) ;
7168 const dataset = client . dataset ( actorRun . defaultDatasetId ) ;
72- const [ datasetInfo , items ] = await Promise . all ( [
73- dataset . get ( ) ,
74- dataset . listItems ( { limit } ) ,
69+ // const dataset = client.dataset('Ehtn0Y4wIKviFT2WB');
70+ const [ items , defaultBuild ] = await Promise . all ( [
71+ dataset . listItems ( ) ,
72+ ( await actorClient . defaultBuild ( ) ) . get ( ) ,
7573 ] ) ;
76- log . info ( `Actor ${ actorName } finished with ${ datasetInfo ?. itemCount } items` ) ;
7774
78- return { actorRun, datasetInfo, items } ;
75+ // Get important properties from storage view definitions and if available return only those properties
76+ const storageDefinition = defaultBuild ?. actorDefinition ?. storages ?. dataset as ActorDefinitionStorage | undefined ;
77+ const importantProperties = getActorDefinitionStorageFieldNames ( storageDefinition || { } ) ;
78+ if ( importantProperties . length > 0 ) {
79+ items . items = items . items . map ( ( item ) => {
80+ return getValuesByDotKeys ( item , importantProperties ) ;
81+ } ) ;
82+ }
83+
84+ log . info ( `Actor ${ actorName } finished with ${ items . count } items` ) ;
85+
86+ return { items } ;
7987 } catch ( error ) {
8088 log . error ( `Error calling actor: ${ error } . Actor: ${ actorName } , input: ${ JSON . stringify ( input ) } ` ) ;
8189 throw new Error ( `Error calling Actor: ${ error } ` ) ;
@@ -132,7 +140,13 @@ export async function getNormalActorsAsTools(
132140 name : actorNameToToolName ( actorDefinitionPruned . actorFullName ) ,
133141 actorFullName : actorDefinitionPruned . actorFullName ,
134142 description : `${ actorDefinitionPruned . description } Instructions: ${ ACTOR_ADDITIONAL_INSTRUCTIONS } ` ,
135- inputSchema : actorDefinitionPruned . input || { } ,
143+ inputSchema : actorDefinitionPruned . input
144+ // So Actor without input schema works - MCP client expects JSON schema valid output
145+ || {
146+ type : 'object' ,
147+ properties : { } ,
148+ required : [ ] ,
149+ } ,
136150 ajvValidate : fixedAjvCompile ( ajv , actorDefinitionPruned . input || { } ) ,
137151 memoryMbytes : memoryMbytes > ACTOR_MAX_MEMORY_MBYTES ? ACTOR_MAX_MEMORY_MBYTES : memoryMbytes ,
138152 } ,
0 commit comments