Skip to content

Commit a281ef4

Browse files
committed
fix: use server.template with list and complete for docs resources
1 parent 5bc812e commit a281ef4

File tree

1 file changed

+54
-25
lines changed

1 file changed

+54
-25
lines changed

packages/mcp-server/src/mcp/handlers/resources/list-sections.ts

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,61 @@ import { get_sections, fetch_with_timeout } from '../../utils.js';
44
export async function list_sections(server: SvelteMcp) {
55
const sections = await get_sections();
66

7-
sections.forEach((section) => {
8-
const section_name = section.title.toLowerCase().replace(/\s+/g, '-');
9-
const resource_name = `docs/svelte/${section_name}`;
10-
const resource_uri = `svelte://docs/${section_name}`;
11-
12-
server.resource(
13-
{
14-
name: resource_name,
15-
enabled: () => true,
16-
description: section.use_cases,
17-
uri: resource_uri,
18-
title: section.title,
7+
server.template(
8+
{
9+
name: 'Svelte Doc Section',
10+
description: 'A single documentation section',
11+
list() {
12+
return sections.map((section) => {
13+
const section_name = section.slug;
14+
const resource_name = section_name;
15+
const resource_uri = `svelte://${section_name}.md`;
16+
return {
17+
name: resource_name,
18+
description: section.use_cases,
19+
uri: resource_uri,
20+
title: section.title,
21+
};
22+
});
1923
},
20-
async (uri) => {
21-
const response = await fetch_with_timeout(section.url);
22-
const content = await response.text();
23-
return {
24-
contents: [
25-
{
26-
uri,
27-
type: 'text',
28-
text: content,
24+
complete: {
25+
slug: (query) => {
26+
const values = sections
27+
.reduce<string[]>((acc, section) => {
28+
const section_name = section.slug;
29+
const resource_name = section_name;
30+
if (section_name.includes(query.toLowerCase())) {
31+
acc.push(resource_name);
32+
}
33+
return acc;
34+
}, [])
35+
// there's a hard limit of 100 for completions
36+
.slice(0, 100);
37+
return {
38+
completion: {
39+
values,
2940
},
30-
],
31-
};
41+
};
42+
},
3243
},
33-
);
34-
});
44+
uri: 'svelte://{/slug*}.md',
45+
},
46+
async (uri, { slug }) => {
47+
const section = sections.find((section) => {
48+
return slug === section.slug;
49+
});
50+
if (!section) throw new Error(`Section not found: ${slug}`);
51+
const response = await fetch_with_timeout(section.url);
52+
const content = await response.text();
53+
return {
54+
contents: [
55+
{
56+
uri,
57+
type: 'text',
58+
text: content,
59+
},
60+
],
61+
};
62+
},
63+
);
3564
}

0 commit comments

Comments
 (0)