Skip to content

Commit 930173a

Browse files
authored
Merge pull request #87 from koic/add_instructions_support_to_mcp_server
Add `instructions` support to `MCP::Server`
2 parents 22a7db7 + f6f064a commit 930173a

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ You can use the `Server#handle_json` method to handle requests.
155155

156156
```ruby
157157
class ApplicationController < ActionController::Base
158-
159158
def index
160159
server = MCP::Server.new(
161160
name: "my_server",
162161
version: "1.0.0",
162+
instructions: "Use the tools of this server as a last resort",
163163
tools: [SomeTool, AnotherTool],
164164
prompts: [MyPrompt],
165165
server_context: { user_id: current_user.id },

lib/mcp/server.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ def initialize(method_name)
3131

3232
include Instrumentation
3333

34-
attr_accessor :name, :version, :tools, :prompts, :resources, :server_context, :configuration, :capabilities, :transport
34+
attr_accessor :name, :version, :instructions, :tools, :prompts, :resources, :server_context, :configuration, :capabilities, :transport
3535

3636
def initialize(
3737
name: "model_context_protocol",
3838
version: DEFAULT_VERSION,
39+
instructions: nil,
3940
tools: [],
4041
prompts: [],
4142
resources: [],
@@ -47,13 +48,20 @@ def initialize(
4748
)
4849
@name = name
4950
@version = version
51+
@instructions = instructions
5052
@tools = tools.to_h { |t| [t.name_value, t] }
5153
@prompts = prompts.to_h { |p| [p.name_value, p] }
5254
@resources = resources
5355
@resource_templates = resource_templates
5456
@resource_index = index_resources_by_uri(resources)
5557
@server_context = server_context
5658
@configuration = MCP.configuration.merge(configuration)
59+
60+
if @configuration.protocol_version == "2024-11-05" && @instructions
61+
message = "`instructions` supported by protocol version 2025-03-26 or higher"
62+
raise ArgumentError, message
63+
end
64+
5765
@capabilities = capabilities || default_capabilities
5866

5967
@handlers = {
@@ -218,7 +226,8 @@ def init(request)
218226
protocolVersion: configuration.protocol_version,
219227
capabilities: capabilities,
220228
serverInfo: server_info,
221-
}
229+
instructions: instructions,
230+
}.compact
222231
end
223232

224233
def list_tools(request)

test/mcp/server_test.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class ServerTest < ActiveSupport::TestCase
6262
@server = Server.new(
6363
name: @server_name,
6464
version: "1.2.3",
65+
instructions: "Optional instructions for the client",
6566
tools: [@tool, @tool_that_raises],
6667
prompts: [@prompt],
6768
resources: [@resource],
@@ -133,6 +134,7 @@ class ServerTest < ActiveSupport::TestCase
133134
name: @server_name,
134135
version: "1.2.3",
135136
},
137+
instructions: "Optional instructions for the client",
136138
},
137139
}
138140

@@ -748,6 +750,33 @@ def call(message:, server_context: nil)
748750
assert_equal Server::DEFAULT_VERSION, response[:result][:serverInfo][:version]
749751
end
750752

753+
test "server uses instructions when not configured" do
754+
server = Server.new(name: "test_server")
755+
request = {
756+
jsonrpc: "2.0",
757+
method: "initialize",
758+
id: 1,
759+
}
760+
761+
response = server.handle(request)
762+
refute response[:result].key?(:instructions)
763+
end
764+
765+
test "server uses instructions when configured with protocol version 2025-03-26" do
766+
configuration = Configuration.new(protocol_version: "2025-03-26")
767+
server = Server.new(name: "test_server", instructions: "The server instructions.", configuration: configuration)
768+
assert_equal("The server instructions.", server.instructions)
769+
end
770+
771+
test "raises error if instructions are used with protocol version 2024-11-05" do
772+
configuration = Configuration.new(protocol_version: "2024-11-05")
773+
774+
exception = assert_raises(ArgumentError) do
775+
Server.new(name: "test_server", instructions: "The server instructions.", configuration: configuration)
776+
end
777+
assert_equal("`instructions` supported by protocol version 2025-03-26 or higher", exception.message)
778+
end
779+
751780
test "#define_tool adds a tool to the server" do
752781
@server.define_tool(
753782
name: "defined_tool",

0 commit comments

Comments
 (0)