Reorganize folders, samples, minor bug fix (#14)
Reorganize folders Samples Casing fixes
This commit is contained in:
parent
e51c78e5e4
commit
3e35efe79c
4 changed files with 501 additions and 20 deletions
|
|
@ -0,0 +1,18 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Anthropic.SDK" Version="4.4.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\..\src\mcpdotnet\mcpdotnet.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
32
samples/anthropic/tools/ToolsConsole/McpToolExtensions.cs
Normal file
32
samples/anthropic/tools/ToolsConsole/McpToolExtensions.cs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
using Anthropic.SDK.Common;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
public static class McpToolExtensions
|
||||
{
|
||||
public static IList<Anthropic.SDK.Common.Tool> ToAnthropicTools(this IEnumerable<McpDotNet.Protocol.Types.Tool> tools)
|
||||
{
|
||||
List<Anthropic.SDK.Common.Tool> 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<string, object>? ToMCPArguments(this JsonNode jsonNode)
|
||||
{
|
||||
if (jsonNode == null)
|
||||
return null;
|
||||
|
||||
// Convert JsonNode to Dictionary<string, object>
|
||||
return jsonNode.AsObject()
|
||||
.ToDictionary(
|
||||
prop => prop.Key,
|
||||
prop => JsonSerializer.Deserialize<object>(prop.Value)!
|
||||
);
|
||||
}
|
||||
}
|
||||
97
samples/anthropic/tools/ToolsConsole/Program.cs
Normal file
97
samples/anthropic/tools/ToolsConsole/Program.cs
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
using McpDotNet.Client;
|
||||
using McpDotNet.Configuration;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Anthropic.SDK;
|
||||
using Anthropic.SDK.Messaging;
|
||||
using Anthropic.SDK.Constants;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
private static async Task<IMcpClient> GetMcpClientAsync()
|
||||
{
|
||||
|
||||
McpClientOptions options = new()
|
||||
{
|
||||
ClientInfo = new() { Name = "SimpleToolsConsole", Version = "1.0.0" }
|
||||
};
|
||||
|
||||
var config = new McpServerConfig
|
||||
{
|
||||
Id = "everything",
|
||||
Name = "Everything",
|
||||
TransportType = "stdio",
|
||||
TransportOptions = new Dictionary<string, string>
|
||||
{
|
||||
["command"] = "npx",
|
||||
["arguments"] = "-y @modelcontextprotocol/server-everything",
|
||||
}
|
||||
};
|
||||
|
||||
var factory = new McpClientFactory(
|
||||
[config],
|
||||
options,
|
||||
NullLoggerFactory.Instance
|
||||
);
|
||||
|
||||
return await factory.GetClientAsync("everything");
|
||||
}
|
||||
|
||||
private static async Task Main(string[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine("Initializing MCP 'everything' server");
|
||||
var client = await GetMcpClientAsync();
|
||||
Console.WriteLine("MCP 'everything' server initialized");
|
||||
Console.WriteLine("Listing tools...");
|
||||
var tools = await client.ListToolsAsync();
|
||||
var anthropicTools = tools.Tools.ToAnthropicTools();
|
||||
Console.WriteLine("Tools available:");
|
||||
foreach (var tool in anthropicTools)
|
||||
{
|
||||
Console.WriteLine(" " + tool.Function.Name);
|
||||
}
|
||||
|
||||
Console.WriteLine("Starting chat with Claude Haiku 3.5...");
|
||||
using var antClient = new AnthropicClient(Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY")!);
|
||||
|
||||
Console.WriteLine("Asking Claude to call the Echo Tool...");
|
||||
|
||||
var messages = new List<Message>
|
||||
{
|
||||
new Message(RoleType.User, "Please call the echo tool with the string 'Hello MCP!' and give me the response as-is.")
|
||||
};
|
||||
|
||||
var parameters = new MessageParameters()
|
||||
{
|
||||
Messages = messages,
|
||||
MaxTokens = 2048,
|
||||
Model = AnthropicModels.Claude35Haiku,
|
||||
Stream = false,
|
||||
Temperature = 1.0m,
|
||||
Tools = anthropicTools
|
||||
};
|
||||
var res = await antClient.Messages.GetClaudeMessageAsync(parameters);
|
||||
|
||||
messages.Add(res.Message);
|
||||
|
||||
foreach (var toolCall in res.ToolCalls)
|
||||
{
|
||||
var response = await client.CallToolAsync(toolCall.Name, toolCall.Arguments?.ToMCPArguments());
|
||||
|
||||
messages.Add(new Message(toolCall, response.Content[0].Text));
|
||||
}
|
||||
|
||||
var finalResult = await antClient.Messages.GetClaudeMessageAsync(parameters);
|
||||
Console.WriteLine("Final result: " + finalResult.Message.ToString());
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
Console.WriteLine("Chat with Claude Haiku 3.5 complete");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Error occurred: " + ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"profiles": {
|
||||
"SimpleToolsConsole": {
|
||||
"commandName": "Project"
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue