MCPcopy Index your code
hub / github.com/modelcontextprotocol/inspector / generateDefaultValue

Function generateDefaultValue

client/src/utils/schemaUtils.ts:89–143  ·  view source on GitHub ↗
(
  schema: JsonSchemaType,
  propertyName?: string,
  parentSchema?: JsonSchemaType,
)

Source from the content-addressed store, hash-verified

87 * @returns A default value matching the schema type
88 */
89export function generateDefaultValue(
90 schema: JsonSchemaType,
91 propertyName?: string,
92 parentSchema?: JsonSchemaType,
93): JsonValue {
94 if ("default" in schema && schema.default !== undefined) {
95 return schema.default;
96 }
97
98 // Check if this property is required in the parent schema
99 const isRequired =
100 propertyName && parentSchema
101 ? isPropertyRequired(propertyName, parentSchema)
102 : false;
103 const isRootSchema = propertyName === undefined && parentSchema === undefined;
104
105 switch (schema.type) {
106 case "string":
107 return isRequired ? "" : undefined;
108 case "number":
109 case "integer":
110 return isRequired ? 0 : undefined;
111 case "boolean":
112 return isRequired ? false : undefined;
113 case "array":
114 return isRequired ? [] : undefined;
115 case "object": {
116 if (!schema.properties) {
117 return isRequired || isRootSchema ? {} : undefined;
118 }
119
120 const obj: JsonObject = {};
121 // Include required properties OR optional properties that declare a default
122 Object.entries(schema.properties).forEach(([key, prop]) => {
123 const hasExplicitDefault =
124 "default" in prop && (prop as JsonSchemaType).default !== undefined;
125 if (isPropertyRequired(key, schema) || hasExplicitDefault) {
126 const value = generateDefaultValue(prop, key, schema);
127 if (value !== undefined) {
128 obj[key] = value;
129 }
130 }
131 });
132
133 if (Object.keys(obj).length === 0) {
134 return isRequired || isRootSchema ? {} : undefined;
135 }
136 return obj;
137 }
138 case "null":
139 return null;
140 default:
141 return undefined;
142 }
143}
144
145/**
146 * Helper function to check if a property is required in a schema

Callers 7

AppsTabFunction · 0.90
ToolsTabFunction · 0.90
ElicitationRequestFunction · 0.90
handleSwitchToFormModeFunction · 0.90
renderFormFieldsFunction · 0.90

Calls 1

isPropertyRequiredFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…