group-text-by-json
Structure text into JSON format using predefined templates with placeholders. Ideal for transforming unstructured input into organized, structured data for processing and analysis.
Instructions
Gives a prompt text for AI to group text based on JSON placeholders. This tool accepts a JSON template with placeholders.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| template | Yes | JSON template with placeholders |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"template": {
"description": "JSON template with placeholders",
"type": "string"
}
},
"required": [
"template"
],
"type": "object"
}
Implementation Reference
- src/common/tools.ts:3-53 (handler)The core implementation of the 'group-text-by-json' tool handler. Parses the JSON template to extract keys and generates a structured prompt for an AI to group subsequent text input based on those keys.const groupTextByJsonTool = (template: string) => { if (!template) { throw new Error("Both template and text are required"); } try { logger.info("Template:", template); let objectKeys: string[] = []; try { const templateObj = JSON.parse(template); objectKeys = deepObjectKeys(templateObj, true); } catch (parseError) { logger.error("Failed to parse template:", parseError); throw new Error(`Invalid template format: ${parseError}`); } const resultPrompt = ` You are a helpful assistant that groups text based on JSON keys. Here are the keys in the template: ${objectKeys.join(", ")}. Please group the text based on the keys. and give me the result in raw text. Don't give it in JSON format or object format. It should be in the following format: Format: <key>: <corresponding text found in the text> Here's an example: sentence: The MacBook Pro costs $2,499. result: brand: MacBook price: $2,499 description: The MacBook Pro is a powerful laptop with a Retina display. `; return { content: [ { type: "text", text: resultPrompt, }, ], }; } catch (error) { logger.error("Error processing template:", error); throw new Error(`Failed to process template: ${error}`); } };
- src/common/types.ts:3-5 (schema)Zod schema defining the input for the 'group-text-by-json' tool: a JSON template string.const GroupTextByJsonSchema = z.object({ template: z.string().describe("JSON template with placeholders"), });
- src/index.ts:39-44 (registration)Registration of the tool in the ListToolsRequestHandler, including name, description, and input schema.{ name: TOOL_NAMES.groupTextByJson, description: "Gives a prompt text for AI to group text based on JSON placeholders. This tool accepts a JSON template with placeholders.", inputSchema: zodToJsonSchema(GroupTextByJsonSchema), },
- src/index.ts:66-68 (registration)Dispatch/execution point in the CallToolRequestHandler switch statement that invokes the handler function.case TOOL_NAMES.groupTextByJson: const groupTextByJsonArgs = args as GroupTextByJsonSchemaType; return groupTextByJsonTool(groupTextByJsonArgs.template);
- src/common/constants.ts:7-7 (helper)Constant mapping for the tool name used in registration and dispatching.groupTextByJson: "group-text-by-json",