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
MastraProvider strict mode used the root-only, input-mutating removeNonRequiredProperties, so "strict" meant something different from the OpenAI providers. It now runs the same toStrictJsonSchema rewrite: optional parameters become required-nullable, tools strict mode cannot express keep their original schema with a warning, and null arguments the tool schema rejects are dropped before execution. Co-authored-by: AseemPrasad <aseemprasad0520@gmail.com> Claude-Session: https://claude.ai/code/session_01TDrxCHn2hg51HmxVstSUgs
jkomyno committed
Aug 26, 2026 at 17:30 UTC
3c3b4dae944b29a3d0862f6f34f9b4de3e5ebf00
5 files changed
+97
-20
.changeset/mastra-strict-keep-optional-parameters.md
new
+5
@@ -0,0 +1,5 @@
1
+---
2
+'@composio/mastra': patch
3
+---
4
+
5
+`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.
docs/content/docs/providers/mastra.mdx
+1
-1
@@ -62,7 +62,7 @@ console.log(text);
62
63
## Provider specifics
64
65
-**Strict mode.** Pass `strict: true` to drop every non-required property from each tool's input schema before Mastra compiles it:
65
+**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:
66
67
```typescript
68
// @noErrors
ts/packages/providers/mastra/README.md
+1
-1
@@ -51,7 +51,7 @@ Each tool gets both an input and an output schema, so Mastra can validate tool r
51
52
## Strict mode
53
54
-Pass `strict: true` to drop every non-required property from each tool's input schema before Mastra compiles it:
54
+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:
55
56
```typescript
57
const composio = new Composio({
ts/packages/providers/mastra/src/index.ts
+30
-14
@@ -13,7 +13,8 @@ import {
13
McpUrlResponse,
14
dereferenceJsonSchema,
15
logger,
16
- removeNonRequiredProperties,
16
+ omitNullToolArguments,
17
+ toStrictJsonSchema,
18
telemetry,
19
type UnresolvedRefReason,
20
normalizeToolArguments,
@@ -89,16 +90,27 @@ export class MastraProvider extends BaseAgenticProvider<
90
wrapTool(tool: Tool, executeTool: ExecuteToolFn): MastraTool {
91
const inputParams = tool.inputParameters;
92
92
- const parameters =
93
- this.strict && inputParams?.type === 'object'
94
- ? removeNonRequiredProperties(
95
- inputParams as {
96
- type: 'object';
97
- properties: Record<string, unknown>;
98
- required?: string[];
99
- }
100
- )
101
- : inputParams;
93
+ // Strict mode applies OpenAI's structured-output contract at every
94
+ // depth: all properties required, closed objects, optional properties
95
+ // widened to accept null instead of being dropped. Tools whose schema
96
+ // strict mode cannot express keep their original schema.
97
+ let parameters: Record<string, unknown> | undefined = inputParams as
98
+ Record<string, unknown> | undefined;
99
+ let strictSource: Record<string, unknown> | undefined;
100
+ if (this.strict && inputParams) {
101
+ const strict = toStrictJsonSchema(inputParams);
102
+ if (strict.unsupported.length > 0) {
103
+ const reasons = strict.unsupported
104
+ .map(entry => `${entry.path || '<root>'}: ${entry.keyword} (${entry.detail})`)
105
+ .join('; ');
106
+ logger.warn(
107
+ `[composio/mastra] Tool ${JSON.stringify(tool.slug)} keeps its non-strict schema because it cannot be expressed as strict structured outputs: ${reasons}`
108
+ );
109
+ } else {
110
+ parameters = strict.schema;
111
+ strictSource = strict.source;
112
+ }
113
+ }
114
115
// Inline internal $ref pointers before handing the schema to
116
// @mastra/schema-compat. AJV (bundled inside schema-compat) refuses to
@@ -146,13 +158,17 @@ export class MastraProvider extends BaseAgenticProvider<
158
const mastraTool = createTool({
159
id: tool.slug,
160
description: tool.description ?? '',
149
- // @ts-ignore
161
inputSchema,
151
- // @ts-ignore
162
outputSchema,
163
execute: async (inputData, _context) => {
164
// Models occasionally emit tool input as a JSON string rather than an object (issue #2406).
155
- const result = await executeTool(tool.slug, normalizeToolArguments(inputData, tool.slug));
165
+ const normalized = normalizeToolArguments(inputData, tool.slug);
166
+ // Under strict mode optional parameters are emitted as required-nullable,
167
+ // so a null the tool's own schema does not accept means "omitted".
168
+ const result = await executeTool(
169
+ tool.slug,
170
+ strictSource ? omitNullToolArguments(normalized, strictSource) : normalized
171
+ );
172
return result;
173
},
174
});
ts/packages/providers/mastra/test/mastra.test.ts
+60
-4
@@ -544,7 +544,7 @@ describe('MastraProvider', () => {
544
expect(strictProvider['strict']).toBe(true);
545
});
546
547
- it('should use removeNonRequiredProperties when strict mode is enabled', () => {
547
+ it('should keep optional properties as required-nullable when strict mode is enabled', () => {
548
const strictProvider = new MastraProvider({ strict: true });
549
550
const toolWithOptionalProps: Tool = {
@@ -567,8 +567,8 @@ describe('MastraProvider', () => {
567
568
strictProvider.wrapTool(toolWithOptionalProps, mockExecuteToolFn);
569
570
- // In strict mode, only required properties should be passed to jsonSchemaToZodSchema
571
- // The removeNonRequiredProperties function should have been called and filtered the schema
570
+ // In strict mode every property is required and closed; optional ones
571
+ // stay available and accept null instead of being dropped.
572
expect(createTool).toHaveBeenCalledWith({
573
id: toolWithOptionalProps.slug,
574
description: toolWithOptionalProps.description,
@@ -581,8 +581,12 @@ describe('MastraProvider', () => {
581
type: 'string',
582
description: 'Required field',
583
},
584
+ optional_field: {
585
+ type: ['string', 'null'],
586
+ description: 'Optional field',
587
+ },
588
},
585
- required: ['required_field'],
589
+ required: ['required_field', 'optional_field'],
590
additionalProperties: false,
591
},
592
},
@@ -591,6 +595,58 @@ describe('MastraProvider', () => {
595
});
596
});
597
598
+ it('keeps the original schema for tools strict mode cannot express', () => {
599
+ const strictProvider = new MastraProvider({ strict: true });
600
+ const toolWithMap: Tool = {
601
+ ...mockTool,
602
+ inputParameters: {
603
+ type: 'object',
604
+ properties: {
605
+ headers: { type: 'object', additionalProperties: { type: 'string' } },
606
+ name: { type: 'string' },
607
+ },
608
+ required: ['headers'],
609
+ },
610
+ };
611
+
612
+ strictProvider.wrapTool(toolWithMap, mockExecuteToolFn);
613
+
614
+ expect(createTool).toHaveBeenCalledWith(
615
+ expect.objectContaining({
616
+ inputSchema: { type: 'mock-zod-schema', originalSchema: toolWithMap.inputParameters },
617
+ })
618
+ );
619
+ });
620
+
621
+ it('omits null arguments the tool schema rejects before executing under strict mode', async () => {
622
+ const strictProvider = new MastraProvider({ strict: true });
623
+ const wrapped = strictProvider.wrapTool(
624
+ {
625
+ ...mockTool,
626
+ inputParameters: {
627
+ type: 'object',
628
+ properties: {
629
+ cfg: {
630
+ type: 'object',
631
+ properties: { url: { type: 'string' }, note: { type: 'string' } },
632
+ required: ['url'],
633
+ },
634
+ clearable: { type: ['string', 'null'] },
635
+ },
636
+ required: ['cfg'],
637
+ },
638
+ },
639
+ mockExecuteToolFn
640
+ ) as unknown as { execute: (input: unknown, context: unknown) => Promise<unknown> };
641
+
642
+ await wrapped.execute({ cfg: { url: 'u', note: null }, clearable: null }, {});
643
+
644
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, {
645
+ cfg: { url: 'u' },
646
+ clearable: null,
647
+ });
648
+ });
649
+
650
it('should use all properties when strict mode is disabled', () => {
651
const nonStrictProvider = new MastraProvider({ strict: false });
652