1- using Anthropic . SDK ;
2- using Microsoft . Extensions . AI ;
3- using Microsoft . Extensions . Configuration ;
4- using Microsoft . Extensions . Hosting ;
5- using ModelContextProtocol . Client ;
6- using ModelContextProtocol . Protocol . Transport ;
1+ // using Anthropic.SDK;
2+ // using Microsoft.Extensions.AI;
3+ // using Microsoft.Extensions.Configuration;
4+ // using Microsoft.Extensions.Hosting;
5+ // using ModelContextProtocol.Client;
6+ // using ModelContextProtocol.Protocol.Transport;
77
8- var builder = Host . CreateEmptyApplicationBuilder ( settings : null ) ;
8+ // var builder = Host.CreateEmptyApplicationBuilder(settings: null);
99
10- builder . Configuration
11- . AddEnvironmentVariables ( )
12- . AddUserSecrets < Program > ( ) ;
10+ // builder.Configuration
11+ // .AddEnvironmentVariables()
12+ // .AddUserSecrets<Program>();
1313
14- var ( command , arguments ) = GetCommandAndArguments ( args ) ;
14+ // var (command, arguments) = GetCommandAndArguments(args);
1515
16- await using var mcpClient = await McpClientFactory . CreateAsync ( new ( )
17- {
18- Id = "demo-server" ,
19- Name = "Demo Server" ,
20- TransportType = TransportTypes . StdIo ,
21- TransportOptions = new ( )
22- {
23- [ "command" ] = command ,
24- [ "arguments" ] = arguments ,
25- }
26- } ) ;
16+ // await using var mcpClient = await McpClientFactory.CreateAsync(new()
17+ // {
18+ // Id = "demo-server",
19+ // Name = "Demo Server",
20+ // TransportType = TransportTypes.StdIo,
21+ // TransportOptions = new()
22+ // {
23+ // ["command"] = command,
24+ // ["arguments"] = arguments,
25+ // }
26+ // });
2727
28- var tools = await mcpClient . ListToolsAsync ( ) ;
29- foreach ( var tool in tools )
30- {
31- Console . WriteLine ( $ "Connected to server with tools: { tool . Name } ") ;
32- }
28+ // var tools = await mcpClient.ListToolsAsync();
29+ // foreach (var tool in tools)
30+ // {
31+ // Console.WriteLine($"Connected to server with tools: {tool.Name}");
32+ // }
3333
34- using var anthropicClient = new AnthropicClient ( new APIAuthentication ( builder . Configuration [ "ANTHROPIC_API_KEY" ] ) )
35- . Messages
36- . AsBuilder ( )
37- . UseFunctionInvocation ( )
38- . Build ( ) ;
34+ // using var anthropicClient = new AnthropicClient(new APIAuthentication(builder.Configuration["ANTHROPIC_API_KEY"]))
35+ // .Messages
36+ // .AsBuilder()
37+ // .UseFunctionInvocation()
38+ // .Build();
3939
40- var options = new ChatOptions
41- {
42- MaxOutputTokens = 1000 ,
43- ModelId = "claude-3-5-sonnet-20241022" ,
44- Tools = [ .. tools ]
45- } ;
40+ // var options = new ChatOptions
41+ // {
42+ // MaxOutputTokens = 1000,
43+ // ModelId = "claude-3-5-sonnet-20241022",
44+ // Tools = [.. tools]
45+ // };
4646
47- Console . ForegroundColor = ConsoleColor . Green ;
48- Console . WriteLine ( "MCP Client Started!" ) ;
49- Console . ResetColor ( ) ;
47+ // Console.ForegroundColor = ConsoleColor.Green;
48+ // Console.WriteLine("MCP Client Started!");
49+ // Console.ResetColor();
5050
51- PromptForInput ( ) ;
52- while ( Console . ReadLine ( ) is string query && ! "exit" . Equals ( query , StringComparison . OrdinalIgnoreCase ) )
53- {
54- if ( string . IsNullOrWhiteSpace ( query ) )
55- {
56- PromptForInput ( ) ;
57- continue ;
58- }
51+ // PromptForInput();
52+ // while(Console.ReadLine() is string query && !"exit".Equals(query, StringComparison.OrdinalIgnoreCase))
53+ // {
54+ // if (string.IsNullOrWhiteSpace(query))
55+ // {
56+ // PromptForInput();
57+ // continue;
58+ // }
5959
60- await foreach ( var message in anthropicClient . GetStreamingResponseAsync ( query , options ) )
61- {
62- Console . Write ( message ) ;
63- }
64- Console . WriteLine ( ) ;
60+ // await foreach (var message in anthropicClient.GetStreamingResponseAsync(query, options))
61+ // {
62+ // Console.Write(message);
63+ // }
64+ // Console.WriteLine();
6565
66- PromptForInput ( ) ;
67- }
66+ // PromptForInput();
67+ // }
6868
69- static void PromptForInput ( )
70- {
71- Console . WriteLine ( "Enter a command (or 'exit' to quit):" ) ;
72- Console . ForegroundColor = ConsoleColor . Cyan ;
73- Console . Write ( "> " ) ;
74- Console . ResetColor ( ) ;
75- }
76-
77- /// <summary>
78- /// Determines the command (executable) to run and the script/path to pass to it. This allows different
79- /// languages/runtime environments to be used as the MCP server.
80- /// </summary>
81- /// <remarks>
82- /// This method uses the file extension of the first argument to determine the command, if it's py, it'll run python,
83- /// if it's js, it'll run node, if it's a directory or a csproj file, it'll run dotnet.
84- ///
85- /// If no arguments are provided, it defaults to running the QuickstartWeatherServer project from the current repo.
86- ///
87- /// This method would only be required if you're creating a generic client, such as we use for the quickstart.
88- /// </remarks>
89- static ( string command , string arguments ) GetCommandAndArguments ( string [ ] args )
69+ //static void PromptForInput()
70+ //{
71+ // Console.WriteLine("Enter a command (or 'exit' to quit):");
72+ // Console.ForegroundColor = ConsoleColor.Cyan;
73+ // Console.Write("> ");
74+ // Console.ResetColor();
75+ //}
76+
77+ ///// <summary>
78+ ///// Determines the command (executable) to run and the script/path to pass to it. This allows different
79+ ///// languages/runtime environments to be used as the MCP server.
80+ ///// </summary>
81+ ///// <remarks>
82+ ///// This method uses the file extension of the first argument to determine the command, if it's py, it'll run python,
83+ ///// if it's js, it'll run node, if it's a directory or a csproj file, it'll run dotnet.
84+ /////
85+ ///// If no arguments are provided, it defaults to running the QuickstartWeatherServer project from the current repo.
86+ /////
87+ ///// This method would only be required if you're creating a generic client, such as we use for the quickstart.
88+ ///// </remarks>
89+ //static (string command, string arguments) GetCommandAndArguments(string[] args)
90+ //{
91+ // return args switch
92+ // {
93+ // [var script] when script.EndsWith(".py") => ("python", script),
94+ // [var script] when script.EndsWith(".js") => ("node", script),
95+ // [var script] when Directory.Exists(script) || (File.Exists(script) && script.EndsWith(".csproj")) => ("dotnet", $"run --project {script} --no-build"),
96+ // _ => ("dotnet", "run --project ../../../../QuickstartWeatherServer --no-build")
97+ // };
98+ //}
99+
100+ using ModelContextProtocol . Protocol . Transport ;
101+ using ModelContextProtocol . Protocol . Types ;
102+ using ModelContextProtocol . Server ;
103+ using System . Text . Json ;
104+
105+ McpServerOptions options = new ( )
90106{
91- return args switch
107+ ServerInfo = new ( ) { Name = "MyServer" , Version = "1.0.0" } ,
108+ Capabilities = new ( )
92109 {
93- [ var script ] when script . EndsWith ( ".py" ) => ( "python" , script ) ,
94- [ var script ] when script . EndsWith ( ".js" ) => ( "node" , script ) ,
95- [ var script ] when Directory . Exists ( script ) || ( File . Exists ( script ) && script . EndsWith ( ".csproj" ) ) => ( "dotnet" , $ "run --project { script } --no-build") ,
96- _ => ( "dotnet" , "run --project ../../../../QuickstartWeatherServer --no-build" )
97- } ;
98- }
110+ Tools = new ( )
111+ {
112+ ListToolsHandler = ( request , cancellationToken ) =>
113+ Task . FromResult ( new ListToolsResult ( )
114+ {
115+ Tools =
116+ [
117+ new Tool ( )
118+ {
119+ Name = "echo" ,
120+ Description = "Echoes the input back to the client." ,
121+ InputSchema = JsonSerializer . Deserialize < JsonElement > ( """
122+ {
123+ "type": "object",
124+ "properties": {
125+ "message": {
126+ "type": "string",
127+ "description": "The input to echo back"
128+ }
129+ },
130+ "required": ["message"]
131+ }
132+ """ ) ,
133+ }
134+ ]
135+ } ) ,
136+
137+ CallToolHandler = ( request , cancellationToken ) =>
138+ {
139+ if ( request . Params ? . Name == "echo" )
140+ {
141+ if ( request . Params . Arguments ? . TryGetValue ( "message" , out var message ) is not true )
142+ {
143+ throw new McpServerException ( "Missing required argument 'message'" ) ;
144+ }
145+
146+ return Task . FromResult ( new CallToolResponse ( )
147+ {
148+ Content = [ new Content ( ) { Text = $ "Echo: { message } ", Type = "text" } ]
149+ } ) ;
150+ }
151+
152+ throw new McpServerException ( $ "Unknown tool: '{ request . Params ? . Name } '") ;
153+ } ,
154+ }
155+ } ,
156+ } ;
157+
158+ await using IMcpServer server = McpServerFactory . Create ( new StdioServerTransport ( "MyServer" ) , options ) ;
159+
160+ await server . StartAsync ( ) ;
161+
162+ // Run until process is stopped by the client (parent process)
163+ await Task . Delay ( Timeout . Infinite ) ;
0 commit comments