Module: Hitch::MCP::Endpoint

Extended by:
ActiveSupport::Concern
Includes:
RequestAdmission
Defined in:
app/controllers/concerns/hitch/mcp/endpoint.rb

Overview

Authenticated, stateless MCP 2026-07-28 JSON endpoint for a dedicated host-owned ActionController::API controller.

Constant Summary

Constants included from RequestAdmission

RequestAdmission::MAX_REQUEST_BODY_BYTES

Instance Method Summary collapse

Instance Method Details

#handleObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/concerns/hitch/mcp/endpoint.rb', line 23

def handle
  unless Internal::MediaType.json_content_type?(request.get_header("CONTENT_TYPE"))
    hitch_mcp_explain!("Content-Type must be exactly application/json")
    return hitch_mcp_protocol_error!(415, -32600, "Invalid Request")
  end
  unless Internal::MediaType.accepts_required_types?(request.get_header("HTTP_ACCEPT"))
    hitch_mcp_explain!("Accept must admit both application/json and text/event-stream")
    return hitch_mcp_protocol_error!(406, -32600, "Invalid Request")
  end

  max_request_bytes = Hitch.configuration.mcp.max_request_bytes
  raw_body = hitch_read_bounded_request_body(max_request_bytes)
  @hitch_mcp_observation&.request_bytes!(raw_body ? raw_body.bytesize : max_request_bytes + 1)
  unless raw_body
    hitch_mcp_explain!("request body exceeds mcp.max_request_bytes (#{max_request_bytes})")
    return hitch_mcp_protocol_error!(413, -32600, "Invalid Request")
  end

  verified_request = Internal::VerifiedRequest.call(
    raw_body: raw_body,
    headers: {
      protocol_version: request.get_header("HTTP_MCP_PROTOCOL_VERSION"),
      method: request.get_header("HTTP_MCP_METHOD"),
      name: request.get_header("HTTP_MCP_NAME")
    }
  )
  @hitch_mcp_observation&.verified!(verified_request)

  hitch_mcp_dispatch!(verified_request)
rescue Internal::VerifiedRequest::Failure => error
  # The wire deliberately says only "Invalid params" for a dozen
  # different rejections. Locally the first backtrace frame names the
  # exact check that refused, which is the difference between a
  # developer fixing their request and guessing at it.
  Internal::LocalDiagnosis.report(
    "MCP request rejected (#{error.code} #{error.message})", error
  )
  hitch_mcp_protocol_error!(
    error.http_status,
    error.code,
    error.message,
    request_id: error.request_id,
    data: error.data
  )
rescue StandardError => error
  request_id = verified_request && verified_request["id"]
  Internal::EndpointErrorReporter.report(category: :dispatch)
  Internal::LocalDiagnosis.report("MCP request failed during dispatch", error)
  hitch_mcp_protocol_error!(200, -32603, "Internal error", request_id: request_id)
ensure
  request.delete_header("RAW_POST_DATA") if request
end