Skip to content

fix(core): normalize MCP tool schemas to root type object#27888

Open
KirtiRamchandani wants to merge 1 commit into
google-gemini:mainfrom
KirtiRamchandani:fix/mcp-tool-schema-normalize
Open

fix(core): normalize MCP tool schemas to root type object#27888
KirtiRamchandani wants to merge 1 commit into
google-gemini:mainfrom
KirtiRamchandani:fix/mcp-tool-schema-normalize

Conversation

@KirtiRamchandani

Copy link
Copy Markdown

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:

tools.N.custom.input_schema.type: Input should be 'object'

Root cause

DiscoveredMCPTool and DiscoveredTool forwarded raw MCP/extension schemas without normalization.

Fix

  • Add normalizeToolSchema() in schemaValidator.ts
  • Apply at MCP discovery (mcp-tool.ts) and extension discovery (tool-registry.ts)
  • Preserve valid object schemas; inject type: "object" when missing; wrap invalid roots

Testing

  • Added unit tests in schemaValidator.test.ts (16 passing)

Fixes #23382

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
@KirtiRamchandani KirtiRamchandani requested review from a team as code owners June 13, 2026 19:39
@github-actions github-actions Bot added the size/m A medium sized PR label Jun 13, 2026
@github-actions

Copy link
Copy Markdown

📊 PR Size: size/M

  • Lines changed: 90
  • Additions: +82
  • Deletions: -8
  • Files changed: 4

@github-actions

Copy link
Copy Markdown

🛑 Action Required: Evaluation Approval

Steering changes have been detected in this PR. To prevent regressions, a maintainer must approve the evaluation run before this PR can be merged.

Maintainers:

  1. Go to the Workflow Run Summary.
  2. Click the yellow 'Review deployments' button.
  3. Select the 'eval-gate' environment and click 'Approve'.

Once approved, the evaluation results will be posted here automatically.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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

  • Schema Normalization: Introduced a new normalizeToolSchema utility function to ensure all tool parameter schemas declare a root type: 'object'.
  • MCP and Extension Integration: Updated DiscoveredMCPTool and ToolRegistry to apply the normalization function during tool discovery, preventing validation errors in strict environments.
  • Testing: Added comprehensive unit tests in schemaValidator.test.ts to verify schema normalization behavior for various input types.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +141 to +160
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: {} };
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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: {} };
}

Comment on lines +255 to +260
it('wraps non-object root types', () => {
expect(normalizeToolSchema({ type: 'string' })).toEqual({
type: 'object',
properties: {},
});
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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: {},
    });
  });

@gemini-cli gemini-cli Bot added priority/p2 Important but can be addressed in a future release. area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality labels Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality priority/p2 Important but can be addressed in a future release. size/m A medium sized PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(core): MCP tool schemas with missing type:'object' cause provider rejection

1 participant