Skip to content

Google GenAI Adapter

This module implements the Google GenAI adapter.

Example Usage:

with MCPAdapt(StdioServerParameters(command="uv", args=["run", "src/echo.py"]), GoogleGenAIAdapter()) as tools: print(tools)

GoogleGenAIAdapter

Bases: ToolAdapter

Adapter for the google.genai package.

Note that the google.genai package do not support async tools at this time so we write only the adapt method.

Source code in src/mcpadapt/google_genai_adapter.py
class GoogleGenAIAdapter(ToolAdapter):
    """Adapter for the `google.genai` package.

    Note that the `google.genai` package do not support async tools at this time so we
    write only the adapt method.
    """

    def adapt(
        self,
        func: Callable[[dict | None], mcp.types.CallToolResult],
        mcp_tool: mcp.types.Tool,
    ) -> tuple[
        types.Tool, tuple[str, Callable[[dict | None], mcp.types.CallToolResult]]
    ]:
        """Adapt a MCP tool to a Google GenAI tool.

        Args:
            func: The function to adapt.
            mcp_tool: The MCP tool to adapt.

        Returns:
            A Google GenAI tool.
        """
        # make sure jsonref are resolved
        input_schema = {
            k: v
            for k, v in jsonref.replace_refs(mcp_tool.inputSchema).items()
            if k != "$defs"
        }

        return (
            types.Tool(
                function_declarations=[
                    {
                        "name": mcp_tool.name,
                        "description": mcp_tool.description,
                        "parameters": input_schema,
                    }
                ],
            ),
            (mcp_tool.name, func),
        )

    def async_adapt(
        self,
        func: Callable[[dict | None], Coroutine[Any, Any, mcp.types.CallToolResult]],
        mcp_tool: mcp.types.Tool,
    ) -> tuple[
        types.Tool,
        tuple[
            str, Callable[[dict | None], Coroutine[Any, Any, mcp.types.CallToolResult]]
        ],
    ]:
        """Adapt a MCP tool to a Google GenAI tool.

        Args:
            func: The function to adapt.
            mcp_tool: The MCP tool to adapt.

        Returns:
            A Google GenAI tool.
        """
        # make sure jsonref are resolved
        input_schema = {
            k: v
            for k, v in jsonref.replace_refs(mcp_tool.inputSchema).items()
            if k != "$defs"
        }

        return (
            types.Tool(
                function_declarations=[
                    {
                        "name": mcp_tool.name,
                        "description": mcp_tool.description,
                        "parameters": input_schema,
                    }
                ],
            ),
            (mcp_tool.name, func),
        )

adapt

adapt(
    func: Callable[[dict | None], CallToolResult],
    mcp_tool: Tool,
) -> tuple[
    Tool,
    tuple[str, Callable[[dict | None], CallToolResult]],
]

Adapt a MCP tool to a Google GenAI tool.

Parameters:

Name Type Description Default
func Callable[[dict | None], CallToolResult]

The function to adapt.

required
mcp_tool Tool

The MCP tool to adapt.

required

Returns:

Type Description
tuple[Tool, tuple[str, Callable[[dict | None], CallToolResult]]]

A Google GenAI tool.

Source code in src/mcpadapt/google_genai_adapter.py
def adapt(
    self,
    func: Callable[[dict | None], mcp.types.CallToolResult],
    mcp_tool: mcp.types.Tool,
) -> tuple[
    types.Tool, tuple[str, Callable[[dict | None], mcp.types.CallToolResult]]
]:
    """Adapt a MCP tool to a Google GenAI tool.

    Args:
        func: The function to adapt.
        mcp_tool: The MCP tool to adapt.

    Returns:
        A Google GenAI tool.
    """
    # make sure jsonref are resolved
    input_schema = {
        k: v
        for k, v in jsonref.replace_refs(mcp_tool.inputSchema).items()
        if k != "$defs"
    }

    return (
        types.Tool(
            function_declarations=[
                {
                    "name": mcp_tool.name,
                    "description": mcp_tool.description,
                    "parameters": input_schema,
                }
            ],
        ),
        (mcp_tool.name, func),
    )

async_adapt

async_adapt(
    func: Callable[
        [dict | None], Coroutine[Any, Any, CallToolResult]
    ],
    mcp_tool: Tool,
) -> tuple[
    Tool,
    tuple[
        str,
        Callable[
            [dict | None],
            Coroutine[Any, Any, CallToolResult],
        ],
    ],
]

Adapt a MCP tool to a Google GenAI tool.

Parameters:

Name Type Description Default
func Callable[[dict | None], Coroutine[Any, Any, CallToolResult]]

The function to adapt.

required
mcp_tool Tool

The MCP tool to adapt.

required

Returns:

Type Description
tuple[Tool, tuple[str, Callable[[dict | None], Coroutine[Any, Any, CallToolResult]]]]

A Google GenAI tool.

Source code in src/mcpadapt/google_genai_adapter.py
def async_adapt(
    self,
    func: Callable[[dict | None], Coroutine[Any, Any, mcp.types.CallToolResult]],
    mcp_tool: mcp.types.Tool,
) -> tuple[
    types.Tool,
    tuple[
        str, Callable[[dict | None], Coroutine[Any, Any, mcp.types.CallToolResult]]
    ],
]:
    """Adapt a MCP tool to a Google GenAI tool.

    Args:
        func: The function to adapt.
        mcp_tool: The MCP tool to adapt.

    Returns:
        A Google GenAI tool.
    """
    # make sure jsonref are resolved
    input_schema = {
        k: v
        for k, v in jsonref.replace_refs(mcp_tool.inputSchema).items()
        if k != "$defs"
    }

    return (
        types.Tool(
            function_declarations=[
                {
                    "name": mcp_tool.name,
                    "description": mcp_tool.description,
                    "parameters": input_schema,
                }
            ],
        ),
        (mcp_tool.name, func),
    )