Skip to content

Commit a36d0d1

Browse files
committed
fix: validate response and default use_cases
1 parent 70f14bd commit a36d0d1

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as v from 'valibot';
2+
3+
export const documentation_sections_schema = v.record(
4+
v.string(),
5+
v.object({
6+
metadata: v.object({
7+
title: v.string(),
8+
use_cases: v.optional(v.string()),
9+
}),
10+
slug: v.string(),
11+
}),
12+
);

packages/mcp-server/src/mcp/utils.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import * as v from 'valibot';
2+
import { documentation_sections_schema } from '../mcp/schemas/index.js';
3+
14
export async function fetch_with_timeout(
25
url: string,
36
timeout_ms: number = 10000,
@@ -14,10 +17,14 @@ export async function fetch_with_timeout(
1417
}
1518

1619
export async function get_sections() {
17-
const sections = await fetch_with_timeout('https://svelte.dev/docs/experimental/sections.json');
18-
return Object.entries(sections).map(([, section]) => ({
20+
const sections = await fetch_with_timeout(
21+
'https://svelte.dev/docs/experimental/sections.json',
22+
).then((res) => res.json());
23+
const validated_sections = v.safeParse(documentation_sections_schema, sections);
24+
if (!validated_sections.success) return [];
25+
return Object.entries(validated_sections.output).map(([, section]) => ({
1926
title: section.metadata.title,
20-
use_cases: '',
27+
use_cases: section.metadata.use_cases ?? 'read document for use cases',
2128
slug: section.slug,
2229
url: `https://svelte.dev/${section.slug}/llms.txt`,
2330
}));

pnpm-lock.yaml

Lines changed: 4 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)