← Back to Labs

Complete the PostToolUse Hook

agentic architectureFill in Blank

Complete a PostToolUse hook that normalizes heterogeneous data formats from MCP tools before the agent processes them. This is the recommended approach when tools return Unix timestamps, ISO dates, and numeric status codes.

const hooks = {
  // This hook intercepts tool results for transformation
  // BEFORE the model processes them.
  async ({ toolName, toolResult }) {
    if (toolName === "get_customer") {
      const data = JSON.parse(toolResult);

      // Normalize Unix timestamp to ISO 8601
      if (data.created_at && typeof data.created_at === "number") {
        data.created_at = ;
      }

      // Convert numeric status codes to readable strings
      const statusMap: Record<number, string> = {
        1: "pending",
        2: "shipped",
        3: "delivered",
      };
      if (data.status in statusMap) {
        data.status = statusMap[data.status as number];
      }

      return JSON.stringify(data);
    }
    return toolResult;
  },
};
Next Exercise →