View the source code for this module on GitHub: https://github.com/mcp-use/mcp-use/blob/main/libraries/typescript/packages/mcp-use/src/react/useFiles.ts
useFiles is a React hook for file upload and download inside widgets. It exposes an isSupported flag plus an upload and a getDownloadUrl function.
useFiles
Hook for file upload and download operations in widgets.isSupported is computed once on mount via useMemo and is true only when the OpenAI window.openai extension exposes both window.openai.uploadFile and window.openai.getFileDownloadUrl as functions. In all other environments (MCP Apps clients, URL params fallback, server-side rendering) it is false.
The hook takes no arguments.
Returns
Signature
Basic usage
Always gate file operations behindisSupported. The following server registers a widget that opens a file manager, and the widget component uses the hook.
Returns
The object returned byuseFiles.
UseFilesResult
The shape of the value returned byuseFiles. Every field is required and present on every call.
Returns
SignatureWhether the host supports file operations.trueonly when the OpenAIwindow.openaiextension exposeswindow.openai.uploadFileandwindow.openai.getFileDownloadUrl. Always check this flag before callinguploadorgetDownloadUrl.Upload a file to the host. Returns aFileMetadatareference ({ fileId }) that can be passed togetDownloadUrlor stored in widget state for later retrieval. By default the file is tracked in widget state (imageIds) so the model can see it; pass{ modelVisible: false }inUploadOptionsto upload privately. Throws if called whenisSupportedisfalse.Get a temporary download URL for a previously uploaded file. The returned URL is valid for a limited time (typically 5 minutes). Do not store the URL; callgetDownloadUrlagain when you need to display or fetch the file. Throws if called whenisSupportedisfalse.
Types
UploadOptions
Options passed as the optional second argument toupload.
Configures a single upload call. All fields are optional.
Parameters
SignatureWhether the uploaded file should be visible to the model. Whentrue(the default), thefileIdis appended toimageIdsin widget state so the ChatGPT host includes the file in the model’s conversation context on future turns. Whenfalse, the file is uploaded but not tracked inimageIds, so the model will not see it. Useful for files used only by the widget (for example a user-provided config file or a privately processed image).
FileMetadata
The opaque file reference returned byupload.
Opaque file reference returned by useFiles().upload(). Pass it to getDownloadUrl() to retrieve a temporary download URL. Defined in mcp-use/react (re-exported from the widget types module).
Returns
SignatureOpaque string identifier assigned by the host. Store it in widget state to retrieve the download URL later.
Model visibility
By default, every uploaded file is tracked in widget state underimageIds. ChatGPT reads this field to include the file in the model’s conversation context on future turns, so the model can reference the content of the uploaded image. Internally, upload preserves existing imageIds (and other widget state) when appending, so uploading a file does not wipe other state.
Pass { modelVisible: false } when you want to upload a file for widget-only use without exposing it to the model.
imageIds state is preserved across setWidgetState calls, so uploading a file does not wipe other widget state. If setWidgetState fails while tracking the imageId, the error is logged with console.warn and the upload still resolves with its FileMetadata.Full upload and download example
Download URLs are temporary (typically valid for 5 minutes). Do not store the URL; call
getDownloadUrl each time you need to display or fetch the file. Store the fileId in widget state instead.Error handling
Callingupload or getDownloadUrl when isSupported is false throws a descriptive error. For upload:
getDownloadUrl:
try/catch to handle host-level errors (network failures, policy violations).
Related
useWidget()for accessing widget props and state.ModelContextfor annotating UI state for the model.