Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/mcp/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
import Ajv from 'ajv';

import { fixedAjvCompile } from '../tools/utils.js';
import type { ActorMcpTool, ToolEntry } from '../types.js';
import { getMCPServerID, getProxyMCPServerToolName } from './utils.js';

Expand All @@ -26,7 +27,7 @@ export async function getMCPServerTools(
name: getProxyMCPServerToolName(serverUrl, tool.name),
description: tool.description || '',
inputSchema: tool.inputSchema,
ajvValidate: ajv.compile(tool.inputSchema),
ajvValidate: fixedAjvCompile(ajv, tool.inputSchema),
};

const wrap: ToolEntry = {
Expand Down
23 changes: 2 additions & 21 deletions src/tools/actor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
import type { ValidateFunction } from 'ajv';
import { Ajv } from 'ajv';
import type { ActorCallOptions, ActorRun, Dataset, PaginatedList } from 'apify-client';
import { z } from 'zod';
Expand Down Expand Up @@ -27,32 +26,14 @@ import {
addEnumsToDescriptionsWithExamples,
buildNestedProperties,
filterSchemaProperties,
fixedAjvCompile,
getToolSchemaID,
markInputPropertiesAsRequired,
shortenProperties,
} from './utils.js';

const ajv = new Ajv({ coerceTypes: 'array', strict: false });

// source https://github.com/ajv-validator/ajv/issues/1413#issuecomment-867064234
function fixedCompile(schema: object): ValidateFunction<unknown> {
const validate = ajv.compile(schema);
ajv.removeSchema(schema);

// Force reset values that aren't reset with removeSchema
/* eslint-disable no-underscore-dangle */
/* eslint-disable @typescript-eslint/no-explicit-any */
(ajv.scope as any)._values.schema!.delete(schema);
(ajv.scope as any)._values.validate!.delete(validate);
const schemaIdx = (ajv.scope as any)._scope.schema.indexOf(schema);
const validateIdx = (ajv.scope as any)._scope.validate.indexOf(validate);
if (schemaIdx !== -1) (ajv.scope as any)._scope.schema.splice(schemaIdx, 1);
if (validateIdx !== -1) (ajv.scope as any)._scope.validate.splice(validateIdx, 1);
/* eslint-enable @typescript-eslint/no-explicit-any */
/* eslint-enable no-underscore-dangle */
return validate;
}

// Define a named return type for callActorGetDataset
export type CallActorGetDatasetResult = {
actorRun: ActorRun;
Expand Down Expand Up @@ -182,7 +163,7 @@ export async function getNormalActorsAsTools(
actorFullName: result.actorFullName,
description: `${result.description} Instructions: ${ACTOR_ADDITIONAL_INSTRUCTIONS}`,
inputSchema: result.input || {},
ajvValidate: fixedCompile(result.input || {}),
ajvValidate: fixedAjvCompile(ajv, result.input || {}),
memoryMbytes: memoryMbytes > ACTOR_MAX_MEMORY_MBYTES ? ACTOR_MAX_MEMORY_MBYTES : memoryMbytes,
},
};
Expand Down
22 changes: 22 additions & 0 deletions src/tools/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type { ValidateFunction } from 'ajv';
import type Ajv from 'ajv';

import { ACTOR_ENUM_MAX_LENGTH, ACTOR_MAX_DESCRIPTION_LENGTH } from '../const.js';
import type { IActorInputSchema, ISchemaProperties } from '../types.js';

Expand All @@ -12,6 +15,25 @@ export function getToolSchemaID(actorName: string): string {
return `https://apify.com/mcp/${actorNameToToolName(actorName)}/schema.json`;
}

// source https://github.com/ajv-validator/ajv/issues/1413#issuecomment-867064234
export function fixedAjvCompile(ajvInstance: Ajv, schema: object): ValidateFunction<unknown> {
const validate = ajvInstance.compile(schema);
ajvInstance.removeSchema(schema);

// Force reset values that aren't reset with removeSchema
/* eslint-disable no-underscore-dangle */
/* eslint-disable @typescript-eslint/no-explicit-any */
(ajvInstance.scope as any)._values.schema!.delete(schema);
(ajvInstance.scope as any)._values.validate!.delete(validate);
const schemaIdx = (ajvInstance.scope as any)._scope.schema.indexOf(schema);
const validateIdx = (ajvInstance.scope as any)._scope.validate.indexOf(validate);
if (schemaIdx !== -1) (ajvInstance.scope as any)._scope.schema.splice(schemaIdx, 1);
if (validateIdx !== -1) (ajvInstance.scope as any)._scope.validate.splice(validateIdx, 1);
/* eslint-enable @typescript-eslint/no-explicit-any */
/* eslint-enable no-underscore-dangle */
return validate;
}

/**
* Builds nested properties for object types in the schema.
*
Expand Down