Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/mcp/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ inline auto metadata_key(const JSON::StringView key, const bool prefix_required)
const auto slash{key.find('/')};
JSON::StringView name{key};
if (slash == JSON::StringView::npos) {
if (prefix_required) {
if (prefix_required || key.empty()) {
return false;
}
} else {
Expand Down
9 changes: 9 additions & 0 deletions test/mcp/mcp_headers_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3738,6 +3738,15 @@ TEST(request_meta_validation_rejects_a_prefix_closing_on_a_dot) {
sourcemeta::core::MCPRequestMetaStatus::InvalidMetaKey);
}

TEST(request_meta_validation_rejects_an_empty_metadata_key) {
const auto envelope{sourcemeta::core::parse_json(R"JSON({
"jsonrpc": "2.0", "id": 1, "method": "tools/list",
"params": { "_meta": { "": true } }
})JSON")};
EXPECT_EQ(sourcemeta::core::mcp_validate_request_meta(envelope).first,
sourcemeta::core::MCPRequestMetaStatus::InvalidMetaKey);
}

// A value that cannot travel in a header as it stands is carried base64
// encoded between the markers, so what the comparison sees is what the
// encoding spells out rather than the encoded form itself
Expand Down
13 changes: 13 additions & 0 deletions test/mcp/mcp_notifications_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,19 @@ TEST(make_notification_rejects_malformed_metadata_key_on_2026_07_28) {
"Invalid notification metadata");
}

TEST(make_notification_rejects_empty_metadata_key_on_2026_07_28) {
EXPECT_MCP_REJECTS(sourcemeta::core::mcp_make_notification(
sourcemeta::core::MCPProtocolVersion::V_2026_07_28,
false, "notifications/cancelled",
sourcemeta::core::parse_json(
R"JSON({
"requestId": 1,
"_meta": { "": 1 }
})JSON"),
std::nullopt),
"Invalid notification metadata");
}

TEST(make_notification_rejects_subscription_identifier_on_2025_11_25) {
EXPECT_MCP_REJECTS(sourcemeta::core::mcp_make_notification(
sourcemeta::core::MCPProtocolVersion::V_2025_11_25,
Expand Down
18 changes: 18 additions & 0 deletions test/mcp/mcp_results_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6725,6 +6725,24 @@ TEST(result_writer_metadata_rejections) {
"Discovery must include its serving revision");
}

TEST(result_writer_rejects_empty_metadata_key) {
std::ostringstream stream;
const auto bad{sourcemeta::core::parse_json(
R"JSON({
"resources": [],
"_meta": {
"": 1
}
})JSON")};
EXPECT_MCP_REJECTS(sourcemeta::core::mcp_write_result(
stream,
sourcemeta::core::MCPProtocolVersion::V_2026_07_28,
"resources/list", sourcemeta::core::JSON{0}, bad,
sourcemeta::core::MCPCachePolicy{}),
"Invalid precomputed metadata");
EXPECT_TRUE(stream.str().empty());
}

// An elicitation schema may describe a single or a multiple choice, and the
// builder carries the schema through untouched
// https://modelcontextprotocol.io/specification/2026-07-28/schema#titledsingleselectenumschema
Expand Down
Loading