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 ; 
7+ 
8+ var  builder  =  Host . CreateEmptyApplicationBuilder ( settings :  null ) ; 
9+ 
10+ builder . Configuration 
11+     . AddEnvironmentVariables ( ) 
12+     . AddUserSecrets < Program > ( ) ; 
13+ 
14+ var  mcpClient  =  await  McpClientFactory . CreateAsync ( new ( ) 
15+ { 
16+     Id  =  "weather" , 
17+     Name  =  "Weather" , 
18+     TransportType  =  TransportTypes . StdIo , 
19+     TransportOptions  =  new ( ) 
20+     { 
21+         [ "command" ]  =  "dotnet" , 
22+         [ "arguments" ]  =  "run --project ../QuickstartWeatherServer" , 
23+     } 
24+ } ) ; 
25+ 
26+ var  anthropicClient  =  new  AnthropicClient ( new  APIAuthentication ( builder . Configuration [ "ANTHROPIC_API_KEY" ] ) ) 
27+     . Messages 
28+     . AsBuilder ( ) 
29+     . UseFunctionInvocation ( ) 
30+     . Build ( ) ; 
31+ 
32+ var  tools  =  await  mcpClient . ListToolsAsync ( ) ; 
33+ foreach  ( var  tool  in  tools ) 
34+ { 
35+     Console . WriteLine ( $ "Tool: { tool . Name } ") ; 
36+ } 
37+ 
38+ while  ( true ) 
39+ { 
40+     Console . WriteLine ( "MCP Client Started!" ) ; 
41+     Console . WriteLine ( "Enter a command (or 'exit' to quit):" ) ; 
42+ 
43+     string ?  command  =  Console . ReadLine ( ) ; 
44+ 
45+     if  ( string . IsNullOrWhiteSpace ( command ) ) 
46+     { 
47+         continue ; 
48+     } 
49+     if  ( string . Equals ( command ,  "exit" ,  StringComparison . OrdinalIgnoreCase ) ) 
50+     { 
51+         break ; 
52+     } 
53+ 
54+     var  response  =  await  ProcessQueryAsync ( command ) ; 
55+ 
56+     if  ( string . IsNullOrWhiteSpace ( response ) ) 
57+     { 
58+         Console . WriteLine ( "No response received." ) ; 
59+     } 
60+     else 
61+     { 
62+         Console . WriteLine ( $ "Response: { response } ") ; 
63+     } 
64+ } 
65+ 
66+ async  Task < string >  ProcessQueryAsync ( string  query ) 
67+ { 
68+     var  options  =  new  ChatOptions 
69+     { 
70+         MaxOutputTokens  =  1000 , 
71+         ModelId  =  "claude-3-5-sonnet-20241022" , 
72+         Tools  =  [ .. tools . Cast < AITool > ( ) ] 
73+     } ; 
74+ 
75+     var  response  =  await  anthropicClient . GetResponseAsync ( query ,  options ) ; 
76+ 
77+     return  "" ; 
78+ } 
0 commit comments