Skip to content

SCAL-354207: Show Analytics Agent files as download cards in the MCP sample app - #79

Open
mouryabalabhadra wants to merge 7 commits into
mainfrom
mcp-file-download-card
Open

mouryabalabhadra wants to merge 7 commits into
mainfrom
mcp-file-download-card

Conversation

@mouryabalabhadra

@mouryabalabhadra mouryabalabhadra commented Dec 5, 2026 •

Copy link
Copy Markdown
Collaborator

What this does

When the Analytics Agent produces a file (a CSV export, a PDF and so on), the sample chat app now shows it as a card under the answer with a Download button, the way Spotter does.

It relies on the get_file tool from thoughtspot/mcp-server#253:

  • The server picks up file updates from the session stream: the digested file update, raw Spotter 3 files lists, and raw SpotterX updates that carry a file_id. Each one becomes a file event to the browser and is saved with the turn, so the cards come back when a chat is reopened.
  • GET /api/files?file_id=...&name=... calls get_file with as_file: true. When the MCP server returns a download link, the backend streams the file through to the browser in chunks instead of reading it into memory. When it returns the file inline (older servers), the backend serves those bytes.
  • The client starts the download through a hidden iframe, so an error page shows up as an error on the card rather than navigating away. The server sets a short-lived cookie when a download starts, and the card keeps its button busy until that cookie arrives or an error page loads.

Safety

  • /api/files serves only file ids this app has shown in a chat. Anything else gets a 404. Stored chats' files are loaded at startup, so cards keep working across a backend restart, and a deleted chat's files are dropped.
  • Files are recorded and shown only from sessions the chat itself created, so a session id the model makes up can't plant a file card or open a file.
  • The backend follows a download link only if it points at the configured MCP server (same scheme, host and port, no userinfo or query, a well-formed grant id), and it doesn't follow redirects.
  • When the model calls get_file itself, the download link is removed from the result before the model sees it, so the link never reaches the model, the server log or saved chat history. Any /files/download/ link left in the text is also scrubbed, whatever shape the result is in. The model is told to point the user at the file's card.
  • MCP and connection errors reach the browser as a generic message and are logged by exception type only. A get_file error result (expired, too large, no access) is shown as is, since it is written for people.
  • Only APP_ORIGINS (default http://localhost:8000, the UI's origin) may call the API from a browser, instead of every origin. This covers the existing endpoints too. The app still has no sign-in of its own.
  • The streamed download has a 60 second timeout between chunks and is closed again after the response.

Testing

Tested by hand against a local MCP server on a V1 cluster with Spotter 3:

  • Asked for a CSV export, got a card, and downloaded it through /api/files. A 9.8 MB CSV streamed fine.
  • Reopened the chat: the cards came back and the files still downloaded.
  • A file id the chat never showed got a 404.
  • After a backend restart, a stored chat's file downloaded without reopening the chat.
  • A get_file call the model makes returns no link.
  • Another origin gets no CORS access; the UI, through the Vite proxy, works as before.
  • Clicked Download in the UI: the card showed progress, then went back to normal, and the saved file matched the server's copy byte for byte.
  • Against a slow upstream, a browser leaving mid-file or before the first chunk closed the upstream connection within a second.

The raw SpotterX path is checked only against hand-built updates, not a live SpotterX session. The disconnect check used a local slow upstream, not ThoughtSpot.

When the Analytics Agent produces a file, such as a CSV export, the chat
shows a card with its name and a download button, as Spotter does.

- Server: a `file` session update (digested or raw) becomes a `file`
  event to the browser. The system prompt tells Claude the card is
  already shown, so it names the file instead of pasting a link or
  calling get_file to download it.
- GET /api/files calls the MCP server's get_file with `as_file`. It
  follows the one-time download link the server returns and streams the
  file on to the browser, or serves the file when get_file returns it
  inline. Only links on the MCP server itself are followed.
- Chat history keeps each reply's files (new `files` column), so the
  cards come back when a chat is reopened.
- Client: the download runs in a hidden iframe, so the browser writes
  the file straight to disk; an error page is shown on the card.

get_file is in the MCP server's beta API version only.
…rs generic

- /api/files now serves only a file_id this app announced as a card, or
  one in a chat it has reopened. The id comes from the browser, so before
  this the route would fetch any file the app's ThoughtSpot user could
  read. This example has one user; an app with several would also key
  the record by user.
- A failed download link is logged on the server and reported to the
  browser as a short message, not the upstream error or body.
- The download link is followed only if it is exactly a grant link on
  the MCP server: same scheme, host and port, no user info or query, and
  a well-formed grant id. Redirects are not followed.
The session a poll reads comes from the model, so only a session this
conversation created may add files /api/files will serve. A failed
download link is logged by exception type only, since the exception
text can include the link.
A card is downloadable and is saved with the chat, so a file from a
session this conversation did not create was still sent, stored, and
trusted again on reopen. Gate it where file events are sent, so only
files from trusted sessions are ever shown, stored or served.
With raw session updates, the MCP server adds a file_id to the SpotterX
Agent's own event, which is not typed `file`, so no card was shown.
Recognise any update carrying a file_id, and label the card with the
name inside the id when the update carries none.
…ndings

- Remove the download link from get_file results before the model sees them, so it
  never reaches the server log or saved chat history. The user downloads from the card.
- Load the file ids of every stored chat at startup, so cards in a chat that stays open
  across a backend restart still download; forget a chat's files when it is deleted.
- Keep MCP and connection errors out of the browser and the upstream body out of the log.
- Have the server set a short-lived cookie when a download starts, and keep the card's
  button busy until it arrives or the error page loads, instead of guessing with a timer.
  A frame from an earlier click no longer changes the card.
- Add a read timeout and a second close to the streamed download.
- Allow only APP_ORIGINS (default http://localhost:8000) to call the API from a browser,
  instead of every origin.
…sees

The link was removed only when the result parsed as the expected JSON; any other shape
went to the model, the log and saved history as is. Now any /files/download/ link is
also scrubbed from the text, whatever its shape.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant