forked from modelcontextprotocol/csharp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteractiveTools.cs
More file actions
126 lines (111 loc) · 3.75 KB
/
InteractiveTools.cs
File metadata and controls
126 lines (111 loc) · 3.75 KB
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
using System.ComponentModel;
using System.Text.Json;
using ModelContextProtocol;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
using static ModelContextProtocol.Protocol.ElicitRequestParams;
namespace Elicitation.Tools;
[McpServerToolType]
public sealed class InteractiveTools
{
// <snippet_GuessTheNumber>
[McpServerTool, Description("A simple game where the user has to guess a number between 1 and 10.")]
public async Task<string> GuessTheNumber(
McpServer server, // Get the McpServer from DI container
CancellationToken token
)
{
// Check if the client supports elicitation
if (server.ClientCapabilities?.Elicitation == null)
{
// fail the tool call
throw new McpException("Client does not support elicitation");
}
// First ask the user if they want to play
var playSchema = new RequestSchema
{
Properties =
{
["Answer"] = new BooleanSchema()
}
};
var playResponse = await server.ElicitAsync(new ElicitRequestParams
{
Message = "Do you want to play a game?",
RequestedSchema = playSchema
}, token);
// Check if user wants to play
if (playResponse.Action != "accept" || playResponse.Content?["Answer"].ValueKind != JsonValueKind.True)
{
return "Maybe next time!";
}
// </snippet_GuessTheNumber>
// Now ask the user to enter their name
var nameSchema = new RequestSchema
{
Properties =
{
["Name"] = new StringSchema()
{
Description = "Name of the player",
MinLength = 2,
MaxLength = 50,
}
}
};
var nameResponse = await server.ElicitAsync(new ElicitRequestParams
{
Message = "What is your name?",
RequestedSchema = nameSchema
}, token);
if (nameResponse.Action != "accept")
{
return "Maybe next time!";
}
string? playerName = nameResponse.Content?["Name"].GetString();
// Generate a random number between 1 and 10
Random random = new Random();
int targetNumber = random.Next(1, 11); // 1 to 10 inclusive
int attempts = 0;
var message = "Guess a number between 1 and 10";
while (true)
{
attempts++;
var guessSchema = new RequestSchema
{
Properties =
{
["Guess"] = new NumberSchema()
{
Type = "integer",
Minimum = 1,
Maximum = 10,
}
}
};
var guessResponse = await server.ElicitAsync(new ElicitRequestParams
{
Message = message,
RequestedSchema = guessSchema
}, token);
if (guessResponse.Action != "accept")
{
return "Maybe next time!";
}
int guess = (int)(guessResponse.Content?["Guess"].GetInt32())!;
// Check if the guess is correct
if (guess == targetNumber)
{
return $"Congratulations {playerName}! You guessed the number {targetNumber} in {attempts} attempts!";
}
else if (guess < targetNumber)
{
message = $"Your guess is too low! Try again (Attempt #{attempts}):";
}
else
{
message = $"Your guess is too high! Try again (Attempt #{attempts}):";
}
}
}
}