Class: KairosMcp::Protocol
- Inherits:
-
Object
- Object
- KairosMcp::Protocol
- Defined in:
- lib/kairos_mcp/protocol.rb
Constant Summary collapse
- STDIO_PROTOCOL_VERSION =
Protocol versions
'2024-11-05'- HTTP_PROTOCOL_VERSION =
'2025-03-26'
Class Method Summary collapse
-
.apply_all_filters(user_context) ⇒ Object
Apply all registered filters to user_context in registration order.
-
.clear_filters! ⇒ Object
For testing only.
-
.register_filter(name, &block) ⇒ Object
Register a named request filter.
- .unregister_filter(name) ⇒ Object
Instance Method Summary collapse
- #handle_message(line) ⇒ Object
-
#initialize(user_context: nil) ⇒ Protocol
constructor
A new instance of Protocol.
Constructor Details
#initialize(user_context: nil) ⇒ Protocol
Returns a new instance of Protocol.
47 48 49 50 51 |
# File 'lib/kairos_mcp/protocol.rb', line 47 def initialize(user_context: nil) @user_context = self.class.apply_all_filters(user_context) @tool_registry = ToolRegistry.new(user_context: @user_context) @initialized = false end |
Class Method Details
.apply_all_filters(user_context) ⇒ Object
Apply all registered filters to user_context in registration order
32 33 34 35 36 |
# File 'lib/kairos_mcp/protocol.rb', line 32 def self.apply_all_filters(user_context) @filter_mutex.synchronize { @filters.values.dup }.reduce(user_context) do |ctx, filter| filter.call(ctx) end end |
.clear_filters! ⇒ Object
For testing only
39 40 41 |
# File 'lib/kairos_mcp/protocol.rb', line 39 def self.clear_filters! @filter_mutex.synchronize { @filters = {} } end |
.register_filter(name, &block) ⇒ Object
Register a named request filter. Filters transform user_context before it reaches ToolRegistry.
23 24 25 |
# File 'lib/kairos_mcp/protocol.rb', line 23 def self.register_filter(name, &block) @filter_mutex.synchronize { @filters[name.to_sym] = block } end |
.unregister_filter(name) ⇒ Object
27 28 29 |
# File 'lib/kairos_mcp/protocol.rb', line 27 def self.unregister_filter(name) @filter_mutex.synchronize { @filters.delete(name.to_sym) } end |
Instance Method Details
#handle_message(line) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/kairos_mcp/protocol.rb', line 53 def (line) request = parse_json(line) return nil unless request id = request['id'] method = request['method'] params = request['params'] || {} result = case method when 'initialize' handle_initialize(params) when 'initialized' return nil when 'tools/list' handle_tools_list when 'tools/call' handle_tools_call(params) else return nil end format_response(id, result) rescue StandardError => e format_error(id, -32603, "Internal error: #{e.}") end |