Skip to content

ontoolinput is not consistently called #476

Open

Description

@bgonzalez-thestable

Describe the bug
Using the useApp hook, I notice app.ontoolinput is not consistently called although it is registered before app.connect

To Reproduce
Steps to reproduce the behavior:

  1. Instantiate useApp() with a minimal setup:
import { useApp } from "@modelcontextprotocol/ext-apps/react";

//...

useApp({
  appInfo: { name: "Describe your app here...", version: "1.0.0" },
  capabilities: {},
  onAppCreated: (app) => {
    app.ontoolinput = (input) => {
      console.info("Received tool call input:", input);
    };
  },
});
  1. Run the app in any host
  2. Call a tool with user input using a manual prompt
  3. Notice how app.ontoolinput is not always called

Expected behavior
app.ontoolinput is called every time a tool is invoked

Logs
If applicable, add logs to help explain your problem.

Additional context
Other callbacks seem to run as expected, including app.ontoolresult.

Activity

  1. netanelavr commented on Mar 8, 2026

    @netanelavr

    Hi @bgonzalez-thestable — I think I found the root cause.

    When hosts load View HTML via srcdoc, PostMessageTransport needs contentWindow at construction time, but it's not available until the iframe loads. The View sends ui/initialize before the host transport is listening — the message is dropped, the bridge never fully initializes, and ontoolinput is never sent.

    Opened an issue with the full breakdown (#542) and a PR with a fix (#543). Hope it helps!

  2. bgonzalez-thestable commented on Mar 11, 2026

    @bgonzalez-thestable
    Author

    Thank you @netanelavr! Do you think this is related to an issue in ChatGPT when revisiting a previous chat the app tool is called and a request/response is shown, but although onAppCreated fires none of its registered events fire?

  3. netanelavr commented on Mar 22, 2026

    @netanelavr

    Thank you @netanelavr! Do you think this is related to an issue in ChatGPT when revisiting a previous chat the app tool is called and a request/response is shown, but although onAppCreated fires none of its registered events fire?

    @bgonzalez-thestable Yeah, could be the same issue if ChatGPT recreates the iframe when you go back to an old chat. Though it might also just be ChatGPT showing cached results without actually calling the tool again so onAppCreated fires but there's no tool input because nothing's being invoked. Worth checking the console to see if ui/initialize gets a response or not.

  4. ochafik commented on Apr 21, 2026

    @ochafik
    Contributor

    v1.7.0 adds diagnostics for two of the three causes here: a console.warn (or throw under {strict:true}) when one-shot handlers are first registered after connect() (#629/#631), and useApp now closes the App on unmount so React StrictMode dev doesn't leave a zombie listener (#631). The third cause — host attaching its transport listener after the View has already sent ui/initialize (#542) — is being addressed via host-ordering docs rather than #543's queue approach. For React, register one-shot handlers via onAppCreated (see basic-server-react). Could you retest with 1.7.0 and share any new console warnings?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions