File tree Expand file tree Collapse file tree 3 files changed +35
-1
lines changed
samples/AspNetCoreSseServer Expand file tree Collapse file tree 3 files changed +35
-1
lines changed Original file line number Diff line number Diff line change 1+ using ModelContextProtocol . Core ;
2+ using ModelContextProtocol . Protocol ;
3+ using ModelContextProtocol . Server ;
4+
5+ namespace AspNetCoreSseServer . Attributes ;
6+
7+ public class LimitCalls ( int maxCalls , int order = 0 ) : ToolFilter ( order )
8+ {
9+ private int _callCount ;
10+
11+ public override ValueTask < CallToolResult > ? OnToolCalling ( Tool tool , RequestContext < CallToolRequestParams > context )
12+ {
13+ _callCount ++ ;
14+ Console . Out . WriteLine ( $ "Tool: { tool . Name } called { _callCount } time(s)") ;
15+
16+ if ( _callCount <= maxCalls )
17+ return null ; //do nothing
18+
19+ return new ValueTask < CallToolResult > ( new CallToolResult ( )
20+ {
21+ Content = [ new TextContentBlock { Text = $ "This tool can only be called { maxCalls } time(s)" } ]
22+ } ) ;
23+ }
24+
25+ public override bool OnToolListed ( Tool tool , RequestContext < ListToolsRequestParams > context )
26+ {
27+ var configuration = context . Services ? . GetService < IConfiguration > ( ) ;
28+ var hide = configuration ? [ "hide-tools-above-limit" ] == "True" ;
29+ return hide && _callCount <= maxCalls ;
30+ }
31+ }
Original file line number Diff line number Diff line change 11using ModelContextProtocol . Server ;
22using System . ComponentModel ;
3+ using AspNetCoreSseServer . Attributes ;
34
45namespace TestServerWithHosting . Tools ;
56
67[ McpServerToolType ]
78public sealed class EchoTool
89{
910 [ McpServerTool , Description ( "Echoes the input back to the client." ) ]
11+ [ LimitCalls ( maxCalls : 10 ) ]
1012 public static string Echo ( string message )
1113 {
1214 return "hello " + message ;
Original file line number Diff line number Diff line change 55 "Microsoft.AspNetCore" : " Warning"
66 }
77 },
8- "AllowedHosts" : " *"
8+ "AllowedHosts" : " *" ,
9+ "hide-tools-above-limit" : true
910}
You can’t perform that action at this time.
0 commit comments