fix(core): normalize MCP tool schemas to root type object#27888
fix(core): normalize MCP tool schemas to root type object#27888KirtiRamchandani wants to merge 1 commit into
Conversation
Third-party MCP servers often omit type:'object' on tool input schemas. Normalize schemas at MCP and extension discovery so strict providers reject fewer tool registrations. Fixes google-gemini#23382
|
📊 PR Size: size/M
|
🛑 Action Required: Evaluation ApprovalSteering changes have been detected in this PR. To prevent regressions, a maintainer must approve the evaluation run before this PR can be merged. Maintainers:
Once approved, the evaluation results will be posted here automatically. |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses compatibility issues with strict JSON Schema validators that reject MCP tool schemas lacking an explicit root 'object' type. By implementing a normalization utility, the system now guarantees that all tool schemas conform to expected standards, ensuring better interoperability with downstream APIs and services. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a normalizeToolSchema utility to ensure tool parameter schemas always declare a root type: "object" for strict JSON Schema validation, integrating it into DiscoveredMCPTool and ToolRegistry. The review feedback identifies a critical bug where valid array-based type definitions (e.g., ["object", "null"]) are discarded, and provides a robust implementation to handle array types, preserve properties, and ensure properties is always present. It also suggests expanding the test suite to cover these edge cases.
| export function normalizeToolSchema(schema: unknown): Record<string, unknown> { | ||
| if (schema == null) { | ||
| return { type: 'object', properties: {} }; | ||
| } | ||
|
|
||
| if (typeof schema !== 'object' || Array.isArray(schema)) { | ||
| return { type: 'object', properties: {} }; | ||
| } | ||
|
|
||
| const objectSchema: Record<string, unknown> = { ...schema }; | ||
| if (objectSchema['type'] === 'object') { | ||
| return objectSchema; | ||
| } | ||
|
|
||
| if (objectSchema['type'] === undefined) { | ||
| return { ...objectSchema, type: 'object' }; | ||
| } | ||
|
|
||
| return { type: 'object', properties: {} }; | ||
| } |
There was a problem hiding this comment.
If the input schema defines type as an array (e.g., type: ["object", "null"] or type: ["object"]), which is valid in JSON Schema, the current implementation will fall through to the default case and return { type: 'object', properties: {} }. This completely discards all the tool's properties and parameters, making the tool unusable.
We should handle array types containing 'object' and also preserve the schema if properties is defined (which strongly indicates it is intended to be an object schema). Additionally, we can optimize this by returning the original schema reference if it is already valid, and ensure that properties is always present (even if empty) to satisfy strict downstream validators.
export function normalizeToolSchema(schema: unknown): Record<string, unknown> {
if (schema == null) {
return { type: 'object', properties: {} };
}
if (typeof schema !== 'object' || Array.isArray(schema)) {
return { type: 'object', properties: {} };
}
const rawSchema = schema as Record<string, unknown>;
if (rawSchema['type'] === 'object') {
return rawSchema;
}
const type = rawSchema['type'];
if (
type === undefined ||
(Array.isArray(type) && type.includes('object')) ||
rawSchema['properties'] !== undefined
) {
return {
properties: {},
...rawSchema,
type: 'object',
};
}
return { type: 'object', properties: {} };
}| it('wraps non-object root types', () => { | ||
| expect(normalizeToolSchema({ type: 'string' })).toEqual({ | ||
| type: 'object', | ||
| properties: {}, | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Let's add test cases to verify the new normalization behaviors, including handling array types containing 'object', preserving properties when the type is mismatched, and ensuring properties is always present when the type is 'object'.
it('wraps non-object root types', () => {
expect(normalizeToolSchema({ type: 'string' })).toEqual({
type: 'object',
properties: {},
});
});
it('handles array types containing object', () => {
expect(
normalizeToolSchema({
type: ['object', 'null'],
properties: { foo: { type: 'string' } },
}),
).toEqual({
type: 'object',
properties: { foo: { type: 'string' } },
});
});
it('preserves properties even if type is mismatched', () => {
expect(
normalizeToolSchema({
type: 'string',
properties: { foo: { type: 'string' } },
}),
).toEqual({
type: 'object',
properties: { foo: { type: 'string' } },
});
});
it('ensures properties is always present when type is object', () => {
expect(normalizeToolSchema({})).toEqual({
type: 'object',
properties: {},
});
});
Problem
MCP servers can advertise tool input schemas without a root
type: "object". Strict JSON Schema validators (Vertex AI strict mode, downstream APIs) reject these with errors like:Root cause
DiscoveredMCPToolandDiscoveredToolforwarded raw MCP/extension schemas without normalization.Fix
normalizeToolSchema()inschemaValidator.tsmcp-tool.ts) and extension discovery (tool-registry.ts)type: "object"when missing; wrap invalid rootsTesting
schemaValidator.test.ts(16 passing)Fixes #23382