Class: BundleUp::MCP

Inherits:
Object
  • Object
show all
Defined in:
lib/bundleup/mcp.rb

Overview

Transport for a connection's MCP server.

post and delete return the raw Faraday response, the way Proxy does. Use connect for a managed session that handles the handshake and response decoding.

Constant Summary collapse

BASE_URL =
'https://mcp.bundleup.io'
CREDENTIAL_SEPARATOR =

Separates the API key from an appended connection ID. Safe to split on: API keys are alphanumeric and connection IDs are cuids.

'.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, connection_id) ⇒ MCP

Returns a new instance of MCP.



20
21
22
23
# File 'lib/bundleup/mcp.rb', line 20

def initialize(api_key, connection_id)
  @api_key = api_key
  @connection_id = connection_id
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



18
19
20
# File 'lib/bundleup/mcp.rb', line 18

def api_key
  @api_key
end

#connection_idObject (readonly)

Returns the value of attribute connection_id.



18
19
20
# File 'lib/bundleup/mcp.rb', line 18

def connection_id
  @connection_id
end

Instance Method Details

#connectObject

Open a managed MCP session for this connection.



51
52
53
# File 'lib/bundleup/mcp.rb', line 51

def connect
  MCPClient.new(BASE_URL, @api_key, @connection_id)
end

#delete(headers: {}) ⇒ Object

End an MCP session. Pass the session's Mcp-Session-Id in headers.



46
47
48
# File 'lib/bundleup/mcp.rb', line 46

def delete(headers: {})
  connection.delete(BASE_URL, nil, default_headers.merge(headers))
end

#hostedObject

The URL and a single bearer token carrying both the API key and the connection, for model-hosted MCP clients that cannot set custom headers. Note this hands your API key to the model provider.



33
34
35
# File 'lib/bundleup/mcp.rb', line 33

def hosted
  { url: BASE_URL, token: "#{@api_key}#{CREDENTIAL_SEPARATOR}#{@connection_id}" }
end

#post(body, headers: {}) ⇒ Object

Send a JSON-RPC message and return the raw response. Pass Mcp-Session-Id in headers to stay on an existing session.



39
40
41
42
43
# File 'lib/bundleup/mcp.rb', line 39

def post(body, headers: {})
  payload = body.is_a?(String) ? body : body.to_json

  connection.post(BASE_URL, payload, default_headers.merge(headers))
end

#transportObject

The URL and headers for an MCP client running in your own process.



26
27
28
# File 'lib/bundleup/mcp.rb', line 26

def transport
  { url: BASE_URL, headers: default_headers }
end