File size: 1,113 Bytes
c890107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# utils.py
import re
from typing import Any
from google.generativeai.types import Tool

def convert_mcp_tool_to_gemini(mcp_tool: Any) -> Tool:
    """Convert MCP tool to Gemini-compatible Tool object."""
    type_mapping = {"number": "NUMBER", "string": "STRING", "integer": "INTEGER", "boolean": "BOOLEAN"}
    properties = {}
    input_schema = mcp_tool.inputSchema
    if "properties" in input_schema and isinstance(input_schema["properties"], dict):
        for param_name, param_schema in input_schema["properties"].items():
            mcp_type_str = param_schema.get("type", "string")
            gemini_type_str = type_mapping.get(mcp_type_str, "STRING")
            properties[param_name] = {"type": gemini_type_str, "description": param_schema.get("description", "")}
    return Tool(
        function_declarations=[{
            "name": mcp_tool.name,
            "description": mcp_tool.description or "",
            "parameters": {
                "type": "OBJECT",
                "properties": properties,
                "required": input_schema.get("required", [])
            }
        }]
    )