Repository: envoyproxy/envoy
File: source/extensions/filters/http/mcp/mcp_json_parser.cc (C++)

Code before:

void McpParserConfig::addMethodConfig(absl::string_view method,
                                      std::vector<AttributeExtractionRule> fields) {
  method_fields_[std::string(method)] = std::move(fields);
}

void McpParserConfig::initializeDefaults() {
  // Always extract core JSON-RPC fields
  always_extract_.insert("jsonrpc");
  always_extract_.insert("method");
  always_extract_.insert("id");

  // Tools
  addMethodConfig(Methods::TOOLS_CALL, {AttributeExtractionRule("params.name")});

  // Resources.
  addMethodConfig(Methods::RESOURCES_LIST, {});
  addMethodConfig(Methods::RESOURCES_READ, {AttributeExtractionRule("params.uri")});
  addMethodConfig(Methods::RESOURCES_SUBSCRIBE, {AttributeExtractionRule("params.uri")});
  addMethodConfig(Methods::RESOURCES_UNSUBSCRIBE, {AttributeExtractionRule("params.uri")});

  // Prompts.
  addMethodConfig(Methods::PROMPTS_LIST, {});
  addMethodConfig(Methods::PROMPTS_GET, {AttributeExtractionRule("params.name")});

  // Completion. No field extraction needed. Router parses ref info from body.
  addMethodConfig(Methods::COMPLETION_COMPLETE, {});

  // Logging
  addMethodConfig(Methods::LOGGING_SET_LEVEL, {AttributeExtractionRule("params.level")});

  // Lifecycle
  addMethodConfig(Methods::INITIALIZE, {AttributeExtractionRule("params.protocolVersion"),
                                        AttributeExtractionRule("params.clientInfo.name")});

  // Notifications.
  addMethodConfig(Methods::NOTIFICATION_INITIALIZED, {});
  addMethodConfig(Methods::NOTIFICATION_CANCELLED, {AttributeExtractionRule("params.requestId")});
  addMethodConfig(Methods::NOTIFICATION_PROGRESS, {AttributeExtractionRule("params.progressToken"),
                                                   AttributeExtractionRule("params.progress")});
  addMethodConfig(Methods::NOTIFICATION_MESSAGE, {AttributeExtractionRule("params.level")});
  addMethodConfig(Methods::NOTIFICATION_ROOTS_LIST_CHANGED, {});
  addMethodConfig(Methods::NOTIFICATION_RESOURCES_LIST_CHANGED, {});
  addMethodConfig(Methods::NOTIFICATION_RESOURCES_UPDATED, {AttributeExtractionRule("params.uri")});
  addMethodConfig(Methods::NOTIFICATION_TOOLS_LIST_CHANGED, {});
  addMethodConfig(Methods::NOTIFICATION_PROMPTS_LIST_CHANGED, {});
}

McpParserConfig
McpParserConfig::fromProto(const envoy::extensions::filters::http::mcp::v3::ParserConfig& proto) {
  McpParserConfig config;

Review comment (refactor):
Can we extract  "params.ref` here by default with the JSON parser? So we don't need to search the string, which is more robust.

Code after:

void McpParserConfig::addMethodConfig(absl::string_view method,
                                      std::vector<AttributeExtractionRule> fields) {
  method_fields_[std::string(method)] = std::move(fields);
}

void McpParserConfig::initializeDefaults() {
  // Always extract core JSON-RPC fields
  always_extract_.insert("jsonrpc");
  always_extract_.insert("method");
  always_extract_.insert("id");

  // Tools
  addMethodConfig(Methods::TOOLS_CALL, {AttributeExtractionRule("params.name")});

  // Resources.
  addMethodConfig(Methods::RESOURCES_LIST, {});
  addMethodConfig(Methods::RESOURCES_READ, {AttributeExtractionRule("params.uri")});
  addMethodConfig(Methods::RESOURCES_SUBSCRIBE, {AttributeExtractionRule("params.uri")});
  addMethodConfig(Methods::RESOURCES_UNSUBSCRIBE, {AttributeExtractionRule("params.uri")});

  // Prompts.
  addMethodConfig(Methods::PROMPTS_LIST, {});
  addMethodConfig(Methods::PROMPTS_GET, {AttributeExtractionRule("params.name")});

  // Completion.
  addMethodConfig(Methods::COMPLETION_COMPLETE, {AttributeExtractionRule("params.ref")});

  // Logging
  addMethodConfig(Methods::LOGGING_SET_LEVEL, {AttributeExtractionRule("params.level")});

  // Lifecycle
  addMethodConfig(Methods::INITIALIZE, {AttributeExtractionRule("params.protocolVersion"),
                                        AttributeExtractionRule("params.clientInfo.name")});

  // Notifications.
  addMethodConfig(Methods::NOTIFICATION_INITIALIZED, {});
  addMethodConfig(Methods::NOTIFICATION_CANCELLED, {AttributeExtractionRule("params.requestId")});
  addMethodConfig(Methods::NOTIFICATION_PROGRESS, {AttributeExtractionRule("params.progressToken"),
                                                   AttributeExtractionRule("params.progress")});
  addMethodConfig(Methods::NOTIFICATION_MESSAGE, {AttributeExtractionRule("params.level")});
  addMethodConfig(Methods::NOTIFICATION_ROOTS_LIST_CHANGED, {});
  addMethodConfig(Methods::NOTIFICATION_RESOURCES_LIST_CHANGED, {});
  addMethodConfig(Methods::NOTIFICATION_RESOURCES_UPDATED, {AttributeExtractionRule("params.uri")});
  addMethodConfig(Methods::NOTIFICATION_TOOLS_LIST_CHANGED, {});
  addMethodConfig(Methods::NOTIFICATION_PROMPTS_LIST_CHANGED, {});
}

McpParserConfig
McpParserConfig::fromProto(const envoy::extensions::filters::http::mcp::v3::ParserConfig& proto) {
  McpParserConfig config;