Skip to content

Commit 622725d

Browse files
Update agent.py, App.jsx with customization examples. Update the Readme file to provide guidance on how to customize the iframe and Spotter MCP behavior for a custom chat interface.
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 49b1b1c commit 622725d

13 files changed

Lines changed: 4642 additions & 31 deletions

‎README.md‎

Lines changed: 218 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,236 @@
1-
<p align="center">
2-
<img src="ts-logo.svg" width=120 align="center" alt="ThoughtSpot" />
3-
<h3 align="center">ThoughtSpot Developer Examples</h3>
4-
</p>
1+
# Python Agent with Simple React UI
52

6-
<br />
7-
<br />
3+
A full-stack example that pairs a **Python (FastAPI) agent** powered by the OpenAI Responses API and ThoughtSpot MCP server with a **React chat UI**.
84

9-
Enjoy our curated collection of examples and solutions. Use these patterns to integrate robust and scalable analytics into your own applications with ThoughtSpot.
10-
We're going to be shipping new examples weekly. Stay tuned!
5+
The backend streams responses to the frontend using Server-Sent Events (SSE), giving users a real-time chat experience while the agent queries ThoughtSpot for data insights and displays ThoughtSpot charts in
6+
an embed. Supports trusted auth and other standard thoughtspot customizations.
117

12-
- [Visual Embed](/visual-embed) – Drop in ThoughtSpot Analytics components (Plain JS/React) for your web apps. [Learn more.](https://developers.thoughtspot.com/docs/getting-started)
13-
- [REST APIs](/rest-api) – Headless AI for BI, the power of ThoughtSpot's platform via APIs. [Learn more.](https://developers.thoughtspot.com/docs/rest-apis)
14-
- [Mobile](/mobile) - Embedded analytics in your own Mobile app. [Learn more.](https://developers.thoughtspot.com/docs)
15-
- [Solutions](/solutions) – Demos, Architectures, and Best Practices
16-
- [Starter](/starter) – Fully functional applications that encompass an idea as a robust starting point.
8+
## Screenshot
179

18-
<br/>
19-
<br/>
10+
![Python Agent with Simple React UI](Screenshot.png)
2011

21-
### Contributing
12+
## Architecture
2213

23-
Relevant mostly for ThoughtSpot employees and partners.
14+
```
15+
┌──────────────┐ SSE stream ┌──────────────┐ MCP ┌─────────────┐
16+
│ React Chat │ ◄────────────► │ FastAPI │ ◄──────► │ ThoughtSpot │
17+
│ (Vite) │ /api/chat │ + OpenAI │ │ MCP Server │
18+
└──────────────┘ └──────────────┘ └─────────────┘
19+
:5173 :8000 agent.thoughtspot.app
20+
```
21+
22+
1. User sends a message from the React UI
23+
2. FastAPI backend forwards it to OpenAI Responses API with the ThoughtSpot MCP tool
24+
3. OpenAI calls ThoughtSpot tools as needed, and streams the response
25+
4. Backend forwards text deltas and status events to the UI over SSE
26+
5. React renders the streamed markdown response in real time
27+
28+
## Prerequisites
29+
30+
- Python 3.10+
31+
- Node.js 18+
32+
- Azure OpenAI endpoint and API key (or an OpenAI-compatible endpoint)
33+
- ThoughtSpot instance with:
34+
- Host URL
35+
- Authentication token (Bearer token)
36+
37+
## Quick Start
38+
39+
### 1. Configure environment
40+
41+
From the project root (`python-react-agent-simple-ui/`):
42+
43+
```bash
44+
cp env.template .env
45+
```
46+
47+
Edit `.env` with your credentials:
48+
49+
```env
50+
# Server-side (used by the Python agent)
51+
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com/openai/v1
52+
AZURE_OPENAI_KEY=your_azure_openai_key_here
2453
25-
- All Web examples are in Typescript + Vite.
26-
- Examples must run with the latest version of the [Visual Embed SDK](https://www.npmjs.com/package/@thoughtspot/visual-embed-sdk).
27-
- All Backend examples in Typescript (no javascript), or other languages.
54+
# Client-side (prefix with VITE_ so Vite exposes them to the browser)
55+
VITE_TS_HOST=your-instance.thoughtspot.cloud
56+
VITE_TS_AUTH_TOKEN=your_thoughtspot_bearer_token
57+
```
58+
59+
The Python server reads `AZURE_OPENAI_*` and `VITE_TS_*` variables from this `.env` file. The Vite client reads variables prefixed with `VITE_` via `import.meta.env`.
60+
61+
### 2. Start the backend
62+
63+
```bash
64+
cd server
65+
python -m venv .venv
66+
source .venv/bin/activate # Windows: .venv\Scripts\activate
67+
pip install -r requirements.txt
68+
uvicorn agent:app --reload
69+
```
70+
71+
The API will be running at `http://localhost:8000`.
2872

29-
#### Adding a new example
73+
### 3. Start the frontend
3074

31-
To quickly start contributing with a new example, run the following commands:
75+
In a separate terminal:
3276

3377
```bash
34-
npm i
35-
npm run new-example
78+
cd client
79+
npm install
80+
npm run dev
81+
```
82+
83+
Open `http://localhost:5173` in your browser. The Vite dev server proxies `/api` requests to the FastAPI backend.
84+
85+
## Project Structure
86+
87+
```
88+
python-react-agent-simple-ui/
89+
├── .env # Shared env vars (create from env.template)
90+
├── env.template # Environment variable template
91+
├── server/
92+
│ ├── agent.py # FastAPI server with OpenAI Responses API + MCP
93+
│ └── requirements.txt # Python dependencies
94+
├── client/
95+
│ ├── package.json # Node dependencies
96+
│ ├── vite.config.js # Vite config with API proxy + envDir
97+
│ ├── index.html # HTML entry point
98+
│ └── src/
99+
│ ├── main.jsx # React entry point
100+
│ ├── App.jsx # Chat UI component
101+
│ └── App.css # Styles
102+
└── README.md
103+
```
104+
105+
## How It Works
106+
107+
### Backend (`server/agent.py`)
108+
109+
- Uses the **OpenAI Responses API** with `stream=True` for real-time generation
110+
- Configures the **ThoughtSpot MCP server** as a tool — OpenAI handles MCP communication server-side
111+
- Supports **multi-turn conversations** via `previous_response_id` — the API maintains conversation context automatically
112+
- Streams **SSE events** to the frontend: `delta` (text), `status` (tool call progress), `done` (response complete), `error`
113+
114+
### Frontend (`client/src/App.jsx`)
115+
116+
- Reads the SSE stream using `fetch` + `ReadableStream` API
117+
- Renders assistant responses as **markdown** with table and code block support
118+
- Shows **real-time status** while the agent connects to and queries ThoughtSpot
119+
- Tracks `response_id` across turns for seamless multi-turn conversation
120+
121+
## Customizations
122+
123+
### 1. OpenAI Configuration (`server/agent.py`)
124+
125+
#### Change the model
126+
127+
Modify the `model` parameter in the `/api/chat` handler:
128+
129+
```python
130+
"model": "gpt-5", # or gpt-5-mini, gpt-4.1-mini, etc.
131+
```
132+
133+
#### Use Standard OpenAI Instead of Azure
134+
135+
Update the `.env` variables and client initialization:
136+
137+
```python
138+
openai_client = OpenAI(
139+
api_key=os.getenv("OPENAI_API_KEY"),
140+
)
36141
```
37142

38-
All examples must comply with the following format:
143+
---
144+
145+
### 2. Spotter MCP customizations (`server/agent.py`)
146+
147+
#### System Prompt
148+
149+
Edit the `SYSTEM_PROMPT` string to change how the agent behaves — tone, focus areas, or which datasource to use. For example, you can force a specific datasource by uncommenting the line at the end of the prompt:
39150

40-
- It must have a `.gitignore` similar to [utilities/plop-templates/ts/.gitignore](./utilities/plop-templates/ts/.gitignore)
151+
```python
152+
SYSTEM_PROMPT = (
153+
"You are a helpful data analyst assistant powered by ThoughtSpot. "
154+
# ...
155+
# "Use this datasource: cd252e5c-b552-49a8-821d-3eadaa049cca to answer all data questions."
156+
)
157+
```
158+
159+
#### Restrict Available Tools
160+
161+
The `MCP_TOOL` config controls which ThoughtSpot tools the agent can call. By default every tool is available. Uncomment and edit the `allowed_tools` list to restrict access:
162+
163+
```python
164+
MCP_TOOL = {
165+
...
166+
"allowed_tools": [
167+
"ping", # Test connectivity and authentication
168+
"getRelevantQuestions", # Get suggested questions for a query
169+
"getAnswer", # Get the answer to a specific question
170+
"createLiveboard", # Create a liveboard from a list of answers
171+
"getDataSourceSuggestions", # Get datasource suggestions for a query
172+
],
173+
}
174+
```
175+
176+
---
177+
178+
### 3. Visual Embed customizations (`client/src/App.jsx`)
179+
180+
All ThoughtSpot embed styling is configured in the `init()` call at the top of **`client/src/App.jsx`**.
181+
182+
#### Style customization via CSS variables
183+
184+
Pass a `customizations.style.customCSS.variables` object to `init()` to override ThoughtSpot's built-in CSS variables. The example below makes secondary buttons and menus use a yellow color scheme with rounded corners:
185+
186+
```javascript
187+
init({
188+
thoughtSpotHost: import.meta.env.VITE_TS_HOST,
189+
authType: AuthType.TrustedAuthTokenCookieless,
190+
getAuthToken: async () => {
191+
return import.meta.env.VITE_TS_AUTH_TOKEN;
192+
},
193+
customizations: {
194+
style: {
195+
customCSS: {
196+
variables: {
197+
"--ts-var-button-border-radius": "10px",
198+
"--ts-var-button--secondary-background": "#FDE9AF",
199+
"--ts-var-button--secondary--hover-background": "#FCD977",
200+
"--ts-var-menu-background": "#FDE9AF",
201+
// Full list of variables: https://developers.thoughtspot.com/docs/custom-css
202+
},
203+
},
204+
},
205+
},
206+
});
207+
```
208+
209+
#### Embed frame size
210+
211+
The `startAutoMCPFrameRenderer` call controls the dimensions of ThoughtSpot content rendered inside the chat:
212+
213+
```javascript
214+
startAutoMCPFrameRenderer({
215+
frameParams: {
216+
height: "600px",
217+
},
218+
});
219+
```
41220

42-
- It must have a `README.md` similar to [utilities/plop-templates/ts/README.md](./plop-templates/example/README.md). The example has to include the following:
43-
- A codesandbox demo URL (all examples are viewable as a codesandbox at `https://githubbox.com/thoughtspot/developer-examples/tree/main/<path>/<to>/<example>`).
221+
## Troubleshooting
44222

45-
- Link to the relevant documentation for the features, the example is trying to showcase.
223+
| Issue | Fix |
224+
|---|---|
225+
| `AZURE_OPENAI_KEY` / endpoint errors | Make sure `.env` exists in the project root and contains valid Azure OpenAI credentials |
226+
| ThoughtSpot auth errors | Verify `VITE_TS_AUTH_TOKEN` is valid and `VITE_TS_HOST` is correct |
227+
| CORS errors in browser | Ensure FastAPI server is running on port 8000 |
228+
| Blank responses | Check FastAPI logs for streaming errors |
46229

47-
- If it requires environment variables, it must have a `.env` file and instructions on how to set them up.
230+
## Learn More
48231

49-
- [Javascript] It must have a `package.json` similar to [utilities/plop-templates/ts/package.json](./utilities/plop-templates/ts/package.json). The license should be `MIT`
232+
- [OpenAI Responses API](https://platform.openai.com/docs/api-reference/responses)
233+
- [ThoughtSpot Developer Docs](https://developers.thoughtspot.com)
234+
- [ThoughtSpot Visual Embed SDK](https://developers.thoughtspot.com/docs/visual-embed-sdk)
235+
- [ThoughtSpot Custom CSS Variables](https://developers.thoughtspot.com/docs/custom-css)
236+
- [Model Context Protocol](https://modelcontextprotocol.io)

‎Screenshot.png‎

87.2 KB
Loading

‎client/index.html‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>ThoughtSpot Agent</title>
7+
</head>
8+
<body>
9+
<div id="root"></div>
10+
<script type="module" src="/src/main.jsx"></script>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)