---
title: "App Inspection (MCP)"
description: "Inspect a running GTKX app and look up its generated bindings from your coding agent."
---

# App Inspection (MCP)

`@gtkx/mcp` is a [Model Context Protocol](https://modelcontextprotocol.io/docs/learn/server-concepts) server that connects a coding agent to your running GTKX app. The agent can inspect widgets, interact with the window, take screenshots, and look up the exact bindings generated for your project.

## Setup

Install the server and the testing package its widget tools use. Scaffolded projects include both.

::: code-group

```bash [npm]
npm install -D @gtkx/mcp @gtkx/testing
```

```bash [pnpm]
pnpm add -D @gtkx/mcp @gtkx/testing
```

```bash [yarn]
yarn add -D @gtkx/mcp @gtkx/testing
```

:::

From the project root, register the server with your client. A scaffolded project already has the Claude configuration.

```bash
npx gtkx mcp init --client claude
```

[`--client`](/guide/cli#gtkx-mcp-init) accepts `claude`, `cursor`, `vscode`, `opencode`, and `codex`, and the command adds a `gtkx` entry to that client's project configuration file.

### Other clients

Configure a stdio server that runs `npx --no gtkx mcp`. Clients start servers without a terminal, where plain `npx` would install and run an unrelated `gtkx` package from the npm registry when `@gtkx/cli` is missing; `--no` makes it fail instead.

Start the server in the app's directory. It reads the configuration and generated bindings of the directory it starts in, and pnpm installs the CLI only in the app, not at a workspace root. An app scaffolded into a [workspace](/guide/workspaces#add-an-app) gets its own `.mcp.json`, and the scaffold also adds a server named `gtkx` to the `.mcp.json` at the workspace root, where AI clients are usually opened. That server starts the app's MCP server through the workspace's package manager:

| Workspace       | Command                                             |
| --------------- | --------------------------------------------------- |
| pnpm            | `pnpm --dir apps/my-app exec gtkx mcp`              |
| npm             | `npm exec --no --workspace apps/my-app -- gtkx mcp` |
| Yarn 2 or newer | `yarn workspace my-app exec gtkx mcp`               |
| Yarn 1          | `yarn --silent --cwd apps/my-app gtkx mcp`          |

When the root `.mcp.json` already has a GTKX server, such as an earlier app's, the scaffold adds none, because [one server drives every running GTKX app](#several-apps). When the file is not a valid MCP configuration, the scaffold leaves it unchanged and prints the entry to add.

### Start the app

```bash
npx gtkx dev
```

The app connects automatically, and either side can start first. The connection exists only during development; production builds exclude it. For a session without a display, run [`npx gtkx dev --headless`](/guide/cli#gtkx-dev) and stop it with Ctrl+C. See [Headless sessions](/guide/cli#headless-sessions) for display options.

## Inspect and verify changes

Ask the agent to inspect the running app before changing it, then verify the result after Fast Refresh updates the window. A useful sequence is:

1. Find a widget by its role, accessible name, or text with `gtkx_query_widgets`.
2. Read its properties or subtree when more context is needed.
3. Click, type, or press keys through the interaction tools.
4. Take a screenshot to check the rendered result.

A screenshot shows the window at the moment it is taken, so one taken straight after a click can catch a transition half done, such as a stack crossfading between pages. Take another once the animation has finished.

### Tools

| Tool                    | Read-only | Use                                                                    |
| ----------------------- | --------- | ---------------------------------------------------------------------- |
| `gtkx_list_apps`        | Yes       | List the connected apps and their windows                              |
| `gtkx_query_widgets`    | Yes       | Find widgets by role, text, accessible name or label                   |
| `gtkx_get_widget_tree`  | Yes       | Read the widget tree, or one subtree, to a chosen depth                |
| `gtkx_get_widget_props` | Yes       | Read a widget's state and the GObject properties you ask for           |
| `gtkx_take_screenshot`  | Yes       | Capture a window as an image, a saved PNG, or both                     |
| `gtkx_click`            | No        | Click a widget                                                         |
| `gtkx_type`             | No        | Type text into an editable widget                                      |
| `gtkx_press_keys`       | No        | Press keys and shortcuts, such as `{Enter}` or `{Control>}s{/Control}` |
| `gtkx_pointer`          | No        | Press, hold or release a mouse button on a widget                      |
| `gtkx_fire_event`       | No        | Emit a GObject signal on a widget                                      |
| `gtkx_list_api`         | Yes       | List the namespaces and symbols of the generated bindings              |
| `gtkx_search_api`       | Yes       | Search the generated bindings by name                                  |
| `gtkx_get_api_docs`     | Yes       | Read the reference for one symbol or member                            |

For a large window, request a subtree or limit the depth rather than reading the whole tree again. Widget IDs last only while their widgets are mounted, so query again after the UI they belong to is replaced.

<span id="simulated-input"></span>The interaction tools use the same queries and `userEvent` helpers as [`@gtkx/testing`](/guide/testing#interacting-and-waiting), so they simulate input rather than drive a real keyboard or mouse.

<span id="errors"></span>An error the app raises while handling an interaction comes back in the tool result, and `gtkx dev` ends the app and starts it again after a saved fix, as it does for any [uncaught error](/guide/error-handling#uncaught-exceptions).

### Several apps

All GTKX MCP servers of a session listen on one socket, so run one server: it lists and drives every running GTKX app, and a second server started next to it exits with an error. Without `applicationId`, tools use the app from the server's project, or the first connected app when none comes from it. Use `gtkx_list_apps` to find the others and pass `applicationId` to target one.

The [Storybook explorer](/guide/storybook) registers under the project's application ID with a `.Storybook` suffix, so `gtkx dev` and `gtkx storybook` can run together. Tools without `applicationId` use the `gtkx dev` app. While it restarts, they wait for it, up to `appTimeout` (10 seconds by default), before falling back to the explorer.

## Look up generated bindings

The reference tools document the project's `@gtkx/gi/*` and `@gtkx/jsx/*` modules. They use the same GIR files as code generation and work without a running app.

Use `gtkx_search_api` to find a symbol or member, then `gtkx_get_api_docs` to read it. [`gtkx docs`](/guide/jsx-elements#element-reference) writes static reference pages from the same bindings.

The server documents the project containing its working directory, falling back to a connected app's project. Pass `projectRoot` to select another project. Clients that browse MCP resources can read the reference there too. A project with `codegen: false` has no generated bindings to document.

### What agents are given

In an application, codegen also writes `.gtkx/reference` for coding agents and maintains a marked GTKX section in `AGENTS.md`. Text outside that section is preserved, and so is a section that a Markdown formatter has only rewrapped or realigned. It creates a `CLAUDE.md` import only when that file does not already exist.

Commit the rules file and ignore `.gtkx/`, as scaffolded projects do. Set `agents.rules` or `agents.reference` to `false` to disable the corresponding output. Disabling the rules removes the GTKX section from `AGENTS.md`, and deletes that file and the `CLAUDE.md` import when nothing else remains in them. Disabling the reference also removes `.gtkx/reference` and its mention from the rules.

The reference covers the project's bindings. To give an agent the GTKX guide as well, see [Point the agent to the guide](#point-the-agent-to-the-guide).

## Point the agent to the guide

The server documents the generated bindings, not how to build a GTKX app. For that, give the agent this version's guide and tutorial as Markdown: [https://gtkx.dev/v2/llms.txt](https://gtkx.dev/v2/llms.txt) lists every page, and [https://gtkx.dev/v2/llms-full.txt](https://gtkx.dev/v2/llms-full.txt) holds all of their text in one large file. Each page also has a Markdown copy at its own URL with `.md` added, such as [https://gtkx.dev/v2/guide/mcp.md](https://gtkx.dev/v2/guide/mcp.md). See [Machine-readable documentation](/versions#machine-readable-documentation) for the other versions.

## Choose the available tools

Set `mcp.readOnly` to expose only the read-only tools, leaving out app interactions. Use `mcp.tools` to select tools by name:

```ts
import { defineConfig } from "@gtkx/config";

export default defineConfig({
    applicationId: "com.example.app",
    mcp: {
        tools: ["gtkx_*", "!gtkx_take_screenshot"],
        readOnly: true,
    },
});
```

`*` matches any text, and a leading `!` removes matches. A list containing only exclusions starts with all tools. Otherwise, patterns add and remove tools in order, starting with none.

[Command-line options](/guide/cli#gtkx-mcp) override the project settings for one client:

```bash
npx --no gtkx mcp --read-only --tools "gtkx_*_api,gtkx_get_widget_tree"
```
