-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMapboxReferenceResource.ts
More file actions
80 lines (71 loc) · 2.25 KB
/
MapboxReferenceResource.ts
File metadata and controls
80 lines (71 loc) · 2.25 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 type { RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';
import type {
ReadResourceResult,
ServerNotification,
ServerRequest
} from '@modelcontextprotocol/sdk/types.js';
import type { HttpRequest } from '../../utils/types.js';
import { BaseResource } from '../BaseResource.js';
import {
parseDocSections,
filterSectionsByCategory,
sectionsToMarkdown
} from '../utils/docParser.js';
/**
* Resource providing Mapbox reference documentation
*/
export class MapboxReferenceResource extends BaseResource {
readonly name = 'Mapbox Reference';
readonly uri = 'resource://mapbox-reference';
readonly description =
'Mapbox reference documentation including tilesets, data products, accounts, pricing, and other reference materials';
readonly mimeType = 'text/markdown';
private httpRequest: HttpRequest;
constructor(params: { httpRequest: HttpRequest }) {
super();
this.httpRequest = params.httpRequest;
}
public async readCallback(
uri: URL,
_extra: RequestHandlerExtra<ServerRequest, ServerNotification>
): Promise<ReadResourceResult> {
try {
const response = await this.httpRequest(
'https://docs.mapbox.com/llms.txt',
{
headers: {
Accept: 'text/markdown, text/plain;q=0.9, */*;q=0.8'
}
}
);
if (!response.ok) {
throw new Error(
`Failed to fetch Mapbox documentation: ${response.statusText}`
);
}
const content = await response.text();
// Parse and filter for reference sections only
const allSections = parseDocSections(content);
const referenceSections = filterSectionsByCategory(
allSections,
'reference'
);
const referenceContent = sectionsToMarkdown(referenceSections);
return {
contents: [
{
uri: uri.href,
mimeType: this.mimeType,
text: `# Mapbox Reference\n\n${referenceContent}`
}
]
};
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : 'Unknown error occurred';
throw new Error(`Failed to fetch Mapbox reference: ${errorMessage}`);
}
}
}