Skip to content

Commit 4992d04

Browse files
committed
fix token issues
1 parent bdeee5e commit 4992d04

File tree

15 files changed

+92
-95
lines changed

15 files changed

+92
-95
lines changed

src/tools/MapboxApiBasedTool.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import { BaseTool, OutputSchema } from './BaseTool.js';
55
export abstract class MapboxApiBasedTool<
66
InputSchema extends ZodTypeAny
77
> extends BaseTool<InputSchema> {
8-
static readonly MAPBOX_ACCESS_TOKEN = process.env.MAPBOX_ACCESS_TOKEN;
9-
static readonly MAPBOX_API_ENDPOINT =
10-
process.env.MAPBOX_API_ENDPOINT || 'https://api.mapbox.com/';
8+
static get mapboxAccessToken() {
9+
return process.env.MAPBOX_ACCESS_TOKEN;
10+
}
11+
12+
static get mapboxApiEndpoint() {
13+
return process.env.MAPBOX_API_ENDPOINT || 'https://api.mapbox.com/';
14+
}
1115

1216
constructor(params: { inputSchema: InputSchema }) {
1317
super(params);
@@ -20,12 +24,12 @@ export abstract class MapboxApiBasedTool<
2024
*/
2125
static getUserNameFromToken(access_token?: string): string {
2226
if (!access_token) {
23-
if (!MapboxApiBasedTool.MAPBOX_ACCESS_TOKEN) {
27+
if (!MapboxApiBasedTool.mapboxAccessToken) {
2428
throw new Error(
2529
'No access token provided. Please set MAPBOX_ACCESS_TOKEN environment variable or pass it as an argument.'
2630
);
2731
}
28-
access_token = MapboxApiBasedTool.MAPBOX_ACCESS_TOKEN;
32+
access_token = MapboxApiBasedTool.mapboxAccessToken;
2933
}
3034

3135
try {
@@ -84,7 +88,7 @@ export abstract class MapboxApiBasedTool<
8488
// In the streamableHttp, the authInfo is injected into extra from `req.auth`
8589
// https://github.com/modelcontextprotocol/typescript-sdk/blob/main/src/server/streamableHttp.ts#L405
8690
const authToken = extra?.authInfo?.token;
87-
const accessToken = authToken || MapboxApiBasedTool.MAPBOX_ACCESS_TOKEN;
91+
const accessToken = authToken || MapboxApiBasedTool.mapboxAccessToken;
8892
if (!accessToken) {
8993
throw new Error(
9094
'No access token available. Please provide via Bearer auth or MAPBOX_ACCESS_TOKEN env var'

src/tools/create-style-tool/CreateStyleTool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class CreateStyleTool extends MapboxApiBasedTool<
2020
accessToken?: string
2121
): Promise<any> {
2222
const username = MapboxApiBasedTool.getUserNameFromToken(accessToken);
23-
const url = `${MapboxApiBasedTool.MAPBOX_API_ENDPOINT}styles/v1/${username}?access_token=${accessToken}`;
23+
const url = `${MapboxApiBasedTool.mapboxApiEndpoint}styles/v1/${username}?access_token=${accessToken}`;
2424

2525
const payload = {
2626
name: input.name,

src/tools/create-token-tool/CreateTokenTool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class CreateTokenTool extends MapboxApiBasedTool<
2727
`CreateTokenTool: Creating public token with note: "${input.note}", scopes: ${JSON.stringify(input.scopes)}`
2828
);
2929

30-
const url = `${MapboxApiBasedTool.MAPBOX_API_ENDPOINT}tokens/v2/${username}?access_token=${accessToken}`;
30+
const url = `${MapboxApiBasedTool.mapboxApiEndpoint}tokens/v2/${username}?access_token=${accessToken}`;
3131

3232
const body: {
3333
note: string;

src/tools/delete-style-tool/DeleteStyleTool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class DeleteStyleTool extends MapboxApiBasedTool<
1919
accessToken?: string
2020
): Promise<any> {
2121
const username = MapboxApiBasedTool.getUserNameFromToken(accessToken);
22-
const url = `${MapboxApiBasedTool.MAPBOX_API_ENDPOINT}styles/v1/${username}/${input.styleId}?access_token=${accessToken}`;
22+
const url = `${MapboxApiBasedTool.mapboxApiEndpoint}styles/v1/${username}/${input.styleId}?access_token=${accessToken}`;
2323

2424
const response = await fetch(url, {
2525
method: 'DELETE'

src/tools/list-styles-tool/ListStylesTool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class ListStylesTool extends MapboxApiBasedTool<
3434
params.append('start', input.start);
3535
}
3636

37-
const url = `${MapboxApiBasedTool.MAPBOX_API_ENDPOINT}styles/v1/${username}?${params.toString()}`;
37+
const url = `${MapboxApiBasedTool.mapboxApiEndpoint}styles/v1/${username}?${params.toString()}`;
3838

3939
const response = await this.fetchImpl(url);
4040

src/tools/list-tokens-tool/ListTokensTool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class ListTokensTool extends MapboxApiBasedTool<
4949
}
5050

5151
let url: string | null =
52-
`${MapboxApiBasedTool.MAPBOX_API_ENDPOINT}tokens/v2/${username}?${params.toString()}`;
52+
`${MapboxApiBasedTool.mapboxApiEndpoint}tokens/v2/${username}?${params.toString()}`;
5353
const allTokens: unknown[] = [];
5454
let pageCount = 0;
5555
let nextPageUrl: string | null = null;

src/tools/preview-style-tool/PreviewStyleTool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class PreviewStyleTool extends BaseTool<typeof PreviewStyleSchema> {
4141
const hashFragment =
4242
hashParams.length > 0 ? `#${hashParams.join('/')}` : '';
4343

44-
const url = `${MapboxApiBasedTool.MAPBOX_API_ENDPOINT}styles/v1/${username}/${input.styleId}.html?${params.toString()}${hashFragment}`;
44+
const url = `${MapboxApiBasedTool.mapboxApiEndpoint}styles/v1/${username}/${input.styleId}.html?${params.toString()}${hashFragment}`;
4545

4646
return {
4747
type: 'text',

src/tools/retrieve-style-tool/RetrieveStyleTool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class RetrieveStyleTool extends MapboxApiBasedTool<
1919
accessToken?: string
2020
): Promise<any> {
2121
const username = MapboxApiBasedTool.getUserNameFromToken(accessToken);
22-
const url = `${MapboxApiBasedTool.MAPBOX_API_ENDPOINT}styles/v1/${username}/${input.styleId}?access_token=${accessToken}`;
22+
const url = `${MapboxApiBasedTool.mapboxApiEndpoint}styles/v1/${username}/${input.styleId}?access_token=${accessToken}`;
2323

2424
const response = await fetch(url);
2525

src/tools/tilequery-tool/TilequeryTool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class TilequeryTool extends MapboxApiBasedTool<typeof TilequerySchema> {
1616
): Promise<any> {
1717
const { tilesetId, longitude, latitude, ...queryParams } = input;
1818
const url = new URL(
19-
`${MapboxApiBasedTool.MAPBOX_API_ENDPOINT}v4/${tilesetId}/tilequery/${longitude},${latitude}.json`
19+
`${MapboxApiBasedTool.mapboxApiEndpoint}v4/${tilesetId}/tilequery/${longitude},${latitude}.json`
2020
);
2121

2222
if (queryParams.radius !== undefined) {

src/tools/update-style-tool/UpdateStyleTool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class UpdateStyleTool extends MapboxApiBasedTool<
1919
accessToken?: string
2020
): Promise<any> {
2121
const username = MapboxApiBasedTool.getUserNameFromToken(accessToken);
22-
const url = `${MapboxApiBasedTool.MAPBOX_API_ENDPOINT}styles/v1/${username}/${input.styleId}?access_token=${accessToken}`;
22+
const url = `${MapboxApiBasedTool.mapboxApiEndpoint}styles/v1/${username}/${input.styleId}?access_token=${accessToken}`;
2323

2424
const payload: any = {};
2525
if (input.name) payload.name = input.name;

0 commit comments

Comments
 (0)