Class: MCP::ServerContext

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, progress:, notification_target:, related_request_id: nil, cancellation: nil) ⇒ ServerContext

Returns a new instance of ServerContext.



7
8
9
10
11
12
13
# File 'lib/mcp/server_context.rb', line 7

def initialize(context, progress:, notification_target:, related_request_id: nil, cancellation: nil)
  @context = context
  @progress = progress
  @notification_target = notification_target
  @related_request_id = related_request_id
  @cancellation = cancellation
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/mcp/server_context.rb', line 111

def method_missing(name, ...)
  if @context.respond_to?(name)
    @context.public_send(name, ...)
  else
    super
  end
end

Instance Attribute Details

#cancellationObject (readonly)

Returns the value of attribute cancellation.



5
6
7
# File 'lib/mcp/server_context.rb', line 5

def cancellation
  @cancellation
end

Instance Method Details

#cancelled?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/mcp/server_context.rb', line 15

def cancelled?
  !!@cancellation&.cancelled?
end

#create_form_elicitation(**kwargs) ⇒ Object

Delegates to the session so the request is scoped to the originating client. Falls back to ‘@context` (via `method_missing`) when `@notification_target` does not support elicitation.



78
79
80
81
82
83
84
85
86
# File 'lib/mcp/server_context.rb', line 78

def create_form_elicitation(**kwargs)
  if @notification_target.respond_to?(:create_form_elicitation)
    @notification_target.create_form_elicitation(**kwargs, related_request_id: @related_request_id)
  elsif @context.respond_to?(:create_form_elicitation)
    @context.create_form_elicitation(**kwargs, related_request_id: @related_request_id)
  else
    raise NoMethodError, "undefined method 'create_form_elicitation' for #{self}"
  end
end

#create_sampling_message(**kwargs) ⇒ Object

Delegates to the session so the request is scoped to the originating client. Falls back to ‘@context` (via `method_missing`) when `@notification_target` does not support sampling.



65
66
67
68
69
70
71
72
73
# File 'lib/mcp/server_context.rb', line 65

def create_sampling_message(**kwargs)
  if @notification_target.respond_to?(:create_sampling_message)
    @notification_target.create_sampling_message(**kwargs, related_request_id: @related_request_id)
  elsif @context.respond_to?(:create_sampling_message)
    @context.create_sampling_message(**kwargs, related_request_id: @related_request_id)
  else
    raise NoMethodError, "undefined method 'create_sampling_message' for #{self}"
  end
end

#create_url_elicitation(**kwargs) ⇒ Object

Delegates to the session so the request is scoped to the originating client. Falls back to ‘@context` when `@notification_target` does not support URL mode elicitation.



90
91
92
93
94
95
96
97
98
# File 'lib/mcp/server_context.rb', line 90

def create_url_elicitation(**kwargs)
  if @notification_target.respond_to?(:create_url_elicitation)
    @notification_target.create_url_elicitation(**kwargs, related_request_id: @related_request_id)
  elsif @context.respond_to?(:create_url_elicitation)
    @context.create_url_elicitation(**kwargs, related_request_id: @related_request_id)
  else
    raise NoMethodError, "undefined method 'create_url_elicitation' for #{self}"
  end
end

#list_rootsObject

Delegates to the session so the request is scoped to the originating client.



54
55
56
57
58
59
60
# File 'lib/mcp/server_context.rb', line 54

def list_roots
  if @notification_target.respond_to?(:list_roots)
    @notification_target.list_roots(related_request_id: @related_request_id)
  else
    raise NoMethodError, "undefined method 'list_roots' for #{self}"
  end
end

#notify_elicitation_complete(**kwargs) ⇒ Object

Delegates to the session so the notification is scoped to the originating client.



101
102
103
104
105
106
107
108
109
# File 'lib/mcp/server_context.rb', line 101

def notify_elicitation_complete(**kwargs)
  if @notification_target.respond_to?(:notify_elicitation_complete)
    @notification_target.notify_elicitation_complete(**kwargs)
  elsif @context.respond_to?(:notify_elicitation_complete)
    @context.notify_elicitation_complete(**kwargs)
  else
    raise NoMethodError, "undefined method 'notify_elicitation_complete' for #{self}"
  end
end

#notify_log_message(data:, level:, logger: nil) ⇒ Object

Sends a log message notification scoped to the originating session.

Parameters:

  • data (Object)

    The log data to send.

  • level (String)

    Log level (e.g., ‘“debug”`, `“info”`, `“error”`).

  • logger (String, nil) (defaults to: nil)

    Logger name.



38
39
40
41
42
# File 'lib/mcp/server_context.rb', line 38

def notify_log_message(data:, level:, logger: nil)
  return unless @notification_target

  @notification_target.notify_log_message(data: data, level: level, logger: logger, related_request_id: @related_request_id)
end

#notify_resources_updated(uri:) ⇒ Object

Sends a resource updated notification scoped to the originating session.

Parameters:

  • uri (String)

    The URI of the updated resource.



47
48
49
50
51
# File 'lib/mcp/server_context.rb', line 47

def notify_resources_updated(uri:)
  return unless @notification_target

  @notification_target.notify_resources_updated(uri: uri)
end

#raise_if_cancelled!Object



19
20
21
# File 'lib/mcp/server_context.rb', line 19

def raise_if_cancelled!
  @cancellation&.raise_if_cancelled!
end

#report_progress(progress, total: nil, message: nil) ⇒ Object

Reports progress for the current tool operation. The notification is automatically scoped to the originating session.

Parameters:

  • progress (Numeric)

    Current progress value.

  • total (Numeric, nil) (defaults to: nil)

    Total expected value.

  • message (String, nil) (defaults to: nil)

    Human-readable status message.



29
30
31
# File 'lib/mcp/server_context.rb', line 29

def report_progress(progress, total: nil, message: nil)
  @progress.report(progress, total: total, message: message)
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/mcp/server_context.rb', line 119

def respond_to_missing?(name, include_private = false)
  @context.respond_to?(name) || super
end