Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1,045 changes: 792 additions & 253 deletions 05-AdvancedTopics/mcp-root-contexts/README.md

Large diffs are not rendered by default.

97 changes: 97 additions & 0 deletions 05-AdvancedTopics/mcp-root-contexts/solution/csharp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# C# Root Context Example

This example demonstrates how to implement MCP root contexts in a .NET application for customer support scenarios.

## Prerequisites

- .NET 8.0 SDK or later
- MCP .NET Client package
- Visual Studio 2022 or VS Code with C# extension

## Setup

1. **Create a new console application:**
```bash
dotnet new console -n RootContextExample
cd RootContextExample
```

2. **Add the MCP Client package:**
```bash
dotnet add package Microsoft.Mcp.Client
```

3. **Copy the example code:**
- Replace the contents of `Program.cs` with `RootContextExample.cs`

4. **Update the project file (`RootContextExample.csproj`):**
```xml
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Mcp.Client" Version="1.0.0" />
</ItemGroup>
</Project>
```

## Configuration

Update the MCP server URL in the `Main` method:
```csharp
var client = new McpClient("YOUR_MCP_SERVER_URL");
```

## Running the Example

1. **Build the project:**
```bash
dotnet build
```

2. **Run the application:**
```bash
dotnet run
```

## What the Example Demonstrates

- ✅ **Context Creation**: Creating a new root context with metadata
- 💬 **Multi-turn Conversations**: Sending multiple messages that reference previous context
- 📝 **Metadata Updates**: Adding information discovered during conversations
- 📊 **Context Monitoring**: Retrieving context information and statistics
- 🗄️ **Context Archival**: Properly cleaning up completed conversations

## Expected Output

```
✅ Created root context with ID: [context-id]
🤖 AI Response: [AI response about scaling issues]
🤖 AI Response: [AI response referencing Kubernetes]
📝 Updated context metadata with conversation insights
📊 Context Information:
• Name: Customer Support Session
• Created: [timestamp]
• Messages: 2
🗄️ Archived context [context-id]
✅ Root context demonstration completed!
```

## Key Features

- **Enterprise-grade patterns**: Uses dependency injection and async/await
- **Type safety**: Strongly-typed objects with compile-time validation
- **Error handling**: Robust exception handling for production use
- **Metadata management**: Flexible metadata storage and retrieval

## Next Steps

- Integrate with your existing .NET application
- Add error handling and logging
- Implement user authentication
- Scale for production workloads
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// .NET Example: Root Context Management Setup
using Microsoft.Mcp.Client;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;

public class RootContextExample
{
private readonly IMcpClient _client;
private readonly IRootContextManager _contextManager;

public RootContextExample(IMcpClient client, IRootContextManager contextManager)
{
_client = client;
_contextManager = contextManager;
}

public async Task<string> CreateSupportContextAsync()
{
// Create a new root context for customer support
var contextResult = await _contextManager.CreateRootContextAsync(new RootContextCreateOptions
{
Name = "Customer Support Session",
Metadata = new Dictionary<string, string>
{
["CustomerName"] = "Acme Corporation",
["PriorityLevel"] = "High",
["Domain"] = "Cloud Services"
}
});

string contextId = contextResult.ContextId;
Console.WriteLine($"✅ Created root context with ID: {contextId}");
return contextId;
}

public async Task ConductConversationAsync(string contextId)
{
// First interaction using the context
var response1 = await _client.SendPromptAsync(
"I'm having issues scaling my web service deployment in the cloud.",
new SendPromptOptions { RootContextId = contextId }
);
Console.WriteLine($"🤖 AI Response: {response1.GeneratedText}");

// Second interaction - AI remembers the previous conversation
var response2 = await _client.SendPromptAsync(
"Yes, we're using containerized deployments with Kubernetes.",
new SendPromptOptions { RootContextId = contextId }
);
Console.WriteLine($"🤖 AI Response: {response2.GeneratedText}");
}

public async Task UpdateContextInfoAsync(string contextId)
{
// Add metadata based on what we learned from the conversation
await _contextManager.UpdateContextMetadataAsync(contextId, new Dictionary<string, string>
{
["TechnicalEnvironment"] = "Kubernetes",
["IssueType"] = "Scaling",
["Status"] = "In Progress"
});

Console.WriteLine("📝 Updated context metadata with conversation insights");
}

public async Task DisplayContextInfoAsync(string contextId)
{
var contextInfo = await _contextManager.GetRootContextInfoAsync(contextId);

Console.WriteLine("📊 Context Information:");
Console.WriteLine($" • Name: {contextInfo.Name}");
Console.WriteLine($" • Created: {contextInfo.CreatedAt}");
Console.WriteLine($" • Messages: {contextInfo.MessageCount}");
}

public async Task ArchiveContextAsync(string contextId)
{
// When the conversation is complete, archive the context
await _contextManager.ArchiveRootContextAsync(contextId);
Console.WriteLine($"🗄️ Archived context {contextId}");
}

public async Task DemonstrateRootContextAsync()
{
var contextId = await CreateSupportContextAsync();
await ConductConversationAsync(contextId);
await UpdateContextInfoAsync(contextId);
await DisplayContextInfoAsync(contextId);
await ArchiveContextAsync(contextId);
}
}

// Program entry point
public class Program
{
public static async Task Main(string[] args)
{
// Initialize MCP client and context manager
var client = new McpClient("https://mcp-server-example.com");
var contextManager = new RootContextManager(client);

// Run the demonstration
var example = new RootContextExample(client, contextManager);
await example.DemonstrateRootContextAsync();

Console.WriteLine("✅ Root context demonstration completed!");
}
}
99 changes: 99 additions & 0 deletions 05-AdvancedTopics/mcp-root-contexts/solution/dotnet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
````markdown
# .NET Root Context Example

This example demonstrates how to implement MCP root contexts in a .NET application for customer support scenarios.

## Prerequisites

- .NET 8.0 SDK or later
- MCP .NET Client package
- Visual Studio 2022 or VS Code with C# extension

## Setup

1. **Create a new console application:**
```bash
dotnet new console -n RootContextExample
cd RootContextExample
```

2. **Add the MCP Client package:**
```bash
dotnet add package Microsoft.Mcp.Client
```

3. **Copy the example code:**
- Replace the contents of `Program.cs` with `RootContextExample.cs`

4. **Update the project file (`RootContextExample.csproj`):**
```xml
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Mcp.Client" Version="1.0.0" />
</ItemGroup>
</Project>
```

## Configuration

Update the MCP server URL in the `Main` method:
```csharp
var client = new McpClient("YOUR_MCP_SERVER_URL");
```

## Running the Example

1. **Build the project:**
```bash
dotnet build
```

2. **Run the application:**
```bash
dotnet run
```

## What the Example Demonstrates

- ✅ **Context Creation**: Creating a new root context with metadata
- 💬 **Multi-turn Conversations**: Sending multiple messages that reference previous context
- 📝 **Metadata Updates**: Adding information discovered during conversations
- 📊 **Context Monitoring**: Retrieving context information and statistics
- 🗄️ **Context Archival**: Properly cleaning up completed conversations

## Expected Output

```
✅ Created root context with ID: [context-id]
🤖 AI Response: [AI response about scaling issues]
🤖 AI Response: [AI response referencing Kubernetes]
📝 Updated context metadata with conversation insights
📊 Context Information:
• Name: Customer Support Session
• Created: [timestamp]
• Messages: 2
🗄️ Archived context [context-id]
✅ Root context demonstration completed!
```

## Key Features

- **Enterprise-grade patterns**: Uses dependency injection and async/await
- **Type safety**: Strongly-typed objects with compile-time validation
- **Error handling**: Robust exception handling for production use
- **Metadata management**: Flexible metadata storage and retrieval

## Next Steps

- Integrate with your existing .NET application
- Add error handling and logging
- Implement user authentication
- Scale for production workloads
````
Loading
Loading