Skip to content

feat: allowlisting MCP tools for generate_content sessions - #2814

Open
a2105z wants to merge 2 commits into
googleapis:mainfrom
a2105z:feat/mcp-allowed-tools
Open

a2105z wants to merge 2 commits into
googleapis:mainfrom
a2105z:feat/mcp-allowed-tools

Conversation

@a2105z

@a2105z a2105z commented Aug 4, 2026

Copy link
Copy Markdown

Summary

  • Fixes #1089: opt-in allowlist so tools=[mcp_session] does not always expand every tool from list_tools().
  • Adds google.genai.mcp.with_allowed_tools(session, [...]), which wraps the session, filters list_tools(), and rejects disallowed call_tool() names.
  • Bare ClientSession behavior is unchanged. Filtering happens before adapter conversion so function declarations and the AFC map stay aligned for both generate_content and Live.

Usage

from google.genai import mcp as genai_mcp

config = types.GenerateContentConfig(
    tools=[genai_mcp.with_allowed_tools(session, ['tool_a', 'tool_b'])],
)

Test plan

  • pytest google/genai/tests/mcp/ -q
  • Coverage for allowlist filter, unknown-name, empty-allowlist, and call_tool rejection
  • Bare MCP session still exposes all tools (opt-in wrapper only)
  • Confirm docs/examples for mcp.with_allowed_tools(session, [...]) look clear in review

Add google.genai.mcp.with_allowed_tools so callers can expose a subset of
MCP session tools to the model and AFC map without changing bare
ClientSession behavior. Fixes googleapis#1089.
@a2105z

a2105z commented Aug 4, 2026

Copy link
Copy Markdown
Author

@Venkaiahbabuneelam hey — opened this for #1089.

Passing an MCP session in tools= currently expands every tool from list_tools() into the model schemas + AFC map. allowed_function_names only limits what the model can pick on the wire, so unused tools still show up, which isnt really what people want here.

Went with an opt-in wrapper instead so bare sessions stay the same:

from google.genai import mcp as genai_mcp

tools=[genai_mcp.with_allowed_tools(session, ["tool_a", "tool_b"])]

Filters in list_tools() and blocks disallowed call_tool() calls, so the declarations and adapter map dont get out of sync. Happy to tweak the API if youd rather it live somewhere else.

@Venkaiahbabuneelam Venkaiahbabuneelam self-assigned this Aug 4, 2026
@Venkaiahbabuneelam Venkaiahbabuneelam added the size:XL Code changes > 100 lines label Aug 4, 2026
Make is_mcp_client_session a TypeGuard and accept allowlist wrappers in
McpToGenAiToolAdapter so list_tools calls type-check under mypy.
@a2105z

a2105z commented Aug 4, 2026

Copy link
Copy Markdown
Author

@Venkaiahbabuneelam quick follow-up — mypy was failing because is_mcp_client_session() didnt narrow the union, so tool.list_tools() looked unsafe. Pushed a fix that makes that check a TypeGuard and lets the adapter accept the allowlist wrapper. Local mypy (same as CI) is clean now (Success: no issues found in 36 source files). Looks like the new workflow run is waiting on approval to start — whenever you can re-run / approve checks thatd be great.

@a2105z

a2105z commented Aug 4, 2026

Copy link
Copy Markdown
Author

@Venkaiahbabuneelam sorry about the mypy noise on this one — that was on me. I missed that the session check wasnt narrowing the type, so CI blew up on list_tools().

Just pushed a fix for it. If you get a sec, could you re-approve / re-run the checks? Appreciate it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XL Code changes > 100 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow allowed_tools for MCP function calling

2 participants