Skip to content

feat(py/plugins/mcp): add the MCP client package - #6365

Merged
hilariie merged 15 commits into
mainfrom
feat/py-mcp-scaffold
Oct 30, 2026
Merged

hilariie merged 15 commits into
mainfrom
feat/py-mcp-scaffold

Conversation

@cabljac

@cabljac cabljac commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Python has no MCP client, so Python users cannot call tools hosted by an MCP server. JS and Go both ship one.

This adds the genkit-mcp package and wires it into the workspace: root dependency and uv source, type-checker source lists, and the mcp dependency moved out of the root dev group into the package that needs it.

The surface follows the other Python plugins (#6671, #6706, #6710). Every module is private, callers pass command, args, env and cwd as flat keyword arguments, and the stdio config those build stays internal. The provider name is also the tool name prefix, as in Go, so there is no tool_prefix, and there is no disabled: not defining the client does the same.

_errors.py lands here because it is nothing but class definitions. Each error is a private GenkitError subclass with a status: FAILED_PRECONDITION for a closed client, UNAVAILABLE for a server that cannot start or dies, INTERNAL for a protocol violation. None derives from RuntimeError: the SDK reports a result failing the server's own output schema as a bare RuntimeError, which later becomes an isError envelope, and a shared base class would send a dead subprocess to the model as a retryable tool error.

The NotImplementedError bodies are intentional. Their signatures and docstrings are the interface the rest of the stack fills in, one module per PR. Nothing is exported yet and the package is absent from the publish workflow. Both arrive with the client.

Part of #6194.

@cabljac
cabljac added this pull request to stack #6368 September 17, 2026 14:31
@github-actions github-actions Bot added docs Improvements or additions to documentation python Python config labels Sep 17, 2026

@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 the genkit-mcp package, which implements a Model Context Protocol (MCP) client for Genkit to expose stdio MCP server tools as Genkit actions. The changes include setting up the package configuration, connection lifecycle, and tool translation interfaces. Feedback on the changes suggests improving type safety in _tools.py by replacing the generic object type annotation for the call parameter in mcp_tool_to_action with a more specific Callable and Awaitable signature.

@cabljac
cabljac force-pushed the feat/py-mcp-scaffold branch 2 times, most recently from dcb06bc to 9cc1156 Compare September 21, 2026 08:37
@cabljac
cabljac marked this pull request as ready for review September 21, 2026 12:16
@cabljac
cabljac force-pushed the feat/py-mcp-scaffold branch from 9cc1156 to f32b298 Compare September 22, 2026 10:26
Comment thread py/packages/genkit-mcp/pyproject.toml Resolved
@cabljac
cabljac force-pushed the feat/py-mcp-scaffold branch 2 times, most recently from 89d3ca1 to c75c750 Compare October 5, 2026 03:20
@cabljac
cabljac force-pushed the feat/py-mcp-scaffold branch from c2a30e7 to 1f0c9dd Compare October 12, 2026 16:03
@cabljac

cabljac commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

Two changes beyond the threads. First, the surface now follows #6671 and #6706: define_mcp_client takes command, args, env and cwd as keyword arguments, the stdio config is internal, tool_prefix and disabled are gone, and errors are private GenkitError subclasses with a status (FAILED_PRECONDITION, UNAVAILABLE, INTERNAL). The package exports McpClient, McpHost, define_mcp_client and define_mcp_host. Second, config hardening: env is Field(repr=False) and the model sets hide_input_in_errors=True, so env values never show in repr or validation errors. extra='forbid' with an optional type: 'stdio', so a typo like enviroment fails instead of launching the server without its secret. Claude Desktop and Cursor blocks still pass; blocks with client-specific keys (Cline autoApprove, VS Code envFile) now fail. Tests in tests/mcp_config_test.py.

@cabljac
cabljac force-pushed the feat/py-mcp-scaffold branch from 1f0c9dd to c8ccd65 Compare October 13, 2026 08:39
@hilariie
hilariie force-pushed the feat/py-mcp-scaffold branch from ac9c59a to 10b9b59 Compare October 16, 2026 12:04
@cabljac
cabljac force-pushed the feat/py-mcp-scaffold branch 2 times, most recently from 6028fb9 to 86c9ddf Compare October 20, 2026 10:38

@huangjeff5 huangjeff5 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.

LGTM. Scaffold is fine as the base of the train; the stubs are filled in by #6366/#6367/#6369. Hilarie's threads are all resolved and CI is green. One thing before merge: this conflicts with main in py/pyproject.toml (workspace lists only), so it needs a rebase.

cabljac and others added 6 commits October 30, 2026 10:56
Python has no MCP client, so Python users cannot call tools hosted by an
MCP server. JS and Go both ship one.

Add the genkit-mcp package and wire it into the workspace: root dependency
and uv source, type-checker source lists, and the mcp dependency moved from
the root dev group into the package that needs it. structlog is declared
here, as the other plugins declare it, rather than leaned on through genkit.

_errors.py lands here, with the scaffold, because it is nothing but class
definitions. The base is McpClientError, and it deliberately does not derive
from RuntimeError: the MCP SDK reports a tool result that fails the server's
own output schema as a bare RuntimeError, and the tool wrapper folds that one
into an isError envelope for the model. Sharing a base class with it would
send a dead subprocess to the model as a retryable tool error. McpError is
taken by the SDK, so the names here are McpClientError,
McpConnectionClosedError, McpConnectionFailedError and McpProtocolError.

McpStdioServerConfig.env documented the opposite of what the SDK does.
stdio_client passes get_default_environment() when env is None, which is
HOME, LOGNAME, PATH, SHELL, TERM and USER on posix, and merges a supplied env
over that same minimum rather than replacing it. Neither case inherits this
process's environment, so a user who expects GEMINI_API_KEY or AWS_* to reach
the child gets a silent failure. cwd accepts os.PathLike too, matching the
SDK, so a Path does not have to be stringified.

The package is deliberately absent from the publish workflow and exports no
public API. Both arrive with the client implementation, so no release can
ship a genkit-mcp whose entry point raises, or a genkit[mcp] extra pointing
at a distribution that does not exist on PyPI.
mcp 2.x renames isError, structuredContent and inputSchema to snake_case,
so a fresh install outside the lock resolved an SDK this package does not
speak. Cap the dependency at <2 to match the 1.26 lock.

The close and restart docstrings contradicted McpConnectionClosedError
about whether a closed client could come back.

The stdio config carries secrets in env, so its values must not leak
through repr or validation errors. A misspelled key such as enviroment
was silently dropped, which launched the server without its secret;
unknown keys now fail, while the type marker that mcpServers blocks
carry is still accepted.
…lic surface

The Python plugins on main export only what a caller needs, take flat
keyword arguments instead of nested option objects (#6706), and report
failures as GenkitError with a status rather than plugin error classes
(#6710). The MCP client follows the same rules.

define_mcp_client takes command, args, env and cwd as keyword
arguments. McpStdioServerConfig stays as the internal model those
arguments build. The provider name is also the tool name prefix, as in
the Go plugin, so tool_prefix goes away. disabled goes away too: not
calling define_mcp_client does the same.

The error classes become private GenkitError subclasses: a closed
client is FAILED_PRECONDITION, a server that cannot start or dies is
UNAVAILABLE, and a protocol violation is INTERNAL.
anyio.fail_after(0) raises at the first checkpoint, so a timeout of 0,
which reads as "no timeout", would fail every request at once. Raise
ValueError for zero or negative values in define_mcp_client and in
McpConnection, and document None as the only way to disable the timeout.
Some servers take credentials as arguments, for example a postgres
connection URL, so args are hidden from repr like env.
cabljac and others added 9 commits October 30, 2026 10:56
…ive values

Core treats 0 like None and uses the 3 second default, and a negative
value disables the cache. A caller passing 0 to mean "always re-list"
would otherwise get 3 seconds.
…v UI

The client holds one connection per event loop, and the Dev UI
reflection server runs its own loop, so a second server process starts
while it runs. State a stateful server keeps diverges between the two,
and servers that take a port, lock file or browser profile can clash.
The copied list of inherited variables mirrored a private SDK constant
and would drift. Reference the public mcp.client.stdio function instead
and keep the warning that the parent environment is not inherited.
Wrap the top-level await in an async main run by asyncio.run, configure
a model, and close the client in a finally block.
Add Framework :: AsyncIO, Framework :: Pydantic :: 2, Typing :: Typed
and the rest of the block genkit-middleware declares.
GoogleAI.gemini_model became GoogleAI.gemini in #6698.
@hilariie
hilariie force-pushed the feat/py-mcp-scaffold branch from e9e3960 to 4fa99ea Compare October 30, 2026 10:39
@hilariie
hilariie merged commit db55235 into main Oct 30, 2026
26 checks passed
@hilariie
hilariie deleted the feat/py-mcp-scaffold branch October 30, 2026 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

config docs Improvements or additions to documentation python Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants