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

Function getValueAtPath

client/src/utils/jsonUtils.ts:223–252  ·  view source on GitHub ↗
(
  obj: JsonValue,
  path: string[],
  defaultValue: JsonValue = null,
)

Source from the content-addressed store, hash-verified

221 * @returns The value at the path, or defaultValue if not found
222 */
223export function getValueAtPath(
224 obj: JsonValue,
225 path: string[],
226 defaultValue: JsonValue = null,
227): JsonValue {
228 if (path.length === 0) return obj;
229
230 const [first, ...rest] = path;
231
232 if (obj === null || obj === undefined) {
233 return defaultValue;
234 }
235
236 if (Array.isArray(obj)) {
237 const index = Number(first);
238 if (isNaN(index) || index < 0 || index >= obj.length) {
239 return defaultValue;
240 }
241 return getValueAtPath(obj[index], rest, defaultValue);
242 }
243
244 if (typeof obj === "object" && obj !== null) {
245 if (!(first in obj)) {
246 return defaultValue;
247 }
248 return getValueAtPath((obj as JsonObject)[first], rest, defaultValue);
249 }
250
251 return defaultValue;
252}

Callers 1

jsonUtils.test.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…