Skip to main content
Glama
mgraczyk

JSON Query MCP

by mgraczyk

json_query_jsonpath

Extract specific data from large JSON files by evaluating JSONPath expressions. Ideal for precise querying and retrieving values within complex JSON structures.

Instructions

Query a JSON file using JSONPath. Use to get values precisely from large JSON files.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYesAbsolute path to the JSON file.
jsonpathYesJSONPath expression to evaluate

Implementation Reference

  • src/server.ts:35-60 (registration)
    Registers the 'json_query_jsonpath' tool using McpServer.tool, including description, input schema with Zod, and inline async handler.
    server.tool(
      'json_query_jsonpath',
      'Query a JSON file using JSONPath. Use to get values precisely from large JSON files.',
      {
        file_path: z.string().describe(PATH_ARG_DESCRIPTION),
        jsonpath: z.string().min(1).describe('JSONPath expression to evaluate'),
      },
      async ({ file_path, jsonpath }) => {
        try {
          const resolvedPath = path.resolve(file_path);
    
          const results = await JsonUtils.queryByJsonPath(jsonpath, resolvedPath);
    
          return {
            content: [
              {
                type: 'text',
                text: JSON.stringify(results, null, 2),
              },
            ],
          };
        } catch (error) {
          return getErrorResponse(error);
        }
      },
    );
  • The tool handler function that resolves the file path, delegates to JsonUtils.queryByJsonPath, formats the result as MCP content, and handles errors.
    async ({ file_path, jsonpath }) => {
      try {
        const resolvedPath = path.resolve(file_path);
    
        const results = await JsonUtils.queryByJsonPath(jsonpath, resolvedPath);
    
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(results, null, 2),
            },
          ],
        };
      } catch (error) {
        return getErrorResponse(error);
      }
    },
  • Core helper method that reads the JSON file, applies JSONPath query using jsonpath-plus library, and returns array of path-value results.
    static async queryByJsonPath(path: string, jsonFile: string): Promise<JsonPathResult[]> {
      const data = await this.readJsonFile(jsonFile);
    
      // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
      const results = JSONPath({
        path,
        json: data as object,
        resultType: 'all',
      }) as { path: string; value: unknown }[];
    
      return results.map((result) => ({
        path: result.path,
        value: result.value,
      }));
    }
  • Zod schema defining input parameters: file_path (string) and jsonpath (non-empty string).
      file_path: z.string().describe(PATH_ARG_DESCRIPTION),
      jsonpath: z.string().min(1).describe('JSONPath expression to evaluate'),
    },
  • TypeScript interface defining the output structure for JSONPath query results.
    export interface JsonPathResult {
      path: string;
      value: unknown;
    }
Install Server

Other Tools

Related Tools

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/mgraczyk/json-query-mcp'

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