-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathUpdateStyleTool.ts
More file actions
87 lines (77 loc) · 2.62 KB
/
Copy pathUpdateStyleTool.ts
File metadata and controls
87 lines (77 loc) · 2.62 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
81
82
83
84
85
86
87
// Copyright (c) Mapbox, Inc.
// Licensed under the MIT License.
import type { HttpRequest } from '../../utils/types.js';
import type { ToolExecutionContext } from '../../utils/tracing.js';
import { filterExpandedMapboxStyles } from '../../utils/styleUtils.js';
import { MapboxApiBasedTool } from '../MapboxApiBasedTool.js';
import {
UpdateStyleInput,
UpdateStyleInputSchema
} from './UpdateStyleTool.input.schema.js';
import { getUserNameFromToken } from '../../utils/jwtUtils.js';
import type { CallToolResult } from '@modelcontextprotocol/sdk/types.js';
import {
MapboxStyleOutputSchema,
MapboxStyleOutput
} from './UpdateStyleTool.output.schema.js';
export class UpdateStyleTool extends MapboxApiBasedTool<
typeof UpdateStyleInputSchema,
typeof MapboxStyleOutputSchema
> {
name = 'update_style_tool';
description = 'Update an existing Mapbox style';
readonly annotations = {
readOnlyHint: false,
destructiveHint: false,
idempotentHint: false,
openWorldHint: true,
title: 'Update Mapbox Style Tool'
};
constructor(params: { httpRequest: HttpRequest }) {
super({
inputSchema: UpdateStyleInputSchema,
outputSchema: MapboxStyleOutputSchema,
httpRequest: params.httpRequest
});
}
protected async execute(
input: UpdateStyleInput,
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 payload: Record<string, unknown> = {};
if (input.name) payload.name = input.name;
if (input.style) Object.assign(payload, input.style);
const response = await this.httpRequest(url, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
if (!response.ok) {
return this.handleApiError(response, 'update 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', `UpdateStyleTool: Successfully updated style ${data.id}`);
return {
content: [
{
type: 'text',
text: JSON.stringify(filterExpandedMapboxStyles(data), null, 2)
}
],
structuredContent: filterExpandedMapboxStyles(data),
isError: false
};
}
}