ChatGPT Apps on TanStack and Railway
By Jack Herrington
TanStack:* Framework for building apps.MCP:* Integration protocol for ChatGPT.ChatGPT App Functionality:* Interactive cardstools
Share:
Key Concepts
- TanStack: A framework for building applications, used here for a guitar inventory e-commerce site.
- MCP (Meta-Channel Protocol): A protocol for integrating applications, specifically adapted by OpenAI for ChatGPT.
- ChatGPT App Functionality: A feature within ChatGPT that allows for interactive cards and integrations.
- Railway: A cloud hosting platform used for deploying applications.
- Developer Mode (ChatGPT): A setting in ChatGPT that enables app integrations.
- Enrock: A tool for creating secure tunnels to expose local applications to the internet.
- GitHub Repository: Used for version control and connecting to Railway for deployments.
- MCP Endpoint: A specific URL on an application that ChatGPT can communicate with.
- Server Handlers (TanStack Start): Functions that define how an application responds to GET, POST, and other HTTP requests.
- CORS (Cross-Origin Resource Sharing): A mechanism that allows web pages to request resources from a different domain.
- Metadata: Information about an application, including its description, tools, and resources, required by ChatGPT.
- Tools (ChatGPT): Functions or capabilities that ChatGPT can call to perform actions or retrieve data.
- Resources (ChatGPT): Files or assets that ChatGPT can access, such as HTML templates.
- Widgets (OpenAI): Interactive UI elements generated by OpenAI based on tool output and templates.
- Tool Output: Data returned by a tool that can be used to populate widgets.
- UI Widget Template: An HTML file that defines the structure of a widget.
useChatGPToolOutputHook: A React hook to access tool output within a widget.window.openaiObject: A JavaScript object injected into the iframe shell, providing access to tool output and functions likeopenExternal.- Vite: A build tool used for compiling JavaScript modules and assets.
vite.config.ts: Configuration file for Vite.build widgetScript: A package.json script to build the widget using Vite.
TanStack Guitar Inventory Integration with ChatGPT
This document details the process of integrating a TanStack-based guitar inventory application with ChatGPT's new app functionality, enabling interactive cards and enhanced user experiences. The integration leverages the MCP UI spec tailored for ChatGPT and utilizes Railway for seamless deployment.
1. Project Setup and Deployment
- TanStack Application: The foundation is a TanStack start application, specifically a "TAN chat boilerplate," demonstrating e-commerce integration with AI using a guitar inventory as an example.
- Developer Mode: To enable ChatGPT integration, the ChatGPT instance must be in developer mode. This is accessible via
Apps and Connectors>Advanced Settings>Developer Mode. - Deployment Requirement: For ChatGPT to connect to the TanStack application, it must be live on the internet.
- Option 1 (Development): Use
Enrockto create a tunnel from localhost to the internet. - Option 2 (Recommended): Deploy the application to a hosting service.
- Option 1 (Development): Use
- Railway Hosting: The video sponsors Railway, a platform for easily shipping TanStack applications.
- Process:
- Log in to Railway via GitHub.
- Connect to a GitHub repository containing the TanStack app.
- Add the GitHub repository as a remote origin to the local project.
- Commit and push changes to the repository.
- Create a new project on Railway, selecting the GitHub repository.
- Railway Features:
- Supports various databases (Postgres, Redis, MongoDB, MySQL).
- Allows integration of arbitrary Docker images.
- Offers persistent volumes for data storage.
- Enables creation of server functions.
- Supports templating for architecture specification.
- Deployment Success: After a quick build and deployment, the application becomes active and accessible via a generated domain (e.g.,
ts-chatgpt-app.production.railway.app).
- Process:
2. Creating the MCP Endpoint
- MCP Route Definition: A new MCP route is created within the TanStack application's code (
src/routes/mcp.ts). createFileRoute: This TanStack function is used to define the route.- Server Handlers: The route is configured with server handlers for different HTTP methods.
GETHandler:- Responds to pre-flight CORS requests (OPTIONS).
- Returns application metadata to ChatGPT.
- Metadata: Includes
chatgptApptype, description ("MCP server chat integration"), name ("guitar app"), and specifies it's an "e-commerce MCP server" with "tools and resources."
POSTHandler:- Handles incoming MCP requests using an
MCP request handler. - Takes an
HTTP requestand anMCP serverobject.
- Handles incoming MCP requests using an
- Defining the MCP Server:
- An
MCP serverobject is defined, named "guitar app." getGuitarsTool:- A simple tool is created to return a list of all available guitars.
- It takes no input parameters.
- It retrieves guitar data from
data/exampleGuitars.ts.
- An
- Build and Push:
- The application is built (
npm run build) to ensure the new route is included. - Changes are committed with a description ("adding an MCP endpoint") and pushed to the GitHub repository.
- Railway automatically triggers a new deployment due to the repository integration.
- The application is built (
- Railway Build Configuration:
- Defaults are used for the builder (Railpack) and build command, simplifying setup.
- Railway automatically detects the Vite app and
startcommand. - Options for custom start commands, resource limits, cron jobs, health checks, serverless functions, and configuration as code are available.
3. Connecting to ChatGPT and Initial Interaction
- ChatGPT App Creation:
- Navigate to
Settings>Enabled Apps and Connectors. - Click
Create. - Name the app "guitar store."
- Provide the MCP endpoint URL (e.g.,
https://ts-chatgpt-app.production.railway.app/mcp). - Authentication is skipped for simplicity.
- Click
Create.
- Navigate to
- Initial Test:
- The connection to "Guitar Store" in dev mode is confirmed.
- A prompt is given: "Recommend a good acoustic guitar from the get guitars tool and make sure that we have guitar store connected."
- ChatGPT calls the
getGuitarstool. - The response includes all guitar data.
- ChatGPT recommends "flowery love guitar" or "traveling man guitar."
4. Implementing Interactive Widgets
showGuitarTool:- A new tool is added to display a specific guitar.
- It takes a
guitar IDas input. - OpenAI Specific Metadata: This is crucial for turning the tool into a widget.
template: Specifies the UI widget template file (ui/widgets/showGuitar.html).invokingandinvokedtext: For user feedback during tool execution.
- Functionality: The function finds the guitar by ID and returns its data.
- Tool Output: The guitar data is sent as
tool output, which will be passed as props to the HTML template.
- Resource Definition (
ui/widgets/showGuitar.html):- This HTML file defines the widget's appearance.
- It has a
widgetURIthat matches the output template defined in the tool. - Initially, it contains a simple placeholder:
<h1>Hello from a guitar app</h1>.
- Deployment and Refresh:
- The code is built and pushed, triggering a Railway deployment.
- The ChatGPT app's "guitar store" connection is refreshed to recognize the new
showGuitartool and its associated widget.
- Second Test:
- The same prompt is used: "Recommend a good acoustic guitar from the get guitars tool and make sure that we have guitar store connected."
- The response now includes an
iframedisplaying "Hello from guitar app." This confirms the widget is being rendered.
5. Creating a Full React Widget
- Goal: Replace the placeholder HTML with a dynamic React application.
src/chatgpt/components/GuitarRecommendation.tsx:- A React component that displays a guitar card with an image, description, and price.
- Includes a "View Details" button.
src/chatgpt/widgetEntry.tsx:- A new entry point for Vite to build the widget as a JavaScript module.
- Uses
ReactDOM.renderto mount theGuitarRecommendationcomponent. useChatGPToolOutputHook: This custom hook (defined insrc/chatgpt/integration.ts) retrieves tool output.- It uses a context to provide the
tool outputto the component. - It checks for
window.openaifor the shell integration object. - It uses an
intervalto poll fortool outputas it might not be immediately available.
- It uses a context to provide the
window.openaiObject: This object, injected into the iframe, provides:- Access to
tool output. - Functions like
openExternalto open URLs in a new browser tab.
- Access to
- "View Details" Button Logic: When clicked, it calls
window.openai.openExternalwith the guitar's URL.
- Vite Configuration (
vite.config.widget.ts):- Configures Vite to build the widget.
- Specifies
tailwindandreactplugins. - Sets the entry point (
widgetEntry.tsx). - Defines the output directory (
widget/dist).
- Build Script (
package.json):- A
build:widgetscript is added to runvite buildusing the widget configuration. - This script then moves the generated
widget.jsandwidget.cssfromwidget/distto thepublicdirectory.
- A
- MCP File Update (
mcp.ts):- The
GEThandler is updated to include thewidget.cssstylesheet and adivto mount thewidget.jsscript.
- The
- Final Deployment and Test:
- The code is built and pushed.
- The application is refreshed in ChatGPT.
- The prompt is given again.
- The response now displays an interactive guitar card with an image, description, and price.
- Clicking "View Details" successfully opens the guitar's page in a new browser tab.
6. Conclusion and Acknowledgements
- Cutting-Edge Technology: The integration is described as technically challenging and cutting-edge, with less-than-ideal documentation. The success of the implementation is a significant achievement.
- Sponsorship: The video is sponsored by Railway, which provided financial support and a robust platform for deploying TanStack applications with necessary infrastructure and logging.
- Call to Action: Viewers are encouraged to ask questions in the comments, like the video, and subscribe to the channel for future content.
This comprehensive summary outlines the technical steps, key concepts, and practical implementation of integrating a TanStack application with ChatGPT's app functionality, highlighting the use of Railway for deployment and the creation of interactive widgets.
Chat with this Video
AI-PoweredLoad the transcript when you're ready to chat so the initial page stays lighter.