Repository navigation
SCAL-354207: Show Analytics Agent files as download cards in the MCP sample app - #79
Open
mouryabalabhadra wants to merge 7 commits into
Open
mouryabalabhadra wants to merge 7 commits into
mouryabalabhadra wants to merge 7 commits into
Conversation
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
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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_filetool from thoughtspot/mcp-server#253:fileupdate, raw Spotter 3fileslists, and raw SpotterX updates that carry afile_id. Each one becomes afileevent 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=...callsget_filewithas_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.Safety
/api/filesserves 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.get_fileitself, 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.get_fileerror result (expired, too large, no access) is shown as is, since it is written for people.APP_ORIGINS(defaulthttp://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.Testing
Tested by hand against a local MCP server on a V1 cluster with Spotter 3:
/api/files. A 9.8 MB CSV streamed fine.get_filecall the model makes returns no link.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.