-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCreateTokenTool.ts
More file actions
123 lines (108 loc) · 3.07 KB
/
Copy pathCreateTokenTool.ts
File metadata and controls
123 lines (108 loc) · 3.07 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) Mapbox, Inc.
// Licensed under the MIT License.
import { CallToolResult } from '@modelcontextprotocol/sdk/types.js';
import type { HttpRequest } from '../../utils/types.js';
import type { ToolExecutionContext } from '../../utils/tracing.js';
import { MapboxApiBasedTool } from '../MapboxApiBasedTool.js';
import {
CreateTokenSchema,
CreateTokenInput
} from './CreateTokenTool.input.schema.js';
import { CreateTokenOutputSchema } from './CreateTokenTool.output.schema.js';
import { getUserNameFromToken } from '../../utils/jwtUtils.js';
export class CreateTokenTool extends MapboxApiBasedTool<
typeof CreateTokenSchema,
typeof CreateTokenOutputSchema
> {
readonly name = 'create_token_tool';
readonly description =
'Create a new Mapbox public access token with specified scopes and optional URL restrictions.';
readonly annotations = {
readOnlyHint: false,
destructiveHint: false,
idempotentHint: false,
openWorldHint: true,
title: 'Create Mapbox Token Tool'
};
constructor(params: { httpRequest: HttpRequest }) {
super({
inputSchema: CreateTokenSchema,
outputSchema: CreateTokenOutputSchema,
httpRequest: params.httpRequest
});
}
protected async execute(
input: CreateTokenInput,
accessToken: string,
_context: ToolExecutionContext
): Promise<CallToolResult> {
const username = getUserNameFromToken(accessToken);
this.log(
'info',
`CreateTokenTool: Creating public token with note: "${input.note}", scopes: ${JSON.stringify(input.scopes)}`
);
const url = `${MapboxApiBasedTool.mapboxApiEndpoint}tokens/v2/${username}?access_token=${accessToken}`;
const body: {
note: string;
scopes: string[];
allowedUrls?: string[];
expires?: string;
} = {
note: input.note,
scopes: input.scopes
};
if (input.allowedUrls) {
if (input.allowedUrls.length > 100) {
return {
content: [
{
type: 'text',
text: 'Maximum 100 allowed URLs per token'
}
],
isError: true
};
}
body.allowedUrls = input.allowedUrls;
}
if (input.expires) {
body.expires = input.expires;
}
const response = await this.httpRequest(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
});
if (!response.ok) {
return this.handleApiError(response, 'create token');
}
const rawData = await response.json();
let data;
try {
data = CreateTokenOutputSchema.parse(rawData);
} catch {
return {
isError: true,
content: [
{
type: 'text',
text: 'Unexpected API response format from Mapbox API'
}
]
};
}
this.log('info', `CreateTokenTool: Successfully created token`);
return {
content: [
{
type: 'text',
text: JSON.stringify(data, null, 2)
}
],
structuredContent: data,
isError: false
};
}
}