-
Notifications
You must be signed in to change notification settings - Fork 631
feat(py): add mcp sample test #4059
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
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @MengqinShen, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a new sample application for the Model Context Protocol (MCP) integrated with the Genkit Python SDK. The sample provides practical, runnable examples that illustrate how to set up and interact with various MCP servers, including those for git operations, filesystem access, and general-purpose tool exposure. It covers both client-side interactions and server-side implementation, aiming to give developers a clear understanding of how to leverage MCP within their Genkit projects for enhanced AI model capabilities. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
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 a comprehensive sample for the MCP (Model Context Protocol) plugin in the Genkit Python SDK. The changes include a README, project configuration, and several Python scripts demonstrating client, server, and HTTP server functionalities. The code is well-structured and provides good examples. My review focuses on improving documentation clarity, code robustness, and adherence to Python best practices. I've identified some areas for improvement, such as clarifying the README, removing redundant code, avoiding private API usage, and handling exceptions more safely. Overall, this is a great addition that will be very helpful for users of the MCP plugin.
|
|
||
| async def handle_sse(request): | ||
| """Handle incoming SSE connections.""" | ||
| async with sse.connect_sse(request.scope, request.receive, request._send) as streams: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the private attribute request._send is not recommended as it relies on internal implementation details of Starlette's Request object. This could break with future updates to the library. It would be better to use a public API if one is available. If not, this reliance on an internal API should be documented with a comment.
| await mcp_host.start() | ||
| tools = await mcp_host.register_tools(ai) | ||
|
|
||
| result = await ai.generate(prompt=f"summarize last 5 commits in '{repo_root}'", tools=tools) | ||
|
|
||
| await mcp_host.close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pattern of manually calling mcp_host.start() at the beginning and mcp_host.close() at the end of each flow is repeated across multiple functions. This is verbose and error-prone, as an exception between these calls would prevent close() from being executed.
Consider refactoring this into a decorator or an async context manager to handle the setup and teardown of the mcp_host automatically. This would make the flow implementations cleaner and more robust.
Example using a decorator:
import functools
def with_mcp_host(flow_func):
@functools.wraps(flow_func)
async def wrapper(*args, **kwargs):
await mcp_host.start()
try:
return await flow_func(*args, **kwargs)
finally:
await mcp_host.close()
return wrapper
@ai.flow(name='git_commits')
@with_mcp_host
async def git_commits(query: str = ''):
tools = await mcp_host.register_tools(ai)
result = await ai.generate(prompt=f"summarize last 5 commits in '{repo_root}'", tools=tools)
return result.text|
|
||
| # Filter for specific tool: 'fs/read_file' | ||
| # JS: tools: ['test-mcp-manager:tool/fs/read_file'] | ||
| import fnmatch |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # Re-registering might be needed if registry was cleaned, but here we just re-connnect | ||
| # Implementation detail: Does register_tools need to be called again? | ||
| # Code shows wrappers capture client, client.session is updated on connect. | ||
| await mcp_host.clients['fs'].connect() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| from pydantic import BaseModel, Field | ||
|
|
||
| from genkit.ai import Genkit | ||
| from genkit.plugins.google_genai import GoogleAI |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the summary of changes below from gemini-code-assist.