This commit is contained in:
65
docs/snippets/csharp/mcp/client.cs
Normal file
65
docs/snippets/csharp/mcp/client.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
```csharp title="client.cs"
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
var client = new McpClient();
|
||||
await client.StartAsync();
|
||||
var content = await client.ExtractFileAsync("document.pdf");
|
||||
Console.WriteLine(content);
|
||||
client.Stop();
|
||||
|
||||
class McpClient
|
||||
{
|
||||
private Process _mcpProcess;
|
||||
private StreamReader _reader;
|
||||
private StreamWriter _writer;
|
||||
|
||||
public async Task StartAsync()
|
||||
{
|
||||
var processInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "kreuzberg",
|
||||
Arguments = "mcp",
|
||||
UseShellExecute = false,
|
||||
RedirectStandardInput = true,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
};
|
||||
|
||||
_mcpProcess = Process.Start(processInfo);
|
||||
_reader = _mcpProcess.StandardOutput;
|
||||
_writer = _mcpProcess.StandardInput;
|
||||
}
|
||||
|
||||
public async Task<string> ExtractFileAsync(string path)
|
||||
{
|
||||
var request = new
|
||||
{
|
||||
method = "tools/call",
|
||||
@params = new
|
||||
{
|
||||
name = "extract_file",
|
||||
arguments = new { path, @async = true }
|
||||
}
|
||||
};
|
||||
|
||||
var jsonRequest = JsonSerializer.Serialize(request);
|
||||
await _writer.WriteLineAsync(jsonRequest);
|
||||
await _writer.FlushAsync();
|
||||
|
||||
var response = await _reader.ReadLineAsync();
|
||||
var json = JsonDocument.Parse(response);
|
||||
return json.RootElement.GetProperty("result").GetProperty("content").GetString();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
_writer?.Dispose();
|
||||
_reader?.Dispose();
|
||||
_mcpProcess?.Kill();
|
||||
}
|
||||
}
|
||||
```
|
||||
38
docs/snippets/csharp/mcp/mcp_custom_client.md
Normal file
38
docs/snippets/csharp/mcp/mcp_custom_client.md
Normal file
@@ -0,0 +1,38 @@
|
||||
<!-- snippet:syntax-only -->
|
||||
|
||||
```csharp title="C#"
|
||||
using System.Diagnostics;
|
||||
using System.Text.Json;
|
||||
|
||||
var processInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "kreuzberg",
|
||||
Arguments = "mcp",
|
||||
UseShellExecute = false,
|
||||
RedirectStandardInput = true,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
};
|
||||
|
||||
using var process = Process.Start(processInfo)
|
||||
?? throw new InvalidOperationException("Failed to start kreuzberg mcp");
|
||||
|
||||
var request = new
|
||||
{
|
||||
method = "tools/call",
|
||||
@params = new
|
||||
{
|
||||
name = "extract_file",
|
||||
arguments = new { path = "document.pdf" },
|
||||
},
|
||||
};
|
||||
|
||||
await process.StandardInput.WriteLineAsync(JsonSerializer.Serialize(request));
|
||||
await process.StandardInput.FlushAsync();
|
||||
|
||||
var line = await process.StandardOutput.ReadLineAsync();
|
||||
Console.WriteLine(line);
|
||||
|
||||
process.StandardInput.Close();
|
||||
await process.WaitForExitAsync();
|
||||
```
|
||||
20
docs/snippets/csharp/mcp/mcp_server_start.md
Normal file
20
docs/snippets/csharp/mcp/mcp_server_start.md
Normal file
@@ -0,0 +1,20 @@
|
||||
<!-- snippet:syntax-only -->
|
||||
|
||||
```csharp title="C#"
|
||||
using System.Diagnostics;
|
||||
|
||||
var processInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "kreuzberg",
|
||||
Arguments = "mcp",
|
||||
UseShellExecute = false,
|
||||
RedirectStandardInput = true,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
};
|
||||
|
||||
using var server = Process.Start(processInfo)
|
||||
?? throw new InvalidOperationException("Failed to start kreuzberg mcp");
|
||||
|
||||
await server.WaitForExitAsync();
|
||||
```
|
||||
50
docs/snippets/csharp/mcp/server.cs
Normal file
50
docs/snippets/csharp/mcp/server.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
```csharp title="server.cs"
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Http;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class McpServer
|
||||
{
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
var processInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "kreuzberg",
|
||||
Arguments = "mcp",
|
||||
UseShellExecute = false,
|
||||
RedirectStandardInput = true,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
};
|
||||
|
||||
var process = Process.Start(processInfo);
|
||||
|
||||
await Task.Delay(Timeout.Infinite);
|
||||
}
|
||||
}
|
||||
|
||||
class McpServerProgram
|
||||
{
|
||||
public static async Task Main()
|
||||
{
|
||||
var server = new KreuzbergMcpServer();
|
||||
|
||||
server.RegisterTool("extract_file", new Dictionary<string, object>
|
||||
{
|
||||
{ "description", "Extract text from a document file" },
|
||||
{ "parameters", new { path = "string" } }
|
||||
});
|
||||
|
||||
server.RegisterTool("extract_bytes", new Dictionary<string, object>
|
||||
{
|
||||
{ "description", "Extract text from document bytes" },
|
||||
{ "parameters", new { data = "string", mimeType = "string" } }
|
||||
});
|
||||
|
||||
await server.StartAsync();
|
||||
}
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user