-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRetrieveStyleTool.ts
More file actions
80 lines (71 loc) · 2.36 KB
/
Copy pathRetrieveStyleTool.ts
File metadata and controls
80 lines (71 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) Mapbox, Inc.
// Licensed under the MIT License.
import { getUserNameFromToken } from '../../utils/jwtUtils.js';
import { filterExpandedMapboxStyles } from '../../utils/styleUtils.js';
import type { ToolExecutionContext } from '../../utils/tracing.js';
import { MapboxApiBasedTool } from '../MapboxApiBasedTool.js';
import {
RetrieveStyleSchema,
RetrieveStyleInput
} from './RetrieveStyleTool.input.schema.js';
import { HttpRequest } from '../../utils/types.js';
import { CallToolResult } from '@modelcontextprotocol/sdk/types.js';
import {
MapboxStyleOutput,
MapboxStyleOutputSchema
} from './RetrieveStyleTool.output.schema.js';
export class RetrieveStyleTool extends MapboxApiBasedTool<
typeof RetrieveStyleSchema,
typeof MapboxStyleOutputSchema
> {
name = 'retrieve_style_tool';
description = 'Retrieve a specific Mapbox style by ID';
readonly annotations = {
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
openWorldHint: true,
title: 'Retrieve Mapbox Style Tool'
};
constructor(params: { httpRequest: HttpRequest }) {
super({
inputSchema: RetrieveStyleSchema,
outputSchema: MapboxStyleOutputSchema,
httpRequest: params.httpRequest
});
}
protected async execute(
input: RetrieveStyleInput,
accessToken: string,
_context: ToolExecutionContext
): Promise<CallToolResult> {
const username = getUserNameFromToken(accessToken);
const url = `${MapboxApiBasedTool.mapboxApiEndpoint}styles/v1/${encodeURIComponent(username)}/${encodeURIComponent(input.styleId)}?access_token=${accessToken}`;
const response = await this.httpRequest(url);
if (!response.ok) {
return this.handleApiError(response, 'retrieve style');
}
const rawData = await response.json();
// Validate response against schema with graceful fallback
let data: MapboxStyleOutput;
try {
data = MapboxStyleOutputSchema.parse(rawData);
} catch (validationError) {
return this.handleValidationError(validationError);
}
this.log(
'info',
`RetrieveStyleTool: Successfully retrieved style ${data.id}`
);
return {
content: [
{
type: 'text',
text: JSON.stringify(filterExpandedMapboxStyles(data), null, 2)
}
],
structuredContent: filterExpandedMapboxStyles(data),
isError: false
};
}
}