Skip to main content

Overview

The useToolData hook provides reactive access to both the tool’s input arguments and output data (structured content). It replaces the need for separate input/output hooks. The output field contains the data from your MCP tool’s structuredContent response.

Import

import { useToolData } from 'sunpeak';

Signature

function useToolData<TInput, TOutput>(
  defaultInput?: TInput,
  defaultOutput?: TOutput
): ToolData<TInput, TOutput>

Parameters

defaultInput
TInput
Fallback value when no input is available. Optional.
defaultOutput
TOutput
Fallback value when no output is available. Optional.

Returns

Returns a ToolData<TInput, TOutput> object with the following fields:
input
TInput | null
The tool call arguments.
inputPartial
TInput | null
Streaming partial input during tool execution.
output
TOutput | null
The structured content from the tool result.
isError
boolean
Whether the tool result indicates an error.
isLoading
boolean
Whether the tool is still executing.
isCancelled
boolean
Whether the tool call was cancelled by the host or user.
cancelReason
string | null
Optional reason for cancellation, or null if not cancelled.

Usage

import { useToolData } from 'sunpeak';

interface ReviewInput {
  changesetId: string;
  title: string;
}

interface ReviewOutput {
  title: string;
  sections: Array<{ title: string; content: unknown[] }>;
}

function ReviewResource() {
  const { input, output, isLoading } = useToolData<ReviewInput, ReviewOutput>(
    undefined,
    { title: 'Review', sections: [] }
  );

  if (isLoading) return <div>Loading...</div>;

  return (
    <div>
      <h1>{output?.title}</h1>
      <p>Changeset: {input?.changesetId}</p>
    </div>
  );
}