Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: Add MCP (Model Context Protocol) server integration
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
  • Loading branch information
gunpal5 committed Nov 5, 2025
commit 263a75d3b997af61987550c99acffcd540af8fa1
179 changes: 179 additions & 0 deletions MCP_INTEGRATION_SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
# MCP Integration Summary

## Overview

This document summarizes the MCP (Model Context Protocol) integration added to the Google_GenerativeAI SDK.

## What Was Added

### 1. Core Implementation (`src/GenerativeAI.Tools/Mcp/`)

#### McpServerConfig.cs
- `McpServerConfig` class for configuring MCP server connections
- `McpToolOptions` class for controlling McpTool behavior
- Support for custom commands, arguments, environment variables, and working directories

#### McpTool.cs
- Main integration class implementing `IFunctionTool` interface
- Connects to MCP servers via the official C# SDK
- Automatically discovers and exposes tools from MCP servers
- Features:
- Auto-discovery of MCP server tools
- Automatic reconnection support
- Error handling with detailed error responses
- Support for multiple concurrent MCP servers
- Async/await pattern with proper disposal
- Converts MCP tools to Google's `FunctionDeclaration` format
- Forwards function calls to MCP servers
- Handles MCP responses and converts them to `FunctionResponse`

### 2. Example Project (`samples/McpIntegrationDemo/`)

#### Program.cs
Comprehensive demo showing:
- Single MCP server integration
- Multiple MCP servers running simultaneously
- Custom configuration with advanced options
- Real-world usage patterns

#### McpIntegrationDemo.csproj
- .NET 8.0 project
- References to GenerativeAI and GenerativeAI.Tools

### 3. Documentation

#### samples/McpIntegrationDemo/README.md
- Quick start guide
- Usage examples (basic, multiple servers, custom config)
- Configuration options reference
- List of available MCP servers
- API reference
- Troubleshooting guide
- Advanced usage patterns

#### src/GenerativeAI.Tools/Mcp/README.md
- Comprehensive technical documentation
- Architecture overview
- Core components explanation
- Detailed usage examples
- Error handling patterns
- Best practices
- Limitations and troubleshooting

### 4. Dependencies

#### Updated GenerativeAI.Tools.csproj
- Added `ModelContextProtocol` NuGet package (v0.4.0-preview.3)
- Official C# SDK for Model Context Protocol

## Key Features

1. **Easy Integration**: Simple API to connect any MCP server
2. **Auto-Discovery**: Automatically finds and exposes all tools
3. **Multiple Servers**: Connect to multiple MCP servers at once
4. **Robust**: Auto-reconnect, error handling, timeouts
5. **Type-Safe**: Fully typed with nullable reference types
6. **Well-Documented**: Comprehensive docs and examples
7. **IDisposable**: Proper resource management
8. **Async**: Full async/await support

## Usage Example

```csharp
// Configure MCP server
var config = new McpServerConfig
{
Name = "my-server",
Command = "npx",
Arguments = new List<string> { "-y", "@modelcontextprotocol/server-everything" }
};

// Create MCP tool
using var mcpTool = await McpTool.CreateAsync(config);

// Add to Gemini model
var model = new GenerativeModel(apiKey, "gemini-2.0-flash-exp");
model.AddFunctionTool(mcpTool);
model.FunctionCallingBehaviour.AutoCallFunction = true;

// Use it!
var result = await model.GenerateContentAsync("Use the echo tool to say hello!");
```

## Architecture

```
Gemini Model (GenerativeAI)
↓ IFunctionTool interface
McpTool (GenerativeAI.Tools.Mcp)
↓ MCP Protocol
McpClient (ModelContextProtocol SDK)
↓ Stdio Transport
MCP Server (Node.js, Python, C#, etc.)
```

## Files Added

```
src/GenerativeAI.Tools/
├── GenerativeAI.Tools.csproj (modified - added ModelContextProtocol package)
└── Mcp/
├── McpServerConfig.cs (new)
├── McpTool.cs (new)
└── README.md (new)

samples/McpIntegrationDemo/
├── McpIntegrationDemo.csproj (new)
├── Program.cs (new)
└── README.md (new)

MCP_INTEGRATION_SUMMARY.md (new)
```

## Compatibility

- **.NET Target Frameworks**: net8.0, netstandard2.0, net9.0, net462
- **MCP SDK**: ModelContextProtocol v0.4.0-preview.3
- **MCP Servers**: Any server implementing the Model Context Protocol specification

## Testing

The integration should be tested with:

1. **Unit Tests**: Test McpTool creation, connection, and function calling
2. **Integration Tests**: Test with actual MCP servers
3. **Examples**: Run the demo project with various MCP servers

To run the demo:
```bash
export GEMINI_API_KEY=your-api-key
cd samples/McpIntegrationDemo
dotnet run
```

## Future Enhancements

Potential improvements for future versions:

1. **HTTP Transport**: Support HTTP-based MCP servers (requires SDK support)
2. **Connection Pooling**: Reuse connections for better performance
3. **Caching**: Cache tool definitions to reduce refresh overhead
4. **Metrics**: Add telemetry and monitoring
5. **Testing**: Unit and integration tests
6. **More Examples**: Additional real-world examples
7. **Configuration**: JSON-based configuration file support

## References

- [MCP Official Website](https://modelcontextprotocol.io/)
- [MCP C# SDK](https://github.com/modelcontextprotocol/csharp-sdk)
- [MCP Specification](https://spec.modelcontextprotocol.io/)
- [MCP Servers Repository](https://github.com/modelcontextprotocol/servers)

## License

This integration follows the same license as the Google_GenerativeAI SDK.

## Contributing

Contributions, bug reports, and feature requests are welcome!
16 changes: 16 additions & 0 deletions samples/McpIntegrationDemo/McpIntegrationDemo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\GenerativeAI\GenerativeAI.csproj" />
<ProjectReference Include="..\..\src\GenerativeAI.Tools\GenerativeAI.Tools.csproj" />
</ItemGroup>

</Project>
Loading