Module: Hitch::MCP::TestHelper

Defined in:
lib/hitch/mcp/test_helper.rb

Constant Summary collapse

PROTOCOL_VERSION =

Public: host test suites pin this.

Protocol::VERSION

Instance Method Summary collapse

Instance Method Details

#mcp_headers(token:, method:, name: nil, protocol_version: PROTOCOL_VERSION) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/hitch/mcp/test_helper.rb', line 19

def mcp_headers(token:, method:, name: nil, protocol_version: PROTOCOL_VERSION)
  resource = mcp_test_resource_uri!
  mcp_test_headers(
    resource:,
    token:,
    method:,
    name:,
    protocol_version:
  )
end

#mint_mcp_token(principal:, scopes: Hitch.configuration.supported_scopes, client_id: "hitch-test-client") ⇒ Object

Mints a real access token through the production authorization-code path (create_authorization! + PKCE exchange) and returns the raw bearer token post_mcp expects. The principal is any persisted record the host treats as the signed-in user.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/hitch/mcp/test_helper.rb', line 78

def mint_mcp_token(principal:, scopes: Hitch.configuration.supported_scopes,
  client_id: "hitch-test-client")
  scopes = Array(scopes)
  unsupported = scopes - Hitch.configuration.supported_scopes
  unless unsupported.empty?
    # The real flow clamps grants to supported_scopes; minting past it
    # would let a test pass against a grant production can never issue.
    raise ArgumentError,
      "scopes are not in Hitch.configuration.supported_scopes: #{unsupported.join(' ')}"
  end

  verifier = SecureRandom.urlsafe_base64(64)
  challenge = Base64.urlsafe_encode64(Digest::SHA256.digest(verifier), padding: false)
  resource_uri = Hitch.configuration.resource_uri
  authorization = Hitch::AccessToken.create_authorization!(
    principal: principal,
    client_id: client_id,
    client_name: client_id,
    code_challenge: challenge,
    code_challenge_method: "S256",
    scopes: scopes.join(" "),
    resource_uri: resource_uri
  )
  Hitch::AccessToken.exchange_authorization_code!(
    raw_code: authorization.raw_authorization_code,
    code_verifier: verifier,
    client_id: client_id,
    resource_uri: resource_uri
  ).fetch(:raw_token)
end

#post_mcp(method:, token:, params: {}, id: "hitch-test", client_info: nil, capabilities: {}, protocol_version: PROTOCOL_VERSION) ⇒ Object



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
# File 'lib/hitch/mcp/test_helper.rb', line 30

def post_mcp(method:, token:, params: {}, id: "hitch-test", client_info: nil,
  capabilities: {}, protocol_version: PROTOCOL_VERSION)
  resource = mcp_test_resource_uri!
  mcp_test_match_scheme!(resource)
  normalized_params = mcp_test_json_hash(params, "params")
  raise ArgumentError, "params must not supply _meta" if normalized_params.key?("_meta")

  normalized_capabilities = mcp_test_json_hash(capabilities, "capabilities")
  normalized_client_info = if client_info.nil?
    nil
  else
    mcp_test_json_hash(client_info, "client_info")
  end
  normalized_id = mcp_test_id(id)
  name = normalized_params["name"] if method == "tools/call"
   = {
    "io.modelcontextprotocol/protocolVersion" => protocol_version.to_s,
    "io.modelcontextprotocol/clientCapabilities" => normalized_capabilities
  }
  if normalized_client_info
    ["io.modelcontextprotocol/clientInfo"] = normalized_client_info
  end
  normalized_params["_meta"] = 
  body = JSON.generate(
    "jsonrpc" => "2.0",
    "id" => normalized_id,
    "method" => method,
    "params" => normalized_params
  )

  post resource.request_uri,
    params: body,
    headers: mcp_test_headers(
      resource:,
      token:,
      method:,
      name:,
      protocol_version:
    )
  response
rescue JSON::GeneratorError, EncodingError => error
  raise ArgumentError, "MCP test request is not valid JSON: #{error.class}"
end