Skip to main content
Glama

Structured Output

structured

Produce JSON strictly conforming to a JSON Schema from text or files. Ideal for extraction, classification, and parsing tasks requiring validated machine-readable results.

Instructions

Generate JSON conforming to a provided JSON Schema. Uses Claude CLI's native --json-schema flag for validated output (not client-side validation).

Use for: data extraction from text/files, classification, entity parsing, or any task needing machine-parseable output.

Cost: Similar to query (~$0.01-0.10/call). Schema complexity doesn't significantly affect cost.

Tips:

  • Pass the JSON Schema as a JSON string in the schema parameter.

  • Schema max size: 20KB. Keep schemas focused for reliable output.

  • For extraction tasks, include source text via the files parameter or inline in the prompt.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
promptYesWhat to generate or extract
schemaYesJSON Schema as a JSON string
filesNoText file paths to include as context
modelNoModel alias or full Claude model name
sessionIdNoClaude session ID to resume with --resume
noSessionPersistenceNoDisable session persistence for ephemeral print calls
workingDirectoryNoWorking directory for file resolution and CLI execution
timeoutNoTimeout in milliseconds (default: 60000)
maxBudgetUsdNoMaximum cost budget in USD for this call (passed to --max-budget-usd)

Implementation Reference

  • Core handler function that executes the structured tool: validates schema, resolves model, reads files, spawns Claude CLI with --json-schema flag, parses structured_output, and returns validated JSON.
    export async function executeStructured(input: StructuredInput): Promise<StructuredResult> {
      const { prompt, files = [], timeout, sessionId, noSessionPersistence, maxBudgetUsd } = input;
      const model = resolveModel("structured", input.model);
    
      const schemaBytes = Buffer.byteLength(input.schema);
      if (schemaBytes > MAX_SCHEMA_SIZE) {
        throw new Error(`Schema too large: ${schemaBytes} bytes (max ${MAX_SCHEMA_SIZE})`);
      }
    
      let parsedSchema: object;
      try {
        parsedSchema = JSON.parse(input.schema) as object;
      } catch {
        throw new Error("Invalid schema: not valid JSON");
      }
    
      const imageFiles = files.filter((f) => isImageFile(f));
      if (imageFiles.length > 0) {
        throw new Error("Structured tool does not support image files (text only)");
      }
    
      if (files.length > MAX_FILES) {
        throw new Error(`Too many files: ${files.length} (max ${MAX_FILES})`);
      }
    
      const cwd = await resolveCwd(input.workingDirectory);
    
      const fileContents = files.length > 0 ? await readFiles(files, cwd) : [];
      const fullPrompt = assemblePrompt(prompt, fileContents);
      const useStdin = fullPrompt.length > STDIN_THRESHOLD || files.length > 0;
      const effectiveTimeout = clampTimeout(timeout, 60_000);
    
      const args = buildClaudeArgs({
        model,
        fallbackModel: getFallbackModel(),
        maxBudgetUsd: resolveMaxBudget(maxBudgetUsd),
        sessionId,
        noSessionPersistence,
        jsonSchema: JSON.stringify(parsedSchema),
        prompt: useStdin ? undefined : fullPrompt,
      });
    
      const result = await spawnClaude({ args, cwd, stdin: useStdin ? fullPrompt : undefined, timeout: effectiveTimeout });
    
      const filesIncluded = fileContents.filter((f) => !f.skipped).map((f) => f.path);
      const filesSkipped = fileContents.filter((f) => f.skipped).map((f) => `${f.path}: ${f.skipped}`);
    
      if (result.timedOut) {
        return {
          response: `Structured query timed out after ${effectiveTimeout / 1000}s.`,
          valid: false,
          model,
          filesIncluded,
          filesSkipped,
          timedOut: true,
        };
      }
    
      const parsed = parseClaudeOutput(result.stdout, result.stderr);
      checkAndThrow(result, parsed);
    
      // Claude CLI places --json-schema output in the structured_output field.
      // Use key-existence check (not truthy) to handle scalar values like false, 0, "", null.
      const raw = parsed.raw as Record<string, unknown> | undefined;
      if (raw && "structured_output" in raw) {
        return {
          response: JSON.stringify(raw.structured_output),
          valid: true,
          model,
          sessionId: parsed.sessionId,
          totalCostUsd: parsed.totalCostUsd,
          usage: parsed.usage,
          filesIncluded,
          filesSkipped,
          timedOut: false,
        };
      }
    
      // Fall back to extracting JSON from the response text
      const extracted = extractJson(parsed.response);
      if (!extracted) {
        return {
          response: parsed.response,
          valid: false,
          errors: "Could not extract JSON from response",
          model,
          sessionId: parsed.sessionId,
          totalCostUsd: parsed.totalCostUsd,
          usage: parsed.usage,
          filesIncluded,
          filesSkipped,
          timedOut: false,
        };
      }
    
      return {
        response: extracted.raw,
        valid: true,
        model,
        sessionId: parsed.sessionId,
        totalCostUsd: parsed.totalCostUsd,
        usage: parsed.usage,
        filesIncluded,
        filesSkipped,
        timedOut: false,
      };
    }
  • Inline handler registered on the MCP server for the 'structured' tool. Calls executeStructured, handles session persistence, builds metadata, and formats errors/success responses.
      async (input) => {
        const start = Date.now();
        try {
          const result = await executeStructured(input);
    
          const sessionId = result.sessionId ?? input.sessionId;
          if (sessionId) {
            persist(sessionStore, sessionId, result);
          }
    
          const meta = buildMeta({
            durationMs: Date.now() - start,
            model: result.model,
            sessionId: result.sessionId,
            totalCostUsd: result.totalCostUsd,
            usage: result.usage,
            timedOut: result.timedOut,
          });
    
          if (!result.valid) {
            return {
              content: [{ type: "text" as const, text: `Error: ${result.errors ?? "Invalid response"}` }],
              isError: true,
              _meta: meta,
            };
          }
    
          const content: Array<{ type: "text"; text: string }> = [
            { type: "text", text: result.response },
          ];
    
          const textMeta: string[] = [];
          if (result.filesIncluded.length > 0) textMeta.push(`Files: ${result.filesIncluded.join(", ")}`);
          if (result.timedOut) textMeta.push("(timed out)");
          if (textMeta.length > 0) {
            content.push({ type: "text", text: textMeta.join("\n") });
          }
    
          return { content, _meta: meta };
        } catch (e) {
          console.error("[structured]", e);
          return {
            content: [{ type: "text" as const, text: `Error: ${getErrorMessage(e)}` }],
            isError: true,
            _meta: buildMeta({ durationMs: Date.now() - start }),
          };
        }
      },
    );
  • StructuredInput interface (prompt, schema, files, model, sessionId, etc.) and StructuredResult interface (response, valid, errors, model, sessionId, usage, etc.) defining the tool's input/output types.
    export interface StructuredInput {
      prompt: string;
      schema: string;
      files?: string[];
      model?: string;
      sessionId?: string;
      noSessionPersistence?: boolean;
      workingDirectory?: string;
      timeout?: number;
      maxBudgetUsd?: number;
    }
    
    export interface StructuredResult {
      response: string;
      valid: boolean;
      errors?: string;
      model?: string;
      sessionId?: string;
      totalCostUsd?: number;
      usage?: ClaudeUsage;
      filesIncluded: string[];
      filesSkipped: string[];
      timedOut: boolean;
    }
  • Registration of the 'structured' tool on the MCP server including title, description, and Zod-based inputSchema with fields: prompt, schema, files, model, sessionId, noSessionPersistence, workingDirectory, timeout, maxBudgetUsd.
    server.registerTool(
      "structured",
      {
        title: "Structured Output",
        description: structuredDescription,
        inputSchema: {
          prompt: z.string().describe("What to generate or extract"),
          schema: z.string().describe("JSON Schema as a JSON string"),
          files: z
            .array(z.string())
            .optional()
            .describe("Text file paths to include as context"),
          model: z.string().optional().describe("Model alias or full Claude model name"),
          sessionId: z
            .string()
            .optional()
            .describe("Claude session ID to resume with --resume"),
          noSessionPersistence: z
            .boolean()
            .optional()
            .describe("Disable session persistence for ephemeral print calls"),
          workingDirectory: z
            .string()
            .optional()
            .describe("Working directory for file resolution and CLI execution"),
          timeout: z
            .number()
            .optional()
            .describe("Timeout in milliseconds (default: 60000)"),
          maxBudgetUsd: z
            .number()
            .positive()
            .optional()
            .describe("Maximum cost budget in USD for this call (passed to --max-budget-usd)"),
        },
        annotations: structuredAnnotations,
  • src/index.ts:143-230 (registration)
    Registration block: calls server.registerTool('structured', ...) with schema, annotations, and the handler callback.
    // --- structured tool ---
    
    server.registerTool(
      "structured",
      {
        title: "Structured Output",
        description: structuredDescription,
        inputSchema: {
          prompt: z.string().describe("What to generate or extract"),
          schema: z.string().describe("JSON Schema as a JSON string"),
          files: z
            .array(z.string())
            .optional()
            .describe("Text file paths to include as context"),
          model: z.string().optional().describe("Model alias or full Claude model name"),
          sessionId: z
            .string()
            .optional()
            .describe("Claude session ID to resume with --resume"),
          noSessionPersistence: z
            .boolean()
            .optional()
            .describe("Disable session persistence for ephemeral print calls"),
          workingDirectory: z
            .string()
            .optional()
            .describe("Working directory for file resolution and CLI execution"),
          timeout: z
            .number()
            .optional()
            .describe("Timeout in milliseconds (default: 60000)"),
          maxBudgetUsd: z
            .number()
            .positive()
            .optional()
            .describe("Maximum cost budget in USD for this call (passed to --max-budget-usd)"),
        },
        annotations: structuredAnnotations,
      },
      async (input) => {
        const start = Date.now();
        try {
          const result = await executeStructured(input);
    
          const sessionId = result.sessionId ?? input.sessionId;
          if (sessionId) {
            persist(sessionStore, sessionId, result);
          }
    
          const meta = buildMeta({
            durationMs: Date.now() - start,
            model: result.model,
            sessionId: result.sessionId,
            totalCostUsd: result.totalCostUsd,
            usage: result.usage,
            timedOut: result.timedOut,
          });
    
          if (!result.valid) {
            return {
              content: [{ type: "text" as const, text: `Error: ${result.errors ?? "Invalid response"}` }],
              isError: true,
              _meta: meta,
            };
          }
    
          const content: Array<{ type: "text"; text: string }> = [
            { type: "text", text: result.response },
          ];
    
          const textMeta: string[] = [];
          if (result.filesIncluded.length > 0) textMeta.push(`Files: ${result.filesIncluded.join(", ")}`);
          if (result.timedOut) textMeta.push("(timed out)");
          if (textMeta.length > 0) {
            content.push({ type: "text", text: textMeta.join("\n") });
          }
    
          return { content, _meta: meta };
        } catch (e) {
          console.error("[structured]", e);
          return {
            content: [{ type: "text" as const, text: `Error: ${getErrorMessage(e)}` }],
            isError: true,
            _meta: buildMeta({ durationMs: Date.now() - start }),
          };
        }
      },
    );
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations are neutral, and the description adds context: it uses the native --json-schema flag (not client-side validation), provides cost estimates, and specifies schema size limits. There are no contradictions with annotations.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is concise, with a clear first sentence, bullet-point tips, and no redundant information. Every sentence adds value.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given 9 parameters with full schema coverage and no output schema, the description covers purpose, use cases, cost, tips, and implementation details. It lacks error behavior details but is otherwise thorough.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so baseline is 3. The description does not add extra meaning beyond the schema's parameter descriptions (e.g., no examples or additional constraints).

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description opens with 'Generate JSON conforming to a provided JSON Schema,' providing a clear verb and resource. It lists specific use cases (e.g., data extraction, classification, entity parsing) and distinguishes itself from siblings by mentioning the native --json-schema flag and cost comparability to query.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description states 'Use for: ... any task needing machine-parseable output' but does not explicitly compare to sibling tools like query or listSessions. It implies when to use but lacks guidance on when not to use or alternatives.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other 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/hampsterx/claude-mcp-bridge'

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