From 1972d6f66e1da0ec743305f7333ce04a17d825bb Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 5 Nov 2026 01:21:09 +0900 Subject: [PATCH] Autoload `MCP::ServerContext` from the gem entry point ## Motivation and Context `MCP::ServerContext` is the class a handler receives as `server_context:`, and the docs name it as such, but `lib/mcp.rb` never listed it: its file was required by `lib/mcp/server.rb` alone, so an application referring to the class before anything had loaded `MCP::Server`, in a type check or a test double, got `NameError: uninitialized constant MCP::ServerContext`. Every other class the docs name resolves from the entry point; this adds the missing entry. Closes #655 ## How Has This Been Tested? test/mcp/server_context_test.rb: a fresh process that requires `mcp` and resolves `MCP::ServerContext` without touching `MCP::Server`; before the change it ends with the `NameError` above. ## Breaking Changes None. --- lib/mcp.rb | 1 + test/mcp/server_context_test.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/mcp.rb b/lib/mcp.rb index 99c083e4..ea612459 100644 --- a/lib/mcp.rb +++ b/lib/mcp.rb @@ -23,6 +23,7 @@ module MCP autoload :ResourceTemplate, "mcp/resource_template" autoload :ResultType, "mcp/result_type" autoload :Server, "mcp/server" + autoload :ServerContext, "mcp/server_context" autoload :ServerSession, "mcp/server_session" autoload :Skill, "mcp/skill" autoload :Tool, "mcp/tool" diff --git a/test/mcp/server_context_test.rb b/test/mcp/server_context_test.rb index 0cc38277..ce591e5b 100644 --- a/test/mcp/server_context_test.rb +++ b/test/mcp/server_context_test.rb @@ -1,9 +1,21 @@ # frozen_string_literal: true require "test_helper" +require "open3" module MCP class ServerContextTest < ActiveSupport::TestCase + # The docs name the class as what a handler receives, so an application may refer to it before anything has + # loaded `MCP::Server`, whose file used to be the only one requiring it. Checked in a fresh process, since this + # one has loaded everything already, and without this one's `RUBYOPT`, so nothing preloads the file for it. + test "ServerContext resolves after the gem alone is required" do + lib = File.expand_path("../../lib", __dir__) + script = 'require "mcp"; MCP::ServerContext' + _stdout, stderr, status = Open3.capture3({ "RUBYOPT" => nil }, RbConfig.ruby, "-I", lib, "-e", script) + + assert_predicate status, :success?, stderr + end + test "ServerContext delegates method calls to the underlying context" do context = { user: "test_user" } progress = Progress.new(notification_target: mock, progress_token: nil)