POST /mcp with a missing or unparseable Content-Type returns HTTP 500 instead of a 4xx
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 72/100
調査の方向性
再現コード (app.cs) はすでに挙動を切り分けています。text/plain は 415 になる一方、absent、empty、/json は JSON body reader にフォールスルーして InvalidOperationException をスローします。ASP.NET Core transport の、MapMcp の Streamable HTTP endpoint が body を読む前に request content type をチェックする箇所から始めて、parse できない値がなぜそのチェックを飛ばすのかを確認してください。完了とは、4 つの不正なケースすべてが 4xx (415) を返し、さらに AspNetCore のテストプロジェクトでそれぞれをカバーするテストがある状態です。
索引モデルが issue の本文から書いたものです。
説明
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
- 主要言語
- C#
- スター
- 4.6k
- フォーク
- 849
- 平均マージ
- 1日 16時間
- マージ済み PR(30日)
- 24
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
modelcontextprotocol/csharp-sdk のほかの issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
modelcontextprotocol/csharp-sdk#2031 ·
メンテナーはふだん 1 日以内に返信
-
Static resources whose URIs differ only in the fragment or user info collapse into one対応中かも @halter73 が 9 日前に担当しました。 オープンP2
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
modelcontextprotocol/csharp-sdk#1970 · コメント 4 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
Reserved URI-template expansion throws for supplementary Unicode characters対応中かも @tarekgh が 2 日前に担当しました。 オープンP3
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
modelcontextprotocol/csharp-sdk#1913 · コメント 1 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
ClaimsPrincipal parameter injection sample request対応中かも @utsavjain2809 が 48 日前に担当しました。 オープンhelp wanted P3
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
modelcontextprotocol/csharp-sdk#1899 ·
メンテナーはふだん 1 日以内に返信
-
Document ClientOAuthOptions.ScopeSelector usage対応中かも @z0rimo が 71 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
modelcontextprotocol/csharp-sdk#1867 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
modelcontextprotocol/csharp-sdk の issue をすべて見る
似ている issue
-
account bug effort:S P2
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
nightscout/nocturne#3745 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
jsboige/CoursIA#27768 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 63/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
martin-stromberg/FinanceManager#562 ·
メンテナーはふだん 1 日以内に返信
-
:eyeglasses: status: needs review needs-area-label
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
gitextensions/gitextensions#13469 ·
メンテナーはふだん 2 日以内に返信