Add conceptual docs on accessing HttpContext (#771)
* Add doc with sample on accessing HttpContext * Apply suggestions from PR review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
parent
ab6d3e1ac5
commit
39b0ed3c18
1 changed files with 288 additions and 2 deletions
28
docs/concepts/httpcontext/samples/Tools/ContextTools.cs
Normal file
28
docs/concepts/httpcontext/samples/Tools/ContextTools.cs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
using ModelContextProtocol.Server;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace HttpContext.Tools;
|
||||
|
||||
// <snippet_AccessHttpContext>
|
||||
public class ContextTools(IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
[McpServerTool(UseStructuredContent = true)]
|
||||
[Description("Retrieves the HTTP headers from the current request and returns them as a JSON object.")]
|
||||
public object GetHttpHeaders()
|
||||
{
|
||||
var context = httpContextAccessor.HttpContext;
|
||||
if (context == null)
|
||||
{
|
||||
return "No HTTP context available";
|
||||
}
|
||||
|
||||
var headers = new Dictionary<string, string>();
|
||||
foreach (var header in context.Request.Headers)
|
||||
{
|
||||
headers[header.Key] = string.Join(", ", header.Value.ToArray());
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
||||
// </snippet_AccessHttpContext>
|
||||
}
|
||||
Loading…
Reference in a new issue