-
Notifications
You must be signed in to change notification settings - Fork 31
feat: Add McpTool for easy MCP Tool integration #85
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
feat: Add McpTool for easy MCP Tool integration #85
Conversation
Implements easy-to-integrate MCP server support using the official C# SDK,
allowing Gemini models to use tools from any MCP server.
Key Features:
- McpTool class implementing IFunctionTool interface
- Auto-discovery of tools from MCP servers
- Support for multiple concurrent MCP servers
- Automatic reconnection and error handling
- Comprehensive configuration options
Implementation:
- McpServerConfig: Configure server connections (command, args, env vars)
- McpToolOptions: Control behavior (auto-reconnect, error handling)
- McpTool: Main class that bridges MCP servers with Google Generative AI
- Async/await pattern with proper IDisposable/IAsyncDisposable support
- Converts MCP tools to Google's FunctionDeclaration format
- Forwards function calls to MCP servers and converts responses
Example Usage:
var config = new McpServerConfig
{
Name = "my-server",
Command = "npx",
Arguments = new List<string> { "-y", "@modelcontextprotocol/server-everything" }
};
using var mcpTool = await McpTool.CreateAsync(config);
var model = new GenerativeModel(apiKey, "gemini-2.0-flash-exp");
model.AddFunctionTool(mcpTool);
Files Added:
- src/GenerativeAI.Tools/Mcp/McpServerConfig.cs
- src/GenerativeAI.Tools/Mcp/McpTool.cs
- src/GenerativeAI.Tools/Mcp/README.md
- samples/McpIntegrationDemo/ (complete example project)
- MCP_INTEGRATION_SUMMARY.md
Dependencies:
- Added ModelContextProtocol v0.4.0-preview.3 NuGet package
Major update to MCP integration to support ALL transport protocols
using the official MCP SDK's native transport classes.
BREAKING CHANGE: McpTool now accepts IClientTransport directly instead
of custom McpServerConfig. Use McpTransportFactory for easy creation.
New Features:
- Support for ALL MCP transport types (stdio, HTTP/SSE, custom)
- Native integration with MCP SDK's IClientTransport interface
- McpTransportFactory with helper methods for common configurations
- HTTP/SSE transport support for remote MCP servers
- HTTP authentication support (Bearer tokens, custom headers)
- Transport factory pattern for auto-reconnection
- Mix and match different transport types in single application
Transport Types Supported:
1. Stdio Transport - Launch MCP servers as subprocesses
2. HTTP/SSE Transport - Connect to remote MCP servers via HTTP
3. Custom Transports - Any IClientTransport implementation
Implementation Changes:
- McpServerConfig.cs: Redesigned with McpTransportFactory class
- CreateStdioTransport() - For stdio with full options
- CreateHttpTransport() - For HTTP/SSE connections
- CreateHttpTransportWithAuth() - HTTP with Bearer auth
- CreateHttpTransportWithHeaders() - HTTP with custom headers
- McpTool.cs: Now accepts IClientTransport
- CreateAsync(IClientTransport) - Use any transport
- CreateAsync(Func<IClientTransport>) - Auto-reconnection support
- CreateMultipleAsync(IEnumerable<IClientTransport>) - Multiple servers
- Improved reconnection logic with transport factories
Examples Updated:
- Example 1: Stdio transport (launch as subprocess)
- Example 2: HTTP/SSE transport (connect to remote server)
- Example 3: Multiple servers with different transports
- Example 4: Custom configuration (env vars, headers, auth)
- Example 5: Auto-reconnection with transport factory
Migration Guide:
Old (v1):
var config = new McpServerConfig { Name = "s", Command = "npx", ... };
using var tool = await McpTool.CreateAsync(config);
New (v2):
var transport = McpTransportFactory.CreateStdioTransport("s", "npx", ...);
using var tool = await McpTool.CreateAsync(transport);
Benefits:
- ✅ Full protocol support (not just stdio)
- ✅ Native MCP SDK integration
- ✅ Extensible for future transports
- ✅ Better separation of concerns
- ✅ Direct access to all SDK features
- ✅ Future-proof architecture
Documentation:
- Updated MCP_INTEGRATION_SUMMARY.md with all transport types
- Updated Program.cs with comprehensive examples
- Added transport factory documentation
- Set proper git author: Gunpal Jain <gunpal5@gmail.com> - Define branch naming conventions (no 'claude' prefix) - Document project structure and coding standards - Ensure all future sessions use correct identity
- Remove standalone markdown files (MCP_INTEGRATION_SUMMARY.md, samples/McpIntegrationDemo/README.md, src/GenerativeAI.Tools/Mcp/README.md) - Add MCP Server Integration section to main README - Include examples for stdio, HTTP/SSE, and multiple servers - Update table of contents - Keep documentation concise and centralized
- Introduced comprehensive integration tests for MCP tools and Gemini models, validating end-to-end functionality and user workflows. - Added ModelContextProtocol.Core dependency (v0.4.0-preview.3) to support MCP tool features. - Updated internal handling of MCP tools with `McpClientTool` for better tool representation and usability. - Improved transport creation methods with enhanced options for stdio, HTTP, authentication, and custom headers. - Verified proper handling of function calls, connection management, and error responses in tools.
|
Caution Review failedThe pull request is closed. WalkthroughThis PR introduces Model Context Protocol (MCP) integration to the Gemini SDK, enabling seamless tool interaction with MCP servers via multiple transports (stdio, HTTP/SSE). It includes new MCP tool implementation, configuration options, transport factories, comprehensive tests, documentation updates, and a sample demo program illustrating integration patterns. Changes
Sequence Diagram(s)sequenceDiagram
participant App as Application
participant MT as McpTool
participant MCP as MCP Server
participant Gemini as Gemini Model
App->>MT: CreateAsync(transport, options)
MT->>MCP: Connect & Initialize
MCP-->>MT: Server Capabilities
MT->>MCP: ListTools()
MCP-->>MT: Tool Definitions
MT->>MT: Generate FunctionDeclarations
App->>MT: AsTool()
App->>Gemini: AttachTool(mcpTool)
App->>Gemini: GenerateContent(prompt)
Gemini->>App: FunctionCall
App->>MT: CallAsync(functionCall)
Note over MT: Auto-reconnect if needed
MT->>MCP: Call Tool
MCP-->>MT: Result
MT->>MT: Convert to FunctionResponse
MT-->>App: FunctionResponse
App->>Gemini: SubmitFunctionResult()
Gemini-->>App: Content
App->>MT: DisposeAsync()
MT->>MCP: Disconnect
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 minutes
Poem
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (9)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary by CodeRabbit
Release Notes
New Features
Documentation