Skip to main content
Glama

Structured Output

structured

Produce machine-parseable JSON conforming to a given JSON Schema from text input. Use for data extraction, classification, or entity parsing tasks.

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

  • The core handler function `executeStructured` that executes the structured output tool logic: validates schema, reads files, invokes Claude CLI with --json-schema, and parses structured_output from Claude's response.
    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,
      };
    }
  • `StructuredInput` interface defining the input schema for the structured tool: prompt, schema (JSON Schema string), files, model, sessionId, noSessionPersistence, workingDirectory, timeout, maxBudgetUsd.
    export interface StructuredInput {
      prompt: string;
      schema: string;
      files?: string[];
      model?: string;
      sessionId?: string;
      noSessionPersistence?: boolean;
      workingDirectory?: string;
      timeout?: number;
      maxBudgetUsd?: number;
    }
  • `StructuredResult` interface defining the output shape: response, valid, errors, model, sessionId, totalCostUsd, usage, filesIncluded, filesSkipped, timedOut.
    export interface StructuredResult {
      response: string;
      valid: boolean;
      errors?: string;
      model?: string;
      sessionId?: string;
      totalCostUsd?: number;
      usage?: ClaudeUsage;
      filesIncluded: string[];
      filesSkipped: string[];
      timedOut: boolean;
    }
  • src/index.ts:145-230 (registration)
    Registration of the 'structured' tool via server.registerTool() with Zod input schema, annotations, and the async handler that calls executeStructured and formats the result.
    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 }),
          };
        }
      },
    );
  • Model resolution configuration: 'structured' is listed as a ToolName type and defaults to 'sonnet' model.
    export type ToolName = "query" | "structured" | "search" | "ping";
    
    const DEFAULT_MODELS: Record<ToolName, string> = {
      query: "sonnet",
      structured: "sonnet",
      search: "sonnet",
      ping: "haiku",
    };
Behavior4/5

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

The annotations (readOnlyHint=false, destructiveHint=false, idempotentHint=false, openWorldHint=true) provide basic safety signals. The description adds valuable behavioral context: it uses native CLI validation (not client-side), cost is ~$0.01-0.10/call and unaffected by schema complexity, and it is designed for machine-parseable output. These details go beyond what annotations convey.

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: a lead sentence stating the core function, then a brief use case bullet, a cost note, and a tips section. Every sentence serves a purpose with no redundancy. The structure is front-loaded with the most important information.

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 the tool has 9 parameters, no output schema, and moderate complexity, the description covers the essential aspects: purpose, use cases, cost, and parameter tips. It could mention default behavior (e.g., timeout default of 60000) or error handling, but the parameter schema covers most details. Overall, it is sufficiently complete for an agent to use correctly.

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

Parameters4/5

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

All 9 parameters have descriptions in the schema (100% coverage), so baseline is 3. The description adds extra meaning: for the 'schema' parameter it notes 'Pass the JSON Schema as a JSON string' and 'Schema max size: 20KB'; for 'files' it explains 'include source text via the files parameter or inline in the prompt.' These tips improve parameter understanding beyond the schema alone.

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 clearly states 'Generate JSON conforming to a provided JSON Schema' using a specific implementation (Claude CLI's --json-schema flag). It lists concrete use cases like data extraction, classification, and entity parsing, which distinguish it from sibling tools such as 'query' (likely free-form) and 'search' (retrieval).

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

Usage Guidelines4/5

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

The description explicitly says 'Use for: data extraction from text/files, classification, entity parsing, or any task needing machine-parseable output.' It also provides practical tips (schema max size, including source text via files parameter) but does not explicitly state when not to use this tool or mention alternatives beyond implying it's better than client-side validation.

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