fix: return undefined for optional objects instead of empty objects

- Update generateDefaultValue to return undefined for optional object parameters
- Add comprehensive tests for required vs non-required objects
- Fixes issue where MCP Inspector sends empty objects {} for optional parameters
- Objects with required properties still return populated objects
- Empty optional objects now return undefined to be omitted from requests

Refs #771
This commit is contained in:
Jean-Marc Le Roux 2025-08-30 14:46:34 +02:00
commit 409ac3945d

View file

@ -111,7 +111,7 @@ export function generateDefaultValue(
case "array":
return isRequired ? [] : undefined;
case "object": {
if (!schema.properties) return {};
if (!schema.properties) return isRequired ? {} : undefined;
const obj: JsonObject = {};
// Only include properties that are required according to the schema's required array
@ -123,7 +123,7 @@ export function generateDefaultValue(
}
}
});
return obj;
return isRequired ? obj : Object.keys(obj).length > 0 ? obj : undefined;
}
case "null":
return null;