Repository navigation
feat(py/plugins/mcp): add the MCP client package - #6365
Conversation
There was a problem hiding this comment.
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.
dcb06bc to
9cc1156
Compare
9cc1156 to
f32b298
Compare
89d3ca1 to
c75c750
Compare
c2a30e7 to
1f0c9dd
Compare
|
Two changes beyond the threads. First, the surface now follows #6671 and #6706: |
1f0c9dd to
c8ccd65
Compare
ac9c59a to
10b9b59
Compare
6028fb9 to
86c9ddf
Compare
4ff1baf to
e9e3960
Compare
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.
…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.
e9e3960 to
4fa99ea
Compare
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-mcppackage and wires it into the workspace: root dependency and uv source, type-checker source lists, and themcpdependency 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,envandcwdas 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 notool_prefix, and there is nodisabled: not defining the client does the same._errors.pylands here because it is nothing but class definitions. Each error is a privateGenkitErrorsubclass with a status:FAILED_PRECONDITIONfor a closed client,UNAVAILABLEfor a server that cannot start or dies,INTERNALfor a protocol violation. None derives fromRuntimeError: the SDK reports a result failing the server's own output schema as a bareRuntimeError, which later becomes anisErrorenvelope, and a shared base class would send a dead subprocess to the model as a retryable tool error.The
NotImplementedErrorbodies 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.