|
| 1 | +using Microsoft.AspNetCore.Http; |
| 2 | +using ModelContextProtocol.Server; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Text.Json; |
| 5 | + |
| 6 | +namespace HttpContext.Tools; |
| 7 | + |
| 8 | +// <snippet_ConstructorParameter> |
| 9 | +public class ContextTools |
| 10 | +{ |
| 11 | + private readonly IHttpContextAccessor _httpContextAccessor; |
| 12 | + |
| 13 | + public ContextTools(IHttpContextAccessor httpContextAccessor) |
| 14 | + { |
| 15 | + _httpContextAccessor = httpContextAccessor; |
| 16 | + } |
| 17 | + |
| 18 | + // remainder of ContextTools follows |
| 19 | + // </snippet_ConstructorParameter> |
| 20 | + |
| 21 | + // <snippet_AccessHttpContext> |
| 22 | + [McpServerTool(UseStructuredContent = true)] |
| 23 | + [Description("Retrieves the HTTP headers from the current request and returns them as a JSON object.")] |
| 24 | + public object GetHttpHeaders() |
| 25 | + { |
| 26 | + var context = _httpContextAccessor.HttpContext; |
| 27 | + if (context == null) |
| 28 | + { |
| 29 | + return "No HTTP context available"; |
| 30 | + } |
| 31 | + |
| 32 | + // Remainder of GetHttpHeaders method follows |
| 33 | + // </snippet_AccessHttpContext> |
| 34 | + |
| 35 | + var headers = new Dictionary<string, string>(); |
| 36 | + foreach (var header in context.Request.Headers) |
| 37 | + { |
| 38 | + headers[header.Key] = string.Join(", ", header.Value.ToArray()); |
| 39 | + } |
| 40 | + |
| 41 | + return headers; |
| 42 | + } |
| 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 | + } |
| 86 | +} |
0 commit comments