genkit-mcp 0.13.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- genkit_mcp-0.13.0/.gitignore +36 -0
- genkit_mcp-0.13.0/LICENSE +201 -0
- genkit_mcp-0.13.0/PKG-INFO +203 -0
- genkit_mcp-0.13.0/README.md +162 -0
- genkit_mcp-0.13.0/pyproject.toml +71 -0
- genkit_mcp-0.13.0/src/genkit_mcp/__init__.py +25 -0
- genkit_mcp-0.13.0/src/genkit_mcp/_client.py +443 -0
- genkit_mcp-0.13.0/src/genkit_mcp/_config.py +78 -0
- genkit_mcp-0.13.0/src/genkit_mcp/_connection.py +623 -0
- genkit_mcp-0.13.0/src/genkit_mcp/_errors.py +96 -0
- genkit_mcp-0.13.0/src/genkit_mcp/_host.py +447 -0
- genkit_mcp-0.13.0/src/genkit_mcp/_tools.py +306 -0
- genkit_mcp-0.13.0/src/genkit_mcp/py.typed +0 -0
- genkit_mcp-0.13.0/tests/fake_server.py +208 -0
- genkit_mcp-0.13.0/tests/mcp_client_test.py +910 -0
- genkit_mcp-0.13.0/tests/mcp_config_test.py +96 -0
- genkit_mcp-0.13.0/tests/mcp_connection_test.py +1187 -0
- genkit_mcp-0.13.0/tests/mcp_errors_test.py +57 -0
- genkit_mcp-0.13.0/tests/mcp_exports_test.py +25 -0
- genkit_mcp-0.13.0/tests/mcp_host_test.py +1039 -0
- genkit_mcp-0.13.0/tests/mcp_tools_test.py +436 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
*$py.class
|
|
2
|
+
*.egg
|
|
3
|
+
*.egg-info/
|
|
4
|
+
*.py[cod]
|
|
5
|
+
*.so
|
|
6
|
+
.DS_Store
|
|
7
|
+
.Python
|
|
8
|
+
.cache/
|
|
9
|
+
.coverage
|
|
10
|
+
.eggs/
|
|
11
|
+
.genkit
|
|
12
|
+
.idea/
|
|
13
|
+
.installed.cfg
|
|
14
|
+
.mypy_cache/
|
|
15
|
+
.pytest_cache/
|
|
16
|
+
.ruff_cache/
|
|
17
|
+
.venv
|
|
18
|
+
ENV/
|
|
19
|
+
__pycache__/
|
|
20
|
+
build/
|
|
21
|
+
develop-eggs/
|
|
22
|
+
dist/
|
|
23
|
+
downloads/
|
|
24
|
+
eggs/
|
|
25
|
+
lib/
|
|
26
|
+
lib64/
|
|
27
|
+
parts/
|
|
28
|
+
sdist/
|
|
29
|
+
site/
|
|
30
|
+
var/
|
|
31
|
+
venv/
|
|
32
|
+
wheels/
|
|
33
|
+
.gemini/artifacts/
|
|
34
|
+
|
|
35
|
+
*.bak
|
|
36
|
+
release-manifest.json
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2025 Google LLC
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: genkit-mcp
|
|
3
|
+
Version: 0.13.0
|
|
4
|
+
Summary: Model Context Protocol client for Genkit. Exposes an MCP server's tools as Genkit tools.
|
|
5
|
+
Project-URL: Bug Tracker, https://github.com/genkit-ai/genkit/issues
|
|
6
|
+
Project-URL: Changelog, https://github.com/genkit-ai/genkit/releases
|
|
7
|
+
Project-URL: Documentation, https://genkit.dev/docs/get-started/?lang=python
|
|
8
|
+
Project-URL: Homepage, https://github.com/genkit-ai/genkit
|
|
9
|
+
Project-URL: Repository, https://github.com/genkit-ai/genkit/tree/main/py
|
|
10
|
+
Author: Google
|
|
11
|
+
License-Expression: Apache-2.0
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: genkit,mcp
|
|
14
|
+
Classifier: Development Status :: 3 - Alpha
|
|
15
|
+
Classifier: Environment :: Console
|
|
16
|
+
Classifier: Environment :: Web Environment
|
|
17
|
+
Classifier: Framework :: AsyncIO
|
|
18
|
+
Classifier: Framework :: Pydantic
|
|
19
|
+
Classifier: Framework :: Pydantic :: 2
|
|
20
|
+
Classifier: Intended Audience :: Developers
|
|
21
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
22
|
+
Classifier: Operating System :: OS Independent
|
|
23
|
+
Classifier: Programming Language :: Python
|
|
24
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
27
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
28
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
29
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
30
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
31
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
32
|
+
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
33
|
+
Classifier: Typing :: Typed
|
|
34
|
+
Requires-Python: >=3.10
|
|
35
|
+
Requires-Dist: anyio>=4.9.0
|
|
36
|
+
Requires-Dist: exceptiongroup>=1.2.0; python_version < '3.11'
|
|
37
|
+
Requires-Dist: genkit<2,>=0.13.0
|
|
38
|
+
Requires-Dist: mcp<2,>=1.26.0
|
|
39
|
+
Requires-Dist: pydantic>=2.10.5
|
|
40
|
+
Description-Content-Type: text/markdown
|
|
41
|
+
|
|
42
|
+
# Genkit MCP Client
|
|
43
|
+
|
|
44
|
+
Connects Genkit to a [Model Context Protocol](https://modelcontextprotocol.io) server over stdio and
|
|
45
|
+
exposes that server's tools as Genkit `tool` actions through a dynamic action provider.
|
|
46
|
+
|
|
47
|
+
Tracking issue: <https://github.com/genkit-ai/genkit/issues/6194>
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
The example uses Gemini through `genkit-google-genai` and needs `GEMINI_API_KEY` set. Any model plugin works.
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
import asyncio
|
|
55
|
+
|
|
56
|
+
from genkit import Genkit
|
|
57
|
+
from genkit_google_genai import GoogleAI
|
|
58
|
+
from genkit_mcp import define_mcp_client
|
|
59
|
+
|
|
60
|
+
ai = Genkit(plugins=[GoogleAI()], model=GoogleAI.gemini('gemini-flash-latest'))
|
|
61
|
+
|
|
62
|
+
client = define_mcp_client(
|
|
63
|
+
ai,
|
|
64
|
+
'everything',
|
|
65
|
+
command='npx',
|
|
66
|
+
args=['-y', '@modelcontextprotocol/server-everything'],
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
async def main() -> None:
|
|
71
|
+
try:
|
|
72
|
+
# What the server offers, under the names a model is shown.
|
|
73
|
+
tools = await client.get_active_tools()
|
|
74
|
+
for tool in tools:
|
|
75
|
+
print(tool.name, tool.description)
|
|
76
|
+
|
|
77
|
+
# Every tool of this server, or just one.
|
|
78
|
+
result = await ai.generate(prompt='Echo "hello".', tools=['everything:tool/*'])
|
|
79
|
+
one = f'{client.name}:tool/{client.tool_name("echo")}'
|
|
80
|
+
result = await ai.generate(prompt='Echo "hello".', tools=[one])
|
|
81
|
+
|
|
82
|
+
# The listing itself is a list of Genkit tools, so it can be handed to a
|
|
83
|
+
# generate call or called directly.
|
|
84
|
+
result = await ai.generate(prompt='Echo "hello".', tools=tools)
|
|
85
|
+
print(result.text)
|
|
86
|
+
echoed = (await tools[0]({'message': 'hello'})).output
|
|
87
|
+
print(echoed)
|
|
88
|
+
finally:
|
|
89
|
+
await client.close()
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
asyncio.run(main())
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Tool names
|
|
96
|
+
|
|
97
|
+
Every tool is named `<prefix>_<tool>`, so the tool above is `everything_echo`. The prefix is the
|
|
98
|
+
provider name you registered the server under, `everything` here. The separator is an underscore
|
|
99
|
+
because Gemini and OpenAI reject a `/` in a function declaration name. A prefix holding a character
|
|
100
|
+
they reject fails when the client is defined, rather than on your first `generate`. A tool name the
|
|
101
|
+
server lists with such a character, such as `look.up`, has it rewritten to an underscore with a
|
|
102
|
+
warning. Two tools that then share a name, such as `look.up` and `look/up`, are both left out with a
|
|
103
|
+
warning, since either could answer a call meant for the other. Length is not checked at definition,
|
|
104
|
+
because the tool half of the name is unknown until the server lists it. A `<prefix>_<tool>` longer
|
|
105
|
+
than 64 characters is warned about when the tools are listed and passed through, so the provider's
|
|
106
|
+
400 arrives on `generate`. Nor are names checked across servers: a
|
|
107
|
+
prefix `fake` with a tool `await_stall` and a prefix `fake_await` with a tool `stall` both make
|
|
108
|
+
`fake_await_stall`, and `generate` refuses to bind both. Each prefix names one provider, so a second
|
|
109
|
+
client under a name already registered fails when it is defined, unless the client holding the name
|
|
110
|
+
is closed.
|
|
111
|
+
|
|
112
|
+
The prefix is deliberately not the name the server advertises for itself, which is arbitrary
|
|
113
|
+
server-controlled text: `@modelcontextprotocol/server-everything` above calls itself
|
|
114
|
+
`mcp-servers/everything`, and that slash would be a 400 from both providers.
|
|
115
|
+
|
|
116
|
+
## Connecting and disconnecting
|
|
117
|
+
|
|
118
|
+
Connecting is lazy. The child process starts the first time something lists the tools, once per
|
|
119
|
+
event loop, and the Dev UI's reflection server has a loop of its own. So under `genkit start`, a
|
|
120
|
+
flow run from the Dev UI and your `main()` talk to two separate server processes. A server that
|
|
121
|
+
keeps state, such as a session or an open file, keeps one copy of it for each, and servers that bind
|
|
122
|
+
a port, take a lock file or own a browser profile can clash.
|
|
123
|
+
|
|
124
|
+
`await client.restart()` reconnects a live client, for a server whose configuration or state changed
|
|
125
|
+
underneath you. A request still in flight when it restarts, on any event loop, fails with status
|
|
126
|
+
`UNAVAILABLE` and can be retried. `await client.close()` disconnects and stops the child process: a
|
|
127
|
+
closed client cannot be restarted. To use that server again, define a new client. Afterwards
|
|
128
|
+
`generate` with one of its tools, and `get_active_tools()`, fail with a `GenkitError` whose status
|
|
129
|
+
is `FAILED_PRECONDITION`. A server that cannot start, or whose process dies, fails with status
|
|
130
|
+
`UNAVAILABLE`, and a request the server does not answer within `request_timeout_millis` fails with
|
|
131
|
+
`DEADLINE_EXCEEDED`. A connection that died on its own is replaced on next use, so one bad server start is
|
|
132
|
+
not permanent. Resolving a tool by its action key, as the Dev UI does to run one, reports neither
|
|
133
|
+
status: core finds no action for a closed client or a server that cannot start.
|
|
134
|
+
|
|
135
|
+
Nothing needs closing at exit. Call `close()` when you are done with a server before your process
|
|
136
|
+
is, not as cleanup on the way out: a process running under `genkit start` outlives your `main()`,
|
|
137
|
+
and a closed client's tools can no longer be selected by a flow. The Dev UI lists each registered
|
|
138
|
+
client's tools, and none for a closed one or one whose server cannot start.
|
|
139
|
+
|
|
140
|
+
## Several servers behind one name
|
|
141
|
+
|
|
142
|
+
`define_mcp_host` registers one provider over any number of servers, so a single selector reaches
|
|
143
|
+
all of them:
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
from genkit import Genkit
|
|
147
|
+
from genkit_mcp import define_mcp_host
|
|
148
|
+
|
|
149
|
+
ai = Genkit()
|
|
150
|
+
|
|
151
|
+
host = define_mcp_host(
|
|
152
|
+
ai,
|
|
153
|
+
'hosted',
|
|
154
|
+
mcp_servers={
|
|
155
|
+
'everything': {'command': 'npx', 'args': ['-y', '@modelcontextprotocol/server-everything']},
|
|
156
|
+
'git': {'command': 'uvx', 'args': ['mcp-server-git']},
|
|
157
|
+
},
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
result = await ai.generate(prompt='Echo "hello".', tools=['hosted:tool/*'])
|
|
161
|
+
one = f'{host.name}:tool/{host.tool_name("everything", "echo")}'
|
|
162
|
+
result = await ai.generate(prompt='Echo "hello".', tools=[one])
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
`mcp_servers` takes an `mcpServers` block of stdio servers, as Claude Desktop and Cursor write it, so
|
|
166
|
+
such a block can be loaded from its file: `mcp_servers=json.load(f)['mcpServers']`. An entry that is
|
|
167
|
+
not a stdio entry, such as one with a `url` or `type: 'sse'`, fails the host with an error naming
|
|
168
|
+
that server. So does an entry with any key besides `command`, `args`, `env`, `cwd`, `type` and
|
|
169
|
+
`disabled`. Some clients write `disabled: true` to switch a server off without deleting its entry.
|
|
170
|
+
Such an entry is checked the same way, but its server is never started or listed.
|
|
171
|
+
`await host.connect(name, entry)` takes one entry too, so a server from the same file can be added
|
|
172
|
+
later: `await host.connect('git', config['mcpServers']['git'])`. Connecting a disabled entry stops
|
|
173
|
+
any server of that name.
|
|
174
|
+
|
|
175
|
+
An entry's `env` often holds secrets, and a traceback that renders frame locals, as Genkit's console
|
|
176
|
+
logging does, can print them. Once a host is defined, the plugin keeps `env` out of its own frames.
|
|
177
|
+
It cannot do that for your frames: in the `connect` call above, `config` stays a local of the
|
|
178
|
+
calling function, so a failed connect logged with its traceback prints the whole block. The same
|
|
179
|
+
goes for a `mcp_servers` mapping that fails validation when the host is defined. The child does not
|
|
180
|
+
inherit this process's environment, so the secrets have to be in `env`. To keep them out of your
|
|
181
|
+
frames, build the entry in a helper and pass its result straight on, as in
|
|
182
|
+
`await host.connect('git', load_server('git'))`, so no frame that can still fail holds it.
|
|
183
|
+
|
|
184
|
+
Each server keeps its own namespace, so the tools above are `everything_echo` and `git_git_log`. The
|
|
185
|
+
key a server has in `mcp_servers` is its prefix. The host's own name is only the provider name, so
|
|
186
|
+
it has to fit a selector but no model ever reads it.
|
|
187
|
+
|
|
188
|
+
A server that cannot start, or whose process dies, fails with status `UNAVAILABLE`, and one that
|
|
189
|
+
does not answer a request in time fails with `DEADLINE_EXCEEDED`. The host logs either and skips it,
|
|
190
|
+
so the other servers stay selectable. A server disconnected or replaced while the host was listing
|
|
191
|
+
it is left out of that listing, and a server that does not advertise the tools capability is not
|
|
192
|
+
asked for tools and lists none. Any other failure fails the whole listing:
|
|
193
|
+
a server that breaks the protocol, a server that answers the listing with an error, and two servers
|
|
194
|
+
that serve a tool of the same name. Only the server you ask for is affected by `await host.connect(...)`,
|
|
195
|
+
`await host.disconnect(...)` and `await host.reconnect(...)`. `connect` and `reconnect` start the
|
|
196
|
+
server and list it before they return, so a server that cannot start fails right there. A server
|
|
197
|
+
that `connect` cannot list is stopped, and the host keeps what it held before, including any server
|
|
198
|
+
of that name. `await host.close()` disconnects every
|
|
199
|
+
server the host holds, and like `McpClient.close()` it is final: afterwards the host fails with
|
|
200
|
+
status `FAILED_PRECONDITION`, so define a new host to use those servers again.
|
|
201
|
+
|
|
202
|
+
One provider means one listing. A tool call that fails on one server drops the listing for all of
|
|
203
|
+
them, so the next `generate` lists every server again.
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# Genkit MCP Client
|
|
2
|
+
|
|
3
|
+
Connects Genkit to a [Model Context Protocol](https://modelcontextprotocol.io) server over stdio and
|
|
4
|
+
exposes that server's tools as Genkit `tool` actions through a dynamic action provider.
|
|
5
|
+
|
|
6
|
+
Tracking issue: <https://github.com/genkit-ai/genkit/issues/6194>
|
|
7
|
+
|
|
8
|
+
## Usage
|
|
9
|
+
|
|
10
|
+
The example uses Gemini through `genkit-google-genai` and needs `GEMINI_API_KEY` set. Any model plugin works.
|
|
11
|
+
|
|
12
|
+
```python
|
|
13
|
+
import asyncio
|
|
14
|
+
|
|
15
|
+
from genkit import Genkit
|
|
16
|
+
from genkit_google_genai import GoogleAI
|
|
17
|
+
from genkit_mcp import define_mcp_client
|
|
18
|
+
|
|
19
|
+
ai = Genkit(plugins=[GoogleAI()], model=GoogleAI.gemini('gemini-flash-latest'))
|
|
20
|
+
|
|
21
|
+
client = define_mcp_client(
|
|
22
|
+
ai,
|
|
23
|
+
'everything',
|
|
24
|
+
command='npx',
|
|
25
|
+
args=['-y', '@modelcontextprotocol/server-everything'],
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
async def main() -> None:
|
|
30
|
+
try:
|
|
31
|
+
# What the server offers, under the names a model is shown.
|
|
32
|
+
tools = await client.get_active_tools()
|
|
33
|
+
for tool in tools:
|
|
34
|
+
print(tool.name, tool.description)
|
|
35
|
+
|
|
36
|
+
# Every tool of this server, or just one.
|
|
37
|
+
result = await ai.generate(prompt='Echo "hello".', tools=['everything:tool/*'])
|
|
38
|
+
one = f'{client.name}:tool/{client.tool_name("echo")}'
|
|
39
|
+
result = await ai.generate(prompt='Echo "hello".', tools=[one])
|
|
40
|
+
|
|
41
|
+
# The listing itself is a list of Genkit tools, so it can be handed to a
|
|
42
|
+
# generate call or called directly.
|
|
43
|
+
result = await ai.generate(prompt='Echo "hello".', tools=tools)
|
|
44
|
+
print(result.text)
|
|
45
|
+
echoed = (await tools[0]({'message': 'hello'})).output
|
|
46
|
+
print(echoed)
|
|
47
|
+
finally:
|
|
48
|
+
await client.close()
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
asyncio.run(main())
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Tool names
|
|
55
|
+
|
|
56
|
+
Every tool is named `<prefix>_<tool>`, so the tool above is `everything_echo`. The prefix is the
|
|
57
|
+
provider name you registered the server under, `everything` here. The separator is an underscore
|
|
58
|
+
because Gemini and OpenAI reject a `/` in a function declaration name. A prefix holding a character
|
|
59
|
+
they reject fails when the client is defined, rather than on your first `generate`. A tool name the
|
|
60
|
+
server lists with such a character, such as `look.up`, has it rewritten to an underscore with a
|
|
61
|
+
warning. Two tools that then share a name, such as `look.up` and `look/up`, are both left out with a
|
|
62
|
+
warning, since either could answer a call meant for the other. Length is not checked at definition,
|
|
63
|
+
because the tool half of the name is unknown until the server lists it. A `<prefix>_<tool>` longer
|
|
64
|
+
than 64 characters is warned about when the tools are listed and passed through, so the provider's
|
|
65
|
+
400 arrives on `generate`. Nor are names checked across servers: a
|
|
66
|
+
prefix `fake` with a tool `await_stall` and a prefix `fake_await` with a tool `stall` both make
|
|
67
|
+
`fake_await_stall`, and `generate` refuses to bind both. Each prefix names one provider, so a second
|
|
68
|
+
client under a name already registered fails when it is defined, unless the client holding the name
|
|
69
|
+
is closed.
|
|
70
|
+
|
|
71
|
+
The prefix is deliberately not the name the server advertises for itself, which is arbitrary
|
|
72
|
+
server-controlled text: `@modelcontextprotocol/server-everything` above calls itself
|
|
73
|
+
`mcp-servers/everything`, and that slash would be a 400 from both providers.
|
|
74
|
+
|
|
75
|
+
## Connecting and disconnecting
|
|
76
|
+
|
|
77
|
+
Connecting is lazy. The child process starts the first time something lists the tools, once per
|
|
78
|
+
event loop, and the Dev UI's reflection server has a loop of its own. So under `genkit start`, a
|
|
79
|
+
flow run from the Dev UI and your `main()` talk to two separate server processes. A server that
|
|
80
|
+
keeps state, such as a session or an open file, keeps one copy of it for each, and servers that bind
|
|
81
|
+
a port, take a lock file or own a browser profile can clash.
|
|
82
|
+
|
|
83
|
+
`await client.restart()` reconnects a live client, for a server whose configuration or state changed
|
|
84
|
+
underneath you. A request still in flight when it restarts, on any event loop, fails with status
|
|
85
|
+
`UNAVAILABLE` and can be retried. `await client.close()` disconnects and stops the child process: a
|
|
86
|
+
closed client cannot be restarted. To use that server again, define a new client. Afterwards
|
|
87
|
+
`generate` with one of its tools, and `get_active_tools()`, fail with a `GenkitError` whose status
|
|
88
|
+
is `FAILED_PRECONDITION`. A server that cannot start, or whose process dies, fails with status
|
|
89
|
+
`UNAVAILABLE`, and a request the server does not answer within `request_timeout_millis` fails with
|
|
90
|
+
`DEADLINE_EXCEEDED`. A connection that died on its own is replaced on next use, so one bad server start is
|
|
91
|
+
not permanent. Resolving a tool by its action key, as the Dev UI does to run one, reports neither
|
|
92
|
+
status: core finds no action for a closed client or a server that cannot start.
|
|
93
|
+
|
|
94
|
+
Nothing needs closing at exit. Call `close()` when you are done with a server before your process
|
|
95
|
+
is, not as cleanup on the way out: a process running under `genkit start` outlives your `main()`,
|
|
96
|
+
and a closed client's tools can no longer be selected by a flow. The Dev UI lists each registered
|
|
97
|
+
client's tools, and none for a closed one or one whose server cannot start.
|
|
98
|
+
|
|
99
|
+
## Several servers behind one name
|
|
100
|
+
|
|
101
|
+
`define_mcp_host` registers one provider over any number of servers, so a single selector reaches
|
|
102
|
+
all of them:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
from genkit import Genkit
|
|
106
|
+
from genkit_mcp import define_mcp_host
|
|
107
|
+
|
|
108
|
+
ai = Genkit()
|
|
109
|
+
|
|
110
|
+
host = define_mcp_host(
|
|
111
|
+
ai,
|
|
112
|
+
'hosted',
|
|
113
|
+
mcp_servers={
|
|
114
|
+
'everything': {'command': 'npx', 'args': ['-y', '@modelcontextprotocol/server-everything']},
|
|
115
|
+
'git': {'command': 'uvx', 'args': ['mcp-server-git']},
|
|
116
|
+
},
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
result = await ai.generate(prompt='Echo "hello".', tools=['hosted:tool/*'])
|
|
120
|
+
one = f'{host.name}:tool/{host.tool_name("everything", "echo")}'
|
|
121
|
+
result = await ai.generate(prompt='Echo "hello".', tools=[one])
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
`mcp_servers` takes an `mcpServers` block of stdio servers, as Claude Desktop and Cursor write it, so
|
|
125
|
+
such a block can be loaded from its file: `mcp_servers=json.load(f)['mcpServers']`. An entry that is
|
|
126
|
+
not a stdio entry, such as one with a `url` or `type: 'sse'`, fails the host with an error naming
|
|
127
|
+
that server. So does an entry with any key besides `command`, `args`, `env`, `cwd`, `type` and
|
|
128
|
+
`disabled`. Some clients write `disabled: true` to switch a server off without deleting its entry.
|
|
129
|
+
Such an entry is checked the same way, but its server is never started or listed.
|
|
130
|
+
`await host.connect(name, entry)` takes one entry too, so a server from the same file can be added
|
|
131
|
+
later: `await host.connect('git', config['mcpServers']['git'])`. Connecting a disabled entry stops
|
|
132
|
+
any server of that name.
|
|
133
|
+
|
|
134
|
+
An entry's `env` often holds secrets, and a traceback that renders frame locals, as Genkit's console
|
|
135
|
+
logging does, can print them. Once a host is defined, the plugin keeps `env` out of its own frames.
|
|
136
|
+
It cannot do that for your frames: in the `connect` call above, `config` stays a local of the
|
|
137
|
+
calling function, so a failed connect logged with its traceback prints the whole block. The same
|
|
138
|
+
goes for a `mcp_servers` mapping that fails validation when the host is defined. The child does not
|
|
139
|
+
inherit this process's environment, so the secrets have to be in `env`. To keep them out of your
|
|
140
|
+
frames, build the entry in a helper and pass its result straight on, as in
|
|
141
|
+
`await host.connect('git', load_server('git'))`, so no frame that can still fail holds it.
|
|
142
|
+
|
|
143
|
+
Each server keeps its own namespace, so the tools above are `everything_echo` and `git_git_log`. The
|
|
144
|
+
key a server has in `mcp_servers` is its prefix. The host's own name is only the provider name, so
|
|
145
|
+
it has to fit a selector but no model ever reads it.
|
|
146
|
+
|
|
147
|
+
A server that cannot start, or whose process dies, fails with status `UNAVAILABLE`, and one that
|
|
148
|
+
does not answer a request in time fails with `DEADLINE_EXCEEDED`. The host logs either and skips it,
|
|
149
|
+
so the other servers stay selectable. A server disconnected or replaced while the host was listing
|
|
150
|
+
it is left out of that listing, and a server that does not advertise the tools capability is not
|
|
151
|
+
asked for tools and lists none. Any other failure fails the whole listing:
|
|
152
|
+
a server that breaks the protocol, a server that answers the listing with an error, and two servers
|
|
153
|
+
that serve a tool of the same name. Only the server you ask for is affected by `await host.connect(...)`,
|
|
154
|
+
`await host.disconnect(...)` and `await host.reconnect(...)`. `connect` and `reconnect` start the
|
|
155
|
+
server and list it before they return, so a server that cannot start fails right there. A server
|
|
156
|
+
that `connect` cannot list is stopped, and the host keeps what it held before, including any server
|
|
157
|
+
of that name. `await host.close()` disconnects every
|
|
158
|
+
server the host holds, and like `McpClient.close()` it is final: afterwards the host fails with
|
|
159
|
+
status `FAILED_PRECONDITION`, so define a new host to use those servers again.
|
|
160
|
+
|
|
161
|
+
One provider means one listing. A tool call that fails on one server drops the listing for all of
|
|
162
|
+
them, so the next `generate` lists every server again.
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Copyright 2026 Google LLC
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
#
|
|
15
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
16
|
+
|
|
17
|
+
[project]
|
|
18
|
+
authors = [{ name = "Google" }]
|
|
19
|
+
classifiers = [
|
|
20
|
+
"Development Status :: 3 - Alpha",
|
|
21
|
+
"Environment :: Console",
|
|
22
|
+
"Environment :: Web Environment",
|
|
23
|
+
"Framework :: AsyncIO",
|
|
24
|
+
"Framework :: Pydantic",
|
|
25
|
+
"Framework :: Pydantic :: 2",
|
|
26
|
+
"Intended Audience :: Developers",
|
|
27
|
+
"License :: OSI Approved :: Apache Software License",
|
|
28
|
+
"Operating System :: OS Independent",
|
|
29
|
+
"Programming Language :: Python",
|
|
30
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
31
|
+
"Programming Language :: Python :: 3.10",
|
|
32
|
+
"Programming Language :: Python :: 3.11",
|
|
33
|
+
"Programming Language :: Python :: 3.12",
|
|
34
|
+
"Programming Language :: Python :: 3.13",
|
|
35
|
+
"Programming Language :: Python :: 3.14",
|
|
36
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
37
|
+
"Topic :: Software Development :: Libraries",
|
|
38
|
+
"Topic :: Software Development :: Libraries :: Application Frameworks",
|
|
39
|
+
"Typing :: Typed",
|
|
40
|
+
]
|
|
41
|
+
dependencies = [
|
|
42
|
+
"anyio>=4.9.0",
|
|
43
|
+
"exceptiongroup>=1.2.0; python_version < '3.11'",
|
|
44
|
+
"genkit>=0.13.0,<2",
|
|
45
|
+
"mcp>=1.26.0,<2",
|
|
46
|
+
"pydantic>=2.10.5",
|
|
47
|
+
]
|
|
48
|
+
description = "Model Context Protocol client for Genkit. Exposes an MCP server's tools as Genkit tools."
|
|
49
|
+
keywords = [
|
|
50
|
+
"genkit",
|
|
51
|
+
"mcp",
|
|
52
|
+
]
|
|
53
|
+
license = "Apache-2.0"
|
|
54
|
+
name = "genkit-mcp"
|
|
55
|
+
readme = "README.md"
|
|
56
|
+
requires-python = ">=3.10"
|
|
57
|
+
version = "0.13.0"
|
|
58
|
+
|
|
59
|
+
[project.urls]
|
|
60
|
+
"Bug Tracker" = "https://github.com/genkit-ai/genkit/issues"
|
|
61
|
+
Changelog = "https://github.com/genkit-ai/genkit/releases"
|
|
62
|
+
"Documentation" = "https://genkit.dev/docs/get-started/?lang=python"
|
|
63
|
+
"Homepage" = "https://github.com/genkit-ai/genkit"
|
|
64
|
+
"Repository" = "https://github.com/genkit-ai/genkit/tree/main/py"
|
|
65
|
+
|
|
66
|
+
[build-system]
|
|
67
|
+
build-backend = "hatchling.build"
|
|
68
|
+
requires = ["hatchling"]
|
|
69
|
+
|
|
70
|
+
[tool.hatch.build.targets.wheel]
|
|
71
|
+
packages = ["src/genkit_mcp"]
|