Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

fix(mastra): keep optional parameters under strict mode

jkomyno3c3b4dae944b
diff --git a/.changeset/mastra-strict-keep-optional-parameters.md b/.changeset/mastra-strict-keep-optional-parameters.md
new file mode 100644
index 000000000..c18c3e025
--- /dev/null
+++ b/.changeset/mastra-strict-keep-optional-parameters.md
@@ -0,0 +1,5 @@
+---
+'@composio/mastra': patch
+---
+
+`MastraProvider({ strict: true })` now keeps optional parameters instead of dropping them: every property becomes required and optional ones accept `null`, matching the OpenAI providers, and a `null` argument the tool's own schema does not accept is dropped before execution. Tools whose schema strict mode cannot express keep their original schema with a warning.
diff --git a/docs/content/docs/providers/mastra.mdx b/docs/content/docs/providers/mastra.mdx
index 97e5a9077..9166f5ce0 100644
--- a/docs/content/docs/providers/mastra.mdx
+++ b/docs/content/docs/providers/mastra.mdx
@@ -62,7 +62,7 @@ console.log(text);
## Provider specifics
-**Strict mode.** Pass `strict: true` to drop every non-required property from each tool's input schema before Mastra compiles it:
+**Strict mode.** Pass `strict: true` to normalize each tool's input schema for OpenAI structured outputs before Mastra compiles it: every object lists all of its properties in `required` and is closed, and optional properties stay available but accept `null`. A `null` the model sends for an optional parameter is dropped before the tool runs. Tools whose schema cannot be expressed in strict mode (objects that accept arbitrary keys, `allOf`, `prefixItems`) keep their original schema and log a warning:
```typescript
// @noErrors
diff --git a/ts/packages/providers/mastra/README.md b/ts/packages/providers/mastra/README.md
index 6b28fe6af..82aa46f07 100644
--- a/ts/packages/providers/mastra/README.md
+++ b/ts/packages/providers/mastra/README.md
@@ -51,7 +51,7 @@ Each tool gets both an input and an output schema, so Mastra can validate tool r
## Strict mode
-Pass `strict: true` to drop every non-required property from each tool's input schema before Mastra compiles it:
+Pass `strict: true` to normalize each tool's input schema for OpenAI structured outputs before Mastra compiles it: every object lists all of its properties in `required` and is closed, and optional properties stay available but accept `null`. A `null` the model sends for an optional parameter is dropped before the tool runs. Tools whose schema cannot be expressed in strict mode (objects that accept arbitrary keys, `allOf`, `prefixItems`) keep their original schema and log a warning:
```typescript
const composio = new Composio({
diff --git a/ts/packages/providers/mastra/src/index.ts b/ts/packages/providers/mastra/src/index.ts
index e975e3ea0..0c468623a 100644
--- a/ts/packages/providers/mastra/src/index.ts
+++ b/ts/packages/providers/mastra/src/index.ts
@@ -13,7 +13,8 @@ import {
McpUrlResponse,
dereferenceJsonSchema,
logger,
- removeNonRequiredProperties,
+ omitNullToolArguments,
+ toStrictJsonSchema,
telemetry,
type UnresolvedRefReason,
normalizeToolArguments,
@@ -89,16 +90,27 @@ export class MastraProvider extends BaseAgenticProvider<
wrapTool(tool: Tool, executeTool: ExecuteToolFn): MastraTool {
const inputParams = tool.inputParameters;
- const parameters =
- this.strict && inputParams?.type === 'object'
- ? removeNonRequiredProperties(
- inputParams as {
- type: 'object';
- properties: Record<string, unknown>;
- required?: string[];
- }
- )
- : inputParams;
+ // Strict mode applies OpenAI's structured-output contract at every
+ // depth: all properties required, closed objects, optional properties
+ // widened to accept null instead of being dropped. Tools whose schema
+ // strict mode cannot express keep their original schema.
+ let parameters: Record<string, unknown> | undefined = inputParams as
+ Record<string, unknown> | undefined;
+ let strictSource: Record<string, unknown> | undefined;
+ if (this.strict && inputParams) {
+ const strict = toStrictJsonSchema(inputParams);
+ if (strict.unsupported.length > 0) {
+ const reasons = strict.unsupported
+ .map(entry => `${entry.path || '<root>'}: ${entry.keyword} (${entry.detail})`)
+ .join('; ');
+ logger.warn(
+ `[composio/mastra] Tool ${JSON.stringify(tool.slug)} keeps its non-strict schema because it cannot be expressed as strict structured outputs: ${reasons}`
+ );
+ } else {
+ parameters = strict.schema;
+ strictSource = strict.source;
+ }
+ }
// Inline internal $ref pointers before handing the schema to
// @mastra/schema-compat. AJV (bundled inside schema-compat) refuses to
@@ -146,13 +158,17 @@ export class MastraProvider extends BaseAgenticProvider<
const mastraTool = createTool({
id: tool.slug,
description: tool.description ?? '',
- // @ts-ignore
inputSchema,
- // @ts-ignore
outputSchema,
execute: async (inputData, _context) => {
// Models occasionally emit tool input as a JSON string rather than an object (issue #2406).
- const result = await executeTool(tool.slug, normalizeToolArguments(inputData, tool.slug));
+ const normalized = normalizeToolArguments(inputData, tool.slug);
+ // Under strict mode optional parameters are emitted as required-nullable,
+ // so a null the tool's own schema does not accept means "omitted".
+ const result = await executeTool(
+ tool.slug,
+ strictSource ? omitNullToolArguments(normalized, strictSource) : normalized
+ );
return result;
},
});
diff --git a/ts/packages/providers/mastra/test/mastra.test.ts b/ts/packages/providers/mastra/test/mastra.test.ts
index bfbc88a10..f9545c881 100644
--- a/ts/packages/providers/mastra/test/mastra.test.ts
+++ b/ts/packages/providers/mastra/test/mastra.test.ts
@@ -544,7 +544,7 @@ describe('MastraProvider', () => {
expect(strictProvider['strict']).toBe(true);
});
- it('should use removeNonRequiredProperties when strict mode is enabled', () => {
+ it('should keep optional properties as required-nullable when strict mode is enabled', () => {
const strictProvider = new MastraProvider({ strict: true });
const toolWithOptionalProps: Tool = {
@@ -567,8 +567,8 @@ describe('MastraProvider', () => {
strictProvider.wrapTool(toolWithOptionalProps, mockExecuteToolFn);
- // In strict mode, only required properties should be passed to jsonSchemaToZodSchema
- // The removeNonRequiredProperties function should have been called and filtered the schema
+ // In strict mode every property is required and closed; optional ones
+ // stay available and accept null instead of being dropped.
expect(createTool).toHaveBeenCalledWith({
id: toolWithOptionalProps.slug,
description: toolWithOptionalProps.description,
@@ -581,8 +581,12 @@ describe('MastraProvider', () => {
type: 'string',
description: 'Required field',
},
+ optional_field: {
+ type: ['string', 'null'],
+ description: 'Optional field',
+ },
},
- required: ['required_field'],
+ required: ['required_field', 'optional_field'],
additionalProperties: false,
},
},
@@ -591,6 +595,58 @@ describe('MastraProvider', () => {
});
});
+ it('keeps the original schema for tools strict mode cannot express', () => {
+ const strictProvider = new MastraProvider({ strict: true });
+ const toolWithMap: Tool = {
+ ...mockTool,
+ inputParameters: {
+ type: 'object',
+ properties: {
+ headers: { type: 'object', additionalProperties: { type: 'string' } },
+ name: { type: 'string' },
+ },
+ required: ['headers'],
+ },
+ };
+
+ strictProvider.wrapTool(toolWithMap, mockExecuteToolFn);
+
+ expect(createTool).toHaveBeenCalledWith(
+ expect.objectContaining({
+ inputSchema: { type: 'mock-zod-schema', originalSchema: toolWithMap.inputParameters },
+ })
+ );
+ });
+
+ it('omits null arguments the tool schema rejects before executing under strict mode', async () => {
+ const strictProvider = new MastraProvider({ strict: true });
+ const wrapped = strictProvider.wrapTool(
+ {
+ ...mockTool,
+ inputParameters: {
+ type: 'object',
+ properties: {
+ cfg: {
+ type: 'object',
+ properties: { url: { type: 'string' }, note: { type: 'string' } },
+ required: ['url'],
+ },
+ clearable: { type: ['string', 'null'] },
+ },
+ required: ['cfg'],
+ },
+ },
+ mockExecuteToolFn
+ ) as unknown as { execute: (input: unknown, context: unknown) => Promise<unknown> };
+
+ await wrapped.execute({ cfg: { url: 'u', note: null }, clearable: null }, {});
+
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, {
+ cfg: { url: 'u' },
+ clearable: null,
+ });
+ });
+
it('should use all properties when strict mode is disabled', () => {
const nonStrictProvider = new MastraProvider({ strict: false });