Skip to content

Commit 60c1e09

Browse files
committed
create single implementation for getTokenURL
1 parent ff6b108 commit 60c1e09

File tree

4 files changed

+31
-44
lines changed

4 files changed

+31
-44
lines changed

src/servers/mcp-server-base.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { z } from "zod";
1111
import { context, type Span, SpanStatusCode } from "@opentelemetry/api";
1212
import { getActiveSpan, withSpan } from "../metrics/tracing/tracing-utils";
1313
import { Trackers, type Tracker, TrackEvent } from "../metrics";
14-
import type { Props } from "../utils";
14+
import { putInKV, type Props } from "../utils";
1515
import { MixpanelTracker } from "../metrics/mixpanel/mixpanel";
1616
import { getThoughtSpotClient } from "../thoughtspot/thoughtspot-client";
1717
import { ThoughtSpotService } from "../thoughtspot/thoughtspot-service";
@@ -148,6 +148,24 @@ export abstract class BaseMCPServer extends Server {
148148
this.addTracker(mixpanel);
149149
}
150150

151+
protected async getTokenUrl(sessionId: string, generationNo: number) {
152+
let tokenUrl = "";
153+
// Generate token and store in KV store
154+
if (this.ctx.env?.OAUTH_KV) {
155+
console.log("[DEBUG] Storing token in KV");
156+
const token = crypto.randomUUID();
157+
const tokenData = {
158+
sessionId: sessionId,
159+
generationNo: generationNo,
160+
instanceURL: this.ctx.props.instanceUrl,
161+
accessToken: this.ctx.props.accessToken
162+
};
163+
await putInKV(token, tokenData, this.ctx.env);
164+
tokenUrl = `${this.ctx.env?.HOST_NAME}/data/img?uniqueId=${token}`;
165+
}
166+
return tokenUrl;
167+
}
168+
151169
/**
152170
* Abstract method to be implemented by subclasses for listing tools
153171
*/

src/servers/mcp-server.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
import { z } from "zod";
77
import { zodToJsonSchema } from "zod-to-json-schema";
88

9-
import { McpServerError, putInKV } from "../utils";
9+
import { McpServerError } from "../utils";
1010
import type {
1111
DataSource
1212
} from "../thoughtspot/thoughtspot-service";
@@ -224,21 +224,7 @@ export class MCPServer extends BaseMCPServer {
224224
const { question, datasourceId: sourceId } = GetAnswerSchema.parse(request.params.arguments);
225225

226226
const answer = await this.getThoughtSpotService().getAnswerForQuestion(question, sourceId, false);
227-
let tokenUrl = "";
228-
229-
// Generate token and store in KV store
230-
if (!answer.error && this.ctx.env?.OAUTH_KV) {
231-
console.log("[DEBUG] Storing token in KV");
232-
const token = crypto.randomUUID();
233-
const tokenData = {
234-
sessionId: answer.session_identifier,
235-
generationNo: answer.generation_number,
236-
instanceURL: this.ctx.props.instanceUrl,
237-
accessToken: this.ctx.props.accessToken
238-
};
239-
await putInKV(token, tokenData, this.ctx.env);
240-
tokenUrl = `${this.ctx.env?.HOST_NAME}/data/img?token=${token}`;
241-
}
227+
const tokenUrl = await this.getTokenUrl(answer.session_identifier, answer.generation_number);
242228

243229
if (answer.error) {
244230
return this.createErrorResponse(answer.error.message, `Error getting answer ${answer.error.message}`);

src/servers/openai-mcp-server.ts

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { zodToJsonSchema } from "zod-to-json-schema";
88
import { BaseMCPServer, type Context } from "./mcp-server-base";
99
import { z } from "zod";
1010
import { WithSpan } from "../metrics/tracing/tracing-utils";
11-
import { putInKV } from "../utils";
1211

1312
const ToolInputSchema = ToolSchema.shape.inputSchema;
1413
type ToolInput = z.infer<typeof ToolInputSchema>;
@@ -134,37 +133,20 @@ export class OpenAIDeepResearchMCPServer extends BaseMCPServer {
134133
const [datasourceId, question = ""] = id.split(":");
135134
const thoughtSpotService = this.getThoughtSpotService();
136135
const answer = await thoughtSpotService.getAnswerForQuestion(question, datasourceId, false);
137-
const image = await thoughtSpotService.getAnswerImagePNG(answer.session_identifier, answer.generation_number);
138-
const imageBuffer = await image.arrayBuffer();
139136
if (answer.error) {
140137
return this.createErrorResponse(answer.error.message, `Error getting answer ${answer.error.message}`);
141138
}
142139

143-
let tokenUrl = "";
140+
const tokenUrl = await this.getTokenUrl(answer.session_identifier, answer.generation_number);
144141

145-
// Generate token and store in KV store
146-
if (!answer.error && this.ctx.env?.OAUTH_KV) {
147-
console.log("[DEBUG] Storing token in KV");
148-
const token = crypto.randomUUID();
149-
const tokenData = {
150-
sessionId: answer.session_identifier,
151-
generationNo: answer.generation_number,
152-
instanceURL: this.ctx.props.instanceUrl,
153-
accessToken: this.ctx.props.accessToken
154-
};
155-
await putInKV(token, tokenData, this.ctx.env);
156-
tokenUrl = `${this.ctx.env?.HOST_NAME}/data/img?token=${token}`;
157-
}
158-
let content = answer.data;
159-
if (tokenUrl) {
160-
content = `Data: ${answer.data}
142+
const content = `Data: ${answer.data}
161143
162-
**CHART VISUALIZATION AVAILABLE:**
163-
There is a data visualization chart for this query available at: "${tokenUrl}".
144+
**CHART VISUALIZATION AVAILABLE:**
145+
There is a data visualization chart for this query available at: "${tokenUrl}".
164146
165-
This URL contains a PNG image showing the visual representation of the above data for the question: "${question}". Please fetch and display this image to provide a complete analysis with both the data and its visual representation. The image must be displayed in the response of this question.
166-
Use just this image in the response, do not use any other images or visualizations or charts. It may take time to get the image, so please wait for the image to be fetched and displayed in the response.`;
167-
}
147+
This URL contains a PNG image showing the visual representation of the above data for the question: "${question}". Please fetch and display this image to provide a complete analysis with both the data and its visual representation. The image must be displayed in the response of this question.
148+
Use just this image in the response, do not use any other images or visualizations or charts. It may take time to get the image, so please wait for the image to be fetched and displayed in the response.`;
149+
168150
console.log(`[DEBUG] question: ${question} tokenUrl: ${tokenUrl}`);
169151
const result = {
170152
id,

wrangler.jsonc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* For more details on how to configure Wrangler, refer to:
33
* https://developers.cloudflare.com/workers/wrangler/configuration/
44
*/
5-
{
5+
{
6+
"keep_vars": true,
67
"$schema": "node_modules/wrangler/config-schema.json",
78
"name": "thoughtspot-mcp-server",
89
"main": "src/index.ts",
@@ -48,4 +49,4 @@
4849
{ "binding": "ANALYTICS", "dataset": "mcp_events" }
4950
],
5051
"assets": { "directory": "./static/", "binding": "ASSETS" },
51-
}
52+
}

0 commit comments

Comments
 (0)