-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTilequeryTool.ts
More file actions
115 lines (98 loc) · 3.05 KB
/
Copy pathTilequeryTool.ts
File metadata and controls
115 lines (98 loc) · 3.05 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
// Copyright (c) Mapbox, Inc.
// Licensed under the MIT License.
import type { 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 {
TilequerySchema,
TilequeryInput
} from './TilequeryTool.input.schema.js';
import {
TilequeryResponse,
TilequeryResponseSchema
} from './TilequeryTool.output.schema.js';
export class TilequeryTool extends MapboxApiBasedTool<
typeof TilequerySchema,
typeof TilequeryResponseSchema
> {
name = 'tilequery_tool';
description =
'Query vector and raster data from Mapbox tilesets at geographic coordinates';
readonly annotations = {
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
openWorldHint: true,
title: 'Mapbox Tilequery Tool'
};
constructor(params: { httpRequest: HttpRequest }) {
super({
inputSchema: TilequerySchema,
outputSchema: TilequeryResponseSchema,
httpRequest: params.httpRequest
});
}
protected async execute(
input: TilequeryInput,
accessToken: string,
_context: ToolExecutionContext
): Promise<CallToolResult> {
const { tilesetId, longitude, latitude, ...queryParams } = input;
const url = new URL(
`${MapboxApiBasedTool.mapboxApiEndpoint}v4/${encodeURIComponent(tilesetId)}/tilequery/${longitude},${latitude}.json`
);
if (queryParams.radius !== undefined) {
url.searchParams.set('radius', queryParams.radius.toString());
}
if (queryParams.limit !== undefined) {
url.searchParams.set('limit', queryParams.limit.toString());
}
if (queryParams.dedupe !== undefined) {
url.searchParams.set('dedupe', queryParams.dedupe.toString());
}
if (queryParams.geometry) {
url.searchParams.set('geometry', queryParams.geometry);
}
if (queryParams.layers && queryParams.layers.length > 0) {
url.searchParams.set('layers', queryParams.layers.join(','));
}
if (queryParams.bands && queryParams.bands.length > 0) {
url.searchParams.set('bands', queryParams.bands.join(','));
}
url.searchParams.set('access_token', accessToken || '');
const response = await this.httpRequest(url.toString());
if (!response.ok) {
return this.handleApiError(response, 'query tile');
}
const rawData = await response.json();
let data: TilequeryResponse;
try {
data = TilequeryResponseSchema.parse(rawData);
} catch {
return {
isError: true,
content: [
{
type: 'text',
text: 'Unexpected API response format from Mapbox API'
}
]
};
}
this.log(
'info',
`TilequeryTool: Successfully completed query, found ${data.features?.length || 0} results`
);
return {
content: [
{
type: 'text',
text: JSON.stringify(data, null, 2)
}
],
structuredContent: data,
isError: false
};
}
}