1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
using Anthropic.SDK; using Anthropic.SDK.Messaging; using Microsoft.Extensions.AI; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using ModelContextProtocol.Client; using ModelContextProtocol.Protocol.Transport; var builder = Host.CreateApplicationBuilder(args); builder.Configuration .AddEnvironmentVariables() .AddUserSecrets<Program>(); var (command, arguments) = GetCommandAndArguments(args); await using var mcpClient = await McpClientFactory.CreateAsync(new() { Id = "demo-server", Name = "Demo Server", TransportType = TransportTypes.Sse, Location = "https://localhost:7133/sse", //TransportType = TransportTypes.StdIo, //TransportOptions = new() //{ // ["command"] = command, // ["arguments"] = arguments, //} }); var tools = await mcpClient.ListToolsAsync(); foreach (var tool in tools) { Console.WriteLine($"Connected to server with tools: {tool.Name}"); } //using var anthropicClient = new AnthropicClient(new APIAuthentication(builder.Configuration["ANTHROPIC_API_KEY"])) // .Messages // .AsBuilder() // .UseFunctionInvocation() // .Build(); //var options = new ChatOptions //{ // MaxOutputTokens = 1000, // ModelId = "claude-3-5-sonnet-20241022", // Tools = [.. tools] //}; Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("MCP Client Started!"); Console.ResetColor(); PromptForInput(); while (Console.ReadLine() is string query && !"exit".Equals(query, StringComparison.OrdinalIgnoreCase)) { if (string.IsNullOrWhiteSpace(query)) { PromptForInput(); continue; } string method; Dictionary<string, object?> parameters; if (query.StartsWith("forecast", StringComparison.OrdinalIgnoreCase)) { method = "GetForecast"; parameters = new Dictionary<string, object?> { ["latitude"] = 39.7456, ["longitude"] = -97.0892 }; } else if (query.StartsWith("alerts", StringComparison.OrdinalIgnoreCase)) { method = "GetAlerts"; parameters = new Dictionary<string, object?> { ["state"] = "KS" }; } else if (query.StartsWith("echo ", StringComparison.OrdinalIgnoreCase)) { method = "Echo"; parameters = new() { ["message"] = query.Substring(5) }; } else if (query.StartsWith("sample ", StringComparison.OrdinalIgnoreCase)) { method = "sampleLLM"; var promptText = query.Substring(7).Trim(); if (string.IsNullOrEmpty(promptText)) { Console.WriteLine("Please provide a prompt, e.g. sample Hello AI"); PromptForInput(); continue; } parameters = new() { ["prompt"] = promptText, ["maxTokens"] = 100 }; } else { Console.WriteLine("Unknown command. Try: 'forecast' or 'alerts'"); PromptForInput(); continue; } Console.WriteLine($"Invoking {method}..."); var response = await mcpClient.CallToolAsync(method, parameters); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Response:"); Console.ResetColor(); if (response is ModelContextProtocol.Protocol.Types.CallToolResponse toolResponse) { foreach (var content in toolResponse.Content) { if (!string.IsNullOrWhiteSpace(content.Text)) { Console.WriteLine(content.Text); } else if (content.Data is string data) { Console.WriteLine(data); } else if (content.Resource is { Uri: not null } resource) { Console.WriteLine($"[Resource]: {resource.Uri}"); } else { Console.WriteLine("[Unknown content format]"); } } } else { Console.WriteLine(response?.ToString() ?? "[null response]"); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Response:"); Console.ResetColor(); Console.WriteLine(response); PromptForInput(); } static void PromptForInput() { Console.WriteLine("Enter a command (or 'exit' to quit):"); Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("> "); Console.ResetColor(); } /// <summary> /// Determines the command (executable) to run and the script/path to pass to it. This allows different /// languages/runtime environments to be used as the MCP server. /// </summary> /// <remarks> /// This method uses the file extension of the first argument to determine the command, if it's py, it'll run python, /// if it's js, it'll run node, if it's a directory or a csproj file, it'll run dotnet. /// /// If no arguments are provided, it defaults to running the QuickstartWeatherServer project from the current repo. /// /// This method would only be required if you're creating a generic client, such as we use for the quickstart. /// </remarks> static (string command, string arguments) GetCommandAndArguments(string[] args) { return ("C:\\Users\\User\\source\\repos\\mcp-csharp-sdk\\artifacts\\bin\\QuickstartWeatherServer\\Debug\\net8.0\\QuickstartWeatherServer.exe", ""); //return args switch //{ // [var script] when script.EndsWith(".py") => ("python", script), // [var script] when script.EndsWith(".js") => ("node", script), // [var script] when Directory.Exists(script) || (File.Exists(script) && script.EndsWith(".csproj")) => ("dotnet", $"run --project {script} --no-build"), // _ => ("dotnet", "run --project ../../../QuickstartWeatherServer --no-build") //}; } |