-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathimport-example.ts
More file actions
255 lines (198 loc) · 7.22 KB
/
import-example.ts
File metadata and controls
255 lines (198 loc) · 7.22 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright (c) Mapbox, Inc.
// Licensed under the MIT License.
/**
* Examples demonstrating how to import and use Mapbox MCP Devkit tools, resources,
* prompts, and utilities in your application.
*/
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
/* =============================================================================
* SIMPLE USAGE: Pre-configured instances (recommended for most use cases)
* ============================================================================= */
// Import pre-configured tool instances with short, clean names
import {
listStyles,
createStyle,
previewStyle
} from '@mapbox/mcp-devkit-server/tools';
// Import pre-configured resource instances
import {
mapboxStyleLayers,
previewStyleUI
} from '@mapbox/mcp-devkit-server/resources';
// Import pre-configured prompt instances
import {
createAndPreviewStyle,
buildCustomMap
} from '@mapbox/mcp-devkit-server/prompts';
// Ready to use - httpRequest is already configured
async function simpleExample() {
const server = new McpServer({
name: 'my-devkit-app',
version: '1.0.0'
});
// Tools, resources, and prompts use installTo() method
listStyles.installTo(server);
createStyle.installTo(server);
previewStyle.installTo(server);
mapboxStyleLayers.installTo(server);
previewStyleUI.installTo(server);
// Prompts can be used directly - they have execute() method
// See src/index.ts for how to register prompts with the server
return server;
}
/* =============================================================================
* ADVANCED USAGE: Tool classes with default httpRequest
* ============================================================================= */
// Import tool classes for custom instantiation
import {
ListStylesTool,
CreateStyleTool,
PreviewStyleTool
} from '@mapbox/mcp-devkit-server/tools';
// Import the default httpRequest function
import { httpRequest } from '@mapbox/mcp-devkit-server/utils';
// Create tools with default pipeline but custom configuration
async function advancedExample() {
const server = new McpServer({
name: 'my-custom-devkit-app',
version: '1.0.0'
});
// Instantiate tools with default httpRequest
const myListStyles = new ListStylesTool({ httpRequest });
const myCreateStyle = new CreateStyleTool({ httpRequest });
const myPreviewStyle = new PreviewStyleTool();
myListStyles.installTo(server);
myCreateStyle.installTo(server);
myPreviewStyle.installTo(server);
return server;
}
/* =============================================================================
* EXPERT USAGE: Custom HTTP pipeline with policies
* ============================================================================= */
// Import HTTP pipeline components
import {
HttpPipeline,
UserAgentPolicy,
RetryPolicy
} from '@mapbox/mcp-devkit-server/utils';
import type { HttpRequest } from '@mapbox/mcp-devkit-server/utils';
// Create a custom HTTP pipeline
function createCustomPipeline(): HttpRequest {
const pipeline = new HttpPipeline();
// Add custom User-Agent
pipeline.usePolicy(new UserAgentPolicy('MyDevkitApp/2.0.0'));
// Add aggressive retry policy: 5 attempts, 300ms min, 3000ms max backoff
pipeline.usePolicy(new RetryPolicy(5, 300, 3000));
return pipeline.execute.bind(pipeline);
}
async function expertExample() {
const server = new McpServer({
name: 'my-expert-devkit-app',
version: '1.0.0'
});
// Use custom pipeline
const customHttpRequest = createCustomPipeline();
// Create tools with custom pipeline
const myListStyles = new ListStylesTool({ httpRequest: customHttpRequest });
const myCreateStyle = new CreateStyleTool({
httpRequest: customHttpRequest
});
myListStyles.installTo(server);
myCreateStyle.installTo(server);
return server;
}
/* =============================================================================
* REGISTRY FUNCTIONS: Batch operations
* ============================================================================= */
// Import registry functions for batch access
import {
getCoreTools,
getElicitationTools
} from '@mapbox/mcp-devkit-server/tools';
import { getAllResources } from '@mapbox/mcp-devkit-server/resources';
import { getAllPrompts } from '@mapbox/mcp-devkit-server/prompts';
async function registryExample() {
const server = new McpServer({
name: 'my-registry-app',
version: '1.0.0'
});
// Register all core tools at once
const coreTools = getCoreTools();
for (const tool of coreTools) {
tool.installTo(server);
}
// Register all elicitation tools
const elicitationTools = getElicitationTools();
for (const tool of elicitationTools) {
tool.installTo(server);
}
// Register all resources
const resources = getAllResources();
for (const resource of resources) {
resource.installTo(server);
}
// Get all prompts - they can be used directly or registered
// See src/index.ts for how to register prompts with the server
const _prompts = getAllPrompts();
return server;
}
/* =============================================================================
* TYPE-SAFE USAGE: Import types
* ============================================================================= */
import type { HttpPolicy } from '@mapbox/mcp-devkit-server/utils';
import type { ToolInstance } from '@mapbox/mcp-devkit-server/tools';
import type { ResourceInstance } from '@mapbox/mcp-devkit-server/resources';
import type { PromptInstance } from '@mapbox/mcp-devkit-server/prompts';
// Create custom policy
class _CustomLoggingPolicy implements HttpPolicy {
readonly id = 'custom-logging';
async handle(
input: string | URL | Request,
init: RequestInit,
next: HttpRequest
): Promise<Response> {
const url = input instanceof Request ? input.url : input.toString();
console.log(`Request: ${url}`);
const response = await next(input, init);
console.log(`Response: ${response.status}`);
return response;
}
}
// Type-safe tool handling
function processTool(tool: ToolInstance) {
console.log(`Processing tool: ${tool.name}`);
// Tool instances have installTo() method
}
function processResource(resource: ResourceInstance) {
console.log(`Processing resource: ${resource.uri}`);
// Resource instances have installTo() method
}
function processPrompt(prompt: PromptInstance) {
console.log(`Processing prompt: ${prompt.name}`);
// Prompt instances can be executed directly or registered via server.registerPrompt()
}
// Use them
processTool(listStyles);
processTool(createStyle);
processResource(mapboxStyleLayers);
processResource(previewStyleUI);
processPrompt(createAndPreviewStyle);
processPrompt(buildCustomMap);
/* =============================================================================
* MAIN: Run examples
* ============================================================================= */
async function main() {
console.log('Simple example:');
await simpleExample();
console.log('\nAdvanced example:');
await advancedExample();
console.log('\nExpert example:');
await expertExample();
console.log('\nRegistry example:');
await registryExample();
console.log('\nAll examples completed successfully!');
}
// Run if executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
main().catch(console.error);
}