POST /mcp with a missing or unparseable Content-Type returns HTTP 500 instead of a 4xx
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 72/100
Hướng nghiên cứu
Bản repro (app.cs) đã cô lập hành vi: text/plain dẫn đến 415 trong khi các trường hợp absent, empty và /json rơi vào JSON body reader và ném InvalidOperationException. Hãy bắt đầu trong transport ASP.NET Core, tại điểm mà endpoint Streamable HTTP của MapMcp kiểm tra content type của request trước khi đọc body, và xem vì sao một giá trị không parse được lại bỏ qua kiểm tra đó. Hoàn thành nghĩa là cả bốn trường hợp xấu đều trả về 4xx (415), cộng với một test trong project test AspNetCore bao phủ từng trường hợp.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Describe the bug
A Streamable HTTP POST whose Content-Type header is missing, empty or cannot be parsed as JSON throws an unhandled InvalidOperationException and the server returns HTTP 500. A well-formed but non-JSON type such as text/plain gets a 415.
To Reproduce
Save this as app.cs and run dotnet run app.cs. It hosts a minimal stateless MCP server in-process and POSTs an initialize request with different Content-Type values.
#:sdk Microsoft.NET.Sdk.Web
#:package [email protected]
// Reproduction: a Streamable HTTP POST with a missing or unparseable Content-Type returns HTTP 500
// (unhandled InvalidOperationException) instead of a 4xx.
//
// Run with: dotnet run app.cs (.NET 10 SDK)
using System.ComponentModel;
using System.Text;
using ModelContextProtocol.Server;
var builder = WebApplication.CreateBuilder(args);
builder.Logging.SetMinimumLevel(LogLevel.None);
builder.WebHost.UseUrls("http://127.0.0.1:0");
builder.Services
.AddMcpServer()
.WithHttpTransport(options => options.Stateless = true)
.WithTools<EchoTool>();
var app = builder.Build();
// Observe only: print the exception that escapes the SDK handler, then rethrow so the host
// still produces its normal response.
app.Use(async (context, next) =>
{
try
{
await next();
}
catch (Exception exception)
{
Console.WriteLine($" unhandled {exception.GetType().Name}: {exception.Message}");
throw;
}
});
app.MapMcp("/mcp");
await app.StartAsync();
const string initialize =
"""{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"repro","version":"1.0"}}}""";
string?[] contentTypes =
[
"application/json",
"text/plain",
null,
"",
"/json"
];
using var client = new HttpClient { BaseAddress = new Uri(app.Urls.First()) };
Console.WriteLine($"{"Content-Type",-30} Status");
foreach (var contentType in contentTypes)
{
using var request = new HttpRequestMessage(HttpMethod.Post, "/mcp");
request.Headers.Accept.ParseAdd("application/json");
request.Headers.Accept.ParseAdd("text/event-stream");
request.Content = new StringContent(initialize, Encoding.UTF8);
request.Content.Headers.Remove("Content-Type");
if (contentType is not null)
request.Content.Headers.TryAddWithoutValidation("Content-Type", contentType);
using var response = await client.SendAsync(request);
Console.WriteLine($"{contentType switch { null => "<absent>", "" => "<empty>", _ => contentType },-30} {(int)response.StatusCode}");
}
await app.StopAsync();
[McpServerToolType]
public sealed class EchoTool
{
[McpServerTool, Description("Echoes the message.")]
public static string Echo(string message) => message;
}
Expected behavior
A 4xx response (for example 415, as text/plain already gets) instead of a 500.
Logs
Content-Type Status
application/json 200
text/plain 415
unhandled InvalidOperationException: Unable to read the request as JSON because the request content type '' is not a known JSON content type.
<absent> 500
unhandled InvalidOperationException: Unable to read the request as JSON because the request content type '' is not a known JSON content type.
<empty> 500
unhandled InvalidOperationException: Unable to read the request as JSON because the request content type '/json' is not a known JSON content type.
/json 500
Additional context
- Reproduced on
ModelContextProtocol.AspNetCore2.3.0
- Ngôn ngữ chính
- C#
- Star
- 4.6k
- Fork
- 849
- Merge trung bình
- 1 ngày 16 giờ
- Pull request đã merge (30 ngày)
- 24
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của modelcontextprotocol/csharp-sdk
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
modelcontextprotocol/csharp-sdk#2031 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Static resources whose URIs differ only in the fragment or user info collapse into oneCó thể đã có người làm @halter73 đã nhận 9 ngày trước. Đang mởP2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
modelcontextprotocol/csharp-sdk#1970 · 4 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Reserved URI-template expansion throws for supplementary Unicode charactersCó thể đã có người làm @tarekgh đã nhận 2 ngày trước. Đang mởP3
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
modelcontextprotocol/csharp-sdk#1913 · 1 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
ClaimsPrincipal parameter injection sample requestCó thể đã có người làm @utsavjain2809 đã nhận 48 ngày trước. Đang mởhelp wanted P3
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
modelcontextprotocol/csharp-sdk#1899 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Document ClientOAuthOptions.ScopeSelector usageCó thể đã có người làm @z0rimo đã nhận 71 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
modelcontextprotocol/csharp-sdk#1867 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của modelcontextprotocol/csharp-sdk
Issue tương tự
-
account bug effort:S P2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
nightscout/nocturne#3745 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
jsboige/CoursIA#27768 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 63/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
martin-stromberg/FinanceManager#562 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
:eyeglasses: status: needs review needs-area-label
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
gitextensions/gitextensions#13469 ·
Maintainer thường phản hồi trong vòng 2 ngày