Repository navigation
fix: render a tool result's structuredContent in the Tools screen (#1908) #1975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
clients/web/src/components/groups/StructuredOutputPanel/StructuredOutputPanel.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import type { Meta, StoryObj } from "@storybook/react-vite"; | ||
| import { expect, userEvent, waitFor, within } from "storybook/test"; | ||
| import { StructuredOutputPanel } from "./StructuredOutputPanel"; | ||
|
|
||
| const nested = { | ||
| items: [ | ||
| { id: 1, name: "Item A", tags: ["foo", "bar"] }, | ||
| { id: 2, name: "Item B", tags: ["baz"] }, | ||
| ], | ||
| total: 2, | ||
| }; | ||
|
|
||
| const flat = { | ||
| temperature: 25, | ||
| unit: "C", | ||
| city: "San Francisco", | ||
| }; | ||
|
|
||
| // Long enough that the payload exceeds the section's max height and scrolls | ||
| // within it rather than pushing the rest of the result panel down. | ||
| const large = { | ||
| rows: Array.from({ length: 60 }, (_, index) => ({ | ||
| id: index + 1, | ||
| label: `Row ${index + 1}`, | ||
| value: index * 7, | ||
| })), | ||
| total: 60, | ||
| }; | ||
|
|
||
| const meta: Meta<typeof StructuredOutputPanel> = { | ||
| title: "Groups/StructuredOutputPanel", | ||
| component: StructuredOutputPanel, | ||
| }; | ||
|
|
||
| export default meta; | ||
| type Story = StoryObj<typeof StructuredOutputPanel>; | ||
|
|
||
| export const Nested: Story = { | ||
| args: { | ||
| structuredContent: nested, | ||
| }, | ||
| play: async ({ canvasElement }) => { | ||
| const canvas = within(canvasElement); | ||
| expect( | ||
| canvas.getByRole("heading", { name: "Structured Output" }), | ||
| ).toBeInTheDocument(); | ||
| // The section starts expanded, so the payload is visible without a click. | ||
| // Asserted on the container's text: once the Prism grammar loads, the JSON | ||
| // is split across per-token elements, so no single node holds the value. | ||
| await waitFor(() => | ||
| expect(canvasElement.textContent).toContain('"Item A"'), | ||
| ); | ||
| }, | ||
| }; | ||
|
|
||
| export const Flat: Story = { | ||
| args: { | ||
| structuredContent: flat, | ||
| }, | ||
| }; | ||
|
|
||
| export const Large: Story = { | ||
| args: { | ||
| structuredContent: large, | ||
| }, | ||
| }; | ||
|
|
||
| // Collapsing hides the payload; expanding brings it back. | ||
| export const Collapsed: Story = { | ||
| args: { | ||
| structuredContent: nested, | ||
| defaultExpanded: false, | ||
| }, | ||
| play: async ({ canvasElement }) => { | ||
| const canvas = within(canvasElement); | ||
| const toggle = canvas.getByRole("button", { | ||
| name: "Expand structured output", | ||
| }); | ||
| expect(toggle).toHaveAttribute("aria-expanded", "false"); | ||
| await userEvent.click(toggle); | ||
| await waitFor(() => | ||
| expect( | ||
| canvas.getByRole("button", { name: "Collapse structured output" }), | ||
| ).toHaveAttribute("aria-expanded", "true"), | ||
| ); | ||
| await waitFor(() => | ||
| expect(canvasElement.textContent).toContain('"Item A"'), | ||
| ); | ||
| }, | ||
| }; | ||
87 changes: 87 additions & 0 deletions
87
clients/web/src/components/groups/StructuredOutputPanel/StructuredOutputPanel.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { describe, it, expect, vi } from "vitest"; | ||
| import userEvent from "@testing-library/user-event"; | ||
| import { renderWithMantine, screen } from "../../../test/renderWithMantine"; | ||
| import { StructuredOutputPanel } from "./StructuredOutputPanel"; | ||
|
|
||
| // The real CodeHighlight dynamic-imports the Prism runtime and each grammar | ||
| // (its own behavior is covered in CodeHighlight.test.tsx). Stub it so these | ||
| // tests assert on the JSON handed to it, synchronously. | ||
| vi.mock("../../elements/CodeHighlight/CodeHighlight", () => ({ | ||
| CodeHighlight: ({ language, code }: { language: string; code: string }) => ( | ||
| <pre data-testid="code-highlight" data-language={language}> | ||
| {code} | ||
| </pre> | ||
| ), | ||
| })); | ||
|
|
||
| const structuredContent = { | ||
| items: [ | ||
| { id: 1, name: "Item A", tags: ["foo", "bar"] }, | ||
| { id: 2, name: "Item B", tags: ["baz"] }, | ||
| ], | ||
| total: 2, | ||
| }; | ||
|
|
||
| describe("StructuredOutputPanel", () => { | ||
| it("renders a labeled section with the payload as pretty-printed JSON", () => { | ||
| renderWithMantine( | ||
| <StructuredOutputPanel structuredContent={structuredContent} />, | ||
| ); | ||
| expect( | ||
| screen.getByRole("heading", { name: "Structured Output" }), | ||
| ).toBeInTheDocument(); | ||
| const code = screen.getByTestId("code-highlight"); | ||
| expect(code).toHaveAttribute("data-language", "json"); | ||
| expect(code).toHaveTextContent(/"total": 2/); | ||
| // Nested values are inspectable field by field, not summarized away. | ||
| expect(code).toHaveTextContent(/"name": "Item A"/); | ||
| expect(code).toHaveTextContent(/"tags"/); | ||
| }); | ||
|
|
||
| it("starts expanded by default", () => { | ||
| renderWithMantine( | ||
| <StructuredOutputPanel structuredContent={structuredContent} />, | ||
| ); | ||
| expect( | ||
| screen.getByRole("button", { name: "Collapse structured output" }), | ||
| ).toHaveAttribute("aria-expanded", "true"); | ||
| }); | ||
|
|
||
| it("starts collapsed when defaultExpanded is false", () => { | ||
| renderWithMantine( | ||
| <StructuredOutputPanel | ||
| structuredContent={structuredContent} | ||
| defaultExpanded={false} | ||
| />, | ||
| ); | ||
| expect( | ||
| screen.getByRole("button", { name: "Expand structured output" }), | ||
| ).toHaveAttribute("aria-expanded", "false"); | ||
| }); | ||
|
|
||
| it("toggles between expanded and collapsed", async () => { | ||
| const user = userEvent.setup(); | ||
| renderWithMantine( | ||
| <StructuredOutputPanel structuredContent={structuredContent} />, | ||
| ); | ||
| await user.click( | ||
| screen.getByRole("button", { name: "Collapse structured output" }), | ||
| ); | ||
| const toggle = screen.getByRole("button", { | ||
| name: "Expand structured output", | ||
| }); | ||
| expect(toggle).toHaveAttribute("aria-expanded", "false"); | ||
| await user.click(toggle); | ||
| expect( | ||
| screen.getByRole("button", { name: "Collapse structured output" }), | ||
| ).toHaveAttribute("aria-expanded", "true"); | ||
| }); | ||
|
|
||
| it("renders an empty structured payload rather than nothing", () => { | ||
| renderWithMantine(<StructuredOutputPanel structuredContent={{}} />); | ||
| expect( | ||
| screen.getByRole("heading", { name: "Structured Output" }), | ||
| ).toBeInTheDocument(); | ||
| expect(screen.getByTestId("code-highlight")).toHaveTextContent("{}"); | ||
| }); | ||
| }); |
102 changes: 102 additions & 0 deletions
102
clients/web/src/components/groups/StructuredOutputPanel/StructuredOutputPanel.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { useState } from "react"; | ||
| import { | ||
| Collapse, | ||
| Group, | ||
| Paper, | ||
| ScrollArea, | ||
| Stack, | ||
| Title, | ||
| } from "@mantine/core"; | ||
| import type { CallToolResult } from "@modelcontextprotocol/client"; | ||
| import { ContentViewer } from "../../elements/ContentViewer/ContentViewer"; | ||
| import { ExpandToggle } from "../../elements/ExpandToggle/ExpandToggle"; | ||
|
|
||
| export interface StructuredOutputPanelProps { | ||
| /** The result's `structuredContent` — the tool's schema-validated payload. */ | ||
| structuredContent: NonNullable<CallToolResult["structuredContent"]>; | ||
| /** Whether the section starts expanded. Defaults to `true`. */ | ||
| defaultExpanded?: boolean; | ||
| } | ||
|
|
||
| // Bordered box matching the "Resource Links" group in the result panel, so the | ||
| // two supplementary sections of a tool result read as siblings. | ||
| const StructuredBox = Paper.withProps({ | ||
| withBorder: true, | ||
| radius: "md", | ||
| p: "md", | ||
| variant: "panel", | ||
| }); | ||
|
|
||
| const StructuredInner = Stack.withProps({ | ||
| gap: "sm", | ||
| }); | ||
|
|
||
| const HeaderRow = Group.withProps({ | ||
| justify: "space-between", | ||
| wrap: "nowrap", | ||
| }); | ||
|
|
||
| // h4 (size h5) for the same reason as the "Resource Links" heading: the panel's | ||
| // "Results" title is h3, so a sub-box heading is h4 and the heading order never | ||
| // skips a level (axe `heading-order`). | ||
| const StructuredHeader = Title.withProps({ | ||
| order: 4, | ||
| size: "h5", | ||
| }); | ||
|
|
||
| // Caps the payload so a large structured result scrolls within the box instead | ||
| // of pushing the content blocks out of view. `Autosize` sizes to the content up | ||
| // to `mah`, so a small object still takes only what it needs. | ||
| const StructuredScroll = ScrollArea.Autosize.withProps({ | ||
| mah: 400, | ||
| type: "auto", | ||
| scrollbars: "y", | ||
| offsetScrollbars: true, | ||
| }); | ||
|
|
||
| /** | ||
| * Collapsible "Structured Output" section for a tool result's | ||
| * `structuredContent` (#1908). A tool declaring an `outputSchema` returns its | ||
| * real payload here — the `content[]` blocks usually only summarize it — so v1 | ||
| * rendered it as its own inspectable JSON section. Without this, the payload is | ||
| * dropped from the Tools screen entirely, with no hint it was ever returned. | ||
| * | ||
| * The JSON is pretty-printed and syntax-highlighted through {@link ContentViewer} | ||
| * (an `application/json` text block), so it is copyable and scannable field by | ||
| * field. | ||
| */ | ||
| export function StructuredOutputPanel({ | ||
| structuredContent, | ||
| defaultExpanded = true, | ||
| }: StructuredOutputPanelProps) { | ||
| const [expanded, setExpanded] = useState(defaultExpanded); | ||
|
|
||
| return ( | ||
| <StructuredBox> | ||
| <StructuredInner> | ||
| <HeaderRow> | ||
| <StructuredHeader>Structured Output</StructuredHeader> | ||
| <ExpandToggle | ||
| expanded={expanded} | ||
| onToggle={() => setExpanded((value) => !value)} | ||
| ariaLabel={`${expanded ? "Collapse" : "Expand"} structured output`} | ||
| /> | ||
| </HeaderRow> | ||
| {/* Content stays mounted across a collapse (Mantine `Collapse`), so the | ||
| highlighted JSON isn't re-rendered from scratch on every toggle. */} | ||
| <Collapse in={expanded}> | ||
| <StructuredScroll> | ||
| <ContentViewer | ||
| block={{ | ||
| type: "text", | ||
| text: JSON.stringify(structuredContent, null, 2), | ||
| }} | ||
| mimeType="application/json" | ||
| copyable | ||
| /> | ||
| </StructuredScroll> | ||
| </Collapse> | ||
| </StructuredInner> | ||
| </StructuredBox> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.