55
66namespace HttpContext . Tools ;
77
8- // <snippet_ConstructorParameter>
9- public class ContextTools
10- {
11- private readonly IHttpContextAccessor _httpContextAccessor ;
12-
13- public ContextTools ( IHttpContextAccessor httpContextAccessor )
14- {
15- _httpContextAccessor = httpContextAccessor ;
16- }
178
18- // remainder of ContextTools follows
19- // </snippet_ConstructorParameter>
20-
21- // <snippet_AccessHttpContext>
9+ // <snippet_AccessHttpContext>
10+ public class ContextTools ( IHttpContextAccessor _httpContextAccessor )
11+ {
2212 [ McpServerTool ( UseStructuredContent = true ) ]
2313 [ Description ( "Retrieves the HTTP headers from the current request and returns them as a JSON object." ) ]
2414 public object GetHttpHeaders ( )
@@ -40,47 +30,4 @@ public object GetHttpHeaders()
4030
4131 return headers ;
4232 }
43-
44- [ McpServerTool ( UseStructuredContent = true ) ]
45- [ Description ( "Retrieves the request information from the current HTTP context and returns it as structured content." ) ]
46- public object GetRequestInfo ( )
47- {
48- var context = _httpContextAccessor . HttpContext ;
49- if ( context == null )
50- {
51- return new { Error = "No HTTP context available" } ;
52- }
53-
54- var requestInfo = new
55- {
56- context . Request . Method ,
57- Path = context . Request . Path . Value ,
58- QueryString = context . Request . QueryString . Value ,
59- context . Request . ContentType ,
60- UserAgent = context . Request . Headers . UserAgent . ToString ( ) ,
61- RemoteIpAddress = context . Connection . RemoteIpAddress ? . ToString ( ) ,
62- context . Request . IsHttps
63- } ;
64-
65- return requestInfo ;
66- }
67-
68- [ McpServerTool ( UseStructuredContent = true ) ]
69- [ Description ( "Retrieves the user claims from the current HTTP context and returns them as a JSON object." ) ]
70- public object GetUserClaims ( )
71- {
72- var context = _httpContextAccessor . HttpContext ;
73- if ( context == null )
74- {
75- return "No HTTP context available" ;
76- }
77-
78- var claims = context . User . Claims . Select ( c => new
79- {
80- c . Type ,
81- c . Value
82- } ) . ToList ( ) ;
83-
84- return claims ;
85- }
8633}
0 commit comments