mcp-use TypeScript API
    Preparing search index...

    Function useViewTool

    • Register an ephemeral tool the host/model can call while this component is mounted.

      Registration is synchronous against the runtime-owned App and independent of connection completion. It is keyed by name only ([runtime, name]): the tool registers once per mounted name and is removed on unmount. Changing inputSchema / outputSchema without changing name does not re-register — schemas are captured at registration time (ext-apps fixes handler arity at registration); to change a tool's schema, register it under a new name. schema is accepted as an alias for inputSchema.

      title, description, and annotations changes are applied in place via the ext-apps handle's update(), passing explicit undefined so omitted fields are cleared. Toggling enabled calls enable() / disable() without re-registering. The handler is kept in a ref so calls always see current React state. Registration goes through the document runtime rather than the guest App directly, allowing the runtime to perform the empty-handler handoff on first registration.

      Cleanup captures the registration handle inside the effect so an older cleanup cannot remove a newer registration (e.g. after a rapid name change). Registration failures are reported with console.error and do not leave half-registered state.

      Type Parameters

      Parameters

      • definition: TDef
      • handler: (
            args: TInput,
        ) =>
            | Exclude<
                [TOutput] extends [never]
                    ? { [key: string]: unknown }
                    :
                        | { [key: string]: unknown } & { isError: true }
                        | { [key: string]: unknown } & { structuredContent: TOutput },
                InputRequiredResult,
            >
            | Promise<
                Exclude<
                    [TOutput] extends [never]
                        ? { [key: string]: unknown }
                        :
                            | { [key: string]: unknown } & { isError: true }
                            | { [key: string]: unknown } & { structuredContent: TOutput },
                    InputRequiredResult,
                >,
            >

      Returns void

      useViewTool(
      { name: "highlight-fruit", inputSchema: z.object({ id: z.string() }) },
      async ({ id }) => {
      setSelected(id);
      return { content: [{ type: "text", text: `Highlighted ${id}` }] };
      }
      );