Skip to content

Commit f82ceac

Browse files
committed
validate sections
1 parent 522fae6 commit f82ceac

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

packages/mcp-server/src/mcp/schemas/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,10 @@ export const documentation_sections_schema = v.record(
1010
slug: v.string(),
1111
}),
1212
);
13+
14+
export const section_schema = v.object({
15+
title: v.string(),
16+
use_cases: v.string(),
17+
slug: v.string(),
18+
url: v.string(),
19+
});

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as v from 'valibot';
2-
import { documentation_sections_schema } from '../mcp/schemas/index.js';
2+
import { documentation_sections_schema, section_schema } from '../mcp/schemas/index.js';
33
import summary_data from '../use_cases.json' with { type: 'json' };
44

55
export async function fetch_with_timeout(
@@ -25,7 +25,8 @@ export async function get_sections() {
2525
).then((res) => res.json());
2626
const validated_sections = v.safeParse(documentation_sections_schema, sections);
2727
if (!validated_sections.success) return [];
28-
return Object.entries(validated_sections.output).map(([, section]) => {
28+
29+
const mapped_sections = Object.entries(validated_sections.output).map(([, section]) => {
2930
const original_slug = section.slug;
3031
const cleaned_slug = original_slug.startsWith('docs/')
3132
? original_slug.slice('docs/'.length)
@@ -40,6 +41,14 @@ export async function get_sections() {
4041
url: `https://svelte.dev/${original_slug}/llms.txt`,
4142
};
4243
});
44+
45+
const validated_output = v.safeParse(v.array(section_schema), mapped_sections);
46+
if (!validated_output.success) {
47+
console.error('Section validation failed:', validated_output.issues);
48+
return [];
49+
}
50+
51+
return validated_output.output;
4352
}
4453

4554
export async function format_sections_list(): Promise<string> {

0 commit comments

Comments
 (0)