Skip to content

Implement JSON-RPC message parsing for accurate MCP event type detection #552

Closed

Description

@JAORMX

Problem

Currently, the audit middleware cannot accurately determine MCP event types because it doesn't parse the JSON-RPC message body to extract the method field. The extractMCPMethod function in pkg/audit/auditor.go is a stub that always returns an empty string.

Current Behavior

  • All MCP requests to /messages endpoint are classified as generic mcp_request events
  • Specific MCP operations (tools/call, resources/read, etc.) cannot be distinguished
  • Event filtering based on specific MCP methods is not possible

Expected Behavior

The audit middleware should:

  1. Parse JSON-RPC messages from the request body
  2. Extract the method field (e.g., "tools/call", "resources/read")
  3. Map methods to specific event types (e.g., mcp_tool_call, mcp_resource_read)
  4. Handle edge cases like batch requests and malformed JSON
  5. Restore the request body for downstream handlers

Implementation Requirements

JSON-RPC Message Structure

{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": { ... },
  "id": 1
}

Batch Requests

[
  {"jsonrpc": "2.0", "method": "tools/call", "id": 1},
  {"jsonrpc": "2.0", "method": "resources/read", "id": 2}
]

Implementation Tasks

  1. Read request body safely without consuming it
  2. Parse JSON with proper error handling
  3. Extract method field from single messages or batch arrays
  4. Map to event types using existing mapMCPMethodToEventType function
  5. Handle multiple methods in batch requests (use first method or create separate events)
  6. Restore request body using io.NopCloser(bytes.NewReader(body))
  7. Add comprehensive tests for various JSON-RPC message formats

Code Location

  • File: pkg/audit/auditor.go
  • Function: extractMCPMethod(*http.Request) string
  • Line: ~200 (approximate)

Benefits

  • Accurate event classification for compliance and monitoring
  • Granular audit filtering based on specific MCP operations
  • Better security insights into tool usage patterns
  • Compliance reporting with detailed operation tracking

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    auditenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions