Content
# XiaoZhi MCP Connector
The purpose of this project is to directly connect the MCP Server developed using the [MCP Official C# SDK](https://github.com/modelcontextprotocol/csharp-sdk) to the XiaoZhi AI MCP access point. The core idea is to customize `ITransport` to connect directly to the XiaoZhi AI WebSocket access point, achieving seamless integration of the MCP protocol.
## Project Features
- **WebSocket Transport**: Connection to the XiaoZhi AI MCP access point based on the WebSocket protocol
- **Automatic Reconnection**: Built-in disconnection reconnection mechanism to ensure connection stability
- **Logging Support**: Complete logging for debugging and monitoring
- **Dependency Injection**: Fully supports .NET dependency injection containers
- **Single Session Mode**: Service architecture optimized for single session scenarios
## Quick Start
### Installation
```bash
dotnet add package XiaoZhi.Mcp.Connector
```
### Basic Usage
```csharp
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using XiaoZhi.Mcp.Connector;
var builder = Host.CreateApplicationBuilder(args);
// Configure XiaoZhi MCP Connector
var xiaoZhiMcpAccessPoint = "wss://api.xiaozhi.me/mcp/?token=exxxxxxxxxx"; // Replace with the actual XiaoZhi AI MCP access point address
builder.Services.AddMcpServer()
.WithWebSocketServerTransport(xiaoZhiMcpAccessPoint)
.WithTools<TodoTool>(); // Add custom tools
var host = builder.Build();
await host.RunAsync();
```
### Configuration Options
```csharp
builder.Services.Configure<XiaoZhiWebSocketOptions>(options =>
{
options.WebSocketUrl = "wss://api.xiaozhi.me/mcp/?token=exxxxxxxxxx"; // Replace with the actual XiaoZhi AI MCP access point address
});
builder.Services.AddMcpServer()
.WithWebSocketServerTransport()
.WithTools<TodoTool>(); // Add custom tools
```
## Project Structure
```
src/XiaoZhi.Mcp.Connector/
├── WebSocketMcpServerBuilderExtensions.cs # Extension methods and service registration
├── XiaoZhiWebSocketOptions.cs # Configuration options
├── WebSocketServerTransport.cs # WebSocket transport implementation
├── SingleSessionMcpServerHostedService.cs # Single session hosted service
└── Throw.cs # Parameter validation utility
```
## Core Components
### WebSocketServerTransport
Custom `ITransport` implementation responsible for:
- WebSocket connection management
- Message sending and receiving handling
- Connection status monitoring
- Exception handling
### WebSocketMcpServerBuilderExtensions
Provides a fluent API to configure the MCP server:
```csharp
public static IMcpServerBuilder WithWebSocketServerTransport(this IMcpServerBuilder builder, string webSocketUrl)
```
### SingleSessionMcpServerHostedService
Background service responsible for:
- MCP server lifecycle management
- Single session handling
- Service startup and shutdown
## Dependencies
- ModelContextProtocol >= 0.3.0-preview.3
- Websocket.Client >= 5.2.0
- Microsoft.Extensions.Hosting
- Microsoft.Extensions.Logging
## Example Project
See the complete example in the `samples/Xiaozhi.Mcp.Connector.Demo/` directory
## Configuration Description
### WebSocket Configuration
- **WebSocketUrl**: The WebSocket address of the XiaoZhi AI MCP access point
- **ReconnectTimeout**: Reconnection timeout (default 30 seconds)
- **IsReconnectionEnabled**: Whether to enable automatic reconnection (enabled by default)
### Logging Configuration
The project uses Microsoft.Extensions.Logging for logging, supporting:
- Connection status change logs
- Reconnection event logs
- Disconnection logs
- Error and exception logs
## Development and Contribution
### Build the Project
```bash
dotnet build
```
### Run the Example
```bash
cd samples/Xiaozhi.Mcp.Connector.Demo
dotnet run
```
Connection Info
You Might Also Like
everything-claude-code
Complete Claude Code configuration collection - agents, skills, hooks,...
markitdown
MarkItDown-MCP is a lightweight server for converting URIs to Markdown.
servers
Model Context Protocol Servers
servers
Model Context Protocol Servers
Time
A Model Context Protocol server for time and timezone conversions.
Filesystem
Node.js MCP Server for filesystem operations with dynamic access control.