| | | 1 | | using System.Text.Json.Nodes; |
| | | 2 | | using Kestrun.Hosting; |
| | | 3 | | namespace Kestrun.Mcp; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Structured error information returned by Kestrun MCP services. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <param name="Code">Stable error code.</param> |
| | | 9 | | /// <param name="Message">Human-readable explanation.</param> |
| | | 10 | | /// <param name="Details">Optional machine-readable details.</param> |
| | | 11 | | public sealed record KestrunMcpError( |
| | | 12 | | string Code, |
| | | 13 | | string Message, |
| | | 14 | | IReadOnlyDictionary<string, object?>? Details = null); |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Summarized route metadata for MCP discovery operations. |
| | | 18 | | /// </summary> |
| | | 19 | | public sealed record KestrunRouteSummary |
| | | 20 | | { |
| | | 21 | | /// <summary>Route pattern.</summary> |
| | | 22 | | public required string Pattern { get; init; } |
| | | 23 | | /// <summary>HTTP methods.</summary> |
| | | 24 | | public required IReadOnlyList<string> Verbs { get; init; } |
| | | 25 | | /// <summary>OpenAPI tags.</summary> |
| | | 26 | | public required IReadOnlyList<string> Tags { get; init; } |
| | | 27 | | /// <summary>OpenAPI summary.</summary> |
| | | 28 | | public string? Summary { get; init; } |
| | | 29 | | /// <summary>OpenAPI description.</summary> |
| | | 30 | | public string? Description { get; init; } |
| | | 31 | | /// <summary>Supported request content types.</summary> |
| | | 32 | | public required IReadOnlyList<string> RequestContentTypes { get; init; } |
| | | 33 | | /// <summary>Supported response content types.</summary> |
| | | 34 | | public required IReadOnlyList<string> ResponseContentTypes { get; init; } |
| | | 35 | | /// <summary>Bound handler name when available.</summary> |
| | | 36 | | public string? HandlerName { get; init; } |
| | | 37 | | /// <summary>Best-effort script/runtime language.</summary> |
| | | 38 | | public string? HandlerLanguage { get; init; } |
| | | 39 | | /// <summary>OpenAPI operation identifier when available.</summary> |
| | | 40 | | public string? OperationId { get; init; } |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Detailed route metadata for a single route selection. |
| | | 45 | | /// </summary> |
| | | 46 | | public sealed record KestrunRouteDetail |
| | | 47 | | { |
| | | 48 | | /// <summary>Route summary payload.</summary> |
| | | 49 | | public required KestrunRouteSummary Route { get; init; } |
| | | 50 | | /// <summary>Request schema keyed by content type when available.</summary> |
| | | 51 | | public required IReadOnlyDictionary<string, JsonNode?> RequestSchemas { get; init; } |
| | | 52 | | /// <summary>Response metadata keyed by status code.</summary> |
| | | 53 | | public required IReadOnlyDictionary<string, KestrunRouteResponseSchema> Responses { get; init; } |
| | | 54 | | /// <summary>Route lookup error when selection fails.</summary> |
| | | 55 | | public KestrunMcpError? Error { get; init; } |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Response metadata extracted from OpenAPI. |
| | | 60 | | /// </summary> |
| | | 61 | | public sealed record KestrunRouteResponseSchema |
| | | 62 | | { |
| | | 63 | | /// <summary>Response description.</summary> |
| | | 64 | | public string? Description { get; init; } |
| | | 65 | | /// <summary>Response schemas keyed by content type.</summary> |
| | | 66 | | public required IReadOnlyDictionary<string, JsonNode?> Content { get; init; } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Structured OpenAPI document payload. |
| | | 71 | | /// </summary> |
| | | 72 | | public sealed record KestrunOpenApiDocumentResult |
| | | 73 | | { |
| | | 74 | | /// <summary>Requested document id.</summary> |
| | | 75 | | public required string DocumentId { get; init; } |
| | | 76 | | /// <summary>Resolved OpenAPI version string.</summary> |
| | | 77 | | public required string Version { get; init; } |
| | | 78 | | /// <summary>Structured document payload.</summary> |
| | | 79 | | public JsonNode? Document { get; init; } |
| | | 80 | | /// <summary>Lookup error when retrieval fails.</summary> |
| | | 81 | | public KestrunMcpError? Error { get; init; } |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Listener metadata exposed through runtime inspection. |
| | | 86 | | /// </summary> |
| | | 87 | | public sealed record KestrunRuntimeListener |
| | | 88 | | { |
| | | 89 | | /// <summary>Listener URL.</summary> |
| | 1 | 90 | | public required string Url { get; init; } |
| | | 91 | | /// <summary>Transport protocols.</summary> |
| | 1 | 92 | | public required string Protocols { get; init; } |
| | | 93 | | /// <summary>Whether HTTPS is enabled.</summary> |
| | 1 | 94 | | public bool UseHttps { get; init; } |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Safe runtime inspection payload. |
| | | 99 | | /// </summary> |
| | | 100 | | public sealed record KestrunRuntimeInspectionResult |
| | | 101 | | { |
| | | 102 | | /// <summary>Application name.</summary> |
| | | 103 | | public required string ApplicationName { get; init; } |
| | | 104 | | /// <summary>Host status.</summary> |
| | | 105 | | public required string Status { get; init; } |
| | | 106 | | /// <summary>Environment name.</summary> |
| | | 107 | | public required string Environment { get; init; } |
| | | 108 | | /// <summary>Start timestamp in UTC.</summary> |
| | | 109 | | public DateTime? StartTimeUtc { get; init; } |
| | | 110 | | /// <summary>Stop timestamp in UTC.</summary> |
| | | 111 | | public DateTime? StopTimeUtc { get; init; } |
| | | 112 | | /// <summary>Current uptime when available.</summary> |
| | | 113 | | public TimeSpan? Uptime { get; init; } |
| | | 114 | | /// <summary>Known listeners.</summary> |
| | | 115 | | public required IReadOnlyList<KestrunRuntimeListener> Listeners { get; init; } |
| | | 116 | | /// <summary>Known route count.</summary> |
| | | 117 | | public int RouteCount { get; init; } |
| | | 118 | | /// <summary>Selected safe configuration values.</summary> |
| | | 119 | | public required IReadOnlyDictionary<string, object?> Configuration { get; init; } |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Proposed request payload used by validation and invocation tools. |
| | | 124 | | /// </summary> |
| | | 125 | | public sealed record KestrunRequestInput |
| | | 126 | | { |
| | | 127 | | /// <summary>HTTP method.</summary> |
| | | 128 | | public string Method { get; init; } = "GET"; |
| | | 129 | | /// <summary>Request path.</summary> |
| | | 130 | | public string Path { get; init; } = "/"; |
| | | 131 | | /// <summary>Request headers.</summary> |
| | | 132 | | public IDictionary<string, string>? Headers { get; init; } |
| | | 133 | | /// <summary>Request query values.</summary> |
| | | 134 | | public IDictionary<string, string>? Query { get; init; } |
| | | 135 | | /// <summary>Request body.</summary> |
| | | 136 | | public object? Body { get; init; } |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Request validation result. |
| | | 141 | | /// </summary> |
| | | 142 | | public sealed record KestrunRequestValidationResult |
| | | 143 | | { |
| | | 144 | | /// <summary>Whether the request would likely succeed.</summary> |
| | | 145 | | public bool IsValid { get; init; } |
| | | 146 | | /// <summary>Likely resulting status code.</summary> |
| | | 147 | | public int StatusCode { get; init; } |
| | | 148 | | /// <summary>Best-effort explanation.</summary> |
| | | 149 | | public required string Message { get; init; } |
| | | 150 | | /// <summary>Matched route summary when available.</summary> |
| | | 151 | | public KestrunRouteSummary? Route { get; init; } |
| | | 152 | | /// <summary>Validation error details.</summary> |
| | | 153 | | public KestrunMcpError? Error { get; init; } |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Route invocation result returned by the safe invoker. |
| | | 158 | | /// </summary> |
| | | 159 | | public sealed record KestrunRouteInvokeResult |
| | | 160 | | { |
| | | 161 | | /// <summary>Response status code.</summary> |
| | | 162 | | public int StatusCode { get; init; } |
| | | 163 | | /// <summary>Response content type.</summary> |
| | | 164 | | public string? ContentType { get; init; } |
| | | 165 | | /// <summary>Response headers.</summary> |
| | | 166 | | public required IReadOnlyDictionary<string, string> Headers { get; init; } |
| | | 167 | | /// <summary>Response body text.</summary> |
| | | 168 | | public string? Body { get; init; } |
| | | 169 | | /// <summary>Invocation error details.</summary> |
| | | 170 | | public KestrunMcpError? Error { get; init; } |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Configures safety boundaries for the MCP request invoker. |
| | | 175 | | /// </summary> |
| | | 176 | | public sealed record KestrunRequestInvokerOptions |
| | | 177 | | { |
| | | 178 | | /// <summary>Whether route invocation is enabled.</summary> |
| | | 179 | | public bool EnableInvocation { get; init; } |
| | | 180 | | /// <summary>Allowlisted route patterns for invocation.</summary> |
| | | 181 | | public IReadOnlyList<string> AllowedPathPatterns { get; init; } = []; |
| | | 182 | | /// <summary>Headers to redact in tool output.</summary> |
| | | 183 | | public IReadOnlySet<string> RedactedHeaders { get; init; } = |
| | | 184 | | new HashSet<string>(StringComparer.OrdinalIgnoreCase) |
| | | 185 | | { |
| | | 186 | | "Authorization", |
| | | 187 | | "Cookie", |
| | | 188 | | "Set-Cookie", |
| | | 189 | | "Proxy-Authorization", |
| | | 190 | | "X-Api-Key", |
| | | 191 | | "Api-Key" |
| | | 192 | | }; |
| | | 193 | | } |
| | | 194 | | |
| | | 195 | | /// <summary> |
| | | 196 | | /// Route inspection contract used by MCP tool handlers. |
| | | 197 | | /// </summary> |
| | | 198 | | public interface IKestrunRouteInspector |
| | | 199 | | { |
| | | 200 | | /// <summary>Returns all registered routes.</summary> |
| | | 201 | | IReadOnlyList<KestrunRouteSummary> ListRoutes(KestrunHost host); |
| | | 202 | | /// <summary>Returns one selected route.</summary> |
| | | 203 | | KestrunRouteDetail GetRoute(KestrunHost host, string? pattern = null, string? operationId = null); |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | /// <summary> |
| | | 207 | | /// OpenAPI retrieval contract used by MCP tool handlers. |
| | | 208 | | /// </summary> |
| | | 209 | | public interface IKestrunOpenApiProvider |
| | | 210 | | { |
| | | 211 | | /// <summary>Returns the requested OpenAPI document.</summary> |
| | | 212 | | KestrunOpenApiDocumentResult GetOpenApi(KestrunHost host, string? documentId = null, string? version = null); |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | /// <summary> |
| | | 216 | | /// Runtime inspection contract used by MCP tool handlers. |
| | | 217 | | /// </summary> |
| | | 218 | | public interface IKestrunRuntimeInspector |
| | | 219 | | { |
| | | 220 | | /// <summary>Returns a safe runtime summary.</summary> |
| | | 221 | | KestrunRuntimeInspectionResult Inspect(KestrunHost host); |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | /// <summary> |
| | | 225 | | /// Request validation contract used by MCP tool handlers. |
| | | 226 | | /// </summary> |
| | | 227 | | public interface IKestrunRequestValidator |
| | | 228 | | { |
| | | 229 | | /// <summary>Validates a proposed request without executing the route handler.</summary> |
| | | 230 | | KestrunRequestValidationResult Validate(KestrunHost host, KestrunRequestInput input); |
| | | 231 | | } |
| | | 232 | | |
| | | 233 | | /// <summary> |
| | | 234 | | /// Request invocation contract used by MCP tool handlers. |
| | | 235 | | /// </summary> |
| | | 236 | | public interface IKestrunRequestInvoker |
| | | 237 | | { |
| | | 238 | | /// <summary>Invokes a route through the normal HTTP pipeline.</summary> |
| | | 239 | | Task<KestrunRouteInvokeResult> InvokeAsync(KestrunHost host, KestrunRequestInput input, CancellationToken cancellati |
| | | 240 | | } |