analyze-document
Extract structured data from documents using Azure Form Recognizer. Ideal for processing receipts, invoices, and IDs by analyzing the document URL with optional custom models.
Instructions
Analyzes a document using Azure Form Recognizer and returns structured data
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| modelId | No | Optional model ID for custom models | |
| url | Yes | URL of the document to analyze |
Implementation Reference
- src/tools/form-recognizer.ts:11-35 (handler)The core handler function that downloads the document from the URL, analyzes it using Azure Form Recognizer client, and returns the JSON string result.export const analyzeDocument = async (url: string, modelId?: string): Promise<string> => { const config = getConfig(); const client = new DocumentAnalysisClient( config.formRecognizerEndpoint, new AzureKeyCredential(config.formRecognizerKey) ); try { // Download the document from URL const response = await axios.get(url, { responseType: "arraybuffer" }); const buffer = Buffer.from(response.data); // Analyze the document const poller = await client.beginAnalyzeDocument( modelId || "prebuilt-document", buffer as FormRecognizerRequestBody ); const result = await poller.pollUntilDone(); return JSON.stringify(result, null, 2); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; throw new Error(`Document analysis failed: ${errorMessage}`); } };
- src/types/tools.ts:15-18 (schema)Zod schema defining the input parameters for the analyze-document tool: url (required string URL) and optional modelId.export const DocumentSchema = z.object({ url: z.string().url().describe("URL of the document to analyze"), modelId: z.string().optional().describe("Optional model ID for custom models"), });
- src/services/tools.ts:8-15 (registration)Registration of the 'analyze-document' tool in the tools array, specifying name, description, schema, and execute wrapper that calls the handler.{ name: "analyze-document", description: "Analyzes a document using Azure Form Recognizer and returns structured data", parameters: DocumentSchema, execute: async (args) => { return await analyzeDocument(args.url, args.modelId); }, },