Skip to content

Commit cf26c91

Browse files
authored
MCP - improve string and string[] schemas (#16361)
* MCP - improve string and string[] schemas * fix spacing
1 parent 5f36ccd commit cf26c91

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

modelcontextprotocol/lib/registerComponentTools.ts

+20-17
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,27 @@ export async function registerComponentTools({
7979
appKey = cp.name
8080
continue
8181
} else if (cp.type === "string") {
82-
schema[cp.name] = z.string()
82+
if (
83+
cp.options &&
84+
Array.isArray(cp.options) &&
85+
cp.options.length > 0 &&
86+
cp.options.some((o) => typeof o === "string")
87+
) {
88+
schema[cp.name] = z.enum(cp.options)
89+
} else {
90+
schema[cp.name] = z.string()
91+
}
8392
} else if (cp.type === "string[]") {
84-
schema[cp.name] = z
85-
.union([
86-
z.string().transform((val) => {
87-
try {
88-
return JSON.parse(val)
89-
} catch {
90-
return [
91-
val,
92-
] // If not valid JSON, treat as single item array
93-
}
94-
}),
95-
z.array(z.string()),
96-
])
97-
.refine((val) => Array.isArray(val), {
98-
message: "Must be an array of strings",
99-
})
93+
if (
94+
cp.options &&
95+
Array.isArray(cp.options) &&
96+
cp.options.length > 0 &&
97+
cp.options.some((o) => o.value != null)
98+
) {
99+
schema[cp.name] = z.array(z.enum(cp.options.map((o) => o.value)))
100+
} else {
101+
schema[cp.name] = z.array(z.string())
102+
}
100103
configurablePropsDescription += `- ${cp.name}: Return JSON in this format: string[]\n`
101104
} else if (cp.type === "number") {
102105
schema[cp.name] = z.number()

0 commit comments

Comments
 (0)