From 3e35efe79c39252a0596fd026b7fcb71753bccf0 Mon Sep 17 00:00:00 2001
From: Peder Holdgaard Pedersen <127606677+PederHP@users.noreply.github.com>
Date: Sat, 18 Jan 2025 12:34:13 +0100
Subject: [PATCH] Reorganize folders, samples, minor bug fix (#14)
Reorganize folders
Samples
Casing fixes
---
README.MD | 4 +-
src/mcpdotnet.sln => mcpdotnet.sln | 16 ++-
samples/README.md | 41 ++++++++
.../ToolsConsole/AnthropicToolsConsole.csproj | 18 ++++
.../tools/ToolsConsole/McpToolExtensions.cs | 32 ++++++
.../anthropic/tools/ToolsConsole/Program.cs | 97 +++++++++++++++++++
.../Properties/launchSettings.json | 7 ++
.../ToolsConsole/MEAIToolsConsole.csproj | 16 +++
.../tools/ToolsConsole/McpAIFunction.cs | 75 ++++++++++++++
.../tools/ToolsConsole/McpToolExtensions.cs | 11 +++
.../tools/ToolsConsole/McpTypeMapper.cs | 31 ++++++
.../tools/ToolsConsole/Program.cs | 85 ++++++++++++++++
.../Properties/launchSettings.json | 7 ++
.../Protocol/Transport/SseTransport.cs | 1 +
src/mcpdotnet/Protocol/Types/Annotations.cs | 4 +-
.../Protocol/Types/CallToolResponse.cs | 2 +
src/mcpdotnet/Protocol/Types/Capabilities.cs | 4 +-
src/mcpdotnet/Protocol/Types/Content.cs | 2 +-
.../Types/CreateMessageRequestParams.cs | 8 ++
.../Protocol/Types/CreateMessageResult.cs | 2 +-
.../Protocol/Types/GetPromptResult.cs | 2 +
src/mcpdotnet/Protocol/Types/JsonSchema.cs | 3 +
.../Protocol/Types/JsonSchemaProperty.cs | 2 +
.../Protocol/Types/ListPromptsResult.cs | 1 +
.../Protocol/Types/ListResourcesResult.cs | 1 +
.../Protocol/Types/ListRootsRequestParams.cs | 1 +
.../Protocol/Types/ListRootsResult.cs | 2 +
.../Protocol/Types/ListToolsResult.cs | 1 +
src/mcpdotnet/Protocol/Types/ModelHint.cs | 1 +
.../Protocol/Types/ModelPreferences.cs | 18 ++--
src/mcpdotnet/Protocol/Types/Prompt.cs | 3 +
.../Protocol/Types/PromptArgument.cs | 3 +
src/mcpdotnet/Protocol/Types/PromptMessage.cs | 2 +
.../Protocol/Types/ReadResourceResult.cs | 1 +
.../Protocol/Types/ResourceContents.cs | 2 +-
src/mcpdotnet/Protocol/Types/Root.cs | 3 +
.../Protocol/Types/SamplingMessage.cs | 7 +-
src/mcpdotnet/Protocol/Types/Tool.cs | 3 +
src/mcpdotnet/mcpdotnet.csproj | 2 +-
.../mcpdotnet.Tests/GlobalUsings.cs | 0
.../mcpdotnet.Tests/IntegrationTestFixture.cs | 0
.../mcpdotnet.Tests/IntegrationTests.cs | 0
.../mcpdotnet.Tests/McpClientFactoryTests.cs | 0
.../mcpdotnet.Tests/SseIntegrationTests.cs | 0
.../mcpdotnet.Tests/SseTransportTests.cs | 0
.../mcpdotnet.Tests/Utils/TestSseServer.cs | 0
.../mcpdotnet.Tests/mcpdotnet.Tests.csproj | 2 +-
47 files changed, 502 insertions(+), 21 deletions(-)
rename src/mcpdotnet.sln => mcpdotnet.sln (53%)
create mode 100644 samples/README.md
create mode 100644 samples/anthropic/tools/ToolsConsole/AnthropicToolsConsole.csproj
create mode 100644 samples/anthropic/tools/ToolsConsole/McpToolExtensions.cs
create mode 100644 samples/anthropic/tools/ToolsConsole/Program.cs
create mode 100644 samples/anthropic/tools/ToolsConsole/Properties/launchSettings.json
create mode 100644 samples/microsoft.extensions.ai/tools/ToolsConsole/MEAIToolsConsole.csproj
create mode 100644 samples/microsoft.extensions.ai/tools/ToolsConsole/McpAIFunction.cs
create mode 100644 samples/microsoft.extensions.ai/tools/ToolsConsole/McpToolExtensions.cs
create mode 100644 samples/microsoft.extensions.ai/tools/ToolsConsole/McpTypeMapper.cs
create mode 100644 samples/microsoft.extensions.ai/tools/ToolsConsole/Program.cs
create mode 100644 samples/microsoft.extensions.ai/tools/ToolsConsole/Properties/launchSettings.json
rename {src => tests}/mcpdotnet.Tests/GlobalUsings.cs (100%)
rename {src => tests}/mcpdotnet.Tests/IntegrationTestFixture.cs (100%)
rename {src => tests}/mcpdotnet.Tests/IntegrationTests.cs (100%)
rename {src => tests}/mcpdotnet.Tests/McpClientFactoryTests.cs (100%)
rename {src => tests}/mcpdotnet.Tests/SseIntegrationTests.cs (100%)
rename {src => tests}/mcpdotnet.Tests/SseTransportTests.cs (100%)
rename {src => tests}/mcpdotnet.Tests/Utils/TestSseServer.cs (100%)
rename {src => tests}/mcpdotnet.Tests/mcpdotnet.Tests.csproj (94%)
diff --git a/README.MD b/README.MD
index f4c9a9f..a147e2f 100644
--- a/README.MD
+++ b/README.MD
@@ -80,6 +80,8 @@ Note that you should pass CancellationToken objects suitable for your use case,
It is also highly recommended that you pass a proper LoggerFactory instance to the factory constructor, to enable logging of MCP client operations.
+You can find samples demonstrating how to use mcpdotnet with an LLM SDK in the [samples](samples) directory, and also refer to the [IntegrationTests](test/McpDotNet.IntegrationTests) project for more examples.
+
Additional examples and documentation will be added as in the near future.
## Roadmap
@@ -89,7 +91,7 @@ Additional examples and documentation will be added as in the near future.
- Transport configuration
- Error handling and recovery
- Increase test coverage
-- Add example applications
+- Add additional samples and examples
- Performance optimization and profiling
## License
diff --git a/src/mcpdotnet.sln b/mcpdotnet.sln
similarity index 53%
rename from src/mcpdotnet.sln
rename to mcpdotnet.sln
index 874f1b3..11078af 100644
--- a/src/mcpdotnet.sln
+++ b/mcpdotnet.sln
@@ -3,9 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.13.35507.96
MinimumVisualStudioVersion = 10.0.40219.1
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mcpdotnet", "mcpdotnet\mcpdotnet.csproj", "{12260CD2-AFFC-4B2E-8898-F442CAA1FA0F}"
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mcpdotnet", "src\mcpdotnet\mcpdotnet.csproj", "{12260CD2-AFFC-4B2E-8898-F442CAA1FA0F}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mcpdotnet.Tests", "mcpdotnet.Tests\mcpdotnet.Tests.csproj", "{FF41F619-833D-4FA2-8C53-04D0A1D5AA61}"
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mcpdotnet.Tests", "tests\mcpdotnet.Tests\mcpdotnet.Tests.csproj", "{FF41F619-833D-4FA2-8C53-04D0A1D5AA61}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MEAIToolsConsole", "samples\microsoft.extensions.ai\tools\ToolsConsole\MEAIToolsConsole.csproj", "{76E295FA-9E85-7B99-332A-2CDBFCD5860A}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AnthropicToolsConsole", "samples\anthropic\tools\ToolsConsole\AnthropicToolsConsole.csproj", "{CA0BB450-1903-2F92-A68D-1285976551D6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -21,6 +25,14 @@ Global
{FF41F619-833D-4FA2-8C53-04D0A1D5AA61}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FF41F619-833D-4FA2-8C53-04D0A1D5AA61}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FF41F619-833D-4FA2-8C53-04D0A1D5AA61}.Release|Any CPU.Build.0 = Release|Any CPU
+ {76E295FA-9E85-7B99-332A-2CDBFCD5860A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {76E295FA-9E85-7B99-332A-2CDBFCD5860A}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {76E295FA-9E85-7B99-332A-2CDBFCD5860A}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {76E295FA-9E85-7B99-332A-2CDBFCD5860A}.Release|Any CPU.Build.0 = Release|Any CPU
+ {CA0BB450-1903-2F92-A68D-1285976551D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {CA0BB450-1903-2F92-A68D-1285976551D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {CA0BB450-1903-2F92-A68D-1285976551D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {CA0BB450-1903-2F92-A68D-1285976551D6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/samples/README.md b/samples/README.md
new file mode 100644
index 0000000..2c543f0
--- /dev/null
+++ b/samples/README.md
@@ -0,0 +1,41 @@
+# Samples
+
+This directory contains example projects demonstrating how to use mcpdotnet with different LLM SDKs and platforms.
+
+## Environment Setup
+
+Before running the samples, you'll need to set up the following environment variables:
+
+- `ANTHROPIC_API_KEY` - Required for the Anthropic sample
+- `OPENAI_API_KEY` - Required for the MEAI/OpenAI sample
+
+## Sample Projects
+
+### Anthropic Integration
+
+Located in `samples/anthropic`, this sample demonstrates integration with Anthropic's Claude API using mcpdotnet. The example shows how to:
+- Configure an MCP client for use with Anthropic
+- Map between MCP protocol types and Anthropic SDK types
+- Handle tool invocations and responses
+
+### Microsoft.Extensions.AI Integration
+
+Located in `samples/microsoft.extensions.ai`, this sample shows how to use mcpdotnet with Microsoft's AI SDK. It demonstrates:
+- Setting up an MCP client with MEAI
+- Mapping MCP tools to MEAI function calls
+- Handling streaming responses and tool invocations
+
+## Implementation Notes
+
+Each sample focuses on the mapping between MCP types and SDK-specific types, which is the primary integration point when working with different LLM SDKs. The samples provide reusable mapping code that can serve as a starting point for your own implementations.
+
+The complexity of this mapping varies depending on how closely the SDK's design aligns with typical LLM APIs. The provided samples demonstrate best practices for both straightforward and more complex mapping scenarios.
+
+## Running the Samples
+
+1. Clone the repository
+2. Set up the required environment variables
+3. Navigate to the desired sample directory
+4. Run `dotnet run` to execute the sample
+
+For more detailed examples of mcpdotnet usage, you can also refer to the integration tests in the `tests/McpDotNet.IntegrationTests` project.
\ No newline at end of file
diff --git a/samples/anthropic/tools/ToolsConsole/AnthropicToolsConsole.csproj b/samples/anthropic/tools/ToolsConsole/AnthropicToolsConsole.csproj
new file mode 100644
index 0000000..04a14b4
--- /dev/null
+++ b/samples/anthropic/tools/ToolsConsole/AnthropicToolsConsole.csproj
@@ -0,0 +1,18 @@
+
+
+
+ Exe
+ net8.0
+ enable
+ enable
+
+
+
+
+
+
+
+
+
+
+
diff --git a/samples/anthropic/tools/ToolsConsole/McpToolExtensions.cs b/samples/anthropic/tools/ToolsConsole/McpToolExtensions.cs
new file mode 100644
index 0000000..c9dbd20
--- /dev/null
+++ b/samples/anthropic/tools/ToolsConsole/McpToolExtensions.cs
@@ -0,0 +1,32 @@
+using Anthropic.SDK.Common;
+using System.Text.Json;
+using System.Text.Json.Nodes;
+
+public static class McpToolExtensions
+{
+ public static IList ToAnthropicTools(this IEnumerable tools)
+ {
+ List result = new();
+ foreach (var tool in tools)
+ {
+ var function = tool.InputSchema == null
+ ? new Function(tool.Name, tool.Description)
+ : new Function(tool.Name, tool.Description, JsonSerializer.Serialize(tool.InputSchema));
+ result.Add(function);
+ }
+ return result;
+ }
+
+ public static Dictionary? ToMCPArguments(this JsonNode jsonNode)
+ {
+ if (jsonNode == null)
+ return null;
+
+ // Convert JsonNode to Dictionary
+ return jsonNode.AsObject()
+ .ToDictionary(
+ prop => prop.Key,
+ prop => JsonSerializer.Deserialize