-
Notifications
You must be signed in to change notification settings - Fork 631
feat(py): add mcp plugin and tests #4054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1063033
feat(py): add mcp plugin and tests
MengqinShen-GL 555d38a
feat(py): fixed with gemini's comments
MengqinShen-GL ab2aee3
feat(py): fixed with gemini's comments
MengqinShen-GL a9d2789
feat(py): fixed with gemini's comments
MengqinShen-GL 0269b4e
feat(py): formatted import block
MengqinShen-GL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Genkit MCP Plugin | ||
|
|
||
| Integrate Model Context Protocol (MCP) with Genkit. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import asyncio | ||
|
|
||
| from genkit.ai import Genkit | ||
| from genkit.plugins.mcp import McpServerConfig, create_mcp_client | ||
|
|
||
| try: | ||
| from genkit.plugins.google_genai import GoogleAI | ||
| except ImportError: | ||
| GoogleAI = None | ||
|
|
||
|
|
||
| # Simple client example connecting to 'everything' server using npx | ||
| async def main(): | ||
| # Define the client plugin | ||
| everything_client = create_mcp_client( | ||
| name='everything', config=McpServerConfig(command='npx', args=['-y', '@modelcontextprotocol/server-everything']) | ||
| ) | ||
|
|
||
| plugins = [everything_client] | ||
| if GoogleAI: | ||
| plugins.append(GoogleAI()) | ||
|
|
||
| ai = Genkit(plugins=plugins) | ||
|
|
||
| await everything_client.connect() | ||
|
|
||
| print('Connected! Listing tools...') | ||
|
|
||
| tools = await everything_client.list_tools() | ||
| for t in tools: | ||
| print(f'- {t.name}: {t.description}') | ||
|
|
||
| await everything_client.close() | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --- | ||
| input: | ||
| schema: | ||
| code: string, the source code to port from one language to another | ||
| fromLang?: string, the original language of the source code (e.g. js, python) | ||
| toLang: string, the destination language of the source code (e.g. python, js) | ||
| --- | ||
|
|
||
| You are assisting the user in translating code between two programming languages. Given the code below, translate it into {{toLang}}. | ||
|
|
||
| ```{{#if fromLang}}{{fromLang}}{{/if}} | ||
| {{code}} | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import asyncio | ||
|
|
||
| from pydantic import BaseModel, Field | ||
|
|
||
| from genkit.ai import Genkit | ||
| from genkit.plugins.mcp import McpServerOptions, create_mcp_server | ||
|
|
||
|
|
||
| # Define input model | ||
| class AddInput(BaseModel): | ||
| a: int = Field(..., description='First number') | ||
| b: int = Field(..., description='Second number') | ||
|
|
||
|
|
||
| import os | ||
|
|
||
|
|
||
| def main(): | ||
| # Load prompts from the 'prompts' directory relative to this script | ||
| script_dir = os.path.dirname(os.path.abspath(__file__)) | ||
| prompts_dir = os.path.join(script_dir, 'prompts') | ||
|
|
||
| ai = Genkit(prompt_dir=prompts_dir) | ||
|
|
||
| @ai.tool(name='add', description='add two numbers together') | ||
| def add(input: AddInput): | ||
| return input.a + input.b | ||
|
|
||
| # Genkit Python prompt definition (simplified) | ||
| # Note: In Python, prompts are typically loaded from files via prompt_dir | ||
| # This inline definition is for demonstration purposes | ||
| happy_prompt = ai.define_prompt( | ||
| input_schema={'action': str}, | ||
| prompt="If you're happy and you know it, {{action}}.", | ||
| ) | ||
|
|
||
| # Create and start MCP server | ||
| # Note: create_mcp_server returns McpServer instance. | ||
| # In JS example: .start() is called. | ||
| server = create_mcp_server(ai, McpServerOptions(name='example_server', version='0.0.1')) | ||
|
|
||
| print('Starting MCP server on stdio...') | ||
| asyncio.run(server.start()) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.