Skip to content

Commit c6ae222

Browse files
committed
Address PR review comments
1 parent c65a120 commit c6ae222

File tree

3 files changed

+7
-94
lines changed

3 files changed

+7
-94
lines changed

docs/concepts/httpcontext/httpcontext.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ The following code snippet illustrates how to add the [IHttpContextAccessor] ser
2222

2323
[!code-csharp[](samples/Program.cs?name=snippet_AddHttpContextAccessor)]
2424

25-
If `ContextTools` is a class whose methods need access to the [HttpContext], the following snippet shows how it can accept
26-
an [IHttpContextAccessor] in its constructor and store it for later use:
25+
Any class that needs access to the [HttpContext] can accept an [IHttpContextAccessor] in its constructor and store it for later use.
26+
Methods of the class can then access the current [HttpContext] using the stored accessor.
2727

28-
[!code-csharp[](samples/Tools/ContextTools.cs?name=snippet_ConstructorParameter)]
28+
The following code snippet shows the `ContextTools` class accepting an [IHttpContextAccessor] in its primary constructor
29+
and the `GetHttpHeaders` method accessing the current [HttpContext] to retrieve the HTTP headers from the current request.
2930

30-
A method of `ContextTools` can then access the current [HttpContext] as follows:
31-
32-
<!-- highlight the last 5 lines -->
3331
[!code-csharp[](samples/Tools/ContextTools.cs?name=snippet_AccessHttpContext)]

docs/concepts/httpcontext/samples/HttpContext.http

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,3 @@ MCP-Protocol-Version: 2025-06-18
1313
"name": "get_http_headers"
1414
}
1515
}
16-
17-
###
18-
19-
POST {{HostAddress}}/
20-
Accept: application/json, text/event-stream
21-
Content-Type: application/json
22-
MCP-Protocol-Version: 2025-06-18
23-
24-
{
25-
"jsonrpc": "2.0",
26-
"id": 2,
27-
"method": "tools/call",
28-
"params": {
29-
"name": "get_request_info"
30-
}
31-
}
32-
33-
###
34-
35-
POST {{HostAddress}}/
36-
Accept: application/json, text/event-stream
37-
Content-Type: application/json
38-
MCP-Protocol-Version: 2025-06-18
39-
40-
{
41-
"jsonrpc": "2.0",
42-
"id": 2,
43-
"method": "tools/call",
44-
"params": {
45-
"name": "get_user_claims"
46-
}
47-
}

docs/concepts/httpcontext/samples/Tools/ContextTools.cs

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,10 @@
55

66
namespace 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

Comments
 (0)