Skip to content

Commit 6df2dd6

Browse files
sararobcopybara-github
authored andcommitted
feat: Support additional schema types for MCP tools (fixes #1807)
PiperOrigin-RevId: 840722317
1 parent 82dc136 commit 6df2dd6

File tree

5 files changed

+118
-151
lines changed

5 files changed

+118
-151
lines changed

google/genai/_mcp_utils.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ def mcp_to_gemini_tool(tool: McpTool) -> types.Tool:
4242
function_declarations=[{
4343
"name": tool.name,
4444
"description": tool.description,
45-
"parameters": types.Schema.from_json_schema(
46-
json_schema=types.JSONSchema(
47-
**_filter_to_supported_schema(tool.inputSchema)
48-
)
49-
),
45+
"parameters_json_schema": tool.inputSchema,
5046
}]
5147
)
5248

google/genai/tests/live/test_live.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,11 +1141,11 @@ async def test_bidi_setup_to_api_with_config_mcp_tools(
11411141
'model': 'models/test_model',
11421142
'tools': [{
11431143
'functionDeclarations': [{
1144-
'parameters': {
1145-
'type': 'OBJECT',
1144+
'parametersJsonSchema': {
1145+
'type': 'object',
11461146
'properties': {
11471147
'location': {
1148-
'type': 'STRING',
1148+
'type': 'string',
11491149
},
11501150
},
11511151
},
@@ -1167,11 +1167,11 @@ async def test_bidi_setup_to_api_with_config_mcp_tools(
11671167
),
11681168
'tools': [{
11691169
'functionDeclarations': [{
1170-
'parameters': {
1171-
'type': 'OBJECT',
1170+
'parametersJsonSchema': {
1171+
'type': 'object',
11721172
'properties': {
11731173
'location': {
1174-
'type': 'STRING',
1174+
'type': 'string',
11751175
},
11761176
},
11771177
},
@@ -1197,6 +1197,10 @@ async def test_bidi_setup_to_api_with_config_mcp_tools(
11971197
],
11981198
},
11991199
)
1200+
1201+
print(result, 'hello result!!')
1202+
print(expected_result_vertexai, 'hello expected_result_vertexai!!')
1203+
print(expected_result_googleai, 'hello expected_result_googleai!!')
12001204

12011205
assert (
12021206
result == expected_result_vertexai
@@ -1238,11 +1242,11 @@ async def list_tools(self):
12381242
'model': 'models/test_model',
12391243
'tools': [{
12401244
'functionDeclarations': [{
1241-
'parameters': {
1242-
'type': 'OBJECT',
1245+
'parametersJsonSchema': {
1246+
'type': 'object',
12431247
'properties': {
12441248
'location': {
1245-
'type': 'STRING',
1249+
'type': 'string',
12461250
},
12471251
},
12481252
},
@@ -1264,11 +1268,11 @@ async def list_tools(self):
12641268
),
12651269
'tools': [{
12661270
'functionDeclarations': [{
1267-
'parameters': {
1268-
'type': 'OBJECT',
1271+
'parametersJsonSchema': {
1272+
'type': 'object',
12691273
'properties': {
12701274
'location': {
1271-
'type': 'STRING',
1275+
'type': 'string',
12721276
},
12731277
},
12741278
},

google/genai/tests/mcp/test_mcp_to_gemini_tools.py

Lines changed: 52 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,17 @@ def test_empty_mcp_tools_list():
3939

4040
def test_unknown_field_conversion():
4141
"""Test conversion of MCP tools with unknown fields to Gemini tools."""
42+
inputSchema = {
43+
'type': 'object',
44+
'properties': {},
45+
'unknown_field': 'unknownField',
46+
'unknown_object': {},
47+
}
4248
mcp_tools = [
4349
mcp_types.Tool(
4450
name='tool',
4551
description='tool-description',
46-
inputSchema={
47-
'type': 'object',
48-
'properties': {},
49-
'unknown_field': 'unknownField',
50-
'unknown_object': {},
51-
},
52+
inputSchema=inputSchema,
5253
),
5354
]
5455
result = _mcp_utils.mcp_to_gemini_tools(mcp_tools)
@@ -58,10 +59,7 @@ def test_unknown_field_conversion():
5859
types.FunctionDeclaration(
5960
name='tool',
6061
description='tool-description',
61-
parameters=types.Schema(
62-
type='OBJECT',
63-
properties={},
64-
),
62+
parameters_json_schema=inputSchema,
6563
),
6664
],
6765
),
@@ -70,24 +68,25 @@ def test_unknown_field_conversion():
7068

7169
def test_items_conversion():
7270
"""Test conversion of MCP tools with items to Gemini tools."""
71+
inputSchema = {
72+
'type': 'array',
73+
'items': {
74+
'type': 'object',
75+
'properties': {
76+
'key1': {
77+
'type': 'string',
78+
},
79+
'key2': {
80+
'type': 'number',
81+
},
82+
},
83+
},
84+
}
7385
mcp_tools = [
7486
mcp_types.Tool(
7587
name='tool',
7688
description='tool-description',
77-
inputSchema={
78-
'type': 'array',
79-
'items': {
80-
'type': 'object',
81-
'properties': {
82-
'key1': {
83-
'type': 'string',
84-
},
85-
'key2': {
86-
'type': 'number',
87-
},
88-
},
89-
},
90-
},
89+
inputSchema=inputSchema,
9190
),
9291
]
9392
result = _mcp_utils.mcp_to_gemini_tools(mcp_tools)
@@ -97,16 +96,7 @@ def test_items_conversion():
9796
types.FunctionDeclaration(
9897
name='tool',
9998
description='tool-description',
100-
parameters=types.Schema(
101-
type='ARRAY',
102-
items=types.Schema(
103-
type='OBJECT',
104-
properties={
105-
'key1': types.Schema(type='STRING'),
106-
'key2': types.Schema(type='NUMBER'),
107-
},
108-
),
109-
),
99+
parameters_json_schema=inputSchema,
110100
),
111101
],
112102
),
@@ -115,21 +105,24 @@ def test_items_conversion():
115105

116106
def test_any_of_conversion():
117107
"""Test conversion of MCP tools with any_of to Gemini tools."""
108+
109+
inputSchema = {
110+
'type': 'object',
111+
'any_of': [
112+
{
113+
'type': 'string',
114+
},
115+
{
116+
'type': 'number',
117+
},
118+
],
119+
}
120+
118121
mcp_tools = [
119122
mcp_types.Tool(
120123
name='tool',
121124
description='tool-description',
122-
inputSchema={
123-
'type': 'object',
124-
'any_of': [
125-
{
126-
'type': 'string',
127-
},
128-
{
129-
'type': 'number',
130-
},
131-
],
132-
},
125+
inputSchema=inputSchema,
133126
),
134127
]
135128
result = _mcp_utils.mcp_to_gemini_tools(mcp_tools)
@@ -139,13 +132,7 @@ def test_any_of_conversion():
139132
types.FunctionDeclaration(
140133
name='tool',
141134
description='tool-description',
142-
parameters=types.Schema(
143-
type='OBJECT',
144-
any_of=[
145-
types.Schema(type='STRING'),
146-
types.Schema(type='NUMBER'),
147-
],
148-
),
135+
parameters_json_schema=inputSchema,
149136
),
150137
],
151138
),
@@ -154,21 +141,22 @@ def test_any_of_conversion():
154141

155142
def test_properties_conversion():
156143
"""Test conversion of MCP tools with properties to Gemini tools."""
144+
inputSchema = {
145+
'type': 'object',
146+
'properties': {
147+
'key1': {
148+
'type': 'string',
149+
},
150+
'key2': {
151+
'type': 'number',
152+
},
153+
},
154+
}
157155
mcp_tools = [
158156
mcp_types.Tool(
159157
name='tool',
160158
description='tool-description',
161-
inputSchema={
162-
'type': 'object',
163-
'properties': {
164-
'key1': {
165-
'type': 'string',
166-
},
167-
'key2': {
168-
'type': 'number',
169-
},
170-
},
171-
},
159+
inputSchema=inputSchema,
172160
),
173161
]
174162
result = _mcp_utils.mcp_to_gemini_tools(mcp_tools)
@@ -178,13 +166,7 @@ def test_properties_conversion():
178166
types.FunctionDeclaration(
179167
name='tool',
180168
description='tool-description',
181-
parameters=types.Schema(
182-
type='OBJECT',
183-
properties={
184-
'key1': types.Schema(type='STRING'),
185-
'key2': types.Schema(type='NUMBER'),
186-
},
187-
),
169+
parameters_json_schema=inputSchema,
188170
),
189171
],
190172
),

google/genai/tests/transformers/test_t_tool.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,33 +105,29 @@ def test_mcp_tool(client):
105105
if not _is_mcp_imported:
106106
return
107107

108+
inputSchema = {
109+
'type': 'object',
110+
'properties': {
111+
'key1': {
112+
'type': 'string',
113+
},
114+
'key2': {
115+
'type': 'number',
116+
},
117+
},
118+
}
119+
108120
mcp_tool = mcp_types.Tool(
109121
name='tool',
110122
description='tool-description',
111-
inputSchema={
112-
'type': 'object',
113-
'properties': {
114-
'key1': {
115-
'type': 'string',
116-
},
117-
'key2': {
118-
'type': 'number',
119-
},
120-
},
121-
},
123+
inputSchema=inputSchema,
122124
)
123125
assert t.t_tool(client, mcp_tool) == types.Tool(
124126
function_declarations=[
125127
types.FunctionDeclaration(
126128
name='tool',
127129
description='tool-description',
128-
parameters=types.Schema(
129-
type='OBJECT',
130-
properties={
131-
'key1': types.Schema(type='STRING'),
132-
'key2': types.Schema(type='NUMBER'),
133-
},
134-
),
130+
parameters_json_schema=inputSchema,
135131
)
136132
]
137133
)

0 commit comments

Comments
 (0)