mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-03-05 15:50:19 +01:00
🧰 fix: Convert const to enum in MCP Schemas for Gemini Compatibility (#11784)
* fix: Convert `const` to `enum` in MCP tool schemas for Gemini/Vertex AI compatibility Gemini/Vertex AI rejects the JSON Schema `const` keyword in function declarations with a 400 error. Previously, the Zod conversion layer accidentally stripped `const`, but after migrating to pass raw JSON schemas directly to providers, the unsupported keyword now reaches Gemini verbatim. Add `normalizeJsonSchema` to recursively convert `const: X` → `enum: [X]`, which is semantically equivalent per the JSON Schema spec and supported by all providers. * fix: Update secure cookie handling in AuthService to use dynamic secure flag Replaced the static `secure: isProduction` with a call to `shouldUseSecureCookie()` in the `setOpenIDAuthTokens` function. This change ensures that the secure cookie setting is evaluated at runtime, improving cookie handling in development environments while maintaining security in production. * refactor: Simplify MCP tool key formatting and remove unused mocks in tests - Updated MCP test suite to replace static tool key formatting with a dynamic delimiter from Constants, enhancing consistency and maintainability. - Removed unused mock implementations for `@langchain/core/tools` and `@librechat/agents`, streamlining the test setup. - Adjusted related test cases to reflect the new tool key format, ensuring all tests remain functional. * chore: import order
This commit is contained in:
parent
276ac8d011
commit
ccbf9dc093
6 changed files with 287 additions and 79 deletions
|
|
@ -248,6 +248,65 @@ export function resolveJsonSchemaRefs<T extends Record<string, unknown>>(
|
|||
return result as T;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively normalizes a JSON schema by converting `const` values to `enum` arrays.
|
||||
* Gemini/Vertex AI does not support the `const` keyword in function declarations,
|
||||
* but `const: X` is semantically equivalent to `enum: [X]` per the JSON Schema spec.
|
||||
*
|
||||
* @param schema - The JSON schema to normalize
|
||||
* @returns The normalized schema with `const` converted to `enum`
|
||||
*/
|
||||
export function normalizeJsonSchema<T extends Record<string, unknown>>(schema: T): T {
|
||||
if (!schema || typeof schema !== 'object') {
|
||||
return schema;
|
||||
}
|
||||
|
||||
if (Array.isArray(schema)) {
|
||||
return schema.map((item) =>
|
||||
item && typeof item === 'object' ? normalizeJsonSchema(item) : item,
|
||||
) as unknown as T;
|
||||
}
|
||||
|
||||
const result: Record<string, unknown> = {};
|
||||
|
||||
for (const [key, value] of Object.entries(schema)) {
|
||||
if (key === 'const' && !('enum' in schema)) {
|
||||
result['enum'] = [value];
|
||||
continue;
|
||||
}
|
||||
|
||||
if (key === 'const' && 'enum' in schema) {
|
||||
// Skip `const` when `enum` already exists
|
||||
continue;
|
||||
}
|
||||
|
||||
if (key === 'properties' && value && typeof value === 'object' && !Array.isArray(value)) {
|
||||
const newProps: Record<string, unknown> = {};
|
||||
for (const [propKey, propValue] of Object.entries(value as Record<string, unknown>)) {
|
||||
newProps[propKey] =
|
||||
propValue && typeof propValue === 'object'
|
||||
? normalizeJsonSchema(propValue as Record<string, unknown>)
|
||||
: propValue;
|
||||
}
|
||||
result[key] = newProps;
|
||||
} else if (
|
||||
(key === 'items' || key === 'additionalProperties') &&
|
||||
value &&
|
||||
typeof value === 'object'
|
||||
) {
|
||||
result[key] = normalizeJsonSchema(value as Record<string, unknown>);
|
||||
} else if ((key === 'oneOf' || key === 'anyOf' || key === 'allOf') && Array.isArray(value)) {
|
||||
result[key] = value.map((item) =>
|
||||
item && typeof item === 'object' ? normalizeJsonSchema(item) : item,
|
||||
);
|
||||
} else {
|
||||
result[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return result as T;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a JSON Schema to a Zod schema.
|
||||
*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue