Skip to main content
Glama

json_sample

Extract JSON data samples from specified arrays within files. Define path, array location, sample count, and method (first or random) to retrieve precise data segments efficiently.

Instructions

Sample JSON data from a JSON file. Requires maxBytes parameter (default 10KB). Returns a random sample of data from the JSON file. The path must be within allowed directories.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
arrayPathYesJSONPath expression to locate the target array (e.g., "$.items" or "$.data.records")
countYesNumber of elements to sample
maxBytesYesMaximum bytes to read from the file. Must be a positive integer. Handler default: 10KB.
methodNoSampling method - "first" for first N elements, "random" for random samplingfirst
pathYesPath to the JSON file containing the array

Implementation Reference

  • Core handler function for json_sample tool. Parses arguments, reads and validates JSON file path, extracts target array using JSONPath, samples specified number of elements (first N or random), and returns formatted JSON response.
    export async function handleJsonSample( args: unknown, allowedDirectories: string[], symlinksMap: Map<string, string>, noFollowSymlinks: boolean ) { const parsed = parseArgs(JsonSampleArgsSchema, args, 'json_sample'); const validPath = await validatePath(parsed.path, allowedDirectories, symlinksMap, noFollowSymlinks); const jsonData = await readJsonFile(validPath, parsed.maxBytes); try { // Use JSONPath to locate the target array const targetArray = JSONPath({ path: parsed.arrayPath, json: jsonData, wrap: false }); if (!Array.isArray(targetArray)) { throw new Error(`Path "${parsed.arrayPath}" did not resolve to an array`); } if (targetArray.length === 0) { return { content: [{ type: "text", text: JSON.stringify([], null, 2) }], }; } let sampledData: any[]; if (parsed.method === 'random') { sampledData = sampleSize(targetArray, Math.min(parsed.count, targetArray.length)); } else { sampledData = take(targetArray, parsed.count); } return { content: [{ type: "text", text: JSON.stringify(sampledData, null, 2) }], }; } catch (error) { if (error instanceof Error) { throw new Error(`JSON sampling failed: ${error.message}`); } throw error; } }
  • TypeBox schema definition for json_sample input arguments: requires path to JSON file, JSONPath to array, count of samples; optional sampling method (first/random) and maxBytes limit.
    export const JsonSampleArgsSchema = Type.Object({ path: Type.String({ description: 'Path to the JSON file containing the array' }), arrayPath: Type.String({ description: 'JSONPath expression to locate the target array (e.g., "$.items" or "$.data.records")' }), count: Type.Integer({ minimum: 1, description: 'Number of elements to sample' }), method: Type.Optional( Type.Union([Type.Literal('first'), Type.Literal('random')], { default: 'first', description: 'Sampling method - "first" for first N elements, "random" for random sampling' }) ), maxBytes: Type.Integer({ minimum: 1, description: 'Maximum bytes to read from the file. Must be a positive integer. Handler default: 10KB.' }) }); export type JsonSampleArgs = Static<typeof JsonSampleArgsSchema>;
  • index.ts:289-290 (registration)
    Registers the json_sample tool handler in the central toolHandlers object, passing server context (allowedDirectories, symlinksMap, noFollowSymlinks).
    json_sample: (a: unknown) => handleJsonSample(a, allowedDirectories, symlinksMap, noFollowSymlinks),
  • index.ts:331-331 (registration)
    Declares json_sample in the allTools array with name and description, used for permission filtering and tool listing.
    { name: "json_sample", description: "Sample JSON data" },
  • Re-exports JsonSampleArgsSchema as toolSchemas.json_sample for use in main index.ts registration loop.
    json_sample: JsonSampleArgsSchema,

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/rawr-ai/mcp-filesystem'

If you have feedback or need assistance with the MCP directory API, please join our Discord server